@docusaurus/theme-common 2.0.0-beta.15a2b59f9 → 2.0.0-beta.17

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.
Files changed (209) hide show
  1. package/lib/components/Collapsible/index.d.ts +36 -0
  2. package/lib/components/Collapsible/index.d.ts.map +1 -0
  3. package/lib/components/Collapsible/index.js +143 -0
  4. package/lib/components/Collapsible/index.js.map +1 -0
  5. package/lib/components/Details/index.d.ts +12 -0
  6. package/lib/components/Details/index.d.ts.map +1 -0
  7. package/lib/components/Details/index.js +66 -0
  8. package/lib/components/Details/index.js.map +1 -0
  9. package/lib/components/Details/styles.module.css +58 -0
  10. package/{src/utils/docsUtils.ts → lib/hooks/styles.css} +3 -4
  11. package/lib/hooks/useHideableNavbar.d.ts +13 -0
  12. package/lib/hooks/useHideableNavbar.d.ts.map +1 -0
  13. package/lib/hooks/useHideableNavbar.js +59 -0
  14. package/lib/hooks/useHideableNavbar.js.map +1 -0
  15. package/lib/hooks/useKeyboardNavigation.d.ts +10 -0
  16. package/lib/hooks/useKeyboardNavigation.d.ts.map +1 -0
  17. package/lib/hooks/useKeyboardNavigation.js +31 -0
  18. package/lib/hooks/useKeyboardNavigation.js.map +1 -0
  19. package/lib/hooks/useLockBodyScroll.d.ts +8 -0
  20. package/lib/hooks/useLockBodyScroll.d.ts.map +1 -0
  21. package/lib/hooks/useLockBodyScroll.js +16 -0
  22. package/lib/hooks/useLockBodyScroll.js.map +1 -0
  23. package/lib/hooks/usePrismTheme.d.ts +9 -0
  24. package/lib/hooks/usePrismTheme.d.ts.map +1 -0
  25. package/lib/hooks/usePrismTheme.js +18 -0
  26. package/lib/hooks/usePrismTheme.js.map +1 -0
  27. package/lib/hooks/useSearchPage.d.ts +14 -0
  28. package/lib/hooks/useSearchPage.d.ts.map +1 -0
  29. package/lib/hooks/useSearchPage.js +42 -0
  30. package/lib/hooks/useSearchPage.js.map +1 -0
  31. package/lib/hooks/useWindowSize.d.ts +15 -0
  32. package/lib/hooks/useWindowSize.d.ts.map +1 -0
  33. package/lib/hooks/useWindowSize.js +56 -0
  34. package/lib/hooks/useWindowSize.js.map +1 -0
  35. package/lib/index.d.ts +32 -3
  36. package/lib/index.d.ts.map +1 -0
  37. package/lib/index.js +26 -2
  38. package/lib/index.js.map +1 -0
  39. package/lib/utils/ThemeClassNames.d.ts +40 -12
  40. package/lib/utils/ThemeClassNames.d.ts.map +1 -0
  41. package/lib/utils/ThemeClassNames.js +41 -3
  42. package/lib/utils/ThemeClassNames.js.map +1 -0
  43. package/lib/utils/announcementBarUtils.d.ts +6 -5
  44. package/lib/utils/announcementBarUtils.d.ts.map +1 -0
  45. package/lib/utils/announcementBarUtils.js +20 -21
  46. package/lib/utils/announcementBarUtils.js.map +1 -0
  47. package/lib/utils/codeBlockUtils.d.ts +10 -0
  48. package/lib/utils/codeBlockUtils.d.ts.map +1 -0
  49. package/lib/utils/codeBlockUtils.js +125 -3
  50. package/lib/utils/codeBlockUtils.js.map +1 -0
  51. package/lib/utils/colorModeUtils.d.ts +18 -0
  52. package/lib/utils/colorModeUtils.d.ts.map +1 -0
  53. package/lib/utils/colorModeUtils.js +107 -0
  54. package/lib/utils/colorModeUtils.js.map +1 -0
  55. package/lib/utils/docSidebarItemsExpandedState.d.ts +17 -0
  56. package/lib/utils/docSidebarItemsExpandedState.d.ts.map +1 -0
  57. package/lib/utils/docSidebarItemsExpandedState.js +23 -0
  58. package/lib/utils/docSidebarItemsExpandedState.js.map +1 -0
  59. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +3 -2
  60. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts.map +1 -0
  61. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +9 -11
  62. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js.map +1 -0
  63. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts +2 -1
  64. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts.map +1 -0
  65. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +2 -3
  66. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js.map +1 -0
  67. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +12 -3
  68. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts.map +1 -0
  69. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +3 -3
  70. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js.map +1 -0
  71. package/lib/utils/docsUtils.d.ts +26 -0
  72. package/lib/utils/docsUtils.d.ts.map +1 -0
  73. package/lib/utils/docsUtils.js +136 -1
  74. package/lib/utils/docsUtils.js.map +1 -0
  75. package/lib/utils/generalUtils.d.ts +7 -0
  76. package/lib/utils/generalUtils.d.ts.map +1 -0
  77. package/lib/utils/generalUtils.js +3 -2
  78. package/lib/utils/generalUtils.js.map +1 -0
  79. package/lib/utils/historyUtils.d.ts +23 -0
  80. package/lib/utils/historyUtils.d.ts.map +1 -0
  81. package/lib/utils/historyUtils.js +41 -0
  82. package/lib/utils/historyUtils.js.map +1 -0
  83. package/lib/utils/jsUtils.d.ts +23 -0
  84. package/lib/utils/jsUtils.d.ts.map +1 -0
  85. package/lib/utils/jsUtils.js +29 -0
  86. package/lib/utils/jsUtils.js.map +1 -0
  87. package/lib/utils/mobileSecondaryMenu.d.ts +21 -0
  88. package/lib/utils/mobileSecondaryMenu.d.ts.map +1 -0
  89. package/lib/utils/mobileSecondaryMenu.js +51 -0
  90. package/lib/utils/mobileSecondaryMenu.js.map +1 -0
  91. package/lib/utils/pathUtils.d.ts +1 -0
  92. package/lib/utils/pathUtils.d.ts.map +1 -0
  93. package/lib/utils/pathUtils.js +5 -4
  94. package/lib/utils/pathUtils.js.map +1 -0
  95. package/lib/utils/reactUtils.d.ts +31 -0
  96. package/lib/utils/reactUtils.d.ts.map +1 -0
  97. package/lib/utils/reactUtils.js +43 -0
  98. package/lib/utils/reactUtils.js.map +1 -0
  99. package/lib/utils/regexpUtils.d.ts +11 -0
  100. package/lib/utils/regexpUtils.d.ts.map +1 -0
  101. package/lib/utils/regexpUtils.js +17 -0
  102. package/lib/utils/regexpUtils.js.map +1 -0
  103. package/lib/utils/routesUtils.d.ts +14 -0
  104. package/lib/utils/routesUtils.d.ts.map +1 -0
  105. package/lib/utils/routesUtils.js +41 -0
  106. package/lib/utils/routesUtils.js.map +1 -0
  107. package/lib/utils/scrollUtils.d.ts +53 -0
  108. package/lib/utils/scrollUtils.d.ts.map +1 -0
  109. package/lib/utils/scrollUtils.js +135 -0
  110. package/lib/utils/scrollUtils.js.map +1 -0
  111. package/lib/utils/searchUtils.d.ts +1 -0
  112. package/lib/utils/searchUtils.d.ts.map +1 -0
  113. package/lib/utils/searchUtils.js +1 -0
  114. package/lib/utils/searchUtils.js.map +1 -0
  115. package/lib/utils/storageUtils.d.ts +5 -0
  116. package/lib/utils/storageUtils.d.ts.map +1 -0
  117. package/lib/utils/storageUtils.js +39 -14
  118. package/lib/utils/storageUtils.js.map +1 -0
  119. package/lib/utils/tabGroupChoiceUtils.d.ts +19 -0
  120. package/lib/utils/tabGroupChoiceUtils.d.ts.map +1 -0
  121. package/lib/utils/tabGroupChoiceUtils.js +55 -0
  122. package/lib/utils/tabGroupChoiceUtils.js.map +1 -0
  123. package/lib/utils/tagsUtils.d.ts +19 -0
  124. package/lib/utils/tagsUtils.d.ts.map +1 -0
  125. package/lib/utils/tagsUtils.js +33 -0
  126. package/lib/utils/tagsUtils.js.map +1 -0
  127. package/lib/utils/tocUtils.d.ts +20 -0
  128. package/lib/utils/tocUtils.d.ts.map +1 -0
  129. package/lib/utils/tocUtils.js +73 -0
  130. package/lib/utils/tocUtils.js.map +1 -0
  131. package/lib/utils/useAlternatePageUtils.d.ts +1 -0
  132. package/lib/utils/useAlternatePageUtils.d.ts.map +1 -0
  133. package/lib/utils/useAlternatePageUtils.js +3 -1
  134. package/lib/utils/useAlternatePageUtils.js.map +1 -0
  135. package/lib/utils/useContextualSearchFilters.d.ts +12 -0
  136. package/lib/utils/useContextualSearchFilters.d.ts.map +1 -0
  137. package/lib/utils/useContextualSearchFilters.js +36 -0
  138. package/lib/utils/useContextualSearchFilters.js.map +1 -0
  139. package/lib/utils/useLocalPathname.d.ts +8 -0
  140. package/lib/utils/useLocalPathname.d.ts.map +1 -0
  141. package/lib/utils/useLocalPathname.js +17 -0
  142. package/lib/utils/useLocalPathname.js.map +1 -0
  143. package/lib/utils/useLocationChange.d.ts +2 -1
  144. package/lib/utils/useLocationChange.d.ts.map +1 -0
  145. package/lib/utils/useLocationChange.js +12 -10
  146. package/lib/utils/useLocationChange.js.map +1 -0
  147. package/lib/utils/usePluralForm.d.ts +1 -0
  148. package/lib/utils/usePluralForm.d.ts.map +1 -0
  149. package/lib/utils/usePluralForm.js +28 -24
  150. package/lib/utils/usePluralForm.js.map +1 -0
  151. package/lib/utils/usePrevious.d.ts +1 -0
  152. package/lib/utils/usePrevious.d.ts.map +1 -0
  153. package/lib/utils/usePrevious.js +4 -2
  154. package/lib/utils/usePrevious.js.map +1 -0
  155. package/lib/utils/useTOCHighlight.d.ts +14 -0
  156. package/lib/utils/useTOCHighlight.d.ts.map +1 -0
  157. package/lib/utils/useTOCHighlight.js +126 -0
  158. package/lib/utils/useTOCHighlight.js.map +1 -0
  159. package/lib/utils/useThemeConfig.d.ts +35 -17
  160. package/lib/utils/useThemeConfig.d.ts.map +1 -0
  161. package/lib/utils/useThemeConfig.js +1 -0
  162. package/lib/utils/useThemeConfig.js.map +1 -0
  163. package/package.json +19 -13
  164. package/src/components/Collapsible/index.tsx +247 -0
  165. package/src/components/Details/index.tsx +102 -0
  166. package/src/components/Details/styles.module.css +58 -0
  167. package/src/hooks/styles.css +10 -0
  168. package/src/hooks/useHideableNavbar.ts +77 -0
  169. package/src/hooks/useKeyboardNavigation.ts +37 -0
  170. package/src/hooks/useLockBodyScroll.ts +18 -0
  171. package/src/hooks/usePrismTheme.ts +20 -0
  172. package/src/hooks/useSearchPage.ts +66 -0
  173. package/src/hooks/useWindowSize.ts +70 -0
  174. package/src/index.ts +95 -3
  175. package/src/types.d.ts +0 -2
  176. package/src/utils/ThemeClassNames.ts +46 -4
  177. package/src/utils/announcementBarUtils.tsx +27 -22
  178. package/src/utils/codeBlockUtils.ts +153 -2
  179. package/src/utils/colorModeUtils.tsx +158 -0
  180. package/src/utils/docSidebarItemsExpandedState.tsx +40 -0
  181. package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +17 -16
  182. package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +3 -4
  183. package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +18 -14
  184. package/src/utils/docsUtils.tsx +231 -0
  185. package/src/utils/generalUtils.ts +3 -2
  186. package/src/utils/historyUtils.ts +51 -0
  187. package/src/utils/jsUtils.ts +36 -0
  188. package/src/utils/mobileSecondaryMenu.tsx +114 -0
  189. package/src/utils/pathUtils.ts +6 -4
  190. package/src/utils/reactUtils.tsx +53 -0
  191. package/src/utils/regexpUtils.ts +23 -0
  192. package/src/utils/routesUtils.ts +56 -0
  193. package/src/utils/scrollUtils.tsx +235 -0
  194. package/src/utils/storageUtils.ts +37 -12
  195. package/src/utils/tabGroupChoiceUtils.tsx +89 -0
  196. package/src/utils/tagsUtils.ts +48 -0
  197. package/src/utils/tocUtils.ts +108 -0
  198. package/src/utils/useAlternatePageUtils.ts +4 -3
  199. package/src/utils/useContextualSearchFilters.ts +53 -0
  200. package/src/utils/useLocalPathname.ts +20 -0
  201. package/src/utils/useLocationChange.ts +13 -11
  202. package/src/utils/usePluralForm.ts +31 -26
  203. package/src/utils/usePrevious.ts +3 -2
  204. package/src/utils/useTOCHighlight.ts +183 -0
  205. package/src/utils/useThemeConfig.ts +37 -17
  206. package/lib/.tsbuildinfo +0 -1
  207. package/src/utils/__tests__/codeBlockUtils.test.ts +0 -54
  208. package/src/utils/__tests__/pathUtils.test.ts +0 -32
  209. package/tsconfig.json +0 -10
