@docusaurus/theme-common 2.0.0-beta.ff31de0ff → 2.0.1
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/lib/components/Collapsible/index.d.ts +64 -0
- package/lib/components/Collapsible/index.d.ts.map +1 -0
- package/lib/components/Collapsible/index.js +150 -0
- package/lib/components/Collapsible/index.js.map +1 -0
- package/lib/components/Details/index.d.ts +17 -0
- package/lib/components/Details/index.d.ts.map +1 -0
- package/lib/components/Details/index.js +76 -0
- package/lib/components/Details/index.js.map +1 -0
- package/lib/components/Details/styles.module.css +62 -0
- package/lib/contexts/announcementBar.d.ts +22 -0
- package/lib/contexts/announcementBar.d.ts.map +1 -0
- package/lib/contexts/announcementBar.js +72 -0
- package/lib/contexts/announcementBar.js.map +1 -0
- package/lib/contexts/blogPost.d.ts +33 -0
- package/lib/contexts/blogPost.d.ts.map +1 -0
- package/lib/contexts/blogPost.js +46 -0
- package/lib/contexts/blogPost.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/doc.d.ts +30 -0
- package/lib/contexts/doc.d.ts.map +1 -0
- package/lib/contexts/doc.js +48 -0
- package/lib/contexts/doc.js.map +1 -0
- package/lib/contexts/docSidebarItemsExpandedState.d.ts +31 -0
- package/lib/contexts/docSidebarItemsExpandedState.d.ts.map +1 -0
- package/lib/contexts/docSidebarItemsExpandedState.js +28 -0
- package/lib/contexts/docSidebarItemsExpandedState.js.map +1 -0
- package/lib/contexts/docsPreferredVersion.d.ts +30 -0
- package/lib/contexts/docsPreferredVersion.d.ts.map +1 -0
- package/lib/contexts/docsPreferredVersion.js +130 -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 +50 -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/contexts/tabGroupChoice.d.ts +21 -0
- package/lib/contexts/tabGroupChoice.d.ts.map +1 -0
- package/lib/contexts/tabGroupChoice.js +49 -0
- package/lib/contexts/tabGroupChoice.js.map +1 -0
- package/lib/{utils/useChangeRoute.d.ts → hooks/styles.css} +4 -1
- 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 +67 -0
- package/lib/hooks/useCodeWordWrap.js.map +1 -0
- package/lib/hooks/useHideableNavbar.d.ts +17 -0
- package/lib/hooks/useHideableNavbar.d.ts.map +1 -0
- package/lib/hooks/useHideableNavbar.js +60 -0
- package/lib/hooks/useHideableNavbar.js.map +1 -0
- package/lib/hooks/useKeyboardNavigation.d.ts +20 -0
- package/lib/hooks/useKeyboardNavigation.d.ts.map +1 -0
- package/lib/hooks/useKeyboardNavigation.js +39 -0
- package/lib/hooks/useKeyboardNavigation.js.map +1 -0
- package/lib/hooks/useLockBodyScroll.d.ts +12 -0
- package/lib/hooks/useLockBodyScroll.d.ts.map +1 -0
- package/lib/hooks/useLockBodyScroll.js +20 -0
- package/lib/hooks/useLockBodyScroll.js.map +1 -0
- package/lib/hooks/useMutationObserver.d.ts +4 -0
- package/lib/hooks/useMutationObserver.d.ts.map +1 -0
- package/lib/hooks/useMutationObserver.js +29 -0
- package/lib/hooks/useMutationObserver.js.map +1 -0
- package/lib/hooks/usePrismTheme.d.ts +13 -0
- package/lib/hooks/usePrismTheme.d.ts.map +1 -0
- package/lib/hooks/usePrismTheme.js +21 -0
- package/lib/hooks/usePrismTheme.js.map +1 -0
- package/lib/hooks/useSearchPage.d.ts +25 -0
- package/lib/hooks/useSearchPage.d.ts.map +1 -0
- package/lib/hooks/useSearchPage.js +43 -0
- package/lib/hooks/useSearchPage.js.map +1 -0
- 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/hooks/useTOCHighlight.js +130 -0
- package/lib/hooks/useTOCHighlight.js.map +1 -0
- package/lib/hooks/useWindowSize.d.ts +28 -0
- package/lib/hooks/useWindowSize.d.ts.map +1 -0
- package/lib/hooks/useWindowSize.js +59 -0
- package/lib/hooks/useWindowSize.js.map +1 -0
- package/lib/index.d.ts +16 -11
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +23 -10
- package/lib/index.js.map +1 -0
- package/lib/internal.d.ts +41 -0
- package/lib/internal.d.ts.map +1 -0
- package/lib/internal.js +52 -0
- package/lib/internal.js.map +1 -0
- package/lib/utils/ThemeClassNames.d.ts +49 -12
- package/lib/utils/ThemeClassNames.d.ts.map +1 -0
- package/lib/utils/ThemeClassNames.js +47 -4
- package/lib/utils/ThemeClassNames.js.map +1 -0
- package/lib/utils/codeBlockUtils.d.ts +63 -0
- package/lib/utils/codeBlockUtils.d.ts.map +1 -0
- package/lib/utils/codeBlockUtils.js +159 -3
- package/lib/utils/codeBlockUtils.js.map +1 -0
- package/lib/utils/docsUtils.d.ts +91 -0
- package/lib/utils/docsUtils.d.ts.map +1 -0
- package/lib/utils/docsUtils.js +217 -1
- package/lib/utils/docsUtils.js.map +1 -0
- 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 +11 -1
- package/lib/utils/generalUtils.d.ts.map +1 -0
- package/lib/utils/generalUtils.js +9 -5
- package/lib/utils/generalUtils.js.map +1 -0
- package/lib/utils/historyUtils.d.ts +17 -0
- package/lib/utils/historyUtils.d.ts.map +1 -0
- package/lib/utils/historyUtils.js +38 -0
- package/lib/utils/historyUtils.js.map +1 -0
- package/lib/utils/jsUtils.d.ts +23 -0
- package/lib/utils/jsUtils.d.ts.map +1 -0
- package/lib/utils/jsUtils.js +29 -0
- package/lib/utils/jsUtils.js.map +1 -0
- package/lib/utils/metadataUtils.d.ts +38 -0
- package/lib/utils/metadataUtils.d.ts.map +1 -0
- package/lib/utils/metadataUtils.js +70 -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 +36 -0
- package/lib/utils/navbarUtils.js.map +1 -0
- package/lib/utils/reactUtils.d.ts +69 -0
- package/lib/utils/reactUtils.d.ts.map +1 -0
- package/lib/utils/reactUtils.js +98 -0
- package/lib/utils/reactUtils.js.map +1 -0
- package/lib/utils/regexpUtils.d.ts +12 -0
- package/lib/utils/regexpUtils.d.ts.map +1 -0
- package/lib/utils/regexpUtils.js +18 -0
- package/lib/utils/regexpUtils.js.map +1 -0
- package/lib/utils/routesUtils.d.ts +26 -0
- package/lib/utils/routesUtils.d.ts.map +1 -0
- package/lib/utils/routesUtils.js +54 -0
- package/lib/utils/routesUtils.js.map +1 -0
- package/lib/utils/scrollUtils.d.ts +83 -0
- package/lib/utils/scrollUtils.d.ts.map +1 -0
- package/lib/utils/scrollUtils.js +200 -0
- package/lib/utils/scrollUtils.js.map +1 -0
- package/lib/utils/searchUtils.d.ts +13 -0
- package/lib/utils/searchUtils.d.ts.map +1 -0
- package/lib/utils/searchUtils.js +37 -0
- package/lib/utils/searchUtils.js.map +1 -0
- package/lib/utils/storageUtils.d.ts +15 -7
- package/lib/utils/storageUtils.d.ts.map +1 -0
- package/lib/utils/storageUtils.js +58 -25
- package/lib/utils/storageUtils.js.map +1 -0
- package/lib/utils/tagsUtils.d.ts +18 -0
- package/lib/utils/tagsUtils.d.ts.map +1 -0
- package/lib/utils/tagsUtils.js +36 -0
- package/lib/utils/tagsUtils.js.map +1 -0
- package/lib/utils/tocUtils.d.ts +36 -0
- package/lib/utils/tocUtils.d.ts.map +1 -0
- package/lib/utils/tocUtils.js +84 -0
- package/lib/utils/tocUtils.js.map +1 -0
- package/lib/utils/useAlternatePageUtils.d.ts +21 -1
- package/lib/utils/useAlternatePageUtils.d.ts.map +1 -0
- package/lib/utils/useAlternatePageUtils.js +9 -4
- package/lib/utils/useAlternatePageUtils.js.map +1 -0
- package/lib/utils/useLocalPathname.d.ts +13 -0
- package/lib/utils/useLocalPathname.d.ts.map +1 -0
- package/lib/utils/useLocalPathname.js +19 -0
- package/lib/utils/useLocalPathname.js.map +1 -0
- package/lib/utils/useLocationChange.d.ts +17 -0
- package/lib/utils/useLocationChange.d.ts.map +1 -0
- package/lib/utils/useLocationChange.js +31 -0
- package/lib/utils/useLocationChange.js.map +1 -0
- package/lib/utils/usePluralForm.d.ts +12 -0
- package/lib/utils/usePluralForm.d.ts.map +1 -0
- package/lib/utils/usePluralForm.js +36 -37
- package/lib/utils/usePluralForm.js.map +1 -0
- package/lib/utils/useThemeConfig.d.ts +56 -27
- package/lib/utils/useThemeConfig.d.ts.map +1 -0
- package/lib/utils/useThemeConfig.js +4 -0
- package/lib/utils/useThemeConfig.js.map +1 -0
- package/package.json +35 -13
- package/src/components/Collapsible/index.tsx +263 -0
- package/src/components/Details/index.tsx +109 -0
- package/src/components/Details/styles.module.css +62 -0
- package/src/contexts/announcementBar.tsx +119 -0
- package/src/contexts/blogPost.tsx +80 -0
- package/src/contexts/colorMode.tsx +198 -0
- package/src/contexts/doc.tsx +71 -0
- package/src/contexts/docSidebarItemsExpandedState.tsx +55 -0
- package/src/contexts/docsPreferredVersion.tsx +251 -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 +100 -0
- package/src/contexts/navbarSecondaryMenu/display.tsx +102 -0
- package/src/contexts/tabGroupChoice.tsx +85 -0
- package/{lib/utils/pathUtils.d.ts → src/hooks/styles.css} +4 -1
- package/src/hooks/useBackToTopButton.ts +73 -0
- package/src/hooks/useCodeWordWrap.ts +105 -0
- package/src/hooks/useHideableNavbar.ts +75 -0
- package/src/hooks/useKeyboardNavigation.ts +45 -0
- package/src/hooks/useLockBodyScroll.ts +21 -0
- package/src/hooks/useMutationObserver.ts +38 -0
- package/src/hooks/usePrismTheme.ts +24 -0
- package/src/hooks/useSearchPage.ts +79 -0
- package/src/hooks/useSkipToContent.ts +58 -0
- package/src/hooks/useTOCHighlight.ts +189 -0
- package/src/hooks/useWindowSize.ts +72 -0
- package/src/index.ts +55 -23
- package/src/internal.ts +122 -0
- package/src/types.d.ts +0 -2
- package/src/utils/ThemeClassNames.ts +54 -5
- package/src/utils/codeBlockUtils.ts +241 -2
- package/src/utils/docsUtils.tsx +334 -0
- package/src/utils/footerUtils.ts +18 -0
- package/src/utils/generalUtils.ts +9 -5
- package/src/utils/historyUtils.ts +45 -0
- package/src/utils/jsUtils.ts +36 -0
- package/src/utils/metadataUtils.tsx +115 -0
- package/src/utils/navbarUtils.tsx +45 -0
- package/src/utils/reactUtils.tsx +129 -0
- package/src/utils/regexpUtils.ts +24 -0
- package/src/utils/routesUtils.ts +75 -0
- package/src/utils/scrollUtils.tsx +304 -0
- package/src/utils/searchUtils.ts +51 -0
- package/src/utils/storageUtils.ts +59 -26
- package/src/utils/tagsUtils.ts +50 -0
- package/src/utils/tocUtils.ts +119 -0
- package/src/utils/useAlternatePageUtils.ts +28 -7
- package/src/utils/useLocalPathname.ts +22 -0
- package/src/utils/useLocationChange.ts +41 -0
- package/src/utils/usePluralForm.ts +53 -39
- package/src/utils/useThemeConfig.ts +55 -29
- package/lib/.tsbuildinfo +0 -4115
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +0 -21
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +0 -94
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts +0 -13
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +0 -20
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +0 -5
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +0 -41
- package/lib/utils/pathUtils.js +0 -13
- package/lib/utils/useChangeRoute.js +0 -18
- package/src/utils/__tests__/codeBlockUtils.test.ts +0 -54
- package/src/utils/__tests__/pathUtils.test.ts +0 -32
- package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +0 -165
- package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +0 -34
- package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +0 -66
- package/src/utils/docsUtils.ts +0 -11
- package/src/utils/pathUtils.ts +0 -17
- package/src/utils/useChangeRoute.ts +0 -21
- package/tsconfig.json +0 -10
|
@@ -0,0 +1,21 @@
|
|
|
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 {useEffect} from 'react';
|
|
9
|
+
|
|
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 {
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
document.body.style.overflow = lock ? 'hidden' : 'visible';
|
|
17
|
+
return () => {
|
|
18
|
+
document.body.style.overflow = 'visible';
|
|
19
|
+
};
|
|
20
|
+
}, [lock]);
|
|
21
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
import {useEffect} from 'react';
|
|
8
|
+
import {useEvent, useShallowMemoObject} from '../utils/reactUtils';
|
|
9
|
+
|
|
10
|
+
type Options = MutationObserverInit;
|
|
11
|
+
|
|
12
|
+
const DefaultOptions: Options = {
|
|
13
|
+
attributes: true,
|
|
14
|
+
characterData: true,
|
|
15
|
+
childList: true,
|
|
16
|
+
subtree: true,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export function useMutationObserver(
|
|
20
|
+
target: Element | undefined | null,
|
|
21
|
+
callback: MutationCallback,
|
|
22
|
+
options: Options = DefaultOptions,
|
|
23
|
+
): void {
|
|
24
|
+
const stableCallback = useEvent(callback);
|
|
25
|
+
|
|
26
|
+
// MutationObserver options are not nested much
|
|
27
|
+
// so this should be to memo options in 99%
|
|
28
|
+
// TODO handle options.attributeFilter array
|
|
29
|
+
const stableOptions: Options = useShallowMemoObject(options);
|
|
30
|
+
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
const observer = new MutationObserver(stableCallback);
|
|
33
|
+
if (target) {
|
|
34
|
+
observer.observe(target, stableOptions);
|
|
35
|
+
}
|
|
36
|
+
return () => observer.disconnect();
|
|
37
|
+
}, [target, stableCallback, stableOptions]);
|
|
38
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
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 {useColorMode} from '../contexts/colorMode';
|
|
9
|
+
import {useThemeConfig} from '../utils/useThemeConfig';
|
|
10
|
+
import type {PrismTheme} from 'prism-react-renderer';
|
|
11
|
+
|
|
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 {
|
|
17
|
+
const {prism} = useThemeConfig();
|
|
18
|
+
const {colorMode} = useColorMode();
|
|
19
|
+
const lightModeTheme = prism.theme;
|
|
20
|
+
const darkModeTheme = prism.darkTheme || lightModeTheme;
|
|
21
|
+
const prismTheme = colorMode === 'dark' ? darkModeTheme : lightModeTheme;
|
|
22
|
+
|
|
23
|
+
return prismTheme;
|
|
24
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
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 {useCallback, useEffect, useState} from 'react';
|
|
9
|
+
import {useHistory} from '@docusaurus/router';
|
|
10
|
+
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
|
11
|
+
|
|
12
|
+
const SEARCH_PARAM_QUERY = 'q';
|
|
13
|
+
|
|
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
|
+
*/
|
|
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
|
+
*/
|
|
25
|
+
setSearchQuery: (newSearchQuery: string) => void;
|
|
26
|
+
/**
|
|
27
|
+
* Given a query, this handle generates the corresponding search page link,
|
|
28
|
+
* with base URL prepended.
|
|
29
|
+
*/
|
|
30
|
+
generateSearchPageLink: (targetSearchQuery: string) => string;
|
|
31
|
+
} {
|
|
32
|
+
const history = useHistory();
|
|
33
|
+
const {
|
|
34
|
+
siteConfig: {baseUrl},
|
|
35
|
+
} = useDocusaurusContext();
|
|
36
|
+
|
|
37
|
+
const [searchQuery, setSearchQueryState] = useState('');
|
|
38
|
+
|
|
39
|
+
// Init search query just after React hydration
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
const searchQueryStringValue =
|
|
42
|
+
new URLSearchParams(window.location.search).get(SEARCH_PARAM_QUERY) ?? '';
|
|
43
|
+
|
|
44
|
+
setSearchQueryState(searchQueryStringValue);
|
|
45
|
+
}, []);
|
|
46
|
+
|
|
47
|
+
const setSearchQuery = useCallback(
|
|
48
|
+
(newSearchQuery: string) => {
|
|
49
|
+
const searchParams = new URLSearchParams(window.location.search);
|
|
50
|
+
|
|
51
|
+
if (newSearchQuery) {
|
|
52
|
+
searchParams.set(SEARCH_PARAM_QUERY, newSearchQuery);
|
|
53
|
+
} else {
|
|
54
|
+
searchParams.delete(SEARCH_PARAM_QUERY);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
history.replace({
|
|
58
|
+
search: searchParams.toString(),
|
|
59
|
+
});
|
|
60
|
+
setSearchQueryState(newSearchQuery);
|
|
61
|
+
},
|
|
62
|
+
[history],
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
const generateSearchPageLink = useCallback(
|
|
66
|
+
(targetSearchQuery: string) =>
|
|
67
|
+
// Refer to https://github.com/facebook/docusaurus/pull/2838
|
|
68
|
+
`${baseUrl}search?${SEARCH_PARAM_QUERY}=${encodeURIComponent(
|
|
69
|
+
targetSearchQuery,
|
|
70
|
+
)}`,
|
|
71
|
+
[baseUrl],
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
searchQuery,
|
|
76
|
+
setSearchQuery,
|
|
77
|
+
generateSearchPageLink,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
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 {useEffect, useRef} from 'react';
|
|
9
|
+
import {useThemeConfig} from '../utils/useThemeConfig';
|
|
10
|
+
|
|
11
|
+
// TODO make the hardcoded theme-classic classnames configurable (or add them
|
|
12
|
+
// to ThemeClassNames?)
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* If the anchor has no height and is just a "marker" in the DOM; we'll use the
|
|
16
|
+
* parent (normally the link text) rect boundaries instead
|
|
17
|
+
*/
|
|
18
|
+
function getVisibleBoundingClientRect(element: HTMLElement): DOMRect {
|
|
19
|
+
const rect = element.getBoundingClientRect();
|
|
20
|
+
const hasNoHeight = rect.top === rect.bottom;
|
|
21
|
+
if (hasNoHeight) {
|
|
22
|
+
return getVisibleBoundingClientRect(element.parentNode as HTMLElement);
|
|
23
|
+
}
|
|
24
|
+
return rect;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Considering we divide viewport into 2 zones of each 50vh, this returns true
|
|
29
|
+
* if an element is in the first zone (i.e., appear in viewport, near the top)
|
|
30
|
+
*/
|
|
31
|
+
function isInViewportTopHalf(boundingRect: DOMRect) {
|
|
32
|
+
return boundingRect.top > 0 && boundingRect.bottom < window.innerHeight / 2;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function getAnchors({
|
|
36
|
+
minHeadingLevel,
|
|
37
|
+
maxHeadingLevel,
|
|
38
|
+
}: {
|
|
39
|
+
minHeadingLevel: number;
|
|
40
|
+
maxHeadingLevel: number;
|
|
41
|
+
}): HTMLElement[] {
|
|
42
|
+
const selectors = [];
|
|
43
|
+
for (let i = minHeadingLevel; i <= maxHeadingLevel; i += 1) {
|
|
44
|
+
selectors.push(`h${i}.anchor`);
|
|
45
|
+
}
|
|
46
|
+
return Array.from(document.querySelectorAll(selectors.join()));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function getActiveAnchor(
|
|
50
|
+
anchors: HTMLElement[],
|
|
51
|
+
{
|
|
52
|
+
anchorTopOffset,
|
|
53
|
+
}: {
|
|
54
|
+
anchorTopOffset: number;
|
|
55
|
+
},
|
|
56
|
+
): Element | null {
|
|
57
|
+
// Naming is hard: The "nextVisibleAnchor" is the first anchor that appear
|
|
58
|
+
// under the viewport top boundary. It does not mean this anchor is visible
|
|
59
|
+
// yet, but if user continues scrolling down, it will be the first to become
|
|
60
|
+
// visible
|
|
61
|
+
const nextVisibleAnchor = anchors.find((anchor) => {
|
|
62
|
+
const boundingRect = getVisibleBoundingClientRect(anchor);
|
|
63
|
+
return boundingRect.top >= anchorTopOffset;
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
if (nextVisibleAnchor) {
|
|
67
|
+
const boundingRect = getVisibleBoundingClientRect(nextVisibleAnchor);
|
|
68
|
+
// If anchor is in the top half of the viewport: it is the one we consider
|
|
69
|
+
// "active" (unless it's too close to the top and and soon to be scrolled
|
|
70
|
+
// outside viewport)
|
|
71
|
+
if (isInViewportTopHalf(boundingRect)) {
|
|
72
|
+
return nextVisibleAnchor;
|
|
73
|
+
}
|
|
74
|
+
// If anchor is in the bottom half of the viewport, or under the viewport,
|
|
75
|
+
// we consider the active anchor is the previous one. This is because the
|
|
76
|
+
// main text appearing in the user screen mostly belong to the previous
|
|
77
|
+
// anchor. Returns null for the first anchor, see
|
|
78
|
+
// https://github.com/facebook/docusaurus/issues/5318
|
|
79
|
+
return anchors[anchors.indexOf(nextVisibleAnchor) - 1] ?? null;
|
|
80
|
+
}
|
|
81
|
+
// No anchor under viewport top (i.e. we are at the bottom of the page),
|
|
82
|
+
// highlight the last anchor found
|
|
83
|
+
return anchors[anchors.length - 1] ?? null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function getLinkAnchorValue(link: HTMLAnchorElement): string {
|
|
87
|
+
return decodeURIComponent(link.href.substring(link.href.indexOf('#') + 1));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function getLinks(linkClassName: string) {
|
|
91
|
+
return Array.from(
|
|
92
|
+
document.getElementsByClassName(linkClassName),
|
|
93
|
+
) as HTMLAnchorElement[];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function getNavbarHeight(): number {
|
|
97
|
+
// Not ideal to obtain actual height this way
|
|
98
|
+
// Using TS ! (not ?) because otherwise a bad selector would be un-noticed
|
|
99
|
+
return document.querySelector('.navbar')!.clientHeight;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function useAnchorTopOffsetRef() {
|
|
103
|
+
const anchorTopOffsetRef = useRef<number>(0);
|
|
104
|
+
const {
|
|
105
|
+
navbar: {hideOnScroll},
|
|
106
|
+
} = useThemeConfig();
|
|
107
|
+
|
|
108
|
+
useEffect(() => {
|
|
109
|
+
anchorTopOffsetRef.current = hideOnScroll ? 0 : getNavbarHeight();
|
|
110
|
+
}, [hideOnScroll]);
|
|
111
|
+
|
|
112
|
+
return anchorTopOffsetRef;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export type TOCHighlightConfig = {
|
|
116
|
+
/** A class name that all TOC links share. */
|
|
117
|
+
linkClassName: string;
|
|
118
|
+
/** The class name applied to the active (highlighted) link. */
|
|
119
|
+
linkActiveClassName: string;
|
|
120
|
+
/**
|
|
121
|
+
* The minimum heading level that the TOC includes. Only headings that are in
|
|
122
|
+
* this range will be eligible as "active heading".
|
|
123
|
+
*/
|
|
124
|
+
minHeadingLevel: number;
|
|
125
|
+
/** @see {@link TOCHighlightConfig.minHeadingLevel} */
|
|
126
|
+
maxHeadingLevel: number;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Side-effect that applies the active class name to the TOC heading that the
|
|
131
|
+
* user is currently viewing. Disabled when `config` is undefined.
|
|
132
|
+
*/
|
|
133
|
+
export function useTOCHighlight(config: TOCHighlightConfig | undefined): void {
|
|
134
|
+
const lastActiveLinkRef = useRef<HTMLAnchorElement | undefined>(undefined);
|
|
135
|
+
|
|
136
|
+
const anchorTopOffsetRef = useAnchorTopOffsetRef();
|
|
137
|
+
|
|
138
|
+
useEffect(() => {
|
|
139
|
+
if (!config) {
|
|
140
|
+
// No-op, highlighting is disabled
|
|
141
|
+
return () => {};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const {
|
|
145
|
+
linkClassName,
|
|
146
|
+
linkActiveClassName,
|
|
147
|
+
minHeadingLevel,
|
|
148
|
+
maxHeadingLevel,
|
|
149
|
+
} = config;
|
|
150
|
+
|
|
151
|
+
function updateLinkActiveClass(link: HTMLAnchorElement, active: boolean) {
|
|
152
|
+
if (active) {
|
|
153
|
+
if (lastActiveLinkRef.current && lastActiveLinkRef.current !== link) {
|
|
154
|
+
lastActiveLinkRef.current.classList.remove(linkActiveClassName);
|
|
155
|
+
}
|
|
156
|
+
link.classList.add(linkActiveClassName);
|
|
157
|
+
lastActiveLinkRef.current = link;
|
|
158
|
+
// link.scrollIntoView({block: 'nearest'});
|
|
159
|
+
} else {
|
|
160
|
+
link.classList.remove(linkActiveClassName);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function updateActiveLink() {
|
|
165
|
+
const links = getLinks(linkClassName);
|
|
166
|
+
const anchors = getAnchors({minHeadingLevel, maxHeadingLevel});
|
|
167
|
+
const activeAnchor = getActiveAnchor(anchors, {
|
|
168
|
+
anchorTopOffset: anchorTopOffsetRef.current,
|
|
169
|
+
});
|
|
170
|
+
const activeLink = links.find(
|
|
171
|
+
(link) => activeAnchor && activeAnchor.id === getLinkAnchorValue(link),
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
links.forEach((link) => {
|
|
175
|
+
updateLinkActiveClass(link, link === activeLink);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
document.addEventListener('scroll', updateActiveLink);
|
|
180
|
+
document.addEventListener('resize', updateActiveLink);
|
|
181
|
+
|
|
182
|
+
updateActiveLink();
|
|
183
|
+
|
|
184
|
+
return () => {
|
|
185
|
+
document.removeEventListener('scroll', updateActiveLink);
|
|
186
|
+
document.removeEventListener('resize', updateActiveLink);
|
|
187
|
+
};
|
|
188
|
+
}, [config, anchorTopOffsetRef]);
|
|
189
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
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 {useEffect, useState} from 'react';
|
|
9
|
+
|
|
10
|
+
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
|
|
11
|
+
|
|
12
|
+
const windowSizes = {
|
|
13
|
+
desktop: 'desktop',
|
|
14
|
+
mobile: 'mobile',
|
|
15
|
+
ssr: 'ssr',
|
|
16
|
+
} as const;
|
|
17
|
+
|
|
18
|
+
type WindowSize = keyof typeof windowSizes;
|
|
19
|
+
|
|
20
|
+
const DesktopThresholdWidth = 996;
|
|
21
|
+
|
|
22
|
+
function getWindowSize() {
|
|
23
|
+
if (!ExecutionEnvironment.canUseDOM) {
|
|
24
|
+
return windowSizes.ssr;
|
|
25
|
+
}
|
|
26
|
+
return window.innerWidth > DesktopThresholdWidth
|
|
27
|
+
? windowSizes.desktop
|
|
28
|
+
: windowSizes.mobile;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const DevSimulateSSR = process.env.NODE_ENV === 'development' && true;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Gets the current window size as an enum value. We don't want it to return the
|
|
35
|
+
* actual width value, so that it only re-renders once a breakpoint is crossed.
|
|
36
|
+
*
|
|
37
|
+
* It may return `"ssr"`, which is very important to handle hydration FOUC or
|
|
38
|
+
* layout shifts. You have to handle it explicitly upfront. On the server, you
|
|
39
|
+
* may need to render BOTH the mobile/desktop elements (and hide one of them
|
|
40
|
+
* with mediaquery). We don't return `undefined` on purpose, to make it more
|
|
41
|
+
* explicit.
|
|
42
|
+
*
|
|
43
|
+
* In development mode, this hook will still return `"ssr"` for one second, to
|
|
44
|
+
* catch potential layout shifts, similar to strict mode calling effects twice.
|
|
45
|
+
*/
|
|
46
|
+
export function useWindowSize(): WindowSize {
|
|
47
|
+
const [windowSize, setWindowSize] = useState<WindowSize>(() => {
|
|
48
|
+
if (DevSimulateSSR) {
|
|
49
|
+
return 'ssr';
|
|
50
|
+
}
|
|
51
|
+
return getWindowSize();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
function updateWindowSize() {
|
|
56
|
+
setWindowSize(getWindowSize());
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const timeout = DevSimulateSSR
|
|
60
|
+
? window.setTimeout(updateWindowSize, 1000)
|
|
61
|
+
: undefined;
|
|
62
|
+
|
|
63
|
+
window.addEventListener('resize', updateWindowSize);
|
|
64
|
+
|
|
65
|
+
return () => {
|
|
66
|
+
window.removeEventListener('resize', updateWindowSize);
|
|
67
|
+
clearTimeout(timeout);
|
|
68
|
+
};
|
|
69
|
+
}, []);
|
|
70
|
+
|
|
71
|
+
return windowSize;
|
|
72
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -5,41 +5,73 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
8
|
+
/*
|
|
9
|
+
* APIs to document
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
useThemeConfig,
|
|
14
|
+
type ThemeConfig,
|
|
15
|
+
type UserThemeConfig,
|
|
16
|
+
type Navbar,
|
|
17
|
+
type NavbarItem,
|
|
18
|
+
type NavbarLogo,
|
|
19
|
+
type MultiColumnFooter,
|
|
20
|
+
type SimpleFooter,
|
|
21
|
+
type Footer,
|
|
22
|
+
type FooterLogo,
|
|
23
|
+
type FooterLinkItem,
|
|
24
|
+
type ColorModeConfig,
|
|
18
25
|
} from './utils/useThemeConfig';
|
|
19
26
|
|
|
20
27
|
export {createStorageSlot, listStorageKeys} from './utils/storageUtils';
|
|
21
28
|
|
|
22
|
-
export {
|
|
29
|
+
export {useContextualSearchFilters} from './utils/searchUtils';
|
|
23
30
|
|
|
24
|
-
export {
|
|
31
|
+
export {useCurrentSidebarCategory} from './utils/docsUtils';
|
|
25
32
|
|
|
26
|
-
export {
|
|
33
|
+
export {usePluralForm} from './utils/usePluralForm';
|
|
27
34
|
|
|
28
|
-
export {
|
|
35
|
+
export {useCollapsible, Collapsible} from './components/Collapsible';
|
|
29
36
|
|
|
30
|
-
export {
|
|
37
|
+
export {ThemeClassNames} from './utils/ThemeClassNames';
|
|
31
38
|
|
|
32
|
-
export {
|
|
39
|
+
export {
|
|
40
|
+
useIsomorphicLayoutEffect,
|
|
41
|
+
useEvent,
|
|
42
|
+
usePrevious,
|
|
43
|
+
composeProviders,
|
|
44
|
+
ReactContextError,
|
|
45
|
+
} from './utils/reactUtils';
|
|
33
46
|
|
|
34
|
-
export {
|
|
47
|
+
export {PageMetadata, HtmlClassNameProvider} from './utils/metadataUtils';
|
|
35
48
|
|
|
36
|
-
export {
|
|
49
|
+
export {useColorMode, type ColorMode} from './contexts/colorMode';
|
|
37
50
|
|
|
38
51
|
export {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
} from './
|
|
52
|
+
NavbarSecondaryMenuFiller,
|
|
53
|
+
type NavbarSecondaryMenuComponent,
|
|
54
|
+
} from './contexts/navbarSecondaryMenu/content';
|
|
42
55
|
|
|
43
|
-
export {
|
|
56
|
+
export {useWindowSize} from './hooks/useWindowSize';
|
|
44
57
|
|
|
45
|
-
|
|
58
|
+
/*
|
|
59
|
+
* APIs kept undocumented, on purpose
|
|
60
|
+
* Note: we still guarantee retro-compatibility on those
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
export {
|
|
64
|
+
translateTagsPageTitle,
|
|
65
|
+
listTagsByLetters,
|
|
66
|
+
type TagLetterEntry,
|
|
67
|
+
} from './utils/tagsUtils';
|
|
68
|
+
|
|
69
|
+
export {isMultiColumnFooterLinks} from './utils/footerUtils';
|
|
70
|
+
|
|
71
|
+
export {isRegexpStringMatch} from './utils/regexpUtils';
|
|
72
|
+
|
|
73
|
+
export {duplicates, uniq} from './utils/jsUtils';
|
|
74
|
+
|
|
75
|
+
export {usePrismTheme} from './hooks/usePrismTheme';
|
|
76
|
+
|
|
77
|
+
export {useDocsPreferredVersion} from './contexts/docsPreferredVersion';
|