@docusaurus/theme-common 2.0.0-beta.12faed89d → 2.0.0-beta.14
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/{utils/useCollapsible.d.ts → components/Collapsible/index.d.ts} +10 -3
- package/lib/components/Collapsible/index.d.ts.map +1 -0
- package/lib/{utils/useCollapsible.js → components/Collapsible/index.js} +37 -14
- package/lib/components/Collapsible/index.js.map +1 -0
- package/lib/components/Details/index.d.ts +13 -0
- package/lib/components/Details/index.d.ts.map +1 -0
- package/lib/components/Details/index.js +65 -0
- package/lib/components/Details/index.js.map +1 -0
- package/lib/components/Details/styles.module.css +58 -0
- package/lib/index.d.ts +20 -5
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +15 -3
- package/lib/index.js.map +1 -0
- package/lib/utils/ThemeClassNames.d.ts +38 -12
- package/lib/utils/ThemeClassNames.d.ts.map +1 -0
- package/lib/utils/ThemeClassNames.js +38 -3
- package/lib/utils/ThemeClassNames.js.map +1 -0
- package/lib/utils/announcementBarUtils.d.ts +4 -3
- package/lib/utils/announcementBarUtils.d.ts.map +1 -0
- package/lib/utils/announcementBarUtils.js +15 -18
- package/lib/utils/announcementBarUtils.js.map +1 -0
- package/lib/utils/codeBlockUtils.d.ts +11 -0
- package/lib/utils/codeBlockUtils.d.ts.map +1 -0
- package/lib/utils/codeBlockUtils.js +120 -0
- package/lib/utils/codeBlockUtils.js.map +1 -0
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +3 -2
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts.map +1 -0
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +3 -2
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js.map +1 -0
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts +1 -0
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts.map +1 -0
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +2 -3
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js.map +1 -0
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +12 -3
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts.map +1 -0
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +2 -2
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js.map +1 -0
- package/lib/utils/docsUtils.d.ts +21 -0
- package/lib/utils/docsUtils.d.ts.map +1 -0
- package/lib/utils/docsUtils.js +107 -0
- package/lib/utils/docsUtils.js.map +1 -0
- package/lib/utils/generalUtils.d.ts +7 -0
- package/lib/utils/generalUtils.d.ts.map +1 -0
- package/lib/utils/generalUtils.js +3 -2
- package/lib/utils/generalUtils.js.map +1 -0
- package/lib/utils/historyUtils.d.ts +12 -0
- package/lib/utils/historyUtils.d.ts.map +1 -0
- package/lib/utils/historyUtils.js +40 -0
- package/lib/utils/historyUtils.js.map +1 -0
- package/lib/utils/jsUtils.d.ts +20 -0
- package/lib/utils/jsUtils.d.ts.map +1 -0
- package/lib/utils/jsUtils.js +26 -0
- package/lib/utils/jsUtils.js.map +1 -0
- package/lib/utils/mobileSecondaryMenu.d.ts +3 -2
- package/lib/utils/mobileSecondaryMenu.d.ts.map +1 -0
- package/lib/utils/mobileSecondaryMenu.js +4 -4
- package/lib/utils/mobileSecondaryMenu.js.map +1 -0
- package/lib/utils/pathUtils.d.ts +1 -0
- package/lib/utils/pathUtils.d.ts.map +1 -0
- package/lib/utils/pathUtils.js +2 -3
- package/lib/utils/pathUtils.js.map +1 -0
- package/lib/utils/reactUtils.d.ts +10 -0
- package/lib/utils/reactUtils.d.ts.map +1 -0
- package/lib/utils/reactUtils.js +27 -0
- package/lib/utils/reactUtils.js.map +1 -0
- package/lib/utils/regexpUtils.d.ts +11 -0
- package/lib/utils/regexpUtils.d.ts.map +1 -0
- package/lib/utils/regexpUtils.js +17 -0
- package/lib/utils/regexpUtils.js.map +1 -0
- package/lib/utils/scrollUtils.d.ts +53 -0
- package/lib/utils/scrollUtils.d.ts.map +1 -0
- package/lib/utils/scrollUtils.js +136 -0
- package/lib/utils/scrollUtils.js.map +1 -0
- package/lib/utils/searchUtils.d.ts +1 -0
- package/lib/utils/searchUtils.d.ts.map +1 -0
- package/lib/utils/searchUtils.js +1 -0
- package/lib/utils/searchUtils.js.map +1 -0
- package/lib/utils/storageUtils.d.ts +5 -0
- package/lib/utils/storageUtils.d.ts.map +1 -0
- package/lib/utils/storageUtils.js +30 -3
- package/lib/utils/storageUtils.js.map +1 -0
- package/lib/utils/tagsUtils.d.ts +19 -0
- package/lib/utils/tagsUtils.d.ts.map +1 -0
- package/lib/utils/tagsUtils.js +34 -0
- package/lib/utils/tagsUtils.js.map +1 -0
- package/lib/utils/tocUtils.d.ts +16 -0
- package/lib/utils/tocUtils.d.ts.map +1 -0
- package/lib/utils/tocUtils.js +35 -0
- package/lib/utils/tocUtils.js.map +1 -0
- package/lib/utils/useAlternatePageUtils.d.ts +1 -0
- package/lib/utils/useAlternatePageUtils.d.ts.map +1 -0
- package/lib/utils/useAlternatePageUtils.js +1 -0
- package/lib/utils/useAlternatePageUtils.js.map +1 -0
- package/lib/utils/useContextualSearchFilters.d.ts +12 -0
- package/lib/utils/useContextualSearchFilters.d.ts.map +1 -0
- package/lib/utils/useContextualSearchFilters.js +37 -0
- package/lib/utils/useContextualSearchFilters.js.map +1 -0
- package/{src/utils/docsUtils.ts → lib/utils/useLocalPathname.d.ts} +2 -5
- package/lib/utils/useLocalPathname.d.ts.map +1 -0
- package/lib/utils/useLocalPathname.js +17 -0
- package/lib/utils/useLocalPathname.js.map +1 -0
- package/lib/utils/useLocationChange.d.ts +1 -0
- package/lib/utils/useLocationChange.d.ts.map +1 -0
- package/lib/utils/useLocationChange.js +10 -11
- package/lib/utils/useLocationChange.js.map +1 -0
- package/lib/utils/usePluralForm.d.ts +1 -0
- package/lib/utils/usePluralForm.d.ts.map +1 -0
- package/lib/utils/usePluralForm.js +2 -3
- package/lib/utils/usePluralForm.js.map +1 -0
- package/lib/utils/usePrevious.d.ts +1 -0
- package/lib/utils/usePrevious.d.ts.map +1 -0
- package/lib/utils/usePrevious.js +4 -2
- package/lib/utils/usePrevious.js.map +1 -0
- package/lib/utils/useTOCHighlight.d.ts +15 -0
- package/lib/utils/useTOCHighlight.d.ts.map +1 -0
- package/lib/utils/useTOCHighlight.js +125 -0
- package/lib/utils/useTOCHighlight.js.map +1 -0
- package/lib/utils/useThemeConfig.d.ts +32 -9
- package/lib/utils/useThemeConfig.d.ts.map +1 -0
- package/lib/utils/useThemeConfig.js +1 -0
- package/lib/utils/useThemeConfig.js.map +1 -0
- package/package.json +18 -12
- package/src/{utils/useCollapsible.tsx → components/Collapsible/index.tsx} +70 -25
- package/src/components/Details/index.tsx +94 -0
- package/src/components/Details/styles.module.css +58 -0
- package/src/index.ts +56 -5
- package/src/types.d.ts +0 -2
- package/src/utils/ThemeClassNames.ts +43 -4
- package/src/utils/announcementBarUtils.tsx +20 -15
- package/src/utils/codeBlockUtils.ts +151 -0
- package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +7 -6
- package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +2 -3
- package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +14 -14
- package/src/utils/docsUtils.tsx +185 -0
- package/src/utils/generalUtils.ts +3 -2
- package/src/utils/historyUtils.ts +50 -0
- package/src/utils/jsUtils.ts +33 -0
- package/src/utils/mobileSecondaryMenu.tsx +9 -6
- package/src/utils/pathUtils.ts +2 -3
- package/src/utils/reactUtils.tsx +34 -0
- package/src/utils/regexpUtils.ts +23 -0
- package/src/utils/scrollUtils.tsx +237 -0
- package/src/utils/storageUtils.ts +27 -4
- package/src/utils/tagsUtils.ts +48 -0
- package/src/utils/tocUtils.ts +55 -0
- package/src/utils/useContextualSearchFilters.ts +50 -0
- package/src/utils/useLocalPathname.ts +20 -0
- package/src/utils/useLocationChange.ts +10 -12
- package/src/utils/usePluralForm.ts +2 -3
- package/src/utils/usePrevious.ts +3 -2
- package/src/utils/useTOCHighlight.ts +179 -0
- package/src/utils/useThemeConfig.ts +34 -9
- package/lib/.tsbuildinfo +0 -1
- package/src/utils/__tests__/codeBlockUtils.test.ts +0 -54
- package/src/utils/__tests__/pathUtils.test.ts +0 -32
- package/tsconfig.json +0 -10
|
@@ -4,40 +4,43 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
+
|
|
7
8
|
import {useCallback} from 'react';
|
|
8
9
|
import {useDocsPreferredVersionContext} from './DocsPreferredVersionProvider';
|
|
9
|
-
import {useAllDocsData, useDocsData} from '@theme/hooks/useDocs';
|
|
10
|
+
import {useAllDocsData, useDocsData, GlobalVersion} from '@theme/hooks/useDocs';
|
|
10
11
|
|
|
11
12
|
import {DEFAULT_PLUGIN_ID} from '@docusaurus/constants';
|
|
12
13
|
|
|
13
|
-
// TODO improve typing
|
|
14
|
-
|
|
15
14
|
// Note, the preferredVersion attribute will always be null before mount
|
|
16
15
|
export function useDocsPreferredVersion(
|
|
17
16
|
pluginId: string | undefined = DEFAULT_PLUGIN_ID,
|
|
18
|
-
) {
|
|
17
|
+
): {
|
|
18
|
+
preferredVersion: GlobalVersion | null | undefined;
|
|
19
|
+
savePreferredVersionName: (versionName: string) => void;
|
|
20
|
+
} {
|
|
19
21
|
const docsData = useDocsData(pluginId);
|
|
20
22
|
const [state, api] = useDocsPreferredVersionContext();
|
|
21
23
|
|
|
22
24
|
const {preferredVersionName} = state[pluginId];
|
|
23
25
|
|
|
24
26
|
const preferredVersion = preferredVersionName
|
|
25
|
-
? docsData.versions.find(
|
|
26
|
-
(version: any) => version.name === preferredVersionName,
|
|
27
|
-
)
|
|
27
|
+
? docsData.versions.find((version) => version.name === preferredVersionName)
|
|
28
28
|
: null;
|
|
29
29
|
|
|
30
30
|
const savePreferredVersionName = useCallback(
|
|
31
31
|
(versionName: string) => {
|
|
32
32
|
api.savePreferredVersion(pluginId, versionName);
|
|
33
33
|
},
|
|
34
|
-
[api],
|
|
34
|
+
[api, pluginId],
|
|
35
35
|
);
|
|
36
36
|
|
|
37
37
|
return {preferredVersion, savePreferredVersionName} as const;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
export function useDocsPreferredVersionByPluginId(): Record<
|
|
40
|
+
export function useDocsPreferredVersionByPluginId(): Record<
|
|
41
|
+
string,
|
|
42
|
+
GlobalVersion | null | undefined
|
|
43
|
+
> {
|
|
41
44
|
const allDocsData = useAllDocsData();
|
|
42
45
|
const [state] = useDocsPreferredVersionContext();
|
|
43
46
|
|
|
@@ -47,17 +50,14 @@ export function useDocsPreferredVersionByPluginId(): Record<string, any> {
|
|
|
47
50
|
|
|
48
51
|
return preferredVersionName
|
|
49
52
|
? docsData.versions.find(
|
|
50
|
-
(version
|
|
53
|
+
(version) => version.name === preferredVersionName,
|
|
51
54
|
)
|
|
52
55
|
: null;
|
|
53
56
|
}
|
|
54
57
|
|
|
55
58
|
const pluginIds = Object.keys(allDocsData);
|
|
56
59
|
|
|
57
|
-
const result: Record<
|
|
58
|
-
string,
|
|
59
|
-
any // TODO find a way to type this properly!
|
|
60
|
-
> = {};
|
|
60
|
+
const result: Record<string, GlobalVersion | null | undefined> = {};
|
|
61
61
|
pluginIds.forEach((pluginId) => {
|
|
62
62
|
result[pluginId] = getPluginIdPreferredVersion(pluginId);
|
|
63
63
|
});
|
|
@@ -0,0 +1,185 @@
|
|
|
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, {createContext, ReactNode, useContext} from 'react';
|
|
9
|
+
import {useAllDocsData} from '@theme/hooks/useDocs';
|
|
10
|
+
import {
|
|
11
|
+
PropSidebar,
|
|
12
|
+
PropSidebarItem,
|
|
13
|
+
PropSidebarItemCategory,
|
|
14
|
+
PropVersionDoc,
|
|
15
|
+
PropVersionMetadata,
|
|
16
|
+
} from '@docusaurus/plugin-content-docs';
|
|
17
|
+
import {isSamePath} from './pathUtils';
|
|
18
|
+
import {useLocation} from '@docusaurus/router';
|
|
19
|
+
|
|
20
|
+
// TODO not ideal, see also "useDocs"
|
|
21
|
+
export const isDocsPluginEnabled: boolean = !!useAllDocsData;
|
|
22
|
+
|
|
23
|
+
// Using a Symbol because null is a valid context value (a doc can have no sidebar)
|
|
24
|
+
// Inspired by https://github.com/jamiebuilds/unstated-next/blob/master/src/unstated-next.tsx
|
|
25
|
+
const EmptyContextValue: unique symbol = Symbol('EmptyContext');
|
|
26
|
+
|
|
27
|
+
const DocsVersionContext = createContext<
|
|
28
|
+
PropVersionMetadata | typeof EmptyContextValue
|
|
29
|
+
>(EmptyContextValue);
|
|
30
|
+
|
|
31
|
+
export function DocsVersionProvider({
|
|
32
|
+
children,
|
|
33
|
+
version,
|
|
34
|
+
}: {
|
|
35
|
+
children: ReactNode;
|
|
36
|
+
version: PropVersionMetadata | typeof EmptyContextValue;
|
|
37
|
+
}): JSX.Element {
|
|
38
|
+
return (
|
|
39
|
+
<DocsVersionContext.Provider value={version}>
|
|
40
|
+
{children}
|
|
41
|
+
</DocsVersionContext.Provider>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function useDocsVersion(): PropVersionMetadata {
|
|
46
|
+
const version = useContext(DocsVersionContext);
|
|
47
|
+
if (version === EmptyContextValue) {
|
|
48
|
+
throw new Error('This hook requires usage of <DocsVersionProvider>');
|
|
49
|
+
}
|
|
50
|
+
return version;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function useDocById(id: string): PropVersionDoc;
|
|
54
|
+
export function useDocById(id: string | undefined): PropVersionDoc | undefined;
|
|
55
|
+
export function useDocById(id: string | undefined): PropVersionDoc | undefined {
|
|
56
|
+
const version = useDocsVersion();
|
|
57
|
+
if (!id) {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
const doc = version.docs[id];
|
|
61
|
+
if (!doc) {
|
|
62
|
+
throw new Error(`no version doc found by id=${id}`);
|
|
63
|
+
}
|
|
64
|
+
return doc;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const DocsSidebarContext = createContext<
|
|
68
|
+
PropSidebar | null | typeof EmptyContextValue
|
|
69
|
+
>(EmptyContextValue);
|
|
70
|
+
|
|
71
|
+
export function DocsSidebarProvider({
|
|
72
|
+
children,
|
|
73
|
+
sidebar,
|
|
74
|
+
}: {
|
|
75
|
+
children: ReactNode;
|
|
76
|
+
sidebar: PropSidebar | null;
|
|
77
|
+
}): JSX.Element {
|
|
78
|
+
return (
|
|
79
|
+
<DocsSidebarContext.Provider value={sidebar}>
|
|
80
|
+
{children}
|
|
81
|
+
</DocsSidebarContext.Provider>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function useDocsSidebar(): PropSidebar | null {
|
|
86
|
+
const sidebar = useContext(DocsSidebarContext);
|
|
87
|
+
if (sidebar === EmptyContextValue) {
|
|
88
|
+
throw new Error('This hook requires usage of <DocsSidebarProvider>');
|
|
89
|
+
}
|
|
90
|
+
return sidebar;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Use the components props and the sidebar in context
|
|
94
|
+
// to get back the related sidebar category that we want to render
|
|
95
|
+
export function findSidebarCategory(
|
|
96
|
+
sidebar: PropSidebar,
|
|
97
|
+
predicate: (category: PropSidebarItemCategory) => boolean,
|
|
98
|
+
): PropSidebarItemCategory | undefined {
|
|
99
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
100
|
+
for (const item of sidebar) {
|
|
101
|
+
if (item.type === 'category') {
|
|
102
|
+
if (predicate(item)) {
|
|
103
|
+
return item;
|
|
104
|
+
} else {
|
|
105
|
+
const subItem = findSidebarCategory(item.items, predicate);
|
|
106
|
+
if (subItem) {
|
|
107
|
+
return subItem;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// If a category card has no link => link to the first subItem having a link
|
|
116
|
+
export function findFirstCategoryLink(
|
|
117
|
+
item: PropSidebarItemCategory,
|
|
118
|
+
): string | undefined {
|
|
119
|
+
if (item.href) {
|
|
120
|
+
return item.href;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
124
|
+
for (const subItem of item.items) {
|
|
125
|
+
if (subItem.type === 'link') {
|
|
126
|
+
return subItem.href;
|
|
127
|
+
}
|
|
128
|
+
if (subItem.type === 'category') {
|
|
129
|
+
const categoryLink = findFirstCategoryLink(subItem);
|
|
130
|
+
if (categoryLink) {
|
|
131
|
+
return categoryLink;
|
|
132
|
+
}
|
|
133
|
+
} else {
|
|
134
|
+
throw new Error(
|
|
135
|
+
`Unexpected category item type for ${JSON.stringify(subItem)}`,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function useCurrentSidebarCategory(): PropSidebarItemCategory {
|
|
143
|
+
const {pathname} = useLocation();
|
|
144
|
+
const sidebar = useDocsSidebar();
|
|
145
|
+
if (!sidebar) {
|
|
146
|
+
throw new Error('Unexpected: cant find current sidebar in context');
|
|
147
|
+
}
|
|
148
|
+
const category = findSidebarCategory(sidebar, (item) =>
|
|
149
|
+
isSamePath(item.href, pathname),
|
|
150
|
+
);
|
|
151
|
+
if (!category) {
|
|
152
|
+
throw new Error(
|
|
153
|
+
`Unexpected: sidebar category could not be found for pathname='${pathname}'.
|
|
154
|
+
Hook useCurrentSidebarCategory() should only be used on Category pages`,
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
return category;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function containsActiveSidebarItem(
|
|
161
|
+
items: PropSidebarItem[],
|
|
162
|
+
activePath: string,
|
|
163
|
+
): boolean {
|
|
164
|
+
return items.some((subItem) => isActiveSidebarItem(subItem, activePath));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function isActiveSidebarItem(
|
|
168
|
+
item: PropSidebarItem,
|
|
169
|
+
activePath: string,
|
|
170
|
+
): boolean {
|
|
171
|
+
const isActive = (testedPath: string | undefined) =>
|
|
172
|
+
typeof testedPath !== 'undefined' && isSamePath(testedPath, activePath);
|
|
173
|
+
|
|
174
|
+
if (item.type === 'link') {
|
|
175
|
+
return isActive(item.href);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (item.type === 'category') {
|
|
179
|
+
return (
|
|
180
|
+
isActive(item.href) || containsActiveSidebarItem(item.items, activePath)
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
@@ -4,11 +4,12 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
+
|
|
7
8
|
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
|
8
9
|
|
|
9
10
|
export const useTitleFormatter = (title?: string | undefined): string => {
|
|
10
|
-
const {siteConfig
|
|
11
|
-
const {title: siteTitle, titleDelimiter
|
|
11
|
+
const {siteConfig} = useDocusaurusContext();
|
|
12
|
+
const {title: siteTitle, titleDelimiter} = siteConfig;
|
|
12
13
|
return title && title.trim().length
|
|
13
14
|
? `${title.trim()} ${titleDelimiter} ${siteTitle}`
|
|
14
15
|
: siteTitle;
|
|
@@ -0,0 +1,50 @@
|
|
|
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 {useHistory} from '@docusaurus/router';
|
|
10
|
+
import type {Location, Action} from '@docusaurus/history';
|
|
11
|
+
|
|
12
|
+
type HistoryBlockHandler = (location: Location, action: Action) => void | false;
|
|
13
|
+
|
|
14
|
+
/*
|
|
15
|
+
Permits to register a handler that will be called on history actions (pop,push,replace)
|
|
16
|
+
If the handler returns false, the navigation transition will be blocked/cancelled
|
|
17
|
+
*/
|
|
18
|
+
export function useHistoryActionHandler(handler: HistoryBlockHandler): void {
|
|
19
|
+
const {block} = useHistory();
|
|
20
|
+
|
|
21
|
+
// Avoid stale closure issues without triggering useless re-renders
|
|
22
|
+
const lastHandlerRef = useRef(handler);
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
lastHandlerRef.current = handler;
|
|
25
|
+
}, [handler]);
|
|
26
|
+
|
|
27
|
+
useEffect(
|
|
28
|
+
() =>
|
|
29
|
+
// See https://github.com/remix-run/history/blob/main/docs/blocking-transitions.md
|
|
30
|
+
block((location, action) => lastHandlerRef.current(location, action)),
|
|
31
|
+
[block, lastHandlerRef],
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/*
|
|
36
|
+
Permits to register a handler that will be called on history pop navigation (backward/forward)
|
|
37
|
+
If the handler returns false, the backward/forward transition will be blocked
|
|
38
|
+
|
|
39
|
+
Unfortunately there's no good way to detect the "direction" (backward/forward) of the POP event.
|
|
40
|
+
*/
|
|
41
|
+
export function useHistoryPopHandler(handler: HistoryBlockHandler): void {
|
|
42
|
+
useHistoryActionHandler((location, action) => {
|
|
43
|
+
if (action === 'POP') {
|
|
44
|
+
// Eventually block navigation if handler returns false
|
|
45
|
+
return handler(location, action);
|
|
46
|
+
}
|
|
47
|
+
// Don't block other navigation actions
|
|
48
|
+
return undefined;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
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 (duplicated).
|
|
14
|
+
* @returns Value of the elements `v` that have a preceding element `u` where `comparator(u, v) === true`. Values within the returned array are not guaranteed to be unique.
|
|
15
|
+
*/
|
|
16
|
+
export function duplicates<T>(
|
|
17
|
+
arr: readonly T[],
|
|
18
|
+
comparator: (a: T, b: T) => boolean = (a, b) => a === b,
|
|
19
|
+
): T[] {
|
|
20
|
+
return arr.filter(
|
|
21
|
+
(v, vIndex) => arr.findIndex((u) => comparator(u, v)) !== vIndex,
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Remove duplicate array items (similar to _.uniq)
|
|
27
|
+
* @param arr The array.
|
|
28
|
+
* @returns An array with duplicate elements removed by reference comparison.
|
|
29
|
+
*/
|
|
30
|
+
export function uniq<T>(arr: T[]): T[] {
|
|
31
|
+
// Note: had problems with [...new Set()]: https://github.com/facebook/docusaurus/issues/4972#issuecomment-863895061
|
|
32
|
+
return Array.from(new Set(arr));
|
|
33
|
+
}
|
|
@@ -29,7 +29,7 @@ type ExtraProps = {
|
|
|
29
29
|
toggleSidebar: () => void;
|
|
30
30
|
};
|
|
31
31
|
|
|
32
|
-
export type MobileSecondaryMenuComponent<Props
|
|
32
|
+
export type MobileSecondaryMenuComponent<Props> = ComponentType<
|
|
33
33
|
Props & ExtraProps
|
|
34
34
|
>;
|
|
35
35
|
|
|
@@ -46,7 +46,11 @@ type ContextValue = ReturnType<typeof useContextValue>;
|
|
|
46
46
|
|
|
47
47
|
const Context = createContext<ContextValue | null>(null);
|
|
48
48
|
|
|
49
|
-
export function MobileSecondaryMenuProvider({
|
|
49
|
+
export function MobileSecondaryMenuProvider({
|
|
50
|
+
children,
|
|
51
|
+
}: {
|
|
52
|
+
children: ReactNode;
|
|
53
|
+
}): JSX.Element {
|
|
50
54
|
return (
|
|
51
55
|
<Context.Provider value={useContextValue()}>{children}</Context.Provider>
|
|
52
56
|
);
|
|
@@ -79,13 +83,14 @@ function useShallowMemoizedObject<O extends Record<string, unknown>>(obj: O) {
|
|
|
79
83
|
return useMemo(
|
|
80
84
|
() => obj,
|
|
81
85
|
// Is this safe?
|
|
86
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
82
87
|
[...Object.keys(obj), ...Object.values(obj)],
|
|
83
88
|
);
|
|
84
89
|
}
|
|
85
90
|
|
|
86
91
|
// Fill the secondary menu placeholder with some real content
|
|
87
92
|
export function MobileSecondaryMenuFiller<
|
|
88
|
-
Props extends Record<string, unknown
|
|
93
|
+
Props extends Record<string, unknown>,
|
|
89
94
|
>({
|
|
90
95
|
component,
|
|
91
96
|
props,
|
|
@@ -103,9 +108,7 @@ export function MobileSecondaryMenuFiller<
|
|
|
103
108
|
setState({component, props: memoizedProps});
|
|
104
109
|
}, [setState, component, memoizedProps]);
|
|
105
110
|
|
|
106
|
-
useEffect(() =>
|
|
107
|
-
return () => setState(null);
|
|
108
|
-
}, [setState]);
|
|
111
|
+
useEffect(() => () => setState(null), [setState]);
|
|
109
112
|
|
|
110
113
|
return null;
|
|
111
114
|
}
|
package/src/utils/pathUtils.ts
CHANGED
|
@@ -10,8 +10,7 @@ export const isSamePath = (
|
|
|
10
10
|
path1: string | undefined,
|
|
11
11
|
path2: string | undefined,
|
|
12
12
|
): boolean => {
|
|
13
|
-
const normalize = (pathname: string | undefined) =>
|
|
14
|
-
|
|
15
|
-
};
|
|
13
|
+
const normalize = (pathname: string | undefined) =>
|
|
14
|
+
!pathname || pathname?.endsWith('/') ? pathname : `${pathname}/`;
|
|
16
15
|
return normalize(path1) === normalize(path2);
|
|
17
16
|
};
|
|
@@ -0,0 +1,34 @@
|
|
|
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
|
+
|
|
10
|
+
// This hook is like useLayoutEffect, but without the SSR warning
|
|
11
|
+
// It seems hacky but it's used in many React libs (Redux, Formik...)
|
|
12
|
+
// Also mentioned here: https://github.com/facebook/react/issues/16956
|
|
13
|
+
// It is useful when you need to update a ref as soon as possible after a React render (before useEffect)
|
|
14
|
+
export const useIsomorphicLayoutEffect =
|
|
15
|
+
typeof window !== 'undefined' ? useLayoutEffect : useEffect;
|
|
16
|
+
|
|
17
|
+
// Permits to transform an unstable callback (like an arrow function provided as props)
|
|
18
|
+
// to a "stable" callback that is safe to use in a useEffect dependency array
|
|
19
|
+
// Useful to avoid React stale closure problems + avoid useless effect re-executions
|
|
20
|
+
//
|
|
21
|
+
// Workaround until the React team recommends a good solution, see https://github.com/facebook/react/issues/16956
|
|
22
|
+
// This generally works has some potential drawbacks, such as https://github.com/facebook/react/issues/16956#issuecomment-536636418
|
|
23
|
+
export function useDynamicCallback<T extends (...args: never[]) => unknown>(
|
|
24
|
+
callback: T,
|
|
25
|
+
): T {
|
|
26
|
+
const ref = useRef<T>(callback);
|
|
27
|
+
|
|
28
|
+
useIsomorphicLayoutEffect(() => {
|
|
29
|
+
ref.current = callback;
|
|
30
|
+
}, [callback]);
|
|
31
|
+
|
|
32
|
+
// @ts-expect-error: TODO, not sure how to fix this TS error
|
|
33
|
+
return useCallback<T>((...args) => ref.current(...args), []);
|
|
34
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
* Utility to convert an optional string into a Regex case sensitive and global
|
|
10
|
+
*/
|
|
11
|
+
export function isRegexpStringMatch(
|
|
12
|
+
regexAsString?: string,
|
|
13
|
+
valueToTest?: string,
|
|
14
|
+
): boolean {
|
|
15
|
+
if (
|
|
16
|
+
typeof regexAsString === 'undefined' ||
|
|
17
|
+
typeof valueToTest === 'undefined'
|
|
18
|
+
) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return new RegExp(regexAsString, 'gi').test(valueToTest);
|
|
23
|
+
}
|