@docusaurus/theme-common 2.0.0-beta.16 → 2.0.0-beta.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (279) hide show
  1. package/Details.d.ts +14 -0
  2. package/lib/components/Collapsible/index.d.ts +32 -4
  3. package/lib/components/Collapsible/index.d.ts.map +1 -1
  4. package/lib/components/Collapsible/index.js +18 -10
  5. package/lib/components/Collapsible/index.js.map +1 -1
  6. package/lib/components/Details/index.d.ts +6 -1
  7. package/lib/components/Details/index.d.ts.map +1 -1
  8. package/lib/components/Details/index.js +11 -6
  9. package/lib/components/Details/index.js.map +1 -1
  10. package/lib/components/Details/styles.module.css +4 -0
  11. package/lib/{utils/announcementBarUtils.d.ts → contexts/announcementBar.d.ts} +7 -3
  12. package/lib/contexts/announcementBar.d.ts.map +1 -0
  13. package/lib/{utils/announcementBarUtils.js → contexts/announcementBar.js} +15 -14
  14. package/lib/contexts/announcementBar.js.map +1 -0
  15. package/lib/contexts/colorMode.d.ts +27 -0
  16. package/lib/contexts/colorMode.d.ts.map +1 -0
  17. package/lib/contexts/colorMode.js +132 -0
  18. package/lib/contexts/colorMode.js.map +1 -0
  19. package/lib/contexts/docSidebarItemsExpandedState.d.ts +31 -0
  20. package/lib/contexts/docSidebarItemsExpandedState.d.ts.map +1 -0
  21. package/lib/{utils → contexts}/docSidebarItemsExpandedState.js +10 -4
  22. package/lib/contexts/docSidebarItemsExpandedState.js.map +1 -0
  23. package/lib/contexts/docsPreferredVersion.d.ts +31 -0
  24. package/lib/contexts/docsPreferredVersion.d.ts.map +1 -0
  25. package/lib/contexts/docsPreferredVersion.js +128 -0
  26. package/lib/contexts/docsPreferredVersion.js.map +1 -0
  27. package/lib/contexts/docsSidebar.d.ts +26 -0
  28. package/lib/contexts/docsSidebar.d.ts.map +1 -0
  29. package/lib/contexts/docsSidebar.js +30 -0
  30. package/lib/contexts/docsSidebar.js.map +1 -0
  31. package/lib/contexts/docsVersion.d.ts +20 -0
  32. package/lib/contexts/docsVersion.d.ts.map +1 -0
  33. package/lib/contexts/docsVersion.js +26 -0
  34. package/lib/contexts/docsVersion.js.map +1 -0
  35. package/lib/contexts/navbarMobileSidebar.d.ts +31 -0
  36. package/lib/contexts/navbarMobileSidebar.d.ts.map +1 -0
  37. package/lib/contexts/navbarMobileSidebar.js +56 -0
  38. package/lib/contexts/navbarMobileSidebar.js.map +1 -0
  39. package/lib/contexts/navbarSecondaryMenu/content.d.ts +37 -0
  40. package/lib/contexts/navbarSecondaryMenu/content.d.ts.map +1 -0
  41. package/lib/contexts/navbarSecondaryMenu/content.js +56 -0
  42. package/lib/contexts/navbarSecondaryMenu/content.js.map +1 -0
  43. package/lib/contexts/navbarSecondaryMenu/display.d.ts +24 -0
  44. package/lib/contexts/navbarSecondaryMenu/display.d.ts.map +1 -0
  45. package/lib/contexts/navbarSecondaryMenu/display.js +62 -0
  46. package/lib/contexts/navbarSecondaryMenu/display.js.map +1 -0
  47. package/lib/{utils/tabGroupChoiceUtils.d.ts → contexts/tabGroupChoice.d.ts} +5 -3
  48. package/lib/contexts/tabGroupChoice.d.ts.map +1 -0
  49. package/lib/{utils/tabGroupChoiceUtils.js → contexts/tabGroupChoice.js} +15 -20
  50. package/lib/contexts/tabGroupChoice.js.map +1 -0
  51. package/lib/hooks/useBackToTopButton.d.ts +27 -0
  52. package/lib/hooks/useBackToTopButton.d.ts.map +1 -0
  53. package/lib/hooks/useBackToTopButton.js +50 -0
  54. package/lib/hooks/useBackToTopButton.js.map +1 -0
  55. package/lib/hooks/useCodeWordWrap.d.ts +14 -0
  56. package/lib/hooks/useCodeWordWrap.d.ts.map +1 -0
  57. package/lib/hooks/useCodeWordWrap.js +41 -0
  58. package/lib/hooks/useCodeWordWrap.js.map +1 -0
  59. package/lib/hooks/useHideableNavbar.d.ts +7 -3
  60. package/lib/hooks/useHideableNavbar.d.ts.map +1 -1
  61. package/lib/hooks/useHideableNavbar.js +10 -9
  62. package/lib/hooks/useHideableNavbar.js.map +1 -1
  63. package/lib/hooks/useKeyboardNavigation.d.ts +11 -1
  64. package/lib/hooks/useKeyboardNavigation.d.ts.map +1 -1
  65. package/lib/hooks/useKeyboardNavigation.js +11 -3
  66. package/lib/hooks/useKeyboardNavigation.js.map +1 -1
  67. package/lib/hooks/useLockBodyScroll.d.ts +5 -1
  68. package/lib/hooks/useLockBodyScroll.d.ts.map +1 -1
  69. package/lib/hooks/useLockBodyScroll.js +5 -1
  70. package/lib/hooks/useLockBodyScroll.js.map +1 -1
  71. package/lib/hooks/usePrismTheme.d.ts +6 -2
  72. package/lib/hooks/usePrismTheme.d.ts.map +1 -1
  73. package/lib/hooks/usePrismTheme.js +9 -6
  74. package/lib/hooks/usePrismTheme.js.map +1 -1
  75. package/lib/hooks/useSearchPage.d.ts +15 -4
  76. package/lib/hooks/useSearchPage.d.ts.map +1 -1
  77. package/lib/hooks/useSearchPage.js +4 -4
  78. package/lib/hooks/useSearchPage.js.map +1 -1
  79. package/lib/hooks/useSkipToContent.d.ts +25 -0
  80. package/lib/hooks/useSkipToContent.d.ts.map +1 -0
  81. package/lib/hooks/useSkipToContent.js +35 -0
  82. package/lib/hooks/useSkipToContent.js.map +1 -0
  83. package/lib/hooks/useTOCHighlight.d.ts +25 -0
  84. package/lib/hooks/useTOCHighlight.d.ts.map +1 -0
  85. package/lib/{utils → hooks}/useTOCHighlight.js +18 -16
  86. package/lib/hooks/useTOCHighlight.js.map +1 -0
  87. package/lib/hooks/useWindowSize.d.ts +14 -1
  88. package/lib/hooks/useWindowSize.d.ts.map +1 -1
  89. package/lib/hooks/useWindowSize.js +14 -11
  90. package/lib/hooks/useWindowSize.js.map +1 -1
  91. package/lib/index.d.ts +31 -32
  92. package/lib/index.d.ts.map +1 -1
  93. package/lib/index.js +31 -26
  94. package/lib/index.js.map +1 -1
  95. package/lib/utils/ThemeClassNames.d.ts +7 -0
  96. package/lib/utils/ThemeClassNames.d.ts.map +1 -1
  97. package/lib/utils/ThemeClassNames.js +7 -4
  98. package/lib/utils/ThemeClassNames.js.map +1 -1
  99. package/lib/utils/codeBlockUtils.d.ts +57 -4
  100. package/lib/utils/codeBlockUtils.d.ts.map +1 -1
  101. package/lib/utils/codeBlockUtils.js +111 -80
  102. package/lib/utils/codeBlockUtils.js.map +1 -1
  103. package/lib/utils/docsUtils.d.ts +83 -18
  104. package/lib/utils/docsUtils.d.ts.map +1 -1
  105. package/lib/utils/docsUtils.js +143 -62
  106. package/lib/utils/docsUtils.js.map +1 -1
  107. package/lib/utils/footerUtils.d.ts +13 -0
  108. package/lib/utils/footerUtils.d.ts.map +1 -0
  109. package/lib/utils/footerUtils.js +14 -0
  110. package/lib/utils/footerUtils.js.map +1 -0
  111. package/lib/utils/generalUtils.d.ts +4 -1
  112. package/lib/utils/generalUtils.d.ts.map +1 -1
  113. package/lib/utils/generalUtils.js +6 -3
  114. package/lib/utils/generalUtils.js.map +1 -1
  115. package/lib/utils/historyUtils.d.ts +1 -7
  116. package/lib/utils/historyUtils.d.ts.map +1 -1
  117. package/lib/utils/historyUtils.js +10 -13
  118. package/lib/utils/historyUtils.js.map +1 -1
  119. package/lib/utils/jsUtils.d.ts +1 -1
  120. package/lib/utils/jsUtils.js +1 -1
  121. package/lib/utils/metadataUtils.d.ts +38 -0
  122. package/lib/utils/metadataUtils.d.ts.map +1 -0
  123. package/lib/utils/metadataUtils.js +61 -0
  124. package/lib/utils/metadataUtils.js.map +1 -0
  125. package/lib/utils/navbarUtils.d.ts +21 -0
  126. package/lib/utils/navbarUtils.d.ts.map +1 -0
  127. package/lib/utils/navbarUtils.js +32 -0
  128. package/lib/utils/navbarUtils.js.map +1 -0
  129. package/lib/utils/reactUtils.d.ts +21 -7
  130. package/lib/utils/reactUtils.d.ts.map +1 -1
  131. package/lib/utils/reactUtils.js +34 -6
  132. package/lib/utils/reactUtils.js.map +1 -1
  133. package/lib/utils/regexpUtils.d.ts +2 -1
  134. package/lib/utils/regexpUtils.d.ts.map +1 -1
  135. package/lib/utils/regexpUtils.js +2 -1
  136. package/lib/utils/regexpUtils.js.map +1 -1
  137. package/lib/utils/routesUtils.d.ts +19 -4
  138. package/lib/utils/routesUtils.d.ts.map +1 -1
  139. package/lib/utils/routesUtils.js +40 -19
  140. package/lib/utils/routesUtils.js.map +1 -1
  141. package/lib/utils/scrollUtils.d.ts +55 -25
  142. package/lib/utils/scrollUtils.d.ts.map +1 -1
  143. package/lib/utils/scrollUtils.js +85 -21
  144. package/lib/utils/scrollUtils.js.map +1 -1
  145. package/lib/utils/searchUtils.d.ts +12 -0
  146. package/lib/utils/searchUtils.d.ts.map +1 -1
  147. package/lib/utils/searchUtils.js +36 -0
  148. package/lib/utils/searchUtils.js.map +1 -1
  149. package/lib/utils/storageUtils.d.ts +12 -9
  150. package/lib/utils/storageUtils.d.ts.map +1 -1
  151. package/lib/utils/storageUtils.js +21 -13
  152. package/lib/utils/storageUtils.js.map +1 -1
  153. package/lib/utils/tagsUtils.d.ts +7 -8
  154. package/lib/utils/tagsUtils.d.ts.map +1 -1
  155. package/lib/utils/tagsUtils.js +8 -6
  156. package/lib/utils/tagsUtils.js.map +1 -1
  157. package/lib/utils/tocUtils.d.ts +17 -1
  158. package/lib/utils/tocUtils.d.ts.map +1 -1
  159. package/lib/utils/tocUtils.js +20 -9
  160. package/lib/utils/tocUtils.js.map +1 -1
  161. package/lib/utils/useAlternatePageUtils.d.ts +20 -1
  162. package/lib/utils/useAlternatePageUtils.d.ts.map +1 -1
  163. package/lib/utils/useAlternatePageUtils.js +6 -3
  164. package/lib/utils/useAlternatePageUtils.js.map +1 -1
  165. package/lib/utils/useLocalPathname.d.ts +5 -0
  166. package/lib/utils/useLocalPathname.d.ts.map +1 -1
  167. package/lib/utils/useLocalPathname.js +6 -4
  168. package/lib/utils/useLocalPathname.js.map +1 -1
  169. package/lib/utils/useLocationChange.d.ts +7 -5
  170. package/lib/utils/useLocationChange.d.ts.map +1 -1
  171. package/lib/utils/useLocationChange.js +6 -2
  172. package/lib/utils/useLocationChange.js.map +1 -1
  173. package/lib/utils/usePluralForm.d.ts +11 -0
  174. package/lib/utils/usePluralForm.d.ts.map +1 -1
  175. package/lib/utils/usePluralForm.js +19 -24
  176. package/lib/utils/usePluralForm.js.map +1 -1
  177. package/lib/utils/useThemeConfig.d.ts +28 -22
  178. package/lib/utils/useThemeConfig.d.ts.map +1 -1
  179. package/lib/utils/useThemeConfig.js +3 -0
  180. package/lib/utils/useThemeConfig.js.map +1 -1
  181. package/package.json +18 -11
  182. package/src/components/Collapsible/index.tsx +42 -24
  183. package/src/components/Details/index.tsx +15 -8
  184. package/src/components/Details/styles.module.css +4 -0
  185. package/src/{utils/announcementBarUtils.tsx → contexts/announcementBar.tsx} +20 -22
  186. package/src/contexts/colorMode.tsx +199 -0
  187. package/src/contexts/docSidebarItemsExpandedState.tsx +55 -0
  188. package/src/contexts/docsPreferredVersion.tsx +253 -0
  189. package/src/contexts/docsSidebar.tsx +50 -0
  190. package/src/contexts/docsVersion.tsx +36 -0
  191. package/src/contexts/navbarMobileSidebar.tsx +99 -0
  192. package/src/contexts/navbarSecondaryMenu/content.tsx +110 -0
  193. package/src/contexts/navbarSecondaryMenu/display.tsx +102 -0
  194. package/src/{utils/tabGroupChoiceUtils.tsx → contexts/tabGroupChoice.tsx} +28 -33
  195. package/src/hooks/useBackToTopButton.ts +73 -0
  196. package/src/hooks/useCodeWordWrap.ts +56 -0
  197. package/src/hooks/useHideableNavbar.ts +12 -14
  198. package/src/hooks/useKeyboardNavigation.ts +11 -3
  199. package/src/hooks/useLockBodyScroll.ts +5 -2
  200. package/src/hooks/usePrismTheme.ts +10 -6
  201. package/src/hooks/useSearchPage.ts +18 -5
  202. package/src/hooks/useSkipToContent.ts +58 -0
  203. package/src/{utils → hooks}/useTOCHighlight.ts +24 -15
  204. package/src/hooks/useWindowSize.ts +14 -12
  205. package/src/index.ts +77 -57
  206. package/src/utils/ThemeClassNames.ts +10 -6
  207. package/src/utils/codeBlockUtils.ts +178 -92
  208. package/src/utils/docsUtils.tsx +205 -101
  209. package/src/utils/footerUtils.ts +18 -0
  210. package/src/utils/generalUtils.ts +6 -3
  211. package/src/utils/historyUtils.ts +11 -17
  212. package/src/utils/jsUtils.ts +1 -1
  213. package/src/utils/metadataUtils.tsx +115 -0
  214. package/src/utils/navbarUtils.tsx +45 -0
  215. package/src/utils/reactUtils.tsx +40 -7
  216. package/src/utils/regexpUtils.ts +2 -1
  217. package/src/utils/routesUtils.ts +58 -22
  218. package/src/utils/scrollUtils.tsx +120 -49
  219. package/src/utils/searchUtils.ts +51 -0
  220. package/src/utils/storageUtils.ts +23 -15
  221. package/src/utils/tagsUtils.ts +12 -10
  222. package/src/utils/tocUtils.ts +22 -11
  223. package/src/utils/useAlternatePageUtils.ts +17 -5
  224. package/src/utils/useLocalPathname.ts +6 -4
  225. package/src/utils/useLocationChange.ts +12 -10
  226. package/src/utils/usePluralForm.ts +27 -24
  227. package/src/utils/useThemeConfig.ts +23 -22
  228. package/yarn-error.log +20199 -0
  229. package/lib/utils/announcementBarUtils.d.ts.map +0 -1
  230. package/lib/utils/announcementBarUtils.js.map +0 -1
  231. package/lib/utils/colorModeUtils.d.ts +0 -18
  232. package/lib/utils/colorModeUtils.d.ts.map +0 -1
  233. package/lib/utils/colorModeUtils.js +0 -106
  234. package/lib/utils/colorModeUtils.js.map +0 -1
  235. package/lib/utils/docSidebarItemsExpandedState.d.ts +0 -17
  236. package/lib/utils/docSidebarItemsExpandedState.d.ts.map +0 -1
  237. package/lib/utils/docSidebarItemsExpandedState.js.map +0 -1
  238. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +0 -22
  239. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts.map +0 -1
  240. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +0 -91
  241. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js.map +0 -1
  242. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts +0 -14
  243. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts.map +0 -1
  244. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +0 -19
  245. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js.map +0 -1
  246. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +0 -14
  247. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts.map +0 -1
  248. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +0 -41
  249. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js.map +0 -1
  250. package/lib/utils/mobileSecondaryMenu.d.ts +0 -21
  251. package/lib/utils/mobileSecondaryMenu.d.ts.map +0 -1
  252. package/lib/utils/mobileSecondaryMenu.js +0 -50
  253. package/lib/utils/mobileSecondaryMenu.js.map +0 -1
  254. package/lib/utils/pathUtils.d.ts +0 -8
  255. package/lib/utils/pathUtils.d.ts.map +0 -1
  256. package/lib/utils/pathUtils.js +0 -17
  257. package/lib/utils/pathUtils.js.map +0 -1
  258. package/lib/utils/tabGroupChoiceUtils.d.ts.map +0 -1
  259. package/lib/utils/tabGroupChoiceUtils.js.map +0 -1
  260. package/lib/utils/useContextualSearchFilters.d.ts +0 -12
  261. package/lib/utils/useContextualSearchFilters.d.ts.map +0 -1
  262. package/lib/utils/useContextualSearchFilters.js +0 -37
  263. package/lib/utils/useContextualSearchFilters.js.map +0 -1
  264. package/lib/utils/usePrevious.d.ts +0 -8
  265. package/lib/utils/usePrevious.d.ts.map +0 -1
  266. package/lib/utils/usePrevious.js +0 -16
  267. package/lib/utils/usePrevious.js.map +0 -1
  268. package/lib/utils/useTOCHighlight.d.ts +0 -14
  269. package/lib/utils/useTOCHighlight.d.ts.map +0 -1
  270. package/lib/utils/useTOCHighlight.js.map +0 -1
  271. package/src/utils/colorModeUtils.tsx +0 -156
  272. package/src/utils/docSidebarItemsExpandedState.tsx +0 -41
  273. package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +0 -167
  274. package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +0 -33
  275. package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +0 -70
  276. package/src/utils/mobileSecondaryMenu.tsx +0 -115
  277. package/src/utils/pathUtils.ts +0 -19
  278. package/src/utils/useContextualSearchFilters.ts +0 -53
  279. package/src/utils/usePrevious.ts +0 -19
@@ -6,25 +6,29 @@
6
6
  */
7
7
 
8
8
  import {useCallback, useEffect, useLayoutEffect, useRef} from 'react';
9
+ import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
9
10
 
10
11
  /**
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...)
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...).
13
14
  * Also mentioned here: https://github.com/facebook/react/issues/16956
15
+ *
14
16
  * It is useful when you need to update a ref as soon as possible after a React
15
- * render (before `useEffect`)
17
+ * render (before `useEffect`).
16
18
  */
17
- export const useIsomorphicLayoutEffect =
18
- typeof window !== 'undefined' ? useLayoutEffect : useEffect;
19
+ export const useIsomorphicLayoutEffect = ExecutionEnvironment.canUseDOM
20
+ ? useLayoutEffect
21
+ : useEffect;
19
22
 
20
23
  /**
21
24
  * 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
25
+ * props) to a "stable" callback that is safe to use in a `useEffect` dependency
23
26
  * array. Useful to avoid React stale closure problems + avoid useless effect
24
- * re-executions
27
+ * re-executions.
25
28
  *
26
29
  * Workaround until the React team recommends a good solution, see
27
30
  * https://github.com/facebook/react/issues/16956
31
+ *
28
32
  * This generally works but has some potential drawbacks, such as
29
33
  * https://github.com/facebook/react/issues/16956#issuecomment-536636418
30
34
  */
@@ -41,3 +45,32 @@ export function useDynamicCallback<T extends (...args: never[]) => unknown>(
41
45
  // but good enough for our use
42
46
  return useCallback<T>((...args) => ref.current(...args), []);
43
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
+ }
@@ -6,7 +6,8 @@
6
6
  */
7
7
 
8
8
  /**
9
- * Converts an optional string into a Regex case insensitive and global
9
+ * Matches a string regex (as provided from the config) against a target in a
10
+ * null-safe fashion, case insensitive and global.
10
11
  */
11
12
  export function isRegexpStringMatch(
12
13
  regexAsString?: string,
@@ -5,35 +5,71 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import GeneratedRoutes, {type Route} from '@generated/routes';
9
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';
10
12
 
11
- function isHomePageRoute(route: Route): boolean {
12
- return route.path === '/' && route.exact === true;
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);
13
26
  }
14
27
 
15
- function isHomeParentRoute(route: Route): boolean {
16
- return route.path === '/' && route.exact === false;
17
- }
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
+ }
18
43
 
19
- // Note that all sites don't always have a homepage in practice
20
- // See https://github.com/facebook/docusaurus/pull/6517#issuecomment-1048709116
21
- export function findHomePageRoute(
22
- routes: Route[] = GeneratedRoutes,
23
- ): Route | undefined {
24
- if (routes.length === 0) {
25
- return undefined;
44
+ function isHomeParentRoute(route: RouteConfig): boolean {
45
+ return route.path === baseUrl && !route.exact;
26
46
  }
27
- const homePage = routes.find(isHomePageRoute);
28
- if (homePage) {
29
- return homePage;
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);
30
60
  }
31
- const indexSubRoutes = routes
32
- .filter(isHomeParentRoute)
33
- .flatMap((route) => route.routes ?? []);
34
- return findHomePageRoute(indexSubRoutes);
61
+
62
+ return doFindHomePageRoute(initialRoutes);
35
63
  }
36
64
 
37
- export function useHomePageRoute(): Route | undefined {
38
- return useMemo(() => findHomePageRoute(), []);
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
+ );
39
75
  }
@@ -6,37 +6,24 @@
6
6
  */
7
7
 
8
8
  import React, {
9
- createContext,
10
- type ReactNode,
11
9
  useCallback,
12
10
  useContext,
13
11
  useEffect,
14
12
  useLayoutEffect,
15
13
  useMemo,
16
14
  useRef,
15
+ type ReactNode,
17
16
  } from 'react';
18
- import {useDynamicCallback} from './reactUtils';
17
+ import {useDynamicCallback, ReactContextError} from './reactUtils';
19
18
  import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
19
+ import useIsBrowser from '@docusaurus/useIsBrowser';
20
20
 
21
- /**
22
- * We need a way to update the scroll position while ignoring scroll events
23
- * without affecting Navbar/BackToTop visibility
24
- *
25
- * This API permits to temporarily disable/ignore scroll events
26
- * Motivated by https://github.com/facebook/docusaurus/pull/5618
27
- */
28
21
  type ScrollController = {
29
- /**
30
- * A boolean ref tracking whether scroll events are enabled
31
- */
22
+ /** A boolean ref tracking whether scroll events are enabled. */
32
23
  scrollEventsEnabledRef: React.MutableRefObject<boolean>;
33
- /**
34
- * Enables scroll events in `useScrollPosition`
35
- */
24
+ /** Enable scroll events in `useScrollPosition`. */
36
25
  enableScrollEvents: () => void;
37
- /**
38
- * Disables scroll events in `useScrollPosition`
39
- */
26
+ /** Disable scroll events in `useScrollPosition`. */
40
27
  disableScrollEvents: () => void;
41
28
  };
42
29
 
@@ -57,7 +44,7 @@ function useScrollControllerContextValue(): ScrollController {
57
44
  );
58
45
  }
59
46
 
60
- const ScrollMonitorContext = createContext<ScrollController | undefined>(
47
+ const ScrollMonitorContext = React.createContext<ScrollController | undefined>(
61
48
  undefined,
62
49
  );
63
50
 
@@ -66,23 +53,31 @@ export function ScrollControllerProvider({
66
53
  }: {
67
54
  children: ReactNode;
68
55
  }): JSX.Element {
56
+ const value = useScrollControllerContextValue();
69
57
  return (
70
- <ScrollMonitorContext.Provider value={useScrollControllerContextValue()}>
58
+ <ScrollMonitorContext.Provider value={value}>
71
59
  {children}
72
60
  </ScrollMonitorContext.Provider>
73
61
  );
74
62
  }
75
63
 
64
+ /**
65
+ * We need a way to update the scroll position while ignoring scroll events
66
+ * so as not to toggle Navbar/BackToTop visibility.
67
+ *
68
+ * This API permits to temporarily disable/ignore scroll events. Motivated by
69
+ * https://github.com/facebook/docusaurus/pull/5618
70
+ */
76
71
  export function useScrollController(): ScrollController {
77
72
  const context = useContext(ScrollMonitorContext);
78
73
  if (context == null) {
79
- throw new Error(
80
- '"useScrollController" is used but no context provider was found in the React tree.',
81
- );
74
+ throw new ReactContextError('ScrollControllerProvider');
82
75
  }
83
76
  return context;
84
77
  }
85
78
 
79
+ type ScrollPosition = {scrollX: number; scrollY: number};
80
+
86
81
  const getScrollPosition = (): ScrollPosition | null =>
87
82
  ExecutionEnvironment.canUseDOM
88
83
  ? {
@@ -91,8 +86,14 @@ const getScrollPosition = (): ScrollPosition | null =>
91
86
  }
92
87
  : null;
93
88
 
94
- type ScrollPosition = {scrollX: number; scrollY: number};
95
-
89
+ /**
90
+ * This hook fires an effect when the scroll position changes. The effect will
91
+ * be provided with the before/after scroll positions. Note that the effect may
92
+ * not be always run: if scrolling is disabled through `useScrollController`, it
93
+ * will be a no-op.
94
+ *
95
+ * @see {@link useScrollController}
96
+ */
96
97
  export function useScrollPosition(
97
98
  effect: (
98
99
  position: ScrollPosition,
@@ -127,22 +128,16 @@ export function useScrollPosition(
127
128
  window.addEventListener('scroll', handleScroll, opts);
128
129
 
129
130
  return () => window.removeEventListener('scroll', handleScroll, opts);
130
- }, [
131
- dynamicEffect,
132
- scrollEventsEnabledRef,
133
131
  // eslint-disable-next-line react-hooks/exhaustive-deps
134
- ...deps,
135
- ]);
132
+ }, [dynamicEffect, scrollEventsEnabledRef, ...deps]);
136
133
  }
137
134
 
138
135
  type UseScrollPositionSaver = {
139
- /**
140
- * Measure the top of an element, and store the details
141
- */
136
+ /** Measure the top of an element, and store the details. */
142
137
  save: (elem: HTMLElement) => void;
143
138
  /**
144
139
  * Restore the page position to keep the stored element's position from
145
- * the top of the viewport, and remove the stored details
140
+ * the top of the viewport, and remove the stored details.
146
141
  */
147
142
  restore: () => {restored: boolean};
148
143
  };
@@ -180,21 +175,24 @@ function useScrollPositionSaver(): UseScrollPositionSaver {
180
175
  return useMemo(() => ({save, restore}), [restore, save]);
181
176
  }
182
177
 
183
- type UseScrollPositionBlockerReturn = {
184
- blockElementScrollPositionUntilNextRender: (el: HTMLElement) => void;
185
- };
186
-
187
178
  /**
188
- * This hook permits to "block" the scroll position of a dom element
179
+ * This hook permits to "block" the scroll position of a DOM element.
189
180
  * The idea is that we should be able to update DOM content above this element
190
- * but the screen position of this element should not change
181
+ * but the screen position of this element should not change.
182
+ *
183
+ * Feature motivated by the Tabs groups: clicking on a tab may affect tabs of
184
+ * the same group upper in the tree, yet to avoid a bad UX, the clicked tab must
185
+ * remain under the user mouse.
191
186
  *
192
- * Feature motivated by the Tabs groups:
193
- * clicking on a tab may affect tabs of the same group upper in the tree
194
- * Yet to avoid a bad UX, the clicked tab must remain under the user mouse!
195
- * See GIF here: https://github.com/facebook/docusaurus/pull/5618
187
+ * @see https://github.com/facebook/docusaurus/pull/5618
196
188
  */
197
- export function useScrollPositionBlocker(): UseScrollPositionBlockerReturn {
189
+ export function useScrollPositionBlocker(): {
190
+ /**
191
+ * Takes an element, and keeps its screen position no matter what's getting
192
+ * rendered above it, until the next render.
193
+ */
194
+ blockElementScrollPositionUntilNextRender: (el: HTMLElement) => void;
195
+ } {
198
196
  const scrollController = useScrollController();
199
197
  const scrollPositionSaver = useScrollPositionSaver();
200
198
 
@@ -210,9 +208,9 @@ export function useScrollPositionBlocker(): UseScrollPositionBlockerReturn {
210
208
  const {restored} = scrollPositionSaver.restore();
211
209
  nextLayoutEffectCallbackRef.current = undefined;
212
210
 
213
- // Restoring the former scroll position will trigger a scroll event
214
- // We need to wait for next scroll event to happen
215
- // before enabling again the scrollController events
211
+ // Restoring the former scroll position will trigger a scroll event. We
212
+ // need to wait for next scroll event to happen before enabling the
213
+ // scrollController events again.
216
214
  if (restored) {
217
215
  const handleScrollRestoreEvent = () => {
218
216
  scrollController.enableScrollEvents();
@@ -235,3 +233,76 @@ export function useScrollPositionBlocker(): UseScrollPositionBlockerReturn {
235
233
  blockElementScrollPositionUntilNextRender,
236
234
  };
237
235
  }
236
+
237
+ type CancelScrollTop = () => void;
238
+
239
+ function smoothScrollNative(top: number): CancelScrollTop {
240
+ window.scrollTo({top, behavior: 'smooth'});
241
+ return () => {
242
+ // Nothing to cancel, it's natively cancelled if user tries to scroll down
243
+ };
244
+ }
245
+
246
+ function smoothScrollPolyfill(top: number): CancelScrollTop {
247
+ let raf: number | null = null;
248
+ const isUpScroll = document.documentElement.scrollTop > top;
249
+ function rafRecursion() {
250
+ const currentScroll = document.documentElement.scrollTop;
251
+ if (
252
+ (isUpScroll && currentScroll > top) ||
253
+ (!isUpScroll && currentScroll < top)
254
+ ) {
255
+ raf = requestAnimationFrame(rafRecursion);
256
+ window.scrollTo(0, Math.floor((currentScroll - top) * 0.85) + top);
257
+ }
258
+ }
259
+ rafRecursion();
260
+
261
+ // Break the recursion. Prevents the user from "fighting" against that
262
+ // recursion producing a weird UX
263
+ return () => raf && cancelAnimationFrame(raf);
264
+ }
265
+
266
+ /**
267
+ * A "smart polyfill" of `window.scrollTo({ top, behavior: "smooth" })`.
268
+ * This currently always uses a polyfilled implementation unless
269
+ * `scroll-behavior: smooth` has been set in CSS, because native support
270
+ * detection for scroll behavior seems unreliable.
271
+ *
272
+ * This hook does not do anything by itself: it returns a start and a stop
273
+ * handle. You can execute either handle at any time.
274
+ */
275
+ export function useSmoothScrollTo(): {
276
+ /**
277
+ * Start the scroll.
278
+ *
279
+ * @param top The final scroll top position.
280
+ */
281
+ startScroll: (top: number) => void;
282
+ /**
283
+ * A cancel function, because the non-native smooth scroll-top
284
+ * implementation must be interrupted if user scrolls down. If there's no
285
+ * existing animation or the scroll is using native behavior, this is a no-op.
286
+ */
287
+ cancelScroll: CancelScrollTop;
288
+ } {
289
+ const cancelRef = useRef<CancelScrollTop | null>(null);
290
+ const isBrowser = useIsBrowser();
291
+ // Not all have support for smooth scrolling (particularly Safari mobile iOS)
292
+ // TODO proper detection is currently unreliable!
293
+ // see https://github.com/wessberg/scroll-behavior-polyfill/issues/16
294
+ // For now, we only use native scroll behavior if smooth is already set,
295
+ // because otherwise the polyfill produces a weird UX when both CSS and JS try
296
+ // to scroll a page, and they cancel each other.
297
+ const supportsNativeSmoothScrolling =
298
+ isBrowser &&
299
+ getComputedStyle(document.documentElement).scrollBehavior === 'smooth';
300
+ return {
301
+ startScroll: (top: number) => {
302
+ cancelRef.current = supportsNativeSmoothScrolling
303
+ ? smoothScrollNative(top)
304
+ : smoothScrollPolyfill(top);
305
+ },
306
+ cancelScroll: () => cancelRef.current?.(),
307
+ };
308
+ }
@@ -5,11 +5,62 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
+ import {
9
+ useAllDocsData,
10
+ useActivePluginAndVersion,
11
+ } from '@docusaurus/plugin-content-docs/client';
12
+ import {useDocsPreferredVersionByPluginId} from '../contexts/docsPreferredVersion';
13
+ import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
14
+
8
15
  export const DEFAULT_SEARCH_TAG = 'default';
9
16
 
17
+ /** The search tag to append as each doc's metadata. */
10
18
  export function docVersionSearchTag(
11
19
  pluginId: string,
12
20
  versionName: string,
13
21
  ): string {
14
22
  return `docs-${pluginId}-${versionName}`;
15
23
  }
24
+
25
+ /**
26
+ * Gets the relevant context information for contextual search.
27
+ *
28
+ * The value is generic and not coupled to Algolia/DocSearch, since we may want
29
+ * to support multiple search engines, or allowing users to use their own search
30
+ * engine solution.
31
+ */
32
+ export function useContextualSearchFilters(): {locale: string; tags: string[]} {
33
+ const {i18n} = useDocusaurusContext();
34
+ const allDocsData = useAllDocsData();
35
+ const activePluginAndVersion = useActivePluginAndVersion();
36
+ const docsPreferredVersionByPluginId = useDocsPreferredVersionByPluginId();
37
+
38
+ // This can't use more specialized hooks because we are mapping over all
39
+ // plugin instances.
40
+ function getDocPluginTags(pluginId: string) {
41
+ const activeVersion =
42
+ activePluginAndVersion?.activePlugin?.pluginId === pluginId
43
+ ? activePluginAndVersion.activeVersion
44
+ : undefined;
45
+
46
+ const preferredVersion = docsPreferredVersionByPluginId[pluginId];
47
+
48
+ const latestVersion = allDocsData[pluginId]!.versions.find(
49
+ (v) => v.isLast,
50
+ )!;
51
+
52
+ const version = activeVersion ?? preferredVersion ?? latestVersion;
53
+
54
+ return docVersionSearchTag(pluginId, version.name);
55
+ }
56
+
57
+ const tags = [
58
+ DEFAULT_SEARCH_TAG,
59
+ ...Object.keys(allDocsData).map(getDocPluginTags),
60
+ ];
61
+
62
+ return {
63
+ locale: i18n.currentLocale,
64
+ tags,
65
+ };
66
+ }
@@ -11,8 +11,12 @@ export type StorageType = typeof StorageTypes[number];
11
11
 
12
12
  const DefaultStorageType: StorageType = 'localStorage';
13
13
 
14
- // Will return null browser storage is unavailable (like running Docusaurus in
15
- // iframe) See https://github.com/facebook/docusaurus/pull/4501
14
+ /**
15
+ * Will return `null` if browser storage is unavailable (like running Docusaurus
16
+ * in an iframe). This should NOT be called in SSR.
17
+ *
18
+ * @see https://github.com/facebook/docusaurus/pull/4501
19
+ */
16
20
  function getBrowserStorage(
17
21
  storageType: StorageType = DefaultStorageType,
18
22
  ): Storage | null {
@@ -32,11 +36,12 @@ function getBrowserStorage(
32
36
  }
33
37
  }
34
38
 
39
+ let hasLoggedBrowserStorageNotAvailableWarning = false;
35
40
  /**
36
- * Poor man's memoization to avoid logging multiple times the same warning
37
- * Sometimes, localStorage/sessionStorage is unavailable due to browser policies
41
+ * Poor man's memoization to avoid logging multiple times the same warning.
42
+ * Sometimes, `localStorage`/`sessionStorage` is unavailable due to browser
43
+ * policies.
38
44
  */
39
- let hasLoggedBrowserStorageNotAvailableWarning = false;
40
45
  function logOnceBrowserStorageNotAvailableWarning(error: Error) {
41
46
  if (!hasLoggedBrowserStorageNotAvailableWarning) {
42
47
  console.warn(
@@ -49,11 +54,11 @@ Possible reasons: running Docusaurus in an iframe, in an incognito browser sessi
49
54
  }
50
55
 
51
56
  // Convenient storage interface for a single storage key
52
- export interface StorageSlot {
57
+ export type StorageSlot = {
53
58
  get: () => string | null;
54
59
  set: (value: string) => void;
55
60
  del: () => void;
56
- }
61
+ };
57
62
 
58
63
  const NoopStorageSlot: StorageSlot = {
59
64
  get: () => null,
@@ -61,7 +66,7 @@ const NoopStorageSlot: StorageSlot = {
61
66
  del: () => {},
62
67
  };
63
68
 
64
- // Fail-fast, as storage APIs should not be used during the SSR process
69
+ // Fail-fast, as storage APIs should not be used during the SSR process
65
70
  function createServerStorageSlot(key: string): StorageSlot {
66
71
  function throwError(): never {
67
72
  throw new Error(`Illegal storage API usage for storage key "${key}".
@@ -77,16 +82,19 @@ Please only call storage APIs in effects and event handlers.`);
77
82
  }
78
83
 
79
84
  /**
80
- * Creates an object for accessing a particular key in localStorage.
81
- * The API is fail-safe, and usage of browser storage should be considered
85
+ * Creates an interface to work on a particular key in the storage model.
86
+ * Note that this function only initializes the interface, but doesn't allocate
87
+ * anything by itself (i.e. no side-effects).
88
+ *
89
+ * The API is fail-safe, since usage of browser storage should be considered
82
90
  * unreliable. Local storage might simply be unavailable (iframe + browser
83
91
  * security) or operations might fail individually. Please assume that using
84
- * this API can be a NO-OP. See also https://github.com/facebook/docusaurus/issues/6036
92
+ * this API can be a no-op. See also https://github.com/facebook/docusaurus/issues/6036
85
93
  */
86
- export const createStorageSlot = (
94
+ export function createStorageSlot(
87
95
  key: string,
88
96
  options?: {persistence?: StorageType},
89
- ): StorageSlot => {
97
+ ): StorageSlot {
90
98
  if (typeof window === 'undefined') {
91
99
  return createServerStorageSlot(key);
92
100
  }
@@ -121,10 +129,10 @@ export const createStorageSlot = (
121
129
  }
122
130
  },
123
131
  };
124
- };
132
+ }
125
133
 
126
134
  /**
127
- * Returns a list of all the keys currently stored in browser storage
135
+ * Returns a list of all the keys currently stored in browser storage,
128
136
  * or an empty list if browser storage can't be accessed.
129
137
  */
130
138
  export function listStorageKeys(
@@ -6,6 +6,7 @@
6
6
  */
7
7
 
8
8
  import {translate} from '@docusaurus/Translate';
9
+ import type {TagsListItem} from '@docusaurus/utils';
9
10
 
10
11
  export const translateTagsPageTitle = (): string =>
11
12
  translate({
@@ -14,23 +15,24 @@ export const translateTagsPageTitle = (): string =>
14
15
  description: 'The title of the tag list page',
15
16
  });
16
17
 
17
- type TagsListItem = Readonly<{name: string; permalink: string; count: number}>; // TODO remove duplicated type :s
18
-
19
- export type TagLetterEntry = Readonly<{letter: string; tags: TagsListItem[]}>;
18
+ export type TagLetterEntry = {letter: string; tags: TagsListItem[]};
20
19
 
21
20
  function getTagLetter(tag: string): string {
22
- return tag[0].toUpperCase();
21
+ return tag[0]!.toUpperCase();
23
22
  }
24
23
 
24
+ /**
25
+ * Takes a list of tags (as provided by the content plugins), and groups them by
26
+ * their initials.
27
+ */
25
28
  export function listTagsByLetters(
26
29
  tags: readonly TagsListItem[],
27
30
  ): TagLetterEntry[] {
28
- // Group by letters
29
- const groups: Record<string, TagsListItem[]> = {};
31
+ const groups: {[initial: string]: TagsListItem[]} = {};
30
32
  Object.values(tags).forEach((tag) => {
31
- const letter = getTagLetter(tag.name);
32
- groups[letter] = groups[letter] ?? [];
33
- groups[letter].push(tag);
33
+ const initial = getTagLetter(tag.label);
34
+ groups[initial] ??= [];
35
+ groups[initial]!.push(tag);
34
36
  });
35
37
 
36
38
  return (
@@ -40,7 +42,7 @@ export function listTagsByLetters(
40
42
  .map(([letter, letterTags]) => {
41
43
  // Sort tags inside a letter
42
44
  const sortedTags = letterTags.sort((tag1, tag2) =>
43
- tag1.name.localeCompare(tag2.name),
45
+ tag1.label.localeCompare(tag2.label),
44
46
  );
45
47
  return {letter, tags: sortedTags};
46
48
  })