Skip to content

Commit

Permalink
feat(v2): improve Algolia search accessibility (#3166)
Browse files Browse the repository at this point in the history
* feat(v2): improve Algolia search accessbility

* fix(v2): make sure to import modal before opening
  • Loading branch information
francoischalifour authored Jul 31, 2020
1 parent d560a52 commit e442ac9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/docusaurus-theme-search-algolia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand All @@ -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 (
<>
Expand All @@ -81,14 +99,15 @@ function DocSearch(props) {
onFocus={importDocSearchModalIfNeeded}
onMouseOver={importDocSearchModalIfNeeded}
onClick={onOpen}
aria-label="Search"
ref={searchButtonRef}
/>

{isOpen &&
createPortal(
<DocSearchModal
onClose={onClose}
initialScrollY={window.scrollY}
initialQuery={initialQuery}
navigator={{
navigate({suggestionUrl}) {
history.push(suggestionUrl);
Expand Down
42 changes: 21 additions & 21 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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/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==
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":
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.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.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":
Expand Down Expand Up @@ -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/[email protected]":
version "2.1.4"
Expand Down

0 comments on commit e442ac9

Please sign in to comment.