@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
@@ -0,0 +1,110 @@
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
+ useContext,
11
+ useEffect,
12
+ useMemo,
13
+ type ReactNode,
14
+ type ComponentType,
15
+ } from 'react';
16
+ import {ReactContextError} from '../../utils/reactUtils';
17
+
18
+ // This context represents a "global layout store". A component (usually a
19
+ // layout component) can request filling this store through
20
+ // `NavbarSecondaryMenuFiller`. It doesn't actually control rendering by itself,
21
+ // and this context should be considered internal implementation. The user-
22
+ // facing value comes from `display.tsx`, which takes the `component` and
23
+ // `props` stored here and renders the actual element.
24
+
25
+ export type NavbarSecondaryMenuComponent<Props> = ComponentType<Props>;
26
+
27
+ /** @internal */
28
+ export type Content =
29
+ | {
30
+ component: NavbarSecondaryMenuComponent<object>;
31
+ props: object;
32
+ }
33
+ | {component: null; props: null};
34
+
35
+ type ContextValue = [
36
+ content: Content,
37
+ setContent: React.Dispatch<React.SetStateAction<Content>>,
38
+ ];
39
+
40
+ const Context = React.createContext<ContextValue | null>(null);
41
+
42
+ /** @internal */
43
+ export function NavbarSecondaryMenuContentProvider({
44
+ children,
45
+ }: {
46
+ children: ReactNode;
47
+ }): JSX.Element {
48
+ const value = useState({component: null, props: null});
49
+ return (
50
+ // @ts-expect-error: this context is hard to type
51
+ <Context.Provider value={value}>{children}</Context.Provider>
52
+ );
53
+ }
54
+
55
+ /** @internal */
56
+ export function useNavbarSecondaryMenuContent(): Content {
57
+ const value = useContext(Context);
58
+ if (!value) {
59
+ throw new ReactContextError('NavbarSecondaryMenuContentProvider');
60
+ }
61
+ return value[0];
62
+ }
63
+
64
+ function useShallowMemoizedObject<O>(obj: O) {
65
+ return useMemo(
66
+ () => obj,
67
+ // Is this safe?
68
+ // eslint-disable-next-line react-hooks/exhaustive-deps
69
+ [...Object.keys(obj), ...Object.values(obj)],
70
+ );
71
+ }
72
+
73
+ /**
74
+ * This component renders nothing by itself, but it fills the placeholder in the
75
+ * generic secondary menu layout. This reduces coupling between the main layout
76
+ * and the specific page.
77
+ *
78
+ * This kind of feature is often called portal/teleport/gateway/outlet...
79
+ * Various unmaintained React libs exist. Most up-to-date one:
80
+ * https://github.com/gregberge/react-teleporter
81
+ * Not sure any of those is safe regarding concurrent mode.
82
+ */
83
+ export function NavbarSecondaryMenuFiller<P extends object>({
84
+ component,
85
+ props,
86
+ }: {
87
+ component: NavbarSecondaryMenuComponent<P>;
88
+ props: P;
89
+ }): JSX.Element | null {
90
+ const context = useContext(Context);
91
+ if (!context) {
92
+ throw new ReactContextError('NavbarSecondaryMenuContentProvider');
93
+ }
94
+ const [, setContent] = context;
95
+
96
+ // To avoid useless context re-renders, props are memoized shallowly
97
+ const memoizedProps = useShallowMemoizedObject(props);
98
+
99
+ useEffect(() => {
100
+ // @ts-expect-error: this context is hard to type
101
+ setContent({component, props: memoizedProps});
102
+ }, [setContent, component, memoizedProps]);
103
+
104
+ useEffect(
105
+ () => () => setContent({component: null, props: null}),
106
+ [setContent],
107
+ );
108
+
109
+ return null;
110
+ }
@@ -0,0 +1,102 @@
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
+ useContext,
11
+ useEffect,
12
+ useMemo,
13
+ useCallback,
14
+ type ReactNode,
15
+ } from 'react';
16
+ import {ReactContextError, usePrevious} from '../../utils/reactUtils';
17
+ import {useNavbarMobileSidebar} from '../navbarMobileSidebar';
18
+ import {useNavbarSecondaryMenuContent, type Content} from './content';
19
+
20
+ type ContextValue = [
21
+ shown: boolean,
22
+ setShown: React.Dispatch<React.SetStateAction<boolean>>,
23
+ ];
24
+
25
+ const Context = React.createContext<ContextValue | null>(null);
26
+
27
+ function useContextValue(): ContextValue {
28
+ const mobileSidebar = useNavbarMobileSidebar();
29
+ const content = useNavbarSecondaryMenuContent();
30
+
31
+ const [shown, setShown] = useState(false);
32
+
33
+ const hasContent = content.component !== null;
34
+ const previousHasContent = usePrevious(hasContent);
35
+
36
+ // When content is become available for the first time (set in useEffect)
37
+ // we set this content to be shown!
38
+ useEffect(() => {
39
+ const contentBecameAvailable = hasContent && !previousHasContent;
40
+ if (contentBecameAvailable) {
41
+ setShown(true);
42
+ }
43
+ }, [hasContent, previousHasContent]);
44
+
45
+ // On sidebar close, secondary menu is set to be shown on next re-opening
46
+ // (if any secondary menu content available)
47
+ useEffect(() => {
48
+ if (!hasContent) {
49
+ setShown(false);
50
+ return;
51
+ }
52
+ if (!mobileSidebar.shown) {
53
+ setShown(true);
54
+ }
55
+ }, [mobileSidebar.shown, hasContent]);
56
+
57
+ return useMemo(() => [shown, setShown], [shown]);
58
+ }
59
+
60
+ /** @internal */
61
+ export function NavbarSecondaryMenuDisplayProvider({
62
+ children,
63
+ }: {
64
+ children: ReactNode;
65
+ }): JSX.Element {
66
+ const value = useContextValue();
67
+ return <Context.Provider value={value}>{children}</Context.Provider>;
68
+ }
69
+
70
+ function renderElement(content: Content): JSX.Element | undefined {
71
+ if (content.component) {
72
+ const Comp = content.component;
73
+ return <Comp {...content.props} />;
74
+ }
75
+ return undefined;
76
+ }
77
+
78
+ /** Wires the logic for rendering the mobile navbar secondary menu. */
79
+ export function useNavbarSecondaryMenu(): {
80
+ /** Whether secondary menu is displayed. */
81
+ shown: boolean;
82
+ /**
83
+ * Hide the secondary menu; fired either when hiding the entire sidebar, or
84
+ * when going back to the primary menu.
85
+ */
86
+ hide: () => void;
87
+ /** The content returned from the current secondary menu filler. */
88
+ content: JSX.Element | undefined;
89
+ } {
90
+ const value = useContext(Context);
91
+ if (!value) {
92
+ throw new ReactContextError('NavbarSecondaryMenuDisplayProvider');
93
+ }
94
+ const [shown, setShown] = value;
95
+ const hide = useCallback(() => setShown(false), [setShown]);
96
+ const content = useNavbarSecondaryMenuContent();
97
+
98
+ return useMemo(
99
+ () => ({shown, hide, content: renderElement(content)}),
100
+ [hide, content, shown],
101
+ );
102
+ }
@@ -9,35 +9,38 @@ import React, {
9
9
  useState,
10
10
  useCallback,
11
11
  useEffect,
12
- createContext,
13
12
  useMemo,
14
13
  useContext,
15
14
  type ReactNode,
16
15
  } from 'react';
17
- import {createStorageSlot, listStorageKeys} from './storageUtils';
16
+ import {createStorageSlot, listStorageKeys} from '../utils/storageUtils';
17
+ import {ReactContextError} from '../utils/reactUtils';
18
18
 
19
19
  const TAB_CHOICE_PREFIX = 'docusaurus.tab.';
20
20
 
21
- type TabGroupChoiceContextValue = {
21
+ type ContextValue = {
22
+ /** A map from `groupId` to the `value` of the saved choice. */
22
23
  readonly tabGroupChoices: {readonly [groupId: string]: string};
24
+ /** Set the new choice value of a group. */
23
25
  readonly setTabGroupChoices: (groupId: string, newChoice: string) => void;
24
26
  };
25
27
 
26
- const TabGroupChoiceContext = createContext<
27
- TabGroupChoiceContextValue | undefined
28
- >(undefined);
28
+ const Context = React.createContext<ContextValue | undefined>(undefined);
29
29
 
30
- function useTabGroupChoiceContextValue(): TabGroupChoiceContextValue {
30
+ function useContextValue(): ContextValue {
31
31
  const [tabGroupChoices, setChoices] = useState<{
32
32
  readonly [groupId: string]: string;
33
33
  }>({});
34
- const setChoiceSyncWithLocalStorage = useCallback((groupId, newChoice) => {
35
- createStorageSlot(`${TAB_CHOICE_PREFIX}${groupId}`).set(newChoice);
36
- }, []);
34
+ const setChoiceSyncWithLocalStorage = useCallback(
35
+ (groupId: string, newChoice: string) => {
36
+ createStorageSlot(`${TAB_CHOICE_PREFIX}${groupId}`).set(newChoice);
37
+ },
38
+ [],
39
+ );
37
40
 
38
41
  useEffect(() => {
39
42
  try {
40
- const localStorageChoices: Record<string, string> = {};
43
+ const localStorageChoices: {[groupId: string]: string} = {};
41
44
  listStorageKeys().forEach((storageKey) => {
42
45
  if (storageKey.startsWith(TAB_CHOICE_PREFIX)) {
43
46
  const groupId = storageKey.substring(TAB_CHOICE_PREFIX.length);
@@ -50,13 +53,18 @@ function useTabGroupChoiceContextValue(): TabGroupChoiceContextValue {
50
53
  }
51
54
  }, []);
52
55
 
53
- return {
54
- tabGroupChoices,
55
- setTabGroupChoices: (groupId: string, newChoice: string) => {
56
+ const setTabGroupChoices = useCallback(
57
+ (groupId: string, newChoice: string) => {
56
58
  setChoices((oldChoices) => ({...oldChoices, [groupId]: newChoice}));
57
59
  setChoiceSyncWithLocalStorage(groupId, newChoice);
58
60
  },
59
- };
61
+ [setChoiceSyncWithLocalStorage],
62
+ );
63
+
64
+ return useMemo(
65
+ () => ({tabGroupChoices, setTabGroupChoices}),
66
+ [tabGroupChoices, setTabGroupChoices],
67
+ );
60
68
  }
61
69
 
62
70
  export function TabGroupChoiceProvider({
@@ -64,27 +72,14 @@ export function TabGroupChoiceProvider({
64
72
  }: {
65
73
  children: ReactNode;
66
74
  }): JSX.Element {
67
- const {tabGroupChoices, setTabGroupChoices} = useTabGroupChoiceContextValue();
68
- const contextValue = useMemo(
69
- () => ({
70
- tabGroupChoices,
71
- setTabGroupChoices,
72
- }),
73
- [tabGroupChoices, setTabGroupChoices],
74
- );
75
- return (
76
- <TabGroupChoiceContext.Provider value={contextValue}>
77
- {children}
78
- </TabGroupChoiceContext.Provider>
79
- );
75
+ const value = useContextValue();
76
+ return <Context.Provider value={value}>{children}</Context.Provider>;
80
77
  }
81
78
 
82
- export function useTabGroupChoice(): TabGroupChoiceContextValue {
83
- const context = useContext(TabGroupChoiceContext);
79
+ export function useTabGroupChoice(): ContextValue {
80
+ const context = useContext(Context);
84
81
  if (context == null) {
85
- throw new Error(
86
- '"useUserPreferencesContext" is used outside of "Layout" component.',
87
- );
82
+ throw new ReactContextError('TabGroupChoiceProvider');
88
83
  }
89
84
  return context;
90
85
  }
@@ -0,0 +1,73 @@
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 {useRef, useState} from 'react';
9
+ import {useScrollPosition, useSmoothScrollTo} from '../utils/scrollUtils';
10
+ import {useLocationChange} from '../utils/useLocationChange';
11
+
12
+ /** Wires the logic for the back to top button. */
13
+ export function useBackToTopButton({
14
+ threshold,
15
+ }: {
16
+ /**
17
+ * The minimum vertical scroll position, above which a scroll-up would not
18
+ * cause `shown` to become `true`. This is because BTT is only useful if the
19
+ * user is far down the page.
20
+ */
21
+ threshold: number;
22
+ }): {
23
+ /**
24
+ * Whether the button should be displayed. We only show if the user has
25
+ * scrolled up and is on a vertical position greater than `threshold`.
26
+ */
27
+ shown: boolean;
28
+ /**
29
+ * A (memoized) handle for starting the scroll, which you can directly plug
30
+ * into the props.
31
+ */
32
+ scrollToTop: () => void;
33
+ } {
34
+ const [shown, setShown] = useState(false);
35
+ const isFocusedAnchor = useRef(false);
36
+ const {startScroll, cancelScroll} = useSmoothScrollTo();
37
+
38
+ useScrollPosition(({scrollY: scrollTop}, lastPosition) => {
39
+ const lastScrollTop = lastPosition?.scrollY;
40
+ // Component is just being mounted. Not really a scroll event from the user.
41
+ // Ignore it.
42
+ if (!lastScrollTop) {
43
+ return;
44
+ }
45
+ if (isFocusedAnchor.current) {
46
+ // This scroll position change is triggered by navigating to an anchor.
47
+ // Ignore it.
48
+ isFocusedAnchor.current = false;
49
+ } else if (scrollTop >= lastScrollTop) {
50
+ // The user has scrolled down to "fight against" the animation. Cancel any
51
+ // animation under progress.
52
+ cancelScroll();
53
+ setShown(false);
54
+ } else if (scrollTop < threshold) {
55
+ // Scrolled to the minimum position; hide the button.
56
+ setShown(false);
57
+ } else if (
58
+ scrollTop + window.innerHeight <
59
+ document.documentElement.scrollHeight
60
+ ) {
61
+ setShown(true);
62
+ }
63
+ });
64
+
65
+ useLocationChange((locationChangeEvent) => {
66
+ if (locationChangeEvent.location.hash) {
67
+ isFocusedAnchor.current = true;
68
+ setShown(false);
69
+ }
70
+ });
71
+
72
+ return {shown, scrollToTop: () => startScroll(0)};
73
+ }
@@ -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 type {RefObject} from 'react';
9
+ import {useState, useCallback, useEffect, useRef} from 'react';
10
+
11
+ export function useCodeWordWrap(): {
12
+ readonly codeBlockRef: RefObject<HTMLPreElement>;
13
+ readonly isEnabled: boolean;
14
+ readonly isCodeScrollable: boolean;
15
+ readonly toggle: () => void;
16
+ } {
17
+ const [isEnabled, setIsEnabled] = useState(false);
18
+ const [isCodeScrollable, setIsCodeScrollable] = useState<boolean>(false);
19
+ const codeBlockRef = useRef<HTMLPreElement>(null);
20
+
21
+ const toggle = useCallback(() => {
22
+ const codeElement = codeBlockRef.current!.querySelector('code')!;
23
+
24
+ if (isEnabled) {
25
+ codeElement.removeAttribute('style');
26
+ } else {
27
+ codeElement.style.whiteSpace = 'pre-wrap';
28
+ }
29
+
30
+ setIsEnabled((value) => !value);
31
+ }, [codeBlockRef, isEnabled]);
32
+
33
+ const updateCodeIsScrollable = useCallback(() => {
34
+ const {scrollWidth, clientWidth} = codeBlockRef.current!;
35
+ const isScrollable =
36
+ scrollWidth > clientWidth ||
37
+ codeBlockRef.current!.querySelector('code')!.hasAttribute('style');
38
+ setIsCodeScrollable(isScrollable);
39
+ }, [codeBlockRef]);
40
+
41
+ useEffect(() => {
42
+ updateCodeIsScrollable();
43
+ }, [isEnabled, updateCodeIsScrollable]);
44
+
45
+ useEffect(() => {
46
+ window.addEventListener('resize', updateCodeIsScrollable, {
47
+ passive: true,
48
+ });
49
+
50
+ return () => {
51
+ window.removeEventListener('resize', updateCodeIsScrollable);
52
+ };
53
+ }, [updateCodeIsScrollable]);
54
+
55
+ return {codeBlockRef, isEnabled, isCodeScrollable, toggle};
56
+ }
@@ -9,14 +9,16 @@ import {useState, useCallback, useRef} from 'react';
9
9
  import {useLocationChange} from '../utils/useLocationChange';
10
10
  import {useScrollPosition} from '../utils/scrollUtils';
11
11
 
12
- type UseHideableNavbarReturns = {
12
+ /**
13
+ * Wires the imperative logic of a hideable navbar.
14
+ * @param hideOnScroll If `false`, this hook is basically a no-op.
15
+ */
16
+ export function useHideableNavbar(hideOnScroll: boolean): {
17
+ /** A ref to the navbar component. Plug this into the actual element. */
13
18
  readonly navbarRef: (node: HTMLElement | null) => void;
19
+ /** If `false`, the navbar component should not be rendered. */
14
20
  readonly isNavbarVisible: boolean;
15
- };
16
-
17
- export default function useHideableNavbar(
18
- hideOnScroll: boolean,
19
- ): UseHideableNavbarReturns {
21
+ } {
20
22
  const [isNavbarVisible, setIsNavbarVisible] = useState(hideOnScroll);
21
23
  const isFocusedAnchor = useRef(false);
22
24
  const navbarHeight = useRef(0);
@@ -26,14 +28,13 @@ export default function useHideableNavbar(
26
28
  }
27
29
  }, []);
28
30
 
29
- useScrollPosition((currentPosition, lastPosition) => {
31
+ useScrollPosition(({scrollY: scrollTop}, lastPosition) => {
30
32
  if (!hideOnScroll) {
31
33
  return;
32
34
  }
33
35
 
34
- const scrollTop = currentPosition.scrollY;
35
-
36
- // It needed for mostly to handle rubber band scrolling
36
+ // Needed mostly for handling rubber band scrolling.
37
+ // See https://github.com/facebook/docusaurus/pull/5721
37
38
  if (scrollTop < navbarHeight.current) {
38
39
  setIsNavbarVisible(true);
39
40
  return;
@@ -70,8 +71,5 @@ export default function useHideableNavbar(
70
71
  setIsNavbarVisible(true);
71
72
  });
72
73
 
73
- return {
74
- navbarRef,
75
- isNavbarVisible,
76
- };
74
+ return {navbarRef, isNavbarVisible};
77
75
  }
@@ -11,9 +11,17 @@ import './styles.css';
11
11
 
12
12
  export const keyboardFocusedClassName = 'navigation-with-keyboard';
13
13
 
14
- // This hook detect keyboard focus indicator to not show outline for mouse users
15
- // Inspired by https://hackernoon.com/removing-that-ugly-focus-ring-and-keeping-it-too-6c8727fefcd2
16
- export default function useKeyboardNavigation(): void {
14
+ /**
15
+ * Side-effect that adds the `keyboardFocusedClassName` to the body element when
16
+ * the keyboard has been pressed, or removes it when the mouse is clicked.
17
+ *
18
+ * The presence of this class name signals that the user may be using keyboard
19
+ * for navigation, and the theme **must** add focus outline when this class name
20
+ * is present. (And optionally not if it's absent, for design purposes)
21
+ *
22
+ * Inspired by https://hackernoon.com/removing-that-ugly-focus-ring-and-keeping-it-too-6c8727fefcd2
23
+ */
24
+ export function useKeyboardNavigation(): void {
17
25
  useEffect(() => {
18
26
  function handleOutlineStyles(e: MouseEvent | KeyboardEvent) {
19
27
  if (e.type === 'keydown' && (e as KeyboardEvent).key === 'Tab') {
@@ -7,10 +7,13 @@
7
7
 
8
8
  import {useEffect} from 'react';
9
9
 
10
- export default function useLockBodyScroll(lock: boolean = true): void {
10
+ /**
11
+ * Side-effect that locks the document body's scroll throughout the lifetime of
12
+ * the containing component. e.g. when the mobile sidebar is expanded.
13
+ */
14
+ export function useLockBodyScroll(lock: boolean = true): void {
11
15
  useEffect(() => {
12
16
  document.body.style.overflow = lock ? 'hidden' : 'visible';
13
-
14
17
  return () => {
15
18
  document.body.style.overflow = 'visible';
16
19
  };
@@ -5,16 +5,20 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import defaultTheme from 'prism-react-renderer/themes/palenight';
9
- import {useColorMode} from '../utils/colorModeUtils';
8
+ import type {PrismTheme} from 'prism-react-renderer';
9
+ import {useColorMode} from '../contexts/colorMode';
10
10
  import {useThemeConfig} from '../utils/useThemeConfig';
11
11
 
12
- export default function usePrismTheme(): typeof defaultTheme {
12
+ /**
13
+ * Returns a color-mode-dependent Prism theme: whatever the user specified in
14
+ * the config. Falls back to `palenight`.
15
+ */
16
+ export function usePrismTheme(): PrismTheme {
13
17
  const {prism} = useThemeConfig();
14
- const {isDarkTheme} = useColorMode();
15
- const lightModeTheme = prism.theme || defaultTheme;
18
+ const {colorMode} = useColorMode();
19
+ const lightModeTheme = prism.theme;
16
20
  const darkModeTheme = prism.darkTheme || lightModeTheme;
17
- const prismTheme = isDarkTheme ? darkModeTheme : lightModeTheme;
21
+ const prismTheme = colorMode === 'dark' ? darkModeTheme : lightModeTheme;
18
22
 
19
23
  return prismTheme;
20
24
  }
@@ -11,13 +11,24 @@ import {useCallback, useEffect, useState} from 'react';
11
11
 
12
12
  const SEARCH_PARAM_QUERY = 'q';
13
13
 
14
- interface UseSearchPageReturn {
14
+ /** Some utility functions around search queries. */
15
+ export function useSearchPage(): {
16
+ /**
17
+ * Works hand-in-hand with `setSearchQuery`; whatever the user has inputted
18
+ * into the search box.
19
+ */
15
20
  searchQuery: string;
21
+ /**
22
+ * Set a new search query. In addition to updating `searchQuery`, this handle
23
+ * also mutates the location and appends the query.
24
+ */
16
25
  setSearchQuery: (newSearchQuery: string) => void;
26
+ /**
27
+ * Given a query, this handle generates the corresponding search page link,
28
+ * with base URL prepended.
29
+ */
17
30
  generateSearchPageLink: (targetSearchQuery: string) => string;
18
- }
19
-
20
- export default function useSearchPage(): UseSearchPageReturn {
31
+ } {
21
32
  const history = useHistory();
22
33
  const {
23
34
  siteConfig: {baseUrl},
@@ -54,7 +65,9 @@ export default function useSearchPage(): UseSearchPageReturn {
54
65
  const generateSearchPageLink = useCallback(
55
66
  (targetSearchQuery: string) =>
56
67
  // Refer to https://github.com/facebook/docusaurus/pull/2838
57
- `${baseUrl}search?q=${encodeURIComponent(targetSearchQuery)}`,
68
+ `${baseUrl}search?${SEARCH_PARAM_QUERY}=${encodeURIComponent(
69
+ targetSearchQuery,
70
+ )}`,
58
71
  [baseUrl],
59
72
  );
60
73
 
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import type React from 'react';
9
+ import {useCallback, useRef} from 'react';
10
+ import {useHistory} from '@docusaurus/router';
11
+ import {useLocationChange} from '../utils/useLocationChange';
12
+ import {ThemeClassNames} from '../utils/ThemeClassNames';
13
+
14
+ function programmaticFocus(el: HTMLElement) {
15
+ el.setAttribute('tabindex', '-1');
16
+ el.focus();
17
+ el.removeAttribute('tabindex');
18
+ }
19
+
20
+ /** This hook wires the logic for a skip-to-content link. */
21
+ export function useSkipToContent(): {
22
+ /**
23
+ * The ref to the container. On page transition, the container will be focused
24
+ * so that keyboard navigators can instantly interact with the link and jump
25
+ * to content. **Note:** the type is `RefObject<HTMLDivElement>` only because
26
+ * the typing for refs don't reflect that the `ref` prop is contravariant, so
27
+ * using `HTMLElement` causes type-checking to fail. You can plug the ref into
28
+ * any HTML element, as long as it can be focused.
29
+ */
30
+ containerRef: React.RefObject<HTMLDivElement>;
31
+ /**
32
+ * Callback fired when the skip to content link has been interacted with. It
33
+ * will programmatically focus the main content.
34
+ */
35
+ handleSkip: (e: React.MouseEvent<HTMLAnchorElement>) => void;
36
+ } {
37
+ const containerRef = useRef<HTMLDivElement>(null);
38
+ const {action} = useHistory();
39
+ const handleSkip = useCallback((e: React.MouseEvent<HTMLAnchorElement>) => {
40
+ e.preventDefault();
41
+
42
+ const targetElement: HTMLElement | null =
43
+ document.querySelector('main:first-of-type') ||
44
+ document.querySelector(`.${ThemeClassNames.wrapper.main}`);
45
+
46
+ if (targetElement) {
47
+ programmaticFocus(targetElement);
48
+ }
49
+ }, []);
50
+
51
+ useLocationChange(({location}) => {
52
+ if (containerRef.current && !location.hash && action === 'PUSH') {
53
+ programmaticFocus(containerRef.current);
54
+ }
55
+ });
56
+
57
+ return {containerRef, handleSkip};
58
+ }