@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,199 @@
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
+ useCallback,
11
+ useEffect,
12
+ useContext,
13
+ useMemo,
14
+ useRef,
15
+ type ReactNode,
16
+ } from 'react';
17
+ import {ReactContextError} from '../utils/reactUtils';
18
+
19
+ import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
20
+ import {createStorageSlot} from '../utils/storageUtils';
21
+ import {useThemeConfig} from '../utils/useThemeConfig';
22
+
23
+ type ContextValue = {
24
+ /** Current color mode. */
25
+ readonly colorMode: ColorMode;
26
+ /** Set new color mode. */
27
+ readonly setColorMode: (colorMode: ColorMode) => void;
28
+
29
+ // TODO legacy APIs kept for retro-compatibility: deprecate them
30
+ readonly isDarkTheme: boolean;
31
+ readonly setLightTheme: () => void;
32
+ readonly setDarkTheme: () => void;
33
+ };
34
+
35
+ const Context = React.createContext<ContextValue | undefined>(undefined);
36
+
37
+ const ColorModeStorageKey = 'theme';
38
+ const ColorModeStorage = createStorageSlot(ColorModeStorageKey);
39
+
40
+ const ColorModes = {
41
+ light: 'light',
42
+ dark: 'dark',
43
+ } as const;
44
+
45
+ export type ColorMode = typeof ColorModes[keyof typeof ColorModes];
46
+
47
+ // Ensure to always return a valid colorMode even if input is invalid
48
+ const coerceToColorMode = (colorMode?: string | null): ColorMode =>
49
+ colorMode === ColorModes.dark ? ColorModes.dark : ColorModes.light;
50
+
51
+ const getInitialColorMode = (defaultMode: ColorMode | undefined): ColorMode =>
52
+ ExecutionEnvironment.canUseDOM
53
+ ? coerceToColorMode(document.documentElement.getAttribute('data-theme'))
54
+ : coerceToColorMode(defaultMode);
55
+
56
+ const storeColorMode = (newColorMode: ColorMode) => {
57
+ ColorModeStorage.set(coerceToColorMode(newColorMode));
58
+ };
59
+
60
+ function useContextValue(): ContextValue {
61
+ const {
62
+ colorMode: {defaultMode, disableSwitch, respectPrefersColorScheme},
63
+ } = useThemeConfig();
64
+ const [colorMode, setColorModeState] = useState(
65
+ getInitialColorMode(defaultMode),
66
+ );
67
+
68
+ useEffect(() => {
69
+ // A site is deployed without disableSwitch
70
+ // => User visits the site and has a persisted value
71
+ // => Site later enabled disableSwitch
72
+ // => Clear the previously stored value to apply the site's setting
73
+ if (disableSwitch) {
74
+ ColorModeStorage.del();
75
+ }
76
+ }, [disableSwitch]);
77
+
78
+ const setColorMode = useCallback(
79
+ (newColorMode: ColorMode | null, options: {persist?: boolean} = {}) => {
80
+ const {persist = true} = options;
81
+ if (newColorMode) {
82
+ setColorModeState(newColorMode);
83
+ if (persist) {
84
+ storeColorMode(newColorMode);
85
+ }
86
+ } else {
87
+ if (respectPrefersColorScheme) {
88
+ setColorModeState(
89
+ window.matchMedia('(prefers-color-scheme: dark)').matches
90
+ ? ColorModes.dark
91
+ : ColorModes.light,
92
+ );
93
+ } else {
94
+ setColorModeState(defaultMode);
95
+ }
96
+ ColorModeStorage.del();
97
+ }
98
+ },
99
+ [respectPrefersColorScheme, defaultMode],
100
+ );
101
+
102
+ useEffect(() => {
103
+ document.documentElement.setAttribute(
104
+ 'data-theme',
105
+ coerceToColorMode(colorMode),
106
+ );
107
+ }, [colorMode]);
108
+
109
+ useEffect(() => {
110
+ if (disableSwitch) {
111
+ return undefined;
112
+ }
113
+ const onChange = (e: StorageEvent) => {
114
+ if (e.key !== ColorModeStorageKey) {
115
+ return;
116
+ }
117
+ const storedColorMode = ColorModeStorage.get();
118
+ if (storedColorMode !== null) {
119
+ setColorMode(coerceToColorMode(storedColorMode));
120
+ }
121
+ };
122
+ window.addEventListener('storage', onChange);
123
+ return () => window.removeEventListener('storage', onChange);
124
+ }, [disableSwitch, setColorMode]);
125
+
126
+ // PCS is coerced to light mode when printing, which causes the color mode to
127
+ // be reset to dark when exiting print mode, disregarding user settings. When
128
+ // the listener fires only because of a print/screen switch, we don't change
129
+ // color mode. See https://github.com/facebook/docusaurus/pull/6490
130
+ const previousMediaIsPrint = useRef(false);
131
+
132
+ useEffect(() => {
133
+ if (disableSwitch && !respectPrefersColorScheme) {
134
+ return undefined;
135
+ }
136
+ const mql = window.matchMedia('(prefers-color-scheme: dark)');
137
+ const onChange = () => {
138
+ if (window.matchMedia('print').matches || previousMediaIsPrint.current) {
139
+ previousMediaIsPrint.current = window.matchMedia('print').matches;
140
+ return;
141
+ }
142
+ setColorMode(null);
143
+ };
144
+ mql.addListener(onChange);
145
+ return () => mql.removeListener(onChange);
146
+ }, [setColorMode, disableSwitch, respectPrefersColorScheme]);
147
+
148
+ return useMemo(
149
+ () => ({
150
+ colorMode,
151
+ setColorMode,
152
+ get isDarkTheme() {
153
+ if (process.env.NODE_ENV === 'development') {
154
+ console.error(
155
+ '`useColorMode().isDarkTheme` is deprecated. Please use `useColorMode().colorMode === "dark"` instead.',
156
+ );
157
+ }
158
+ return colorMode === ColorModes.dark;
159
+ },
160
+ setLightTheme() {
161
+ if (process.env.NODE_ENV === 'development') {
162
+ console.error(
163
+ '`useColorMode().setLightTheme` is deprecated. Please use `useColorMode().setColorMode("light")` instead.',
164
+ );
165
+ }
166
+ setColorMode(ColorModes.light);
167
+ },
168
+ setDarkTheme() {
169
+ if (process.env.NODE_ENV === 'development') {
170
+ console.error(
171
+ '`useColorMode().setDarkTheme` is deprecated. Please use `useColorMode().setColorMode("dark")` instead.',
172
+ );
173
+ }
174
+ setColorMode(ColorModes.dark);
175
+ },
176
+ }),
177
+ [colorMode, setColorMode],
178
+ );
179
+ }
180
+
181
+ export function ColorModeProvider({
182
+ children,
183
+ }: {
184
+ children: ReactNode;
185
+ }): JSX.Element {
186
+ const value = useContextValue();
187
+ return <Context.Provider value={value}>{children}</Context.Provider>;
188
+ }
189
+
190
+ export function useColorMode(): ContextValue {
191
+ const context = useContext(Context);
192
+ if (context == null) {
193
+ throw new ReactContextError(
194
+ 'ColorModeProvider',
195
+ 'Please see https://docusaurus.io/docs/api/themes/configuration#use-color-mode.',
196
+ );
197
+ }
198
+ return context;
199
+ }
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import React, {type ReactNode, useMemo, useState, useContext} from 'react';
9
+ import {ReactContextError} from '../utils/reactUtils';
10
+
11
+ type ContextValue = {
12
+ /**
13
+ * The item that the user last opened, `null` when there's none open. On
14
+ * initial render, it will always be `null`, which doesn't necessarily mean
15
+ * there's no category open (can have 0, 1, or many being initially open).
16
+ */
17
+ expandedItem: number | null;
18
+ /**
19
+ * Set the currently expanded item, when the user opens one. Set the value to
20
+ * `null` when the user closes an open category.
21
+ */
22
+ setExpandedItem: (a: number | null) => void;
23
+ };
24
+
25
+ const EmptyContext: unique symbol = Symbol('EmptyContext');
26
+ const Context = React.createContext<ContextValue | typeof EmptyContext>(
27
+ EmptyContext,
28
+ );
29
+
30
+ /**
31
+ * Should be used to wrap one sidebar category level. This provider syncs the
32
+ * expanded states of all sibling categories, and categories can choose to
33
+ * collapse itself if another one is expanded.
34
+ */
35
+ export function DocSidebarItemsExpandedStateProvider({
36
+ children,
37
+ }: {
38
+ children: ReactNode;
39
+ }): JSX.Element {
40
+ const [expandedItem, setExpandedItem] = useState<number | null>(null);
41
+ const contextValue = useMemo(
42
+ () => ({expandedItem, setExpandedItem}),
43
+ [expandedItem],
44
+ );
45
+
46
+ return <Context.Provider value={contextValue}>{children}</Context.Provider>;
47
+ }
48
+
49
+ export function useDocSidebarItemsExpandedState(): ContextValue {
50
+ const value = useContext(Context);
51
+ if (value === EmptyContext) {
52
+ throw new ReactContextError('DocSidebarItemsExpandedStateProvider');
53
+ }
54
+ return value;
55
+ }
@@ -0,0 +1,253 @@
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
+ useContext,
10
+ useEffect,
11
+ useMemo,
12
+ useState,
13
+ useCallback,
14
+ type ReactNode,
15
+ } from 'react';
16
+ import {
17
+ useThemeConfig,
18
+ type DocsVersionPersistence,
19
+ } from '../utils/useThemeConfig';
20
+ import {isDocsPluginEnabled} from '../utils/docsUtils';
21
+ import {ReactContextError} from '../utils/reactUtils';
22
+ import {createStorageSlot} from '../utils/storageUtils';
23
+
24
+ import {
25
+ useAllDocsData,
26
+ useDocsData,
27
+ type GlobalPluginData,
28
+ type GlobalVersion,
29
+ } from '@docusaurus/plugin-content-docs/client';
30
+
31
+ import {DEFAULT_PLUGIN_ID} from '@docusaurus/constants';
32
+
33
+ const storageKey = (pluginId: string) => `docs-preferred-version-${pluginId}`;
34
+
35
+ const DocsPreferredVersionStorage = {
36
+ save: (
37
+ pluginId: string,
38
+ persistence: DocsVersionPersistence,
39
+ versionName: string,
40
+ ): void => {
41
+ createStorageSlot(storageKey(pluginId), {persistence}).set(versionName);
42
+ },
43
+
44
+ read: (
45
+ pluginId: string,
46
+ persistence: DocsVersionPersistence,
47
+ ): string | null =>
48
+ createStorageSlot(storageKey(pluginId), {persistence}).get(),
49
+
50
+ clear: (pluginId: string, persistence: DocsVersionPersistence): void => {
51
+ createStorageSlot(storageKey(pluginId), {persistence}).del();
52
+ },
53
+ };
54
+
55
+ type DocsPreferredVersionName = string | null;
56
+
57
+ /** State for a single docs plugin instance */
58
+ type DocsPreferredVersionPluginState = {
59
+ preferredVersionName: DocsPreferredVersionName;
60
+ };
61
+
62
+ /**
63
+ * We need to store the state in storage globally, with one preferred version
64
+ * per docs plugin instance.
65
+ */
66
+ type DocsPreferredVersionState = {
67
+ [pluginId: string]: DocsPreferredVersionPluginState;
68
+ };
69
+
70
+ /**
71
+ * Initial state is always null as we can't read local storage from node SSR
72
+ */
73
+ const getInitialState = (pluginIds: string[]): DocsPreferredVersionState =>
74
+ Object.fromEntries(pluginIds.map((id) => [id, {preferredVersionName: null}]));
75
+
76
+ /**
77
+ * Read storage for all docs plugins, assigning each doc plugin a preferred
78
+ * version (if found)
79
+ */
80
+ function readStorageState({
81
+ pluginIds,
82
+ versionPersistence,
83
+ allDocsData,
84
+ }: {
85
+ pluginIds: string[];
86
+ versionPersistence: DocsVersionPersistence;
87
+ allDocsData: {[pluginId: string]: GlobalPluginData};
88
+ }): DocsPreferredVersionState {
89
+ /**
90
+ * The storage value we read might be stale, and belong to a version that does
91
+ * not exist in the site anymore. In such case, we remove the storage value to
92
+ * avoid downstream errors.
93
+ */
94
+ function restorePluginState(
95
+ pluginId: string,
96
+ ): DocsPreferredVersionPluginState {
97
+ const preferredVersionNameUnsafe = DocsPreferredVersionStorage.read(
98
+ pluginId,
99
+ versionPersistence,
100
+ );
101
+ const pluginData = allDocsData[pluginId]!;
102
+ const versionExists = pluginData.versions.some(
103
+ (version) => version.name === preferredVersionNameUnsafe,
104
+ );
105
+ if (versionExists) {
106
+ return {preferredVersionName: preferredVersionNameUnsafe};
107
+ }
108
+ DocsPreferredVersionStorage.clear(pluginId, versionPersistence);
109
+ return {preferredVersionName: null};
110
+ }
111
+ return Object.fromEntries(
112
+ pluginIds.map((id) => [id, restorePluginState(id)]),
113
+ );
114
+ }
115
+
116
+ function useVersionPersistence(): DocsVersionPersistence {
117
+ return useThemeConfig().docs.versionPersistence;
118
+ }
119
+
120
+ type ContextValue = [
121
+ state: DocsPreferredVersionState,
122
+ api: {
123
+ savePreferredVersion: (pluginId: string, versionName: string) => void;
124
+ },
125
+ ];
126
+
127
+ const Context = React.createContext<ContextValue | null>(null);
128
+
129
+ function useContextValue(): ContextValue {
130
+ const allDocsData = useAllDocsData();
131
+ const versionPersistence = useVersionPersistence();
132
+ const pluginIds = useMemo(() => Object.keys(allDocsData), [allDocsData]);
133
+
134
+ // Initial state is empty, as we can't read browser storage in node/SSR
135
+ const [state, setState] = useState(() => getInitialState(pluginIds));
136
+
137
+ // On mount, we set the state read from browser storage
138
+ useEffect(() => {
139
+ setState(readStorageState({allDocsData, versionPersistence, pluginIds}));
140
+ }, [allDocsData, versionPersistence, pluginIds]);
141
+
142
+ // The API that we expose to consumer hooks (memo for constant object)
143
+ const api = useMemo(() => {
144
+ function savePreferredVersion(pluginId: string, versionName: string) {
145
+ DocsPreferredVersionStorage.save(
146
+ pluginId,
147
+ versionPersistence,
148
+ versionName,
149
+ );
150
+ setState((s) => ({
151
+ ...s,
152
+ [pluginId]: {preferredVersionName: versionName},
153
+ }));
154
+ }
155
+
156
+ return {
157
+ savePreferredVersion,
158
+ };
159
+ }, [versionPersistence]);
160
+
161
+ return [state, api];
162
+ }
163
+
164
+ function DocsPreferredVersionContextProviderUnsafe({
165
+ children,
166
+ }: {
167
+ children: ReactNode;
168
+ }): JSX.Element {
169
+ const value = useContextValue();
170
+ return <Context.Provider value={value}>{children}</Context.Provider>;
171
+ }
172
+
173
+ /**
174
+ * This is a maybe-layer. If the docs plugin is not enabled, this provider is a
175
+ * simple pass-through.
176
+ */
177
+ export function DocsPreferredVersionContextProvider({
178
+ children,
179
+ }: {
180
+ children: JSX.Element;
181
+ }): JSX.Element {
182
+ if (isDocsPluginEnabled) {
183
+ return (
184
+ <DocsPreferredVersionContextProviderUnsafe>
185
+ {children}
186
+ </DocsPreferredVersionContextProviderUnsafe>
187
+ );
188
+ }
189
+ return children;
190
+ }
191
+
192
+ function useDocsPreferredVersionContext(): ContextValue {
193
+ const value = useContext(Context);
194
+ if (!value) {
195
+ throw new ReactContextError('DocsPreferredVersionContextProvider');
196
+ }
197
+ return value;
198
+ }
199
+
200
+ /**
201
+ * Returns a read-write interface to a plugin's preferred version. The
202
+ * "preferred version" is defined as the last version that the user visited.
203
+ * For example, if a user is using v3, even when v4 is later published, the user
204
+ * would still be browsing v3 docs when she opens the website next time. Note,
205
+ * the `preferredVersion` attribute will always be `null` before mount.
206
+ */
207
+ export function useDocsPreferredVersion(
208
+ pluginId: string | undefined = DEFAULT_PLUGIN_ID,
209
+ ): {
210
+ preferredVersion: GlobalVersion | null;
211
+ savePreferredVersionName: (versionName: string) => void;
212
+ } {
213
+ const docsData = useDocsData(pluginId);
214
+ const [state, api] = useDocsPreferredVersionContext();
215
+
216
+ const {preferredVersionName} = state[pluginId]!;
217
+
218
+ const preferredVersion =
219
+ docsData.versions.find(
220
+ (version) => version.name === preferredVersionName,
221
+ ) ?? null;
222
+
223
+ const savePreferredVersionName = useCallback(
224
+ (versionName: string) => {
225
+ api.savePreferredVersion(pluginId, versionName);
226
+ },
227
+ [api, pluginId],
228
+ );
229
+
230
+ return {preferredVersion, savePreferredVersionName};
231
+ }
232
+
233
+ export function useDocsPreferredVersionByPluginId(): {
234
+ [pluginId: string]: GlobalVersion | null;
235
+ } {
236
+ const allDocsData = useAllDocsData();
237
+ const [state] = useDocsPreferredVersionContext();
238
+
239
+ function getPluginIdPreferredVersion(pluginId: string) {
240
+ const docsData = allDocsData[pluginId]!;
241
+ const {preferredVersionName} = state[pluginId]!;
242
+
243
+ return (
244
+ docsData.versions.find(
245
+ (version) => version.name === preferredVersionName,
246
+ ) ?? null
247
+ );
248
+ }
249
+ const pluginIds = Object.keys(allDocsData);
250
+ return Object.fromEntries(
251
+ pluginIds.map((id) => [id, getPluginIdPreferredVersion(id)]),
252
+ );
253
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import React, {useMemo, useContext, type ReactNode} from 'react';
9
+ import type {PropSidebar} from '@docusaurus/plugin-content-docs';
10
+ import {ReactContextError} from '../utils/reactUtils';
11
+
12
+ // Using a Symbol because null is a valid context value (a doc with no sidebar)
13
+ // Inspired by https://github.com/jamiebuilds/unstated-next/blob/master/src/unstated-next.tsx
14
+ const EmptyContext: unique symbol = Symbol('EmptyContext');
15
+
16
+ type ContextValue = {name: string; items: PropSidebar};
17
+
18
+ const Context = React.createContext<ContextValue | null | typeof EmptyContext>(
19
+ EmptyContext,
20
+ );
21
+
22
+ /**
23
+ * Provide the current sidebar to your children.
24
+ */
25
+ export function DocsSidebarProvider({
26
+ children,
27
+ name,
28
+ items,
29
+ }: {
30
+ children: ReactNode;
31
+ name: string | undefined;
32
+ items: PropSidebar | undefined;
33
+ }): JSX.Element {
34
+ const stableValue: ContextValue | null = useMemo(
35
+ () => (name && items ? {name, items} : null),
36
+ [name, items],
37
+ );
38
+ return <Context.Provider value={stableValue}>{children}</Context.Provider>;
39
+ }
40
+
41
+ /**
42
+ * Gets the sidebar that's currently displayed, or `null` if there isn't one
43
+ */
44
+ export function useDocsSidebar(): ContextValue | null {
45
+ const value = useContext(Context);
46
+ if (value === EmptyContext) {
47
+ throw new ReactContextError('DocsSidebarProvider');
48
+ }
49
+ return value;
50
+ }
@@ -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
+ import React, {type ReactNode, useContext} from 'react';
9
+ import type {PropVersionMetadata} from '@docusaurus/plugin-content-docs';
10
+ import {ReactContextError} from '../utils/reactUtils';
11
+
12
+ const Context = React.createContext<PropVersionMetadata | null>(null);
13
+
14
+ /**
15
+ * Provide the current version's metadata to your children.
16
+ */
17
+ export function DocsVersionProvider({
18
+ children,
19
+ version,
20
+ }: {
21
+ children: ReactNode;
22
+ version: PropVersionMetadata | null;
23
+ }): JSX.Element {
24
+ return <Context.Provider value={version}>{children}</Context.Provider>;
25
+ }
26
+
27
+ /**
28
+ * Gets the version metadata of the current doc page.
29
+ */
30
+ export function useDocsVersion(): PropVersionMetadata {
31
+ const version = useContext(Context);
32
+ if (version === null) {
33
+ throw new ReactContextError('DocsVersionProvider');
34
+ }
35
+ return version;
36
+ }
@@ -0,0 +1,99 @@
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
+ useCallback,
10
+ useEffect,
11
+ useState,
12
+ useMemo,
13
+ type ReactNode,
14
+ } from 'react';
15
+ import {useNavbarSecondaryMenuContent} from './navbarSecondaryMenu/content';
16
+ import {useWindowSize} from '../hooks/useWindowSize';
17
+ import {useHistoryPopHandler} from '../utils/historyUtils';
18
+ import {useThemeConfig} from '../utils/useThemeConfig';
19
+ import {ReactContextError} from '../utils/reactUtils';
20
+
21
+ type ContextValue = {
22
+ /**
23
+ * Mobile sidebar should be disabled in case it's empty, i.e. no secondary
24
+ * menu + no navbar items). If disabled, the toggle button should not be
25
+ * displayed at all.
26
+ */
27
+ disabled: boolean;
28
+ /**
29
+ * Signals whether the actual sidebar should be displayed (contrary to
30
+ * `disabled` which is about the toggle button). Sidebar should not visible
31
+ * until user interaction to avoid SSR rendering.
32
+ */
33
+ shouldRender: boolean;
34
+ /** The displayed state. Can be toggled with the `toggle` callback. */
35
+ shown: boolean;
36
+ /** Toggle the `shown` attribute. */
37
+ toggle: () => void;
38
+ };
39
+
40
+ const Context = React.createContext<ContextValue | undefined>(undefined);
41
+
42
+ function useIsNavbarMobileSidebarDisabled() {
43
+ const secondaryMenuContent = useNavbarSecondaryMenuContent();
44
+ const {items} = useThemeConfig().navbar;
45
+ return items.length === 0 && !secondaryMenuContent.component;
46
+ }
47
+
48
+ function useContextValue(): ContextValue {
49
+ const disabled = useIsNavbarMobileSidebarDisabled();
50
+ const windowSize = useWindowSize();
51
+
52
+ const shouldRender = !disabled && windowSize === 'mobile';
53
+
54
+ const [shown, setShown] = useState(false);
55
+
56
+ // Close mobile sidebar on navigation pop
57
+ // Most likely firing when using the Android back button (but not only)
58
+ useHistoryPopHandler(() => {
59
+ if (shown) {
60
+ setShown(false);
61
+ // Prevent pop navigation; seems desirable enough
62
+ // See https://github.com/facebook/docusaurus/pull/5462#issuecomment-911699846
63
+ return false;
64
+ }
65
+ return undefined;
66
+ });
67
+
68
+ const toggle = useCallback(() => {
69
+ setShown((s) => !s);
70
+ }, []);
71
+
72
+ useEffect(() => {
73
+ if (windowSize === 'desktop') {
74
+ setShown(false);
75
+ }
76
+ }, [windowSize]);
77
+
78
+ return useMemo(
79
+ () => ({disabled, shouldRender, toggle, shown}),
80
+ [disabled, shouldRender, toggle, shown],
81
+ );
82
+ }
83
+
84
+ export function NavbarMobileSidebarProvider({
85
+ children,
86
+ }: {
87
+ children: ReactNode;
88
+ }): JSX.Element {
89
+ const value = useContextValue();
90
+ return <Context.Provider value={value}>{children}</Context.Provider>;
91
+ }
92
+
93
+ export function useNavbarMobileSidebar(): ContextValue {
94
+ const context = React.useContext(Context);
95
+ if (context === undefined) {
96
+ throw new ReactContextError('NavbarMobileSidebarProvider');
97
+ }
98
+ return context;
99
+ }