@@ -0,0 +1,231 @@
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, type ReactNode, useContext} from 'react';
9
+ import {
10
+ useActivePlugin,
11
+ useAllDocsData,
12
+ } from '@docusaurus/plugin-content-docs/client';
13
+ import type {
14
+ PropSidebar,
15
+ PropSidebarItem,
16
+ PropSidebarItemCategory,
17
+ PropVersionDoc,
18
+ PropVersionMetadata,
19
+ PropSidebarBreadcrumbsItem,
20
+ } from '@docusaurus/plugin-content-docs';
21
+ import {isSamePath} from './pathUtils';
22
+ import {ReactContextError} from './reactUtils';
23
+ import {useLocation} from '@docusaurus/router';
24
+
25
+ // TODO not ideal, see also "useDocs"
26
+ export const isDocsPluginEnabled: boolean = !!useAllDocsData;
27
+
28
+ // Using a Symbol because null is a valid context value (a doc with no sidebar)
29
+ // Inspired by https://github.com/jamiebuilds/unstated-next/blob/master/src/unstated-next.tsx
30
+ const EmptyContextValue: unique symbol = Symbol('EmptyContext');
31
+
32
+ const DocsVersionContext = createContext<
33
+ PropVersionMetadata | typeof EmptyContextValue
34
+ >(EmptyContextValue);
35
+
36
+ export function DocsVersionProvider({
37
+ children,
38
+ version,
39
+ }: {
40
+ children: ReactNode;
41
+ version: PropVersionMetadata | typeof EmptyContextValue;
42
+ }): JSX.Element {
43
+ return (
44
+ <DocsVersionContext.Provider value={version}>
45
+ {children}
46
+ </DocsVersionContext.Provider>
47
+ );
48
+ }
49
+
50
+ export function useDocsVersion(): PropVersionMetadata {
51
+ const version = useContext(DocsVersionContext);
52
+ if (version === EmptyContextValue) {
53
+ throw new ReactContextError('DocsVersionProvider');
54
+ }
55
+ return version;
56
+ }
57
+
58
+ export function useDocById(id: string): PropVersionDoc;
59
+ export function useDocById(id: string | undefined): PropVersionDoc | undefined;
60
+ export function useDocById(id: string | undefined): PropVersionDoc | undefined {
61
+ const version = useDocsVersion();
62
+ if (!id) {
63
+ return undefined;
64
+ }
65
+ const doc = version.docs[id];
66
+ if (!doc) {
67
+ throw new Error(`no version doc found by id=${id}`);
68
+ }
69
+ return doc;
70
+ }
71
+
72
+ const DocsSidebarContext = createContext<
73
+ PropSidebar | null | typeof EmptyContextValue
74
+ >(EmptyContextValue);
75
+
76
+ export function DocsSidebarProvider({
77
+ children,
78
+ sidebar,
79
+ }: {
80
+ children: ReactNode;
81
+ sidebar: PropSidebar | null;
82
+ }): JSX.Element {
83
+ return (
84
+ <DocsSidebarContext.Provider value={sidebar}>
85
+ {children}
86
+ </DocsSidebarContext.Provider>
87
+ );
88
+ }
89
+
90
+ export function useDocsSidebar(): PropSidebar | null {
91
+ const sidebar = useContext(DocsSidebarContext);
92
+ if (sidebar === EmptyContextValue) {
93
+ throw new ReactContextError('DocsSidebarProvider');
94
+ }
95
+ return sidebar;
96
+ }
97
+
98
+ // Use the components props and the sidebar in context
99
+ // to get back the related sidebar category that we want to render
100
+ export function findSidebarCategory(
101
+ sidebar: PropSidebar,
102
+ predicate: (category: PropSidebarItemCategory) => boolean,
103
+ ): PropSidebarItemCategory | undefined {
104
+ for (const item of sidebar) {
105
+ if (item.type === 'category') {
106
+ if (predicate(item)) {
107
+ return item;
108
+ }
109
+ const subItem = findSidebarCategory(item.items, predicate);
110
+ if (subItem) {
111
+ return subItem;
112
+ }
113
+ }
114
+ }
115
+ return undefined;
116
+ }
117
+
118
+ // If a category card has no link => link to the first subItem having a link
119
+ export function findFirstCategoryLink(
120
+ item: PropSidebarItemCategory,
121
+ ): string | undefined {
122
+ if (item.href) {
123
+ return item.href;
124
+ }
125
+
126
+ for (const subItem of item.items) {
127
+ if (subItem.type === 'link') {
128
+ return subItem.href;
129
+ } else if (subItem.type === 'category') {
130
+ const categoryLink = findFirstCategoryLink(subItem);
131
+ if (categoryLink) {
132
+ return categoryLink;
133
+ }
134
+ } else if (subItem.type === 'html') {
135
+ // skip
136
+ } else {
137
+ throw new Error(
138
+ `Unexpected category item type for ${JSON.stringify(subItem)}`,
139
+ );
140
+ }
141
+ }
142
+ return undefined;
143
+ }
144
+
145
+ export function useCurrentSidebarCategory(): PropSidebarItemCategory {
146
+ const {pathname} = useLocation();
147
+ const sidebar = useDocsSidebar();
148
+ if (!sidebar) {
149
+ throw new Error('Unexpected: cant find current sidebar in context');
150
+ }
151
+ const category = findSidebarCategory(sidebar, (item) =>
152
+ isSamePath(item.href, pathname),
153
+ );
154
+ if (!category) {
155
+ throw new Error(
156
+ `Unexpected: sidebar category could not be found for pathname='${pathname}'.
157
+ Hook useCurrentSidebarCategory() should only be used on Category pages`,
158
+ );
159
+ }
160
+ return category;
161
+ }
162
+
163
+ function containsActiveSidebarItem(
164
+ items: PropSidebarItem[],
165
+ activePath: string,
166
+ ): boolean {
167
+ return items.some((subItem) => isActiveSidebarItem(subItem, activePath));
168
+ }
169
+
170
+ export function isActiveSidebarItem(
171
+ item: PropSidebarItem,
172
+ activePath: string,
173
+ ): boolean {
174
+ const isActive = (testedPath: string | undefined) =>
175
+ typeof testedPath !== 'undefined' && isSamePath(testedPath, activePath);
176
+
177
+ if (item.type === 'link') {
178
+ return isActive(item.href);
179
+ }
180
+
181
+ if (item.type === 'category') {
182
+ return (
183
+ isActive(item.href) || containsActiveSidebarItem(item.items, activePath)
184
+ );
185
+ }
186
+
187
+ return false;
188
+ }
189
+
190
+ export function getBreadcrumbs({
191
+ sidebar,
192
+ pathname,
193
+ }: {
194
+ sidebar: PropSidebar;
195
+ pathname: string;
196
+ }): PropSidebarBreadcrumbsItem[] {
197
+ const breadcrumbs: PropSidebarBreadcrumbsItem[] = [];
198
+
199
+ function extract(items: PropSidebar) {
200
+ for (const item of items) {
201
+ if (
202
+ item.type === 'category' &&
203
+ (isSamePath(item.href, pathname) || extract(item.items))
204
+ ) {
205
+ breadcrumbs.push(item);
206
+ return true;
207
+ } else if (item.type === 'link' && isSamePath(item.href, pathname)) {
208
+ breadcrumbs.push(item);
209
+ return true;
210
+ }
211
+ }
212
+
213
+ return false;
214
+ }
215
+
216
+ extract(sidebar);
217
+
218
+ return breadcrumbs.reverse();
219
+ }
220
+
221
+ export function useSidebarBreadcrumbs(): PropSidebarBreadcrumbsItem[] | null {
222
+ const sidebar = useDocsSidebar();
223
+ const {pathname} = useLocation();
224
+ const breadcrumbsOption = useActivePlugin()?.pluginData.breadcrumbs;
225
+
226
+ if (breadcrumbsOption === false || !sidebar) {
227
+ return null;
228
+ }
229
+
230
+ return getBreadcrumbs({sidebar, pathname});
231
+ }
@@ -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 = {}} = useDocusaurusContext();
11
- const {title: siteTitle, titleDelimiter = '|'} = siteConfig;
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,51 @@
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 '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,
16
+ * push, replace) If the handler returns false, the navigation transition will
17
+ * be blocked/cancelled
18
+ */
19
+ export function useHistoryActionHandler(handler: HistoryBlockHandler): void {
20
+ const {block} = useHistory();
21
+
22
+ // Avoid stale closure issues without triggering useless re-renders
23
+ const lastHandlerRef = useRef(handler);
24
+ useEffect(() => {
25
+ lastHandlerRef.current = handler;
26
+ }, [handler]);
27
+
28
+ useEffect(
29
+ () =>
30
+ // See https://github.com/remix-run/history/blob/main/docs/blocking-transitions.md
31
+ block((location, action) => lastHandlerRef.current(location, action)),
32
+ [block, lastHandlerRef],
33
+ );
34
+ }
35
+
36
+ /**
37
+ * Permits to register a handler that will be called on history pop navigation
38
+ * (backward/forward) If the handler returns false, the backward/forward
39
+ * transition will be blocked. Unfortunately there's no good way to detect the
40
+ * "direction" (backward/forward) of the POP event.
41
+ */
42
+ export function useHistoryPopHandler(handler: HistoryBlockHandler): void {
43
+ useHistoryActionHandler((location, action) => {
44
+ if (action === 'POP') {
45
+ // Eventually block navigation if handler returns false
46
+ return handler(location, action);
47
+ }
48
+ // Don't block other navigation actions
49
+ return undefined;
50
+ });
51
+ }
@@ -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,114 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import React, {
9
+ useState,
10
+ type ReactNode,
11
+ useContext,
12
+ createContext,
13
+ useEffect,
14
+ type ComponentType,
15
+ useMemo,
16
+ } from 'react';
17
+ import {ReactContextError} from './reactUtils';
18
+
19
+ /*
20
+ The idea behind all this is that a specific component must be able to fill a
21
+ placeholder in the generic layout. The doc page should be able to fill the
22
+ secondary menu of the main mobile navbar. This permits to reduce coupling
23
+ between the main layout and the specific page.
24
+
25
+ This kind of feature is often called portal/teleport/gateway... various
26
+ unmaintained React libs exist. Most up-to-date one: https://github.com/gregberge/react-teleporter
27
+ Not sure any of those is safe regarding concurrent mode.
28
+ */
29
+
30
+ type ExtraProps = {
31
+ toggleSidebar: () => void;
32
+ };
33
+
34
+ export type MobileSecondaryMenuComponent<Props> = ComponentType<
35
+ Props & ExtraProps
36
+ >;
37
+
38
+ type State = {
39
+ component: MobileSecondaryMenuComponent<unknown>;
40
+ props: unknown;
41
+ } | null;
42
+
43
+ function useContextValue() {
44
+ return useState<State>(null);
45
+ }
46
+
47
+ type ContextValue = ReturnType<typeof useContextValue>;
48
+
49
+ const Context = createContext<ContextValue | null>(null);
50
+
51
+ export function MobileSecondaryMenuProvider({
52
+ children,
53
+ }: {
54
+ children: ReactNode;
55
+ }): JSX.Element {
56
+ return (
57
+ <Context.Provider value={useContextValue()}>{children}</Context.Provider>
58
+ );
59
+ }
60
+
61
+ function useMobileSecondaryMenuContext(): ContextValue {
62
+ const value = useContext(Context);
63
+ if (value === null) {
64
+ throw new ReactContextError('MobileSecondaryMenuProvider');
65
+ }
66
+ return value;
67
+ }
68
+
69
+ export function useMobileSecondaryMenuRenderer(): (
70
+ extraProps: ExtraProps,
71
+ ) => ReactNode | undefined {
72
+ const [state] = useMobileSecondaryMenuContext();
73
+ if (state) {
74
+ const Comp = state.component;
75
+ return function render(extraProps) {
76
+ return <Comp {...state.props} {...extraProps} />;
77
+ };
78
+ }
79
+ return () => undefined;
80
+ }
81
+
82
+ function useShallowMemoizedObject<O extends Record<string, unknown>>(obj: O) {
83
+ return useMemo(
84
+ () => obj,
85
+ // Is this safe?
86
+ // eslint-disable-next-line react-hooks/exhaustive-deps
87
+ [...Object.keys(obj), ...Object.values(obj)],
88
+ );
89
+ }
90
+
91
+ // Fill the secondary menu placeholder with some real content
92
+ export function MobileSecondaryMenuFiller<
93
+ Props extends Record<string, unknown>,
94
+ >({
95
+ component,
96
+ props,
97
+ }: {
98
+ component: MobileSecondaryMenuComponent<Props & ExtraProps>;
99
+ props: Props;
100
+ }): JSX.Element | null {
101
+ const [, setState] = useMobileSecondaryMenuContext();
102
+
103
+ // To avoid useless context re-renders, props are memoized shallowly
104
+ const memoizedProps = useShallowMemoizedObject(props);
105
+
106
+ useEffect(() => {
107
+ // @ts-expect-error: context is not 100% type-safe but it's ok
108
+ setState({component, props: memoizedProps});
109
+ }, [setState, component, memoizedProps]);
110
+
111
+ useEffect(() => () => setState(null), [setState]);
112
+
113
+ return null;
114
+ }
@@ -5,13 +5,15 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- // Compare the 2 paths, ignoring trailing /
8
+ // Compare the 2 paths, case insensitive and ignoring trailing slash
9
9
  export const isSamePath = (
10
10
  path1: string | undefined,
11
11
  path2: string | undefined,
12
12
  ): boolean => {
13
- const normalize = (pathname: string | undefined) => {
14
- return !pathname || pathname?.endsWith('/') ? pathname : `${pathname}/`;
15
- };
13
+ const normalize = (pathname: string | undefined) =>
14
+ (!pathname || pathname?.endsWith('/')
15
+ ? pathname
16
+ : `${pathname}/`
17
+ )?.toLowerCase();
16
18
  return normalize(path1) === normalize(path2);
17
19
  };
@@ -0,0 +1,53 @@
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
+ /**
11
+ * This hook is like useLayoutEffect, but without the SSR warning
12
+ * It seems hacky but it's used in many React libs (Redux, Formik...)
13
+ * Also mentioned here: https://github.com/facebook/react/issues/16956
14
+ * It is useful when you need to update a ref as soon as possible after a React
15
+ * render (before `useEffect`)
16
+ */
17
+ export const useIsomorphicLayoutEffect =
18
+ typeof window !== 'undefined' ? useLayoutEffect : useEffect;
19
+
20
+ /**
21
+ * Permits to transform an unstable callback (like an arrow function provided as
22
+ * props) to a "stable" callback that is safe to use in a useEffect dependency
23
+ * array. Useful to avoid React stale closure problems + avoid useless effect
24
+ * re-executions
25
+ *
26
+ * Workaround until the React team recommends a good solution, see
27
+ * https://github.com/facebook/react/issues/16956
28
+ * This generally works but has some potential drawbacks, such as
29
+ * https://github.com/facebook/react/issues/16956#issuecomment-536636418
30
+ */
31
+ export function useDynamicCallback<T extends (...args: never[]) => unknown>(
32
+ callback: T,
33
+ ): T {
34
+ const ref = useRef<T>(callback);
35
+
36
+ useIsomorphicLayoutEffect(() => {
37
+ ref.current = callback;
38
+ }, [callback]);
39
+
40
+ // @ts-expect-error: TS is right that this callback may be a supertype of T,
41
+ // but good enough for our use
42
+ return useCallback<T>((...args) => ref.current(...args), []);
43
+ }
44
+
45
+ export class ReactContextError extends Error {
46
+ constructor(providerName: string, additionalInfo?: string) {
47
+ super();
48
+ this.name = 'ReactContextError';
49
+ this.message = `Hook ${
50
+ this.stack?.split('\n')[1]?.match(/at (?<name>\w+)/)?.groups!.name
51
+ } is called outside the <${providerName}>. ${additionalInfo || ''}`;
52
+ }
53
+ }
@@ -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
+ * Converts an optional string into a Regex case insensitive 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
+ }
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import GeneratedRoutes, {type Route} from '@generated/routes';
9
+ import {useMemo} from 'react';
10
+ import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
11
+
12
+ // Note that all sites don't always have a homepage in practice
13
+ // See https://github.com/facebook/docusaurus/pull/6517#issuecomment-1048709116
14
+ export function findHomePageRoute({
15
+ baseUrl,
16
+ routes: initialRoutes,
17
+ }: {
18
+ routes: Route[];
19
+ baseUrl: string;
20
+ }): Route | undefined {
21
+ function isHomePageRoute(route: Route): boolean {
22
+ return route.path === baseUrl && route.exact === true;
23
+ }
24
+
25
+ function isHomeParentRoute(route: Route): boolean {
26
+ return route.path === baseUrl && !route.exact;
27
+ }
28
+
29
+ function doFindHomePageRoute(routes: Route[]): Route | undefined {
30
+ if (routes.length === 0) {
31
+ return undefined;
32
+ }
33
+ const homePage = routes.find(isHomePageRoute);
34
+ if (homePage) {
35
+ return homePage;
36
+ }
37
+ const indexSubRoutes = routes
38
+ .filter(isHomeParentRoute)
39
+ .flatMap((route) => route.routes ?? []);
40
+ return doFindHomePageRoute(indexSubRoutes);
41
+ }
42
+
43
+ return doFindHomePageRoute(initialRoutes);
44
+ }
45
+
46
+ export function useHomePageRoute(): Route | undefined {
47
+ const {baseUrl} = useDocusaurusContext().siteConfig;
48
+ return useMemo(
49
+ () =>
50
+ findHomePageRoute({
51
+ routes: GeneratedRoutes,
52
+ baseUrl,
53
+ }),
54
+ [baseUrl],
55
+ );
56
+ }