mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-27 05:28:43 +02:00
feat(v2): add search page (#2756)
This commit is contained in:
parent
1fe2dc192e
commit
3ad4550854
12 changed files with 688 additions and 38 deletions
|
@ -0,0 +1,41 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import {useHistory, useLocation} from '@docusaurus/router';
|
||||
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
|
||||
|
||||
const SEARCH_PARAM_QUERY = 'q';
|
||||
|
||||
function useSearchQuery() {
|
||||
const history = useHistory();
|
||||
const location = useLocation();
|
||||
|
||||
return {
|
||||
searchValue:
|
||||
(ExecutionEnvironment.canUseDOM &&
|
||||
new URLSearchParams(location.search).get(SEARCH_PARAM_QUERY)) ||
|
||||
'',
|
||||
updateSearchPath: (searchValue) => {
|
||||
const searchParams = new URLSearchParams(location.search);
|
||||
|
||||
if (searchValue) {
|
||||
searchParams.set(SEARCH_PARAM_QUERY, searchValue);
|
||||
} else {
|
||||
searchParams.delete(SEARCH_PARAM_QUERY);
|
||||
}
|
||||
|
||||
history.replace({
|
||||
search: searchParams.toString(),
|
||||
});
|
||||
},
|
||||
navigateToSearchPage: (searchValue) => {
|
||||
history.push(`/search?q=${searchValue}`);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export default useSearchQuery;
|
Loading…
Add table
Add a link
Reference in a new issue