mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-24 20:17:50 +02:00
feat(v2): improve Algolia search accessibility (#3166)
* feat(v2): improve Algolia search accessbility * fix(v2): make sure to import modal before opening
This commit is contained in:
parent
d560a52ef2
commit
e442ac95ee
3 changed files with 42 additions and 23 deletions
|
@ -8,7 +8,7 @@
|
|||
},
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@docsearch/react": "^1.0.0-alpha.24",
|
||||
"@docsearch/react": "^1.0.0-alpha.25",
|
||||
"@hapi/joi": "^17.1.1",
|
||||
"algoliasearch": "^4.0.0",
|
||||
"algoliasearch-helper": "^3.1.1",
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import React, {useState, useCallback} from 'react';
|
||||
import React, {useState, useRef, useCallback} from 'react';
|
||||
import {createPortal} from 'react-dom';
|
||||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
||||
import {useHistory} from '@docusaurus/router';
|
||||
|
@ -35,7 +35,9 @@ function DocSearch(props) {
|
|||
const {siteMetadata} = useDocusaurusContext();
|
||||
const {withBaseUrl} = useBaseUrlUtils();
|
||||
const history = useHistory();
|
||||
const searchButtonRef = useRef(null);
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const [initialQuery, setInitialQuery] = useState(null);
|
||||
|
||||
const importDocSearchModalIfNeeded = useCallback(() => {
|
||||
if (DocSearchModal) {
|
||||
|
@ -61,7 +63,23 @@ function DocSearch(props) {
|
|||
setIsOpen(false);
|
||||
}, [setIsOpen]);
|
||||
|
||||
useDocSearchKeyboardEvents({isOpen, onOpen, onClose});
|
||||
const onInput = useCallback(
|
||||
(event) => {
|
||||
importDocSearchModalIfNeeded().then(() => {
|
||||
setIsOpen(true);
|
||||
setInitialQuery(event.key);
|
||||
});
|
||||
},
|
||||
[importDocSearchModalIfNeeded, setIsOpen, setInitialQuery],
|
||||
);
|
||||
|
||||
useDocSearchKeyboardEvents({
|
||||
isOpen,
|
||||
onOpen,
|
||||
onClose,
|
||||
onInput,
|
||||
searchButtonRef,
|
||||
});
|
||||
|
||||
return (
|
||||
<>
|
||||
|
@ -81,7 +99,7 @@ function DocSearch(props) {
|
|||
onFocus={importDocSearchModalIfNeeded}
|
||||
onMouseOver={importDocSearchModalIfNeeded}
|
||||
onClick={onOpen}
|
||||
aria-label="Search"
|
||||
ref={searchButtonRef}
|
||||
/>
|
||||
|
||||
{isOpen &&
|
||||
|
@ -89,6 +107,7 @@ function DocSearch(props) {
|
|||
<DocSearchModal
|
||||
onClose={onClose}
|
||||
initialScrollY={window.scrollY}
|
||||
initialQuery={initialQuery}
|
||||
navigator={{
|
||||
navigate({suggestionUrl}) {
|
||||
history.push(suggestionUrl);
|
||||
|
|
38
yarn.lock
38
yarn.lock
|
@ -1764,19 +1764,19 @@
|
|||
enabled "2.0.x"
|
||||
kuler "^2.0.0"
|
||||
|
||||
"@docsearch/css@^1.0.0-alpha.24":
|
||||
version "1.0.0-alpha.24"
|
||||
resolved "https://registry.yarnpkg.com/@docsearch/css/-/css-1.0.0-alpha.24.tgz#a2e8d7e7ac50ebea17f90e974c5174b0d1a5c855"
|
||||
integrity sha512-BCNomH+wdpg+hWTCczwQATS4hbztOvzU/6GXco+KIgVrvpTovtPsS7BYTQMCRhs2gPSk3p3DqgW95mwCoAvt0w==
|
||||
"@docsearch/css@^1.0.0-alpha.25":
|
||||
version "1.0.0-alpha.25"
|
||||
resolved "https://registry.yarnpkg.com/@docsearch/css/-/css-1.0.0-alpha.25.tgz#08eb99fc8364c71427e0885cb09892c607e0617a"
|
||||
integrity sha512-Mq2rFnq/SyQhDah/YjvE0dLnz8JozncOCKPqjbfyt/Q5sJwJhNfUlrZhvk2OgOirKX4izYrFq4PZgGKDokiEgQ==
|
||||
|
||||
"@docsearch/react@^1.0.0-alpha.24":
|
||||
version "1.0.0-alpha.24"
|
||||
resolved "https://registry.yarnpkg.com/@docsearch/react/-/react-1.0.0-alpha.24.tgz#c0fb18e0df9494b86e50588892a0b25e293aa0ac"
|
||||
integrity sha512-abfYBMrZcxmUhyxjdYPCRqBCC5XMilKEmondjkjUQG0W1sYmpjrVOMYBiTq6ZTW+7urVvXPjD9bm0fBZnlI9ow==
|
||||
"@docsearch/react@^1.0.0-alpha.25":
|
||||
version "1.0.0-alpha.25"
|
||||
resolved "https://registry.yarnpkg.com/@docsearch/react/-/react-1.0.0-alpha.25.tgz#e3bf76e888b28c1b37c6412fd99939b83a10eca6"
|
||||
integrity sha512-s2hwj47lcpNBZVYojhbMZZ+grzm1AFFcbkgJYAl97rgzvG5X7lr8sGvzEIgjlE64t4T6FWtrM/B8XxZq1K9SxA==
|
||||
dependencies:
|
||||
"@docsearch/css" "^1.0.0-alpha.24"
|
||||
"@francoischalifour/autocomplete-core" "^1.0.0-alpha.24"
|
||||
"@francoischalifour/autocomplete-preset-algolia" "^1.0.0-alpha.24"
|
||||
"@docsearch/css" "^1.0.0-alpha.25"
|
||||
"@francoischalifour/autocomplete-core" "^1.0.0-alpha.25"
|
||||
"@francoischalifour/autocomplete-preset-algolia" "^1.0.0-alpha.25"
|
||||
algoliasearch "^4.0.0"
|
||||
|
||||
"@endiliey/react-ideal-image@^0.0.11":
|
||||
|
@ -1876,15 +1876,15 @@
|
|||
unique-filename "^1.1.1"
|
||||
which "^1.3.1"
|
||||
|
||||
"@francoischalifour/autocomplete-core@^1.0.0-alpha.24":
|
||||
version "1.0.0-alpha.24"
|
||||
resolved "https://registry.yarnpkg.com/@francoischalifour/autocomplete-core/-/autocomplete-core-1.0.0-alpha.24.tgz#fc71704a17cf9326a66d97134508abdf02313181"
|
||||
integrity sha512-rdWCKeIeDYjUXokdoyRNrFTreGZ8WLO/mhxAIWyLJ8ymdfXsortJqPL3fSDe57khXllGaZc/qxNsZi5RrpDRmQ==
|
||||
"@francoischalifour/autocomplete-core@^1.0.0-alpha.25":
|
||||
version "1.0.0-alpha.25"
|
||||
resolved "https://registry.yarnpkg.com/@francoischalifour/autocomplete-core/-/autocomplete-core-1.0.0-alpha.25.tgz#16a2a88ec0919b90fa3378f1f3652ff8f9df2a05"
|
||||
integrity sha512-wQjL1NjgGGlbeTa+fg1+HNoUiJ0+a32xEE+8wKKHu1FX4dH/FRMkn8sEulsq6QHZyg41YMeJkOUHUwBnNtyWeA==
|
||||
|
||||
"@francoischalifour/autocomplete-preset-algolia@^1.0.0-alpha.24":
|
||||
version "1.0.0-alpha.24"
|
||||
resolved "https://registry.yarnpkg.com/@francoischalifour/autocomplete-preset-algolia/-/autocomplete-preset-algolia-1.0.0-alpha.24.tgz#f305d529d9e6c31b7e14d7aff45e912a23b187e5"
|
||||
integrity sha512-LHcbVKZaki42J30zg30ZoAuEJVysfIMSE91JT9YuOnpch+26hC8Vff9VlCs+6ACYxXGuGHdX7uuKYOx7GcoQ3A==
|
||||
"@francoischalifour/autocomplete-preset-algolia@^1.0.0-alpha.25":
|
||||
version "1.0.0-alpha.25"
|
||||
resolved "https://registry.yarnpkg.com/@francoischalifour/autocomplete-preset-algolia/-/autocomplete-preset-algolia-1.0.0-alpha.25.tgz#703eb85d4f55eab66fd9cf5f18749ee830093f78"
|
||||
integrity sha512-tXrNYJ5my5LzphHfVLzSkuH2XMOn8CnLkG/FkaoiNeFQ97NpYrI0LOQxOPloalz2/dulgvvSqIg85XGzjAKfEg==
|
||||
|
||||
"@hapi/address@2.x.x":
|
||||
version "2.1.4"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue