@docusaurus/theme-common 2.0.0-beta.2 → 2.0.0-beta.20
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 +64 -0
- package/lib/components/Collapsible/index.d.ts.map +1 -0
- package/lib/components/Collapsible/index.js +152 -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 +71 -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/{utils/announcementBarUtils.js → contexts/announcementBar.js} +30 -31
- 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/contexts/docSidebarItemsExpandedState.js +28 -0
- 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/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/usePrevious.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 +41 -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/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 +38 -10
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +38 -9
- package/lib/index.js.map +1 -0
- package/lib/utils/ThemeClassNames.d.ts +47 -12
- package/lib/utils/ThemeClassNames.d.ts.map +1 -0
- package/lib/utils/ThemeClassNames.js +45 -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 +157 -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 +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 +42 -0
- package/lib/utils/reactUtils.d.ts.map +1 -0
- package/lib/utils/reactUtils.js +64 -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 +55 -22
- 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 +9 -6
- package/lib/utils/useLocationChange.d.ts.map +1 -0
- package/lib/utils/useLocationChange.js +17 -11
- 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 +55 -24
- 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 +26 -13
- package/src/components/Collapsible/index.tsx +265 -0
- package/src/components/Details/index.tsx +109 -0
- package/src/components/Details/styles.module.css +62 -0
- package/src/{utils/announcementBarUtils.tsx → contexts/announcementBar.tsx} +43 -39
- 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/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 +56 -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/usePrismTheme.ts +24 -0
- package/src/hooks/useSearchPage.ts +79 -0
- package/src/hooks/useSkipToContent.ts +58 -0
- package/src/hooks/useTOCHighlight.ts +192 -0
- package/src/hooks/useWindowSize.ts +72 -0
- package/src/index.ts +130 -19
- package/src/types.d.ts +0 -2
- package/src/utils/ThemeClassNames.ts +51 -5
- package/src/utils/codeBlockUtils.ts +239 -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 +76 -0
- package/src/utils/regexpUtils.ts +24 -0
- package/src/utils/routesUtils.ts +75 -0
- package/src/utils/scrollUtils.tsx +308 -0
- package/src/utils/searchUtils.ts +51 -0
- package/src/utils/storageUtils.ts +56 -23
- package/src/utils/tagsUtils.ts +50 -0
- package/src/utils/tocUtils.ts +119 -0
- package/src/utils/useAlternatePageUtils.ts +19 -6
- package/src/utils/useLocalPathname.ts +22 -0
- package/src/utils/useLocationChange.ts +24 -20
- package/src/utils/usePluralForm.ts +46 -38
- package/src/utils/useThemeConfig.ts +54 -26
- package/lib/.tsbuildinfo +0 -1
- package/lib/utils/announcementBarInlineJavaScript.d.ts +0 -0
- package/lib/utils/announcementBarInlineJavaScript.js +0 -1
- package/lib/utils/announcementBarUtils.d.ts +0 -17
- 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/usePrevious.js +0 -14
- 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/usePrevious.ts +0 -18
- package/tsconfig.json +0 -10
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
import {useHistory} from '@docusaurus/router';
|
|
10
|
+
import {useDynamicCallback} from './reactUtils';
|
|
11
|
+
import type {Location, Action} from 'history';
|
|
12
|
+
|
|
13
|
+
type HistoryBlockHandler = (location: Location, action: Action) => void | false;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Permits to register a handler that will be called on history actions (pop,
|
|
17
|
+
* push, replace). If the handler returns `false`, the navigation transition
|
|
18
|
+
* will be blocked/cancelled.
|
|
19
|
+
*/
|
|
20
|
+
function useHistoryActionHandler(handler: HistoryBlockHandler): void {
|
|
21
|
+
const {block} = useHistory();
|
|
22
|
+
const stableHandler = useDynamicCallback(handler);
|
|
23
|
+
useEffect(
|
|
24
|
+
// See https://github.com/remix-run/history/blob/main/docs/blocking-transitions.md
|
|
25
|
+
() => block((location, action) => stableHandler(location, action)),
|
|
26
|
+
[block, stableHandler],
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Permits to register a handler that will be called on history pop navigation
|
|
32
|
+
* (backward/forward). If the handler returns `false`, the backward/forward
|
|
33
|
+
* transition will be blocked. Unfortunately there's no good way to detect the
|
|
34
|
+
* "direction" (backward/forward) of the POP event.
|
|
35
|
+
*/
|
|
36
|
+
export function useHistoryPopHandler(handler: HistoryBlockHandler): void {
|
|
37
|
+
useHistoryActionHandler((location, action) => {
|
|
38
|
+
if (action === 'POP') {
|
|
39
|
+
// Maybe block navigation if handler returns false
|
|
40
|
+
return handler(location, action);
|
|
41
|
+
}
|
|
42
|
+
// Don't block other navigation actions
|
|
43
|
+
return undefined;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
// A replacement of lodash in client code
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Gets the duplicate values in an array.
|
|
12
|
+
* @param arr The array.
|
|
13
|
+
* @param comparator Compares two values and returns `true` if they are equal
|
|
14
|
+
* (duplicated).
|
|
15
|
+
* @returns Value of the elements `v` that have a preceding element `u` where
|
|
16
|
+
* `comparator(u, v) === true`. Values within the returned array are not
|
|
17
|
+
* guaranteed to be unique.
|
|
18
|
+
*/
|
|
19
|
+
export function duplicates<T>(
|
|
20
|
+
arr: readonly T[],
|
|
21
|
+
comparator: (a: T, b: T) => boolean = (a, b) => a === b,
|
|
22
|
+
): T[] {
|
|
23
|
+
return arr.filter(
|
|
24
|
+
(v, vIndex) => arr.findIndex((u) => comparator(u, v)) !== vIndex,
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Remove duplicate array items (similar to `_.uniq`)
|
|
30
|
+
* @param arr The array.
|
|
31
|
+
* @returns An array with duplicate elements removed by reference comparison.
|
|
32
|
+
*/
|
|
33
|
+
export function uniq<T>(arr: T[]): T[] {
|
|
34
|
+
// Note: had problems with [...new Set()]: https://github.com/facebook/docusaurus/issues/4972#issuecomment-863895061
|
|
35
|
+
return Array.from(new Set(arr));
|
|
36
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
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, {type ReactNode} from 'react';
|
|
9
|
+
import Head from '@docusaurus/Head';
|
|
10
|
+
import clsx from 'clsx';
|
|
11
|
+
import useRouteContext from '@docusaurus/useRouteContext';
|
|
12
|
+
import {useBaseUrlUtils} from '@docusaurus/useBaseUrl';
|
|
13
|
+
import {useTitleFormatter} from './generalUtils';
|
|
14
|
+
|
|
15
|
+
type PageMetadataProps = {
|
|
16
|
+
readonly title?: string;
|
|
17
|
+
readonly description?: string;
|
|
18
|
+
readonly keywords?: readonly string[] | string;
|
|
19
|
+
readonly image?: string;
|
|
20
|
+
readonly children?: ReactNode;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Helper component to manipulate page metadata and override site defaults.
|
|
25
|
+
* Works in the same way as Helmet.
|
|
26
|
+
*/
|
|
27
|
+
export function PageMetadata({
|
|
28
|
+
title,
|
|
29
|
+
description,
|
|
30
|
+
keywords,
|
|
31
|
+
image,
|
|
32
|
+
children,
|
|
33
|
+
}: PageMetadataProps): JSX.Element {
|
|
34
|
+
const pageTitle = useTitleFormatter(title);
|
|
35
|
+
const {withBaseUrl} = useBaseUrlUtils();
|
|
36
|
+
const pageImage = image ? withBaseUrl(image, {absolute: true}) : undefined;
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<Head>
|
|
40
|
+
{title && <title>{pageTitle}</title>}
|
|
41
|
+
{title && <meta property="og:title" content={pageTitle} />}
|
|
42
|
+
|
|
43
|
+
{description && <meta name="description" content={description} />}
|
|
44
|
+
{description && <meta property="og:description" content={description} />}
|
|
45
|
+
|
|
46
|
+
{keywords && (
|
|
47
|
+
<meta
|
|
48
|
+
name="keywords"
|
|
49
|
+
content={
|
|
50
|
+
// https://github.com/microsoft/TypeScript/issues/17002
|
|
51
|
+
(Array.isArray(keywords) ? keywords.join(',') : keywords) as string
|
|
52
|
+
}
|
|
53
|
+
/>
|
|
54
|
+
)}
|
|
55
|
+
|
|
56
|
+
{pageImage && <meta property="og:image" content={pageImage} />}
|
|
57
|
+
{pageImage && <meta name="twitter:image" content={pageImage} />}
|
|
58
|
+
|
|
59
|
+
{children}
|
|
60
|
+
</Head>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const HtmlClassNameContext = React.createContext<string | undefined>(undefined);
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Every layer of this provider will append a class name to the HTML element.
|
|
68
|
+
* There's no consumer for this hook: it's side-effect-only. This wrapper is
|
|
69
|
+
* necessary because Helmet does not "merge" classes.
|
|
70
|
+
* @see https://github.com/staylor/react-helmet-async/issues/161
|
|
71
|
+
*/
|
|
72
|
+
export function HtmlClassNameProvider({
|
|
73
|
+
className: classNameProp,
|
|
74
|
+
children,
|
|
75
|
+
}: {
|
|
76
|
+
className: string;
|
|
77
|
+
children: ReactNode;
|
|
78
|
+
}): JSX.Element {
|
|
79
|
+
const classNameContext = React.useContext(HtmlClassNameContext);
|
|
80
|
+
const className = clsx(classNameContext, classNameProp);
|
|
81
|
+
return (
|
|
82
|
+
<HtmlClassNameContext.Provider value={className}>
|
|
83
|
+
<Head>
|
|
84
|
+
<html className={className} />
|
|
85
|
+
</Head>
|
|
86
|
+
{children}
|
|
87
|
+
</HtmlClassNameContext.Provider>
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function pluginNameToClassName(pluginName: string) {
|
|
92
|
+
return `plugin-${pluginName.replace(
|
|
93
|
+
/docusaurus-(?:plugin|theme)-(?:content-)?/gi,
|
|
94
|
+
'',
|
|
95
|
+
)}`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* A very thin wrapper around `HtmlClassNameProvider` that adds the plugin ID +
|
|
100
|
+
* name to the HTML class name.
|
|
101
|
+
*/
|
|
102
|
+
export function PluginHtmlClassNameProvider({
|
|
103
|
+
children,
|
|
104
|
+
}: {
|
|
105
|
+
children: ReactNode;
|
|
106
|
+
}): JSX.Element {
|
|
107
|
+
const routeContext = useRouteContext();
|
|
108
|
+
const nameClass = pluginNameToClassName(routeContext.plugin.name);
|
|
109
|
+
const idClass = `plugin-id-${routeContext.plugin.id}`;
|
|
110
|
+
return (
|
|
111
|
+
<HtmlClassNameProvider className={clsx(nameClass, idClass)}>
|
|
112
|
+
{children}
|
|
113
|
+
</HtmlClassNameProvider>
|
|
114
|
+
);
|
|
115
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
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, {type ReactNode} from 'react';
|
|
9
|
+
import {NavbarMobileSidebarProvider} from '../contexts/navbarMobileSidebar';
|
|
10
|
+
import {NavbarSecondaryMenuContentProvider} from '../contexts/navbarSecondaryMenu/content';
|
|
11
|
+
import {NavbarSecondaryMenuDisplayProvider} from '../contexts/navbarSecondaryMenu/display';
|
|
12
|
+
|
|
13
|
+
const DefaultNavItemPosition = 'right';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Split links by left/right. If position is unspecified, fallback to right.
|
|
17
|
+
*/
|
|
18
|
+
export function splitNavbarItems<T extends {position?: 'left' | 'right'}>(
|
|
19
|
+
items: T[],
|
|
20
|
+
): [leftItems: T[], rightItems: T[]] {
|
|
21
|
+
function isLeft(item: T): boolean {
|
|
22
|
+
return (item.position ?? DefaultNavItemPosition) === 'left';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const leftItems = items.filter(isLeft);
|
|
26
|
+
const rightItems = items.filter((item) => !isLeft(item));
|
|
27
|
+
|
|
28
|
+
return [leftItems, rightItems];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Composes multiple navbar state providers that are mutually dependent and
|
|
33
|
+
* hence can't be re-ordered.
|
|
34
|
+
*/
|
|
35
|
+
export function NavbarProvider({children}: {children: ReactNode}): JSX.Element {
|
|
36
|
+
return (
|
|
37
|
+
<NavbarSecondaryMenuContentProvider>
|
|
38
|
+
<NavbarMobileSidebarProvider>
|
|
39
|
+
<NavbarSecondaryMenuDisplayProvider>
|
|
40
|
+
{children}
|
|
41
|
+
</NavbarSecondaryMenuDisplayProvider>
|
|
42
|
+
</NavbarMobileSidebarProvider>
|
|
43
|
+
</NavbarSecondaryMenuContentProvider>
|
|
44
|
+
);
|
|
45
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
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, useLayoutEffect, useRef} from 'react';
|
|
9
|
+
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* This hook is like `useLayoutEffect`, but without the SSR warning.
|
|
13
|
+
* It seems hacky but it's used in many React libs (Redux, Formik...).
|
|
14
|
+
* Also mentioned here: https://github.com/facebook/react/issues/16956
|
|
15
|
+
*
|
|
16
|
+
* It is useful when you need to update a ref as soon as possible after a React
|
|
17
|
+
* render (before `useEffect`).
|
|
18
|
+
*/
|
|
19
|
+
export const useIsomorphicLayoutEffect = ExecutionEnvironment.canUseDOM
|
|
20
|
+
? useLayoutEffect
|
|
21
|
+
: useEffect;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Permits to transform an unstable callback (like an arrow function provided as
|
|
25
|
+
* props) to a "stable" callback that is safe to use in a `useEffect` dependency
|
|
26
|
+
* array. Useful to avoid React stale closure problems + avoid useless effect
|
|
27
|
+
* re-executions.
|
|
28
|
+
*
|
|
29
|
+
* Workaround until the React team recommends a good solution, see
|
|
30
|
+
* https://github.com/facebook/react/issues/16956
|
|
31
|
+
*
|
|
32
|
+
* This generally works but has some potential drawbacks, such as
|
|
33
|
+
* https://github.com/facebook/react/issues/16956#issuecomment-536636418
|
|
34
|
+
*/
|
|
35
|
+
export function useDynamicCallback<T extends (...args: never[]) => unknown>(
|
|
36
|
+
callback: T,
|
|
37
|
+
): T {
|
|
38
|
+
const ref = useRef<T>(callback);
|
|
39
|
+
|
|
40
|
+
useIsomorphicLayoutEffect(() => {
|
|
41
|
+
ref.current = callback;
|
|
42
|
+
}, [callback]);
|
|
43
|
+
|
|
44
|
+
// @ts-expect-error: TS is right that this callback may be a supertype of T,
|
|
45
|
+
// but good enough for our use
|
|
46
|
+
return useCallback<T>((...args) => ref.current(...args), []);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Gets `value` from the last render.
|
|
51
|
+
*/
|
|
52
|
+
export function usePrevious<T>(value: T): T | undefined {
|
|
53
|
+
const ref = useRef<T>();
|
|
54
|
+
|
|
55
|
+
useIsomorphicLayoutEffect(() => {
|
|
56
|
+
ref.current = value;
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
return ref.current;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* This error is thrown when a context is consumed outside its provider. Allows
|
|
64
|
+
* reusing a generic error message format and reduces bundle size. The hook's
|
|
65
|
+
* name will be extracted from its stack, so only the provider's name is needed.
|
|
66
|
+
*/
|
|
67
|
+
export class ReactContextError extends Error {
|
|
68
|
+
constructor(providerName: string, additionalInfo?: string) {
|
|
69
|
+
super();
|
|
70
|
+
this.name = 'ReactContextError';
|
|
71
|
+
this.message = `Hook ${
|
|
72
|
+
this.stack?.split('\n')[1]?.match(/at (?:\w+\.)?(?<name>\w+)/)?.groups!
|
|
73
|
+
.name
|
|
74
|
+
} is called outside the <${providerName}>. ${additionalInfo || ''}`;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -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
|
+
/**
|
|
9
|
+
* Matches a string regex (as provided from the config) against a target in a
|
|
10
|
+
* null-safe fashion, case insensitive and global.
|
|
11
|
+
*/
|
|
12
|
+
export function isRegexpStringMatch(
|
|
13
|
+
regexAsString?: string,
|
|
14
|
+
valueToTest?: string,
|
|
15
|
+
): boolean {
|
|
16
|
+
if (
|
|
17
|
+
typeof regexAsString === 'undefined' ||
|
|
18
|
+
typeof valueToTest === 'undefined'
|
|
19
|
+
) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return new RegExp(regexAsString, 'gi').test(valueToTest);
|
|
24
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
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 {useMemo} from 'react';
|
|
9
|
+
import generatedRoutes from '@generated/routes';
|
|
10
|
+
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
|
11
|
+
import type {RouteConfig} from 'react-router-config';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Compare the 2 paths, case insensitive and ignoring trailing slash
|
|
15
|
+
*/
|
|
16
|
+
export function isSamePath(
|
|
17
|
+
path1: string | undefined,
|
|
18
|
+
path2: string | undefined,
|
|
19
|
+
): boolean {
|
|
20
|
+
const normalize = (pathname: string | undefined) =>
|
|
21
|
+
(!pathname || pathname?.endsWith('/')
|
|
22
|
+
? pathname
|
|
23
|
+
: `${pathname}/`
|
|
24
|
+
)?.toLowerCase();
|
|
25
|
+
return normalize(path1) === normalize(path2);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Note that sites don't always have a homepage in practice, so we can't assume
|
|
30
|
+
* that linking to '/' is always safe.
|
|
31
|
+
* @see https://github.com/facebook/docusaurus/pull/6517#issuecomment-1048709116
|
|
32
|
+
*/
|
|
33
|
+
export function findHomePageRoute({
|
|
34
|
+
baseUrl,
|
|
35
|
+
routes: initialRoutes,
|
|
36
|
+
}: {
|
|
37
|
+
routes: RouteConfig[];
|
|
38
|
+
baseUrl: string;
|
|
39
|
+
}): RouteConfig | undefined {
|
|
40
|
+
function isHomePageRoute(route: RouteConfig): boolean {
|
|
41
|
+
return route.path === baseUrl && route.exact === true;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function isHomeParentRoute(route: RouteConfig): boolean {
|
|
45
|
+
return route.path === baseUrl && !route.exact;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function doFindHomePageRoute(routes: RouteConfig[]): RouteConfig | undefined {
|
|
49
|
+
if (routes.length === 0) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
const homePage = routes.find(isHomePageRoute);
|
|
53
|
+
if (homePage) {
|
|
54
|
+
return homePage;
|
|
55
|
+
}
|
|
56
|
+
const indexSubRoutes = routes
|
|
57
|
+
.filter(isHomeParentRoute)
|
|
58
|
+
.flatMap((route) => route.routes ?? []);
|
|
59
|
+
return doFindHomePageRoute(indexSubRoutes);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return doFindHomePageRoute(initialRoutes);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Fetches the route that points to "/". Use this instead of the naive "/",
|
|
67
|
+
* because the homepage may not exist.
|
|
68
|
+
*/
|
|
69
|
+
export function useHomePageRoute(): RouteConfig | undefined {
|
|
70
|
+
const {baseUrl} = useDocusaurusContext().siteConfig;
|
|
71
|
+
return useMemo(
|
|
72
|
+
() => findHomePageRoute({routes: generatedRoutes, baseUrl}),
|
|
73
|
+
[baseUrl],
|
|
74
|
+
);
|
|
75
|
+
}
|