@docusaurus/theme-common 2.0.0-beta.16 → 2.0.0-beta.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Details.d.ts +14 -0
- package/lib/components/Collapsible/index.d.ts +32 -4
- package/lib/components/Collapsible/index.d.ts.map +1 -1
- package/lib/components/Collapsible/index.js +18 -10
- package/lib/components/Collapsible/index.js.map +1 -1
- package/lib/components/Details/index.d.ts +6 -1
- package/lib/components/Details/index.d.ts.map +1 -1
- package/lib/components/Details/index.js +11 -6
- package/lib/components/Details/index.js.map +1 -1
- package/lib/components/Details/styles.module.css +4 -0
- package/lib/{utils/announcementBarUtils.d.ts → contexts/announcementBar.d.ts} +7 -3
- package/lib/contexts/announcementBar.d.ts.map +1 -0
- package/lib/{utils/announcementBarUtils.js → contexts/announcementBar.js} +15 -14
- package/lib/contexts/announcementBar.js.map +1 -0
- package/lib/contexts/colorMode.d.ts +27 -0
- package/lib/contexts/colorMode.d.ts.map +1 -0
- package/lib/contexts/colorMode.js +132 -0
- package/lib/contexts/colorMode.js.map +1 -0
- package/lib/contexts/docSidebarItemsExpandedState.d.ts +31 -0
- package/lib/contexts/docSidebarItemsExpandedState.d.ts.map +1 -0
- package/lib/{utils → contexts}/docSidebarItemsExpandedState.js +10 -4
- package/lib/contexts/docSidebarItemsExpandedState.js.map +1 -0
- package/lib/contexts/docsPreferredVersion.d.ts +31 -0
- package/lib/contexts/docsPreferredVersion.d.ts.map +1 -0
- package/lib/contexts/docsPreferredVersion.js +128 -0
- package/lib/contexts/docsPreferredVersion.js.map +1 -0
- package/lib/contexts/docsSidebar.d.ts +26 -0
- package/lib/contexts/docsSidebar.d.ts.map +1 -0
- package/lib/contexts/docsSidebar.js +30 -0
- package/lib/contexts/docsSidebar.js.map +1 -0
- package/lib/contexts/docsVersion.d.ts +20 -0
- package/lib/contexts/docsVersion.d.ts.map +1 -0
- package/lib/contexts/docsVersion.js +26 -0
- package/lib/contexts/docsVersion.js.map +1 -0
- package/lib/contexts/navbarMobileSidebar.d.ts +31 -0
- package/lib/contexts/navbarMobileSidebar.d.ts.map +1 -0
- package/lib/contexts/navbarMobileSidebar.js +56 -0
- package/lib/contexts/navbarMobileSidebar.js.map +1 -0
- package/lib/contexts/navbarSecondaryMenu/content.d.ts +37 -0
- package/lib/contexts/navbarSecondaryMenu/content.d.ts.map +1 -0
- package/lib/contexts/navbarSecondaryMenu/content.js +56 -0
- package/lib/contexts/navbarSecondaryMenu/content.js.map +1 -0
- package/lib/contexts/navbarSecondaryMenu/display.d.ts +24 -0
- package/lib/contexts/navbarSecondaryMenu/display.d.ts.map +1 -0
- package/lib/contexts/navbarSecondaryMenu/display.js +62 -0
- package/lib/contexts/navbarSecondaryMenu/display.js.map +1 -0
- package/lib/{utils/tabGroupChoiceUtils.d.ts → contexts/tabGroupChoice.d.ts} +5 -3
- package/lib/contexts/tabGroupChoice.d.ts.map +1 -0
- package/lib/{utils/tabGroupChoiceUtils.js → contexts/tabGroupChoice.js} +15 -20
- package/lib/contexts/tabGroupChoice.js.map +1 -0
- package/lib/hooks/useBackToTopButton.d.ts +27 -0
- package/lib/hooks/useBackToTopButton.d.ts.map +1 -0
- package/lib/hooks/useBackToTopButton.js +50 -0
- package/lib/hooks/useBackToTopButton.js.map +1 -0
- package/lib/hooks/useCodeWordWrap.d.ts +14 -0
- package/lib/hooks/useCodeWordWrap.d.ts.map +1 -0
- package/lib/hooks/useCodeWordWrap.js +41 -0
- package/lib/hooks/useCodeWordWrap.js.map +1 -0
- package/lib/hooks/useHideableNavbar.d.ts +7 -3
- package/lib/hooks/useHideableNavbar.d.ts.map +1 -1
- package/lib/hooks/useHideableNavbar.js +10 -9
- package/lib/hooks/useHideableNavbar.js.map +1 -1
- package/lib/hooks/useKeyboardNavigation.d.ts +11 -1
- package/lib/hooks/useKeyboardNavigation.d.ts.map +1 -1
- package/lib/hooks/useKeyboardNavigation.js +11 -3
- package/lib/hooks/useKeyboardNavigation.js.map +1 -1
- package/lib/hooks/useLockBodyScroll.d.ts +5 -1
- package/lib/hooks/useLockBodyScroll.d.ts.map +1 -1
- package/lib/hooks/useLockBodyScroll.js +5 -1
- package/lib/hooks/useLockBodyScroll.js.map +1 -1
- package/lib/hooks/usePrismTheme.d.ts +6 -2
- package/lib/hooks/usePrismTheme.d.ts.map +1 -1
- package/lib/hooks/usePrismTheme.js +9 -6
- package/lib/hooks/usePrismTheme.js.map +1 -1
- package/lib/hooks/useSearchPage.d.ts +15 -4
- package/lib/hooks/useSearchPage.d.ts.map +1 -1
- package/lib/hooks/useSearchPage.js +4 -4
- package/lib/hooks/useSearchPage.js.map +1 -1
- package/lib/hooks/useSkipToContent.d.ts +25 -0
- package/lib/hooks/useSkipToContent.d.ts.map +1 -0
- package/lib/hooks/useSkipToContent.js +35 -0
- package/lib/hooks/useSkipToContent.js.map +1 -0
- package/lib/hooks/useTOCHighlight.d.ts +25 -0
- package/lib/hooks/useTOCHighlight.d.ts.map +1 -0
- package/lib/{utils → hooks}/useTOCHighlight.js +18 -16
- package/lib/hooks/useTOCHighlight.js.map +1 -0
- package/lib/hooks/useWindowSize.d.ts +14 -1
- package/lib/hooks/useWindowSize.d.ts.map +1 -1
- package/lib/hooks/useWindowSize.js +14 -11
- package/lib/hooks/useWindowSize.js.map +1 -1
- package/lib/index.d.ts +31 -32
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +31 -26
- package/lib/index.js.map +1 -1
- package/lib/utils/ThemeClassNames.d.ts +7 -0
- package/lib/utils/ThemeClassNames.d.ts.map +1 -1
- package/lib/utils/ThemeClassNames.js +7 -4
- package/lib/utils/ThemeClassNames.js.map +1 -1
- package/lib/utils/codeBlockUtils.d.ts +57 -4
- package/lib/utils/codeBlockUtils.d.ts.map +1 -1
- package/lib/utils/codeBlockUtils.js +111 -80
- package/lib/utils/codeBlockUtils.js.map +1 -1
- package/lib/utils/docsUtils.d.ts +83 -18
- package/lib/utils/docsUtils.d.ts.map +1 -1
- package/lib/utils/docsUtils.js +143 -62
- package/lib/utils/docsUtils.js.map +1 -1
- package/lib/utils/footerUtils.d.ts +13 -0
- package/lib/utils/footerUtils.d.ts.map +1 -0
- package/lib/utils/footerUtils.js +14 -0
- package/lib/utils/footerUtils.js.map +1 -0
- package/lib/utils/generalUtils.d.ts +4 -1
- package/lib/utils/generalUtils.d.ts.map +1 -1
- package/lib/utils/generalUtils.js +6 -3
- package/lib/utils/generalUtils.js.map +1 -1
- package/lib/utils/historyUtils.d.ts +1 -7
- package/lib/utils/historyUtils.d.ts.map +1 -1
- package/lib/utils/historyUtils.js +10 -13
- package/lib/utils/historyUtils.js.map +1 -1
- package/lib/utils/jsUtils.d.ts +1 -1
- package/lib/utils/jsUtils.js +1 -1
- package/lib/utils/metadataUtils.d.ts +38 -0
- package/lib/utils/metadataUtils.d.ts.map +1 -0
- package/lib/utils/metadataUtils.js +61 -0
- package/lib/utils/metadataUtils.js.map +1 -0
- package/lib/utils/navbarUtils.d.ts +21 -0
- package/lib/utils/navbarUtils.d.ts.map +1 -0
- package/lib/utils/navbarUtils.js +32 -0
- package/lib/utils/navbarUtils.js.map +1 -0
- package/lib/utils/reactUtils.d.ts +21 -7
- package/lib/utils/reactUtils.d.ts.map +1 -1
- package/lib/utils/reactUtils.js +34 -6
- package/lib/utils/reactUtils.js.map +1 -1
- package/lib/utils/regexpUtils.d.ts +2 -1
- package/lib/utils/regexpUtils.d.ts.map +1 -1
- package/lib/utils/regexpUtils.js +2 -1
- package/lib/utils/regexpUtils.js.map +1 -1
- package/lib/utils/routesUtils.d.ts +19 -4
- package/lib/utils/routesUtils.d.ts.map +1 -1
- package/lib/utils/routesUtils.js +40 -19
- package/lib/utils/routesUtils.js.map +1 -1
- package/lib/utils/scrollUtils.d.ts +55 -25
- package/lib/utils/scrollUtils.d.ts.map +1 -1
- package/lib/utils/scrollUtils.js +85 -21
- package/lib/utils/scrollUtils.js.map +1 -1
- package/lib/utils/searchUtils.d.ts +12 -0
- package/lib/utils/searchUtils.d.ts.map +1 -1
- package/lib/utils/searchUtils.js +36 -0
- package/lib/utils/searchUtils.js.map +1 -1
- package/lib/utils/storageUtils.d.ts +12 -9
- package/lib/utils/storageUtils.d.ts.map +1 -1
- package/lib/utils/storageUtils.js +21 -13
- package/lib/utils/storageUtils.js.map +1 -1
- package/lib/utils/tagsUtils.d.ts +7 -8
- package/lib/utils/tagsUtils.d.ts.map +1 -1
- package/lib/utils/tagsUtils.js +8 -6
- package/lib/utils/tagsUtils.js.map +1 -1
- package/lib/utils/tocUtils.d.ts +17 -1
- package/lib/utils/tocUtils.d.ts.map +1 -1
- package/lib/utils/tocUtils.js +20 -9
- package/lib/utils/tocUtils.js.map +1 -1
- package/lib/utils/useAlternatePageUtils.d.ts +20 -1
- package/lib/utils/useAlternatePageUtils.d.ts.map +1 -1
- package/lib/utils/useAlternatePageUtils.js +6 -3
- package/lib/utils/useAlternatePageUtils.js.map +1 -1
- package/lib/utils/useLocalPathname.d.ts +5 -0
- package/lib/utils/useLocalPathname.d.ts.map +1 -1
- package/lib/utils/useLocalPathname.js +6 -4
- package/lib/utils/useLocalPathname.js.map +1 -1
- package/lib/utils/useLocationChange.d.ts +7 -5
- package/lib/utils/useLocationChange.d.ts.map +1 -1
- package/lib/utils/useLocationChange.js +6 -2
- package/lib/utils/useLocationChange.js.map +1 -1
- package/lib/utils/usePluralForm.d.ts +11 -0
- package/lib/utils/usePluralForm.d.ts.map +1 -1
- package/lib/utils/usePluralForm.js +19 -24
- package/lib/utils/usePluralForm.js.map +1 -1
- package/lib/utils/useThemeConfig.d.ts +28 -22
- package/lib/utils/useThemeConfig.d.ts.map +1 -1
- package/lib/utils/useThemeConfig.js +3 -0
- package/lib/utils/useThemeConfig.js.map +1 -1
- package/package.json +18 -11
- package/src/components/Collapsible/index.tsx +42 -24
- package/src/components/Details/index.tsx +15 -8
- package/src/components/Details/styles.module.css +4 -0
- package/src/{utils/announcementBarUtils.tsx → contexts/announcementBar.tsx} +20 -22
- package/src/contexts/colorMode.tsx +199 -0
- package/src/contexts/docSidebarItemsExpandedState.tsx +55 -0
- package/src/contexts/docsPreferredVersion.tsx +253 -0
- package/src/contexts/docsSidebar.tsx +50 -0
- package/src/contexts/docsVersion.tsx +36 -0
- package/src/contexts/navbarMobileSidebar.tsx +99 -0
- package/src/contexts/navbarSecondaryMenu/content.tsx +110 -0
- package/src/contexts/navbarSecondaryMenu/display.tsx +102 -0
- package/src/{utils/tabGroupChoiceUtils.tsx → contexts/tabGroupChoice.tsx} +28 -33
- package/src/hooks/useBackToTopButton.ts +73 -0
- package/src/hooks/useCodeWordWrap.ts +56 -0
- package/src/hooks/useHideableNavbar.ts +12 -14
- package/src/hooks/useKeyboardNavigation.ts +11 -3
- package/src/hooks/useLockBodyScroll.ts +5 -2
- package/src/hooks/usePrismTheme.ts +10 -6
- package/src/hooks/useSearchPage.ts +18 -5
- package/src/hooks/useSkipToContent.ts +58 -0
- package/src/{utils → hooks}/useTOCHighlight.ts +24 -15
- package/src/hooks/useWindowSize.ts +14 -12
- package/src/index.ts +77 -57
- package/src/utils/ThemeClassNames.ts +10 -6
- package/src/utils/codeBlockUtils.ts +178 -92
- package/src/utils/docsUtils.tsx +205 -101
- package/src/utils/footerUtils.ts +18 -0
- package/src/utils/generalUtils.ts +6 -3
- package/src/utils/historyUtils.ts +11 -17
- package/src/utils/jsUtils.ts +1 -1
- package/src/utils/metadataUtils.tsx +115 -0
- package/src/utils/navbarUtils.tsx +45 -0
- package/src/utils/reactUtils.tsx +40 -7
- package/src/utils/regexpUtils.ts +2 -1
- package/src/utils/routesUtils.ts +58 -22
- package/src/utils/scrollUtils.tsx +120 -49
- package/src/utils/searchUtils.ts +51 -0
- package/src/utils/storageUtils.ts +23 -15
- package/src/utils/tagsUtils.ts +12 -10
- package/src/utils/tocUtils.ts +22 -11
- package/src/utils/useAlternatePageUtils.ts +17 -5
- package/src/utils/useLocalPathname.ts +6 -4
- package/src/utils/useLocationChange.ts +12 -10
- package/src/utils/usePluralForm.ts +27 -24
- package/src/utils/useThemeConfig.ts +23 -22
- package/yarn-error.log +20199 -0
- package/lib/utils/announcementBarUtils.d.ts.map +0 -1
- package/lib/utils/announcementBarUtils.js.map +0 -1
- package/lib/utils/colorModeUtils.d.ts +0 -18
- package/lib/utils/colorModeUtils.d.ts.map +0 -1
- package/lib/utils/colorModeUtils.js +0 -106
- package/lib/utils/colorModeUtils.js.map +0 -1
- package/lib/utils/docSidebarItemsExpandedState.d.ts +0 -17
- package/lib/utils/docSidebarItemsExpandedState.d.ts.map +0 -1
- package/lib/utils/docSidebarItemsExpandedState.js.map +0 -1
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +0 -22
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts.map +0 -1
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +0 -91
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js.map +0 -1
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts +0 -14
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts.map +0 -1
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +0 -19
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js.map +0 -1
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +0 -14
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts.map +0 -1
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +0 -41
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js.map +0 -1
- package/lib/utils/mobileSecondaryMenu.d.ts +0 -21
- package/lib/utils/mobileSecondaryMenu.d.ts.map +0 -1
- package/lib/utils/mobileSecondaryMenu.js +0 -50
- package/lib/utils/mobileSecondaryMenu.js.map +0 -1
- package/lib/utils/pathUtils.d.ts +0 -8
- package/lib/utils/pathUtils.d.ts.map +0 -1
- package/lib/utils/pathUtils.js +0 -17
- package/lib/utils/pathUtils.js.map +0 -1
- package/lib/utils/tabGroupChoiceUtils.d.ts.map +0 -1
- package/lib/utils/tabGroupChoiceUtils.js.map +0 -1
- package/lib/utils/useContextualSearchFilters.d.ts +0 -12
- package/lib/utils/useContextualSearchFilters.d.ts.map +0 -1
- package/lib/utils/useContextualSearchFilters.js +0 -37
- package/lib/utils/useContextualSearchFilters.js.map +0 -1
- package/lib/utils/usePrevious.d.ts +0 -8
- package/lib/utils/usePrevious.d.ts.map +0 -1
- package/lib/utils/usePrevious.js +0 -16
- package/lib/utils/usePrevious.js.map +0 -1
- package/lib/utils/useTOCHighlight.d.ts +0 -14
- package/lib/utils/useTOCHighlight.d.ts.map +0 -1
- package/lib/utils/useTOCHighlight.js.map +0 -1
- package/src/utils/colorModeUtils.tsx +0 -156
- package/src/utils/docSidebarItemsExpandedState.tsx +0 -41
- package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +0 -167
- package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +0 -33
- package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +0 -70
- package/src/utils/mobileSecondaryMenu.tsx +0 -115
- package/src/utils/pathUtils.ts +0 -19
- package/src/utils/useContextualSearchFilters.ts +0 -53
- package/src/utils/usePrevious.ts +0 -19
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import React, {
|
|
9
|
+
useState,
|
|
10
|
+
useContext,
|
|
11
|
+
useEffect,
|
|
12
|
+
useMemo,
|
|
13
|
+
type ReactNode,
|
|
14
|
+
type ComponentType,
|
|
15
|
+
} from 'react';
|
|
16
|
+
import {ReactContextError} from '../../utils/reactUtils';
|
|
17
|
+
|
|
18
|
+
// This context represents a "global layout store". A component (usually a
|
|
19
|
+
// layout component) can request filling this store through
|
|
20
|
+
// `NavbarSecondaryMenuFiller`. It doesn't actually control rendering by itself,
|
|
21
|
+
// and this context should be considered internal implementation. The user-
|
|
22
|
+
// facing value comes from `display.tsx`, which takes the `component` and
|
|
23
|
+
// `props` stored here and renders the actual element.
|
|
24
|
+
|
|
25
|
+
export type NavbarSecondaryMenuComponent<Props> = ComponentType<Props>;
|
|
26
|
+
|
|
27
|
+
/** @internal */
|
|
28
|
+
export type Content =
|
|
29
|
+
| {
|
|
30
|
+
component: NavbarSecondaryMenuComponent<object>;
|
|
31
|
+
props: object;
|
|
32
|
+
}
|
|
33
|
+
| {component: null; props: null};
|
|
34
|
+
|
|
35
|
+
type ContextValue = [
|
|
36
|
+
content: Content,
|
|
37
|
+
setContent: React.Dispatch<React.SetStateAction<Content>>,
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
const Context = React.createContext<ContextValue | null>(null);
|
|
41
|
+
|
|
42
|
+
/** @internal */
|
|
43
|
+
export function NavbarSecondaryMenuContentProvider({
|
|
44
|
+
children,
|
|
45
|
+
}: {
|
|
46
|
+
children: ReactNode;
|
|
47
|
+
}): JSX.Element {
|
|
48
|
+
const value = useState({component: null, props: null});
|
|
49
|
+
return (
|
|
50
|
+
// @ts-expect-error: this context is hard to type
|
|
51
|
+
<Context.Provider value={value}>{children}</Context.Provider>
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** @internal */
|
|
56
|
+
export function useNavbarSecondaryMenuContent(): Content {
|
|
57
|
+
const value = useContext(Context);
|
|
58
|
+
if (!value) {
|
|
59
|
+
throw new ReactContextError('NavbarSecondaryMenuContentProvider');
|
|
60
|
+
}
|
|
61
|
+
return value[0];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function useShallowMemoizedObject<O>(obj: O) {
|
|
65
|
+
return useMemo(
|
|
66
|
+
() => obj,
|
|
67
|
+
// Is this safe?
|
|
68
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
69
|
+
[...Object.keys(obj), ...Object.values(obj)],
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* This component renders nothing by itself, but it fills the placeholder in the
|
|
75
|
+
* generic secondary menu layout. This reduces coupling between the main layout
|
|
76
|
+
* and the specific page.
|
|
77
|
+
*
|
|
78
|
+
* This kind of feature is often called portal/teleport/gateway/outlet...
|
|
79
|
+
* Various unmaintained React libs exist. Most up-to-date one:
|
|
80
|
+
* https://github.com/gregberge/react-teleporter
|
|
81
|
+
* Not sure any of those is safe regarding concurrent mode.
|
|
82
|
+
*/
|
|
83
|
+
export function NavbarSecondaryMenuFiller<P extends object>({
|
|
84
|
+
component,
|
|
85
|
+
props,
|
|
86
|
+
}: {
|
|
87
|
+
component: NavbarSecondaryMenuComponent<P>;
|
|
88
|
+
props: P;
|
|
89
|
+
}): JSX.Element | null {
|
|
90
|
+
const context = useContext(Context);
|
|
91
|
+
if (!context) {
|
|
92
|
+
throw new ReactContextError('NavbarSecondaryMenuContentProvider');
|
|
93
|
+
}
|
|
94
|
+
const [, setContent] = context;
|
|
95
|
+
|
|
96
|
+
// To avoid useless context re-renders, props are memoized shallowly
|
|
97
|
+
const memoizedProps = useShallowMemoizedObject(props);
|
|
98
|
+
|
|
99
|
+
useEffect(() => {
|
|
100
|
+
// @ts-expect-error: this context is hard to type
|
|
101
|
+
setContent({component, props: memoizedProps});
|
|
102
|
+
}, [setContent, component, memoizedProps]);
|
|
103
|
+
|
|
104
|
+
useEffect(
|
|
105
|
+
() => () => setContent({component: null, props: null}),
|
|
106
|
+
[setContent],
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import React, {
|
|
9
|
+
useState,
|
|
10
|
+
useContext,
|
|
11
|
+
useEffect,
|
|
12
|
+
useMemo,
|
|
13
|
+
useCallback,
|
|
14
|
+
type ReactNode,
|
|
15
|
+
} from 'react';
|
|
16
|
+
import {ReactContextError, usePrevious} from '../../utils/reactUtils';
|
|
17
|
+
import {useNavbarMobileSidebar} from '../navbarMobileSidebar';
|
|
18
|
+
import {useNavbarSecondaryMenuContent, type Content} from './content';
|
|
19
|
+
|
|
20
|
+
type ContextValue = [
|
|
21
|
+
shown: boolean,
|
|
22
|
+
setShown: React.Dispatch<React.SetStateAction<boolean>>,
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
const Context = React.createContext<ContextValue | null>(null);
|
|
26
|
+
|
|
27
|
+
function useContextValue(): ContextValue {
|
|
28
|
+
const mobileSidebar = useNavbarMobileSidebar();
|
|
29
|
+
const content = useNavbarSecondaryMenuContent();
|
|
30
|
+
|
|
31
|
+
const [shown, setShown] = useState(false);
|
|
32
|
+
|
|
33
|
+
const hasContent = content.component !== null;
|
|
34
|
+
const previousHasContent = usePrevious(hasContent);
|
|
35
|
+
|
|
36
|
+
// When content is become available for the first time (set in useEffect)
|
|
37
|
+
// we set this content to be shown!
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
const contentBecameAvailable = hasContent && !previousHasContent;
|
|
40
|
+
if (contentBecameAvailable) {
|
|
41
|
+
setShown(true);
|
|
42
|
+
}
|
|
43
|
+
}, [hasContent, previousHasContent]);
|
|
44
|
+
|
|
45
|
+
// On sidebar close, secondary menu is set to be shown on next re-opening
|
|
46
|
+
// (if any secondary menu content available)
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
if (!hasContent) {
|
|
49
|
+
setShown(false);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (!mobileSidebar.shown) {
|
|
53
|
+
setShown(true);
|
|
54
|
+
}
|
|
55
|
+
}, [mobileSidebar.shown, hasContent]);
|
|
56
|
+
|
|
57
|
+
return useMemo(() => [shown, setShown], [shown]);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** @internal */
|
|
61
|
+
export function NavbarSecondaryMenuDisplayProvider({
|
|
62
|
+
children,
|
|
63
|
+
}: {
|
|
64
|
+
children: ReactNode;
|
|
65
|
+
}): JSX.Element {
|
|
66
|
+
const value = useContextValue();
|
|
67
|
+
return <Context.Provider value={value}>{children}</Context.Provider>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function renderElement(content: Content): JSX.Element | undefined {
|
|
71
|
+
if (content.component) {
|
|
72
|
+
const Comp = content.component;
|
|
73
|
+
return <Comp {...content.props} />;
|
|
74
|
+
}
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Wires the logic for rendering the mobile navbar secondary menu. */
|
|
79
|
+
export function useNavbarSecondaryMenu(): {
|
|
80
|
+
/** Whether secondary menu is displayed. */
|
|
81
|
+
shown: boolean;
|
|
82
|
+
/**
|
|
83
|
+
* Hide the secondary menu; fired either when hiding the entire sidebar, or
|
|
84
|
+
* when going back to the primary menu.
|
|
85
|
+
*/
|
|
86
|
+
hide: () => void;
|
|
87
|
+
/** The content returned from the current secondary menu filler. */
|
|
88
|
+
content: JSX.Element | undefined;
|
|
89
|
+
} {
|
|
90
|
+
const value = useContext(Context);
|
|
91
|
+
if (!value) {
|
|
92
|
+
throw new ReactContextError('NavbarSecondaryMenuDisplayProvider');
|
|
93
|
+
}
|
|
94
|
+
const [shown, setShown] = value;
|
|
95
|
+
const hide = useCallback(() => setShown(false), [setShown]);
|
|
96
|
+
const content = useNavbarSecondaryMenuContent();
|
|
97
|
+
|
|
98
|
+
return useMemo(
|
|
99
|
+
() => ({shown, hide, content: renderElement(content)}),
|
|
100
|
+
[hide, content, shown],
|
|
101
|
+
);
|
|
102
|
+
}
|
|
@@ -9,35 +9,38 @@ import React, {
|
|
|
9
9
|
useState,
|
|
10
10
|
useCallback,
|
|
11
11
|
useEffect,
|
|
12
|
-
createContext,
|
|
13
12
|
useMemo,
|
|
14
13
|
useContext,
|
|
15
14
|
type ReactNode,
|
|
16
15
|
} from 'react';
|
|
17
|
-
import {createStorageSlot, listStorageKeys} from '
|
|
16
|
+
import {createStorageSlot, listStorageKeys} from '../utils/storageUtils';
|
|
17
|
+
import {ReactContextError} from '../utils/reactUtils';
|
|
18
18
|
|
|
19
19
|
const TAB_CHOICE_PREFIX = 'docusaurus.tab.';
|
|
20
20
|
|
|
21
|
-
type
|
|
21
|
+
type ContextValue = {
|
|
22
|
+
/** A map from `groupId` to the `value` of the saved choice. */
|
|
22
23
|
readonly tabGroupChoices: {readonly [groupId: string]: string};
|
|
24
|
+
/** Set the new choice value of a group. */
|
|
23
25
|
readonly setTabGroupChoices: (groupId: string, newChoice: string) => void;
|
|
24
26
|
};
|
|
25
27
|
|
|
26
|
-
const
|
|
27
|
-
TabGroupChoiceContextValue | undefined
|
|
28
|
-
>(undefined);
|
|
28
|
+
const Context = React.createContext<ContextValue | undefined>(undefined);
|
|
29
29
|
|
|
30
|
-
function
|
|
30
|
+
function useContextValue(): ContextValue {
|
|
31
31
|
const [tabGroupChoices, setChoices] = useState<{
|
|
32
32
|
readonly [groupId: string]: string;
|
|
33
33
|
}>({});
|
|
34
|
-
const setChoiceSyncWithLocalStorage = useCallback(
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
const setChoiceSyncWithLocalStorage = useCallback(
|
|
35
|
+
(groupId: string, newChoice: string) => {
|
|
36
|
+
createStorageSlot(`${TAB_CHOICE_PREFIX}${groupId}`).set(newChoice);
|
|
37
|
+
},
|
|
38
|
+
[],
|
|
39
|
+
);
|
|
37
40
|
|
|
38
41
|
useEffect(() => {
|
|
39
42
|
try {
|
|
40
|
-
const localStorageChoices:
|
|
43
|
+
const localStorageChoices: {[groupId: string]: string} = {};
|
|
41
44
|
listStorageKeys().forEach((storageKey) => {
|
|
42
45
|
if (storageKey.startsWith(TAB_CHOICE_PREFIX)) {
|
|
43
46
|
const groupId = storageKey.substring(TAB_CHOICE_PREFIX.length);
|
|
@@ -50,13 +53,18 @@ function useTabGroupChoiceContextValue(): TabGroupChoiceContextValue {
|
|
|
50
53
|
}
|
|
51
54
|
}, []);
|
|
52
55
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
setTabGroupChoices: (groupId: string, newChoice: string) => {
|
|
56
|
+
const setTabGroupChoices = useCallback(
|
|
57
|
+
(groupId: string, newChoice: string) => {
|
|
56
58
|
setChoices((oldChoices) => ({...oldChoices, [groupId]: newChoice}));
|
|
57
59
|
setChoiceSyncWithLocalStorage(groupId, newChoice);
|
|
58
60
|
},
|
|
59
|
-
|
|
61
|
+
[setChoiceSyncWithLocalStorage],
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
return useMemo(
|
|
65
|
+
() => ({tabGroupChoices, setTabGroupChoices}),
|
|
66
|
+
[tabGroupChoices, setTabGroupChoices],
|
|
67
|
+
);
|
|
60
68
|
}
|
|
61
69
|
|
|
62
70
|
export function TabGroupChoiceProvider({
|
|
@@ -64,27 +72,14 @@ export function TabGroupChoiceProvider({
|
|
|
64
72
|
}: {
|
|
65
73
|
children: ReactNode;
|
|
66
74
|
}): JSX.Element {
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
() => ({
|
|
70
|
-
tabGroupChoices,
|
|
71
|
-
setTabGroupChoices,
|
|
72
|
-
}),
|
|
73
|
-
[tabGroupChoices, setTabGroupChoices],
|
|
74
|
-
);
|
|
75
|
-
return (
|
|
76
|
-
<TabGroupChoiceContext.Provider value={contextValue}>
|
|
77
|
-
{children}
|
|
78
|
-
</TabGroupChoiceContext.Provider>
|
|
79
|
-
);
|
|
75
|
+
const value = useContextValue();
|
|
76
|
+
return <Context.Provider value={value}>{children}</Context.Provider>;
|
|
80
77
|
}
|
|
81
78
|
|
|
82
|
-
export function useTabGroupChoice():
|
|
83
|
-
const context = useContext(
|
|
79
|
+
export function useTabGroupChoice(): ContextValue {
|
|
80
|
+
const context = useContext(Context);
|
|
84
81
|
if (context == null) {
|
|
85
|
-
throw new
|
|
86
|
-
'"useUserPreferencesContext" is used outside of "Layout" component.',
|
|
87
|
-
);
|
|
82
|
+
throw new ReactContextError('TabGroupChoiceProvider');
|
|
88
83
|
}
|
|
89
84
|
return context;
|
|
90
85
|
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {useRef, useState} from 'react';
|
|
9
|
+
import {useScrollPosition, useSmoothScrollTo} from '../utils/scrollUtils';
|
|
10
|
+
import {useLocationChange} from '../utils/useLocationChange';
|
|
11
|
+
|
|
12
|
+
/** Wires the logic for the back to top button. */
|
|
13
|
+
export function useBackToTopButton({
|
|
14
|
+
threshold,
|
|
15
|
+
}: {
|
|
16
|
+
/**
|
|
17
|
+
* The minimum vertical scroll position, above which a scroll-up would not
|
|
18
|
+
* cause `shown` to become `true`. This is because BTT is only useful if the
|
|
19
|
+
* user is far down the page.
|
|
20
|
+
*/
|
|
21
|
+
threshold: number;
|
|
22
|
+
}): {
|
|
23
|
+
/**
|
|
24
|
+
* Whether the button should be displayed. We only show if the user has
|
|
25
|
+
* scrolled up and is on a vertical position greater than `threshold`.
|
|
26
|
+
*/
|
|
27
|
+
shown: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* A (memoized) handle for starting the scroll, which you can directly plug
|
|
30
|
+
* into the props.
|
|
31
|
+
*/
|
|
32
|
+
scrollToTop: () => void;
|
|
33
|
+
} {
|
|
34
|
+
const [shown, setShown] = useState(false);
|
|
35
|
+
const isFocusedAnchor = useRef(false);
|
|
36
|
+
const {startScroll, cancelScroll} = useSmoothScrollTo();
|
|
37
|
+
|
|
38
|
+
useScrollPosition(({scrollY: scrollTop}, lastPosition) => {
|
|
39
|
+
const lastScrollTop = lastPosition?.scrollY;
|
|
40
|
+
// Component is just being mounted. Not really a scroll event from the user.
|
|
41
|
+
// Ignore it.
|
|
42
|
+
if (!lastScrollTop) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (isFocusedAnchor.current) {
|
|
46
|
+
// This scroll position change is triggered by navigating to an anchor.
|
|
47
|
+
// Ignore it.
|
|
48
|
+
isFocusedAnchor.current = false;
|
|
49
|
+
} else if (scrollTop >= lastScrollTop) {
|
|
50
|
+
// The user has scrolled down to "fight against" the animation. Cancel any
|
|
51
|
+
// animation under progress.
|
|
52
|
+
cancelScroll();
|
|
53
|
+
setShown(false);
|
|
54
|
+
} else if (scrollTop < threshold) {
|
|
55
|
+
// Scrolled to the minimum position; hide the button.
|
|
56
|
+
setShown(false);
|
|
57
|
+
} else if (
|
|
58
|
+
scrollTop + window.innerHeight <
|
|
59
|
+
document.documentElement.scrollHeight
|
|
60
|
+
) {
|
|
61
|
+
setShown(true);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
useLocationChange((locationChangeEvent) => {
|
|
66
|
+
if (locationChangeEvent.location.hash) {
|
|
67
|
+
isFocusedAnchor.current = true;
|
|
68
|
+
setShown(false);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
return {shown, scrollToTop: () => startScroll(0)};
|
|
73
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type {RefObject} from 'react';
|
|
9
|
+
import {useState, useCallback, useEffect, useRef} from 'react';
|
|
10
|
+
|
|
11
|
+
export function useCodeWordWrap(): {
|
|
12
|
+
readonly codeBlockRef: RefObject<HTMLPreElement>;
|
|
13
|
+
readonly isEnabled: boolean;
|
|
14
|
+
readonly isCodeScrollable: boolean;
|
|
15
|
+
readonly toggle: () => void;
|
|
16
|
+
} {
|
|
17
|
+
const [isEnabled, setIsEnabled] = useState(false);
|
|
18
|
+
const [isCodeScrollable, setIsCodeScrollable] = useState<boolean>(false);
|
|
19
|
+
const codeBlockRef = useRef<HTMLPreElement>(null);
|
|
20
|
+
|
|
21
|
+
const toggle = useCallback(() => {
|
|
22
|
+
const codeElement = codeBlockRef.current!.querySelector('code')!;
|
|
23
|
+
|
|
24
|
+
if (isEnabled) {
|
|
25
|
+
codeElement.removeAttribute('style');
|
|
26
|
+
} else {
|
|
27
|
+
codeElement.style.whiteSpace = 'pre-wrap';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
setIsEnabled((value) => !value);
|
|
31
|
+
}, [codeBlockRef, isEnabled]);
|
|
32
|
+
|
|
33
|
+
const updateCodeIsScrollable = useCallback(() => {
|
|
34
|
+
const {scrollWidth, clientWidth} = codeBlockRef.current!;
|
|
35
|
+
const isScrollable =
|
|
36
|
+
scrollWidth > clientWidth ||
|
|
37
|
+
codeBlockRef.current!.querySelector('code')!.hasAttribute('style');
|
|
38
|
+
setIsCodeScrollable(isScrollable);
|
|
39
|
+
}, [codeBlockRef]);
|
|
40
|
+
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
updateCodeIsScrollable();
|
|
43
|
+
}, [isEnabled, updateCodeIsScrollable]);
|
|
44
|
+
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
window.addEventListener('resize', updateCodeIsScrollable, {
|
|
47
|
+
passive: true,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
return () => {
|
|
51
|
+
window.removeEventListener('resize', updateCodeIsScrollable);
|
|
52
|
+
};
|
|
53
|
+
}, [updateCodeIsScrollable]);
|
|
54
|
+
|
|
55
|
+
return {codeBlockRef, isEnabled, isCodeScrollable, toggle};
|
|
56
|
+
}
|
|
@@ -9,14 +9,16 @@ import {useState, useCallback, useRef} from 'react';
|
|
|
9
9
|
import {useLocationChange} from '../utils/useLocationChange';
|
|
10
10
|
import {useScrollPosition} from '../utils/scrollUtils';
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Wires the imperative logic of a hideable navbar.
|
|
14
|
+
* @param hideOnScroll If `false`, this hook is basically a no-op.
|
|
15
|
+
*/
|
|
16
|
+
export function useHideableNavbar(hideOnScroll: boolean): {
|
|
17
|
+
/** A ref to the navbar component. Plug this into the actual element. */
|
|
13
18
|
readonly navbarRef: (node: HTMLElement | null) => void;
|
|
19
|
+
/** If `false`, the navbar component should not be rendered. */
|
|
14
20
|
readonly isNavbarVisible: boolean;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export default function useHideableNavbar(
|
|
18
|
-
hideOnScroll: boolean,
|
|
19
|
-
): UseHideableNavbarReturns {
|
|
21
|
+
} {
|
|
20
22
|
const [isNavbarVisible, setIsNavbarVisible] = useState(hideOnScroll);
|
|
21
23
|
const isFocusedAnchor = useRef(false);
|
|
22
24
|
const navbarHeight = useRef(0);
|
|
@@ -26,14 +28,13 @@ export default function useHideableNavbar(
|
|
|
26
28
|
}
|
|
27
29
|
}, []);
|
|
28
30
|
|
|
29
|
-
useScrollPosition((
|
|
31
|
+
useScrollPosition(({scrollY: scrollTop}, lastPosition) => {
|
|
30
32
|
if (!hideOnScroll) {
|
|
31
33
|
return;
|
|
32
34
|
}
|
|
33
35
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
// It needed for mostly to handle rubber band scrolling
|
|
36
|
+
// Needed mostly for handling rubber band scrolling.
|
|
37
|
+
// See https://github.com/facebook/docusaurus/pull/5721
|
|
37
38
|
if (scrollTop < navbarHeight.current) {
|
|
38
39
|
setIsNavbarVisible(true);
|
|
39
40
|
return;
|
|
@@ -70,8 +71,5 @@ export default function useHideableNavbar(
|
|
|
70
71
|
setIsNavbarVisible(true);
|
|
71
72
|
});
|
|
72
73
|
|
|
73
|
-
return {
|
|
74
|
-
navbarRef,
|
|
75
|
-
isNavbarVisible,
|
|
76
|
-
};
|
|
74
|
+
return {navbarRef, isNavbarVisible};
|
|
77
75
|
}
|
|
@@ -11,9 +11,17 @@ import './styles.css';
|
|
|
11
11
|
|
|
12
12
|
export const keyboardFocusedClassName = 'navigation-with-keyboard';
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Side-effect that adds the `keyboardFocusedClassName` to the body element when
|
|
16
|
+
* the keyboard has been pressed, or removes it when the mouse is clicked.
|
|
17
|
+
*
|
|
18
|
+
* The presence of this class name signals that the user may be using keyboard
|
|
19
|
+
* for navigation, and the theme **must** add focus outline when this class name
|
|
20
|
+
* is present. (And optionally not if it's absent, for design purposes)
|
|
21
|
+
*
|
|
22
|
+
* Inspired by https://hackernoon.com/removing-that-ugly-focus-ring-and-keeping-it-too-6c8727fefcd2
|
|
23
|
+
*/
|
|
24
|
+
export function useKeyboardNavigation(): void {
|
|
17
25
|
useEffect(() => {
|
|
18
26
|
function handleOutlineStyles(e: MouseEvent | KeyboardEvent) {
|
|
19
27
|
if (e.type === 'keydown' && (e as KeyboardEvent).key === 'Tab') {
|
|
@@ -7,10 +7,13 @@
|
|
|
7
7
|
|
|
8
8
|
import {useEffect} from 'react';
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Side-effect that locks the document body's scroll throughout the lifetime of
|
|
12
|
+
* the containing component. e.g. when the mobile sidebar is expanded.
|
|
13
|
+
*/
|
|
14
|
+
export function useLockBodyScroll(lock: boolean = true): void {
|
|
11
15
|
useEffect(() => {
|
|
12
16
|
document.body.style.overflow = lock ? 'hidden' : 'visible';
|
|
13
|
-
|
|
14
17
|
return () => {
|
|
15
18
|
document.body.style.overflow = 'visible';
|
|
16
19
|
};
|
|
@@ -5,16 +5,20 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import
|
|
9
|
-
import {useColorMode} from '../
|
|
8
|
+
import type {PrismTheme} from 'prism-react-renderer';
|
|
9
|
+
import {useColorMode} from '../contexts/colorMode';
|
|
10
10
|
import {useThemeConfig} from '../utils/useThemeConfig';
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Returns a color-mode-dependent Prism theme: whatever the user specified in
|
|
14
|
+
* the config. Falls back to `palenight`.
|
|
15
|
+
*/
|
|
16
|
+
export function usePrismTheme(): PrismTheme {
|
|
13
17
|
const {prism} = useThemeConfig();
|
|
14
|
-
const {
|
|
15
|
-
const lightModeTheme = prism.theme
|
|
18
|
+
const {colorMode} = useColorMode();
|
|
19
|
+
const lightModeTheme = prism.theme;
|
|
16
20
|
const darkModeTheme = prism.darkTheme || lightModeTheme;
|
|
17
|
-
const prismTheme =
|
|
21
|
+
const prismTheme = colorMode === 'dark' ? darkModeTheme : lightModeTheme;
|
|
18
22
|
|
|
19
23
|
return prismTheme;
|
|
20
24
|
}
|
|
@@ -11,13 +11,24 @@ import {useCallback, useEffect, useState} from 'react';
|
|
|
11
11
|
|
|
12
12
|
const SEARCH_PARAM_QUERY = 'q';
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
/** Some utility functions around search queries. */
|
|
15
|
+
export function useSearchPage(): {
|
|
16
|
+
/**
|
|
17
|
+
* Works hand-in-hand with `setSearchQuery`; whatever the user has inputted
|
|
18
|
+
* into the search box.
|
|
19
|
+
*/
|
|
15
20
|
searchQuery: string;
|
|
21
|
+
/**
|
|
22
|
+
* Set a new search query. In addition to updating `searchQuery`, this handle
|
|
23
|
+
* also mutates the location and appends the query.
|
|
24
|
+
*/
|
|
16
25
|
setSearchQuery: (newSearchQuery: string) => void;
|
|
26
|
+
/**
|
|
27
|
+
* Given a query, this handle generates the corresponding search page link,
|
|
28
|
+
* with base URL prepended.
|
|
29
|
+
*/
|
|
17
30
|
generateSearchPageLink: (targetSearchQuery: string) => string;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export default function useSearchPage(): UseSearchPageReturn {
|
|
31
|
+
} {
|
|
21
32
|
const history = useHistory();
|
|
22
33
|
const {
|
|
23
34
|
siteConfig: {baseUrl},
|
|
@@ -54,7 +65,9 @@ export default function useSearchPage(): UseSearchPageReturn {
|
|
|
54
65
|
const generateSearchPageLink = useCallback(
|
|
55
66
|
(targetSearchQuery: string) =>
|
|
56
67
|
// Refer to https://github.com/facebook/docusaurus/pull/2838
|
|
57
|
-
`${baseUrl}search
|
|
68
|
+
`${baseUrl}search?${SEARCH_PARAM_QUERY}=${encodeURIComponent(
|
|
69
|
+
targetSearchQuery,
|
|
70
|
+
)}`,
|
|
58
71
|
[baseUrl],
|
|
59
72
|
);
|
|
60
73
|
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type React from 'react';
|
|
9
|
+
import {useCallback, useRef} from 'react';
|
|
10
|
+
import {useHistory} from '@docusaurus/router';
|
|
11
|
+
import {useLocationChange} from '../utils/useLocationChange';
|
|
12
|
+
import {ThemeClassNames} from '../utils/ThemeClassNames';
|
|
13
|
+
|
|
14
|
+
function programmaticFocus(el: HTMLElement) {
|
|
15
|
+
el.setAttribute('tabindex', '-1');
|
|
16
|
+
el.focus();
|
|
17
|
+
el.removeAttribute('tabindex');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** This hook wires the logic for a skip-to-content link. */
|
|
21
|
+
export function useSkipToContent(): {
|
|
22
|
+
/**
|
|
23
|
+
* The ref to the container. On page transition, the container will be focused
|
|
24
|
+
* so that keyboard navigators can instantly interact with the link and jump
|
|
25
|
+
* to content. **Note:** the type is `RefObject<HTMLDivElement>` only because
|
|
26
|
+
* the typing for refs don't reflect that the `ref` prop is contravariant, so
|
|
27
|
+
* using `HTMLElement` causes type-checking to fail. You can plug the ref into
|
|
28
|
+
* any HTML element, as long as it can be focused.
|
|
29
|
+
*/
|
|
30
|
+
containerRef: React.RefObject<HTMLDivElement>;
|
|
31
|
+
/**
|
|
32
|
+
* Callback fired when the skip to content link has been interacted with. It
|
|
33
|
+
* will programmatically focus the main content.
|
|
34
|
+
*/
|
|
35
|
+
handleSkip: (e: React.MouseEvent<HTMLAnchorElement>) => void;
|
|
36
|
+
} {
|
|
37
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
38
|
+
const {action} = useHistory();
|
|
39
|
+
const handleSkip = useCallback((e: React.MouseEvent<HTMLAnchorElement>) => {
|
|
40
|
+
e.preventDefault();
|
|
41
|
+
|
|
42
|
+
const targetElement: HTMLElement | null =
|
|
43
|
+
document.querySelector('main:first-of-type') ||
|
|
44
|
+
document.querySelector(`.${ThemeClassNames.wrapper.main}`);
|
|
45
|
+
|
|
46
|
+
if (targetElement) {
|
|
47
|
+
programmaticFocus(targetElement);
|
|
48
|
+
}
|
|
49
|
+
}, []);
|
|
50
|
+
|
|
51
|
+
useLocationChange(({location}) => {
|
|
52
|
+
if (containerRef.current && !location.hash && action === 'PUSH') {
|
|
53
|
+
programmaticFocus(containerRef.current);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
return {containerRef, handleSkip};
|
|
58
|
+
}
|