@docusaurus/theme-common 2.0.0-beta.1ec2c95e3 → 2.0.0-beta.21

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 (251) hide show
  1. package/Details.d.ts +14 -0
  2. package/lib/components/Collapsible/index.d.ts +64 -0
  3. package/lib/components/Collapsible/index.d.ts.map +1 -0
  4. package/lib/components/Collapsible/index.js +150 -0
  5. package/lib/components/Collapsible/index.js.map +1 -0
  6. package/lib/components/Details/index.d.ts +17 -0
  7. package/lib/components/Details/index.d.ts.map +1 -0
  8. package/lib/components/Details/index.js +76 -0
  9. package/lib/components/Details/index.js.map +1 -0
  10. package/lib/components/Details/styles.module.css +62 -0
  11. package/lib/contexts/announcementBar.d.ts +22 -0
  12. package/lib/contexts/announcementBar.d.ts.map +1 -0
  13. package/lib/{utils/announcementBarUtils.js → contexts/announcementBar.js} +30 -31
  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/contexts/docSidebarItemsExpandedState.js +28 -0
  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 +130 -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/contexts/tabGroupChoice.d.ts +21 -0
  48. package/lib/contexts/tabGroupChoice.d.ts.map +1 -0
  49. package/lib/contexts/tabGroupChoice.js +49 -0
  50. package/lib/contexts/tabGroupChoice.js.map +1 -0
  51. package/lib/{utils/usePrevious.d.ts → hooks/styles.css} +4 -1
  52. package/lib/hooks/useBackToTopButton.d.ts +27 -0
  53. package/lib/hooks/useBackToTopButton.d.ts.map +1 -0
  54. package/lib/hooks/useBackToTopButton.js +50 -0
  55. package/lib/hooks/useBackToTopButton.js.map +1 -0
  56. package/lib/hooks/useCodeWordWrap.d.ts +14 -0
  57. package/lib/hooks/useCodeWordWrap.d.ts.map +1 -0
  58. package/lib/hooks/useCodeWordWrap.js +41 -0
  59. package/lib/hooks/useCodeWordWrap.js.map +1 -0
  60. package/lib/hooks/useHideableNavbar.d.ts +17 -0
  61. package/lib/hooks/useHideableNavbar.d.ts.map +1 -0
  62. package/lib/hooks/useHideableNavbar.js +60 -0
  63. package/lib/hooks/useHideableNavbar.js.map +1 -0
  64. package/lib/hooks/useKeyboardNavigation.d.ts +20 -0
  65. package/lib/hooks/useKeyboardNavigation.d.ts.map +1 -0
  66. package/lib/hooks/useKeyboardNavigation.js +39 -0
  67. package/lib/hooks/useKeyboardNavigation.js.map +1 -0
  68. package/lib/hooks/useLockBodyScroll.d.ts +12 -0
  69. package/lib/hooks/useLockBodyScroll.d.ts.map +1 -0
  70. package/lib/hooks/useLockBodyScroll.js +20 -0
  71. package/lib/hooks/useLockBodyScroll.js.map +1 -0
  72. package/lib/hooks/usePrismTheme.d.ts +13 -0
  73. package/lib/hooks/usePrismTheme.d.ts.map +1 -0
  74. package/lib/hooks/usePrismTheme.js +21 -0
  75. package/lib/hooks/usePrismTheme.js.map +1 -0
  76. package/lib/hooks/useSearchPage.d.ts +25 -0
  77. package/lib/hooks/useSearchPage.d.ts.map +1 -0
  78. package/lib/hooks/useSearchPage.js +43 -0
  79. package/lib/hooks/useSearchPage.js.map +1 -0
  80. package/lib/hooks/useSkipToContent.d.ts +25 -0
  81. package/lib/hooks/useSkipToContent.d.ts.map +1 -0
  82. package/lib/hooks/useSkipToContent.js +35 -0
  83. package/lib/hooks/useSkipToContent.js.map +1 -0
  84. package/lib/hooks/useTOCHighlight.d.ts +25 -0
  85. package/lib/hooks/useTOCHighlight.d.ts.map +1 -0
  86. package/lib/hooks/useTOCHighlight.js +130 -0
  87. package/lib/hooks/useTOCHighlight.js.map +1 -0
  88. package/lib/hooks/useWindowSize.d.ts +28 -0
  89. package/lib/hooks/useWindowSize.d.ts.map +1 -0
  90. package/lib/hooks/useWindowSize.js +59 -0
  91. package/lib/hooks/useWindowSize.js.map +1 -0
  92. package/lib/index.d.ts +38 -10
  93. package/lib/index.d.ts.map +1 -0
  94. package/lib/index.js +38 -9
  95. package/lib/index.js.map +1 -0
  96. package/lib/utils/ThemeClassNames.d.ts +47 -12
  97. package/lib/utils/ThemeClassNames.d.ts.map +1 -0
  98. package/lib/utils/ThemeClassNames.js +45 -4
  99. package/lib/utils/ThemeClassNames.js.map +1 -0
  100. package/lib/utils/codeBlockUtils.d.ts +63 -0
  101. package/lib/utils/codeBlockUtils.d.ts.map +1 -0
  102. package/lib/utils/codeBlockUtils.js +159 -3
  103. package/lib/utils/codeBlockUtils.js.map +1 -0
  104. package/lib/utils/docsUtils.d.ts +91 -0
  105. package/lib/utils/docsUtils.d.ts.map +1 -0
  106. package/lib/utils/docsUtils.js +217 -1
  107. package/lib/utils/docsUtils.js.map +1 -0
  108. package/lib/utils/footerUtils.d.ts +13 -0
  109. package/lib/utils/footerUtils.d.ts.map +1 -0
  110. package/lib/utils/footerUtils.js +14 -0
  111. package/lib/utils/footerUtils.js.map +1 -0
  112. package/lib/utils/generalUtils.d.ts +11 -1
  113. package/lib/utils/generalUtils.d.ts.map +1 -0
  114. package/lib/utils/generalUtils.js +9 -5
  115. package/lib/utils/generalUtils.js.map +1 -0
  116. package/lib/utils/historyUtils.d.ts +17 -0
  117. package/lib/utils/historyUtils.d.ts.map +1 -0
  118. package/lib/utils/historyUtils.js +38 -0
  119. package/lib/utils/historyUtils.js.map +1 -0
  120. package/lib/utils/jsUtils.d.ts +23 -0
  121. package/lib/utils/jsUtils.d.ts.map +1 -0
  122. package/lib/utils/jsUtils.js +29 -0
  123. package/lib/utils/jsUtils.js.map +1 -0
  124. package/lib/utils/metadataUtils.d.ts +38 -0
  125. package/lib/utils/metadataUtils.d.ts.map +1 -0
  126. package/lib/utils/metadataUtils.js +70 -0
  127. package/lib/utils/metadataUtils.js.map +1 -0
  128. package/lib/utils/navbarUtils.d.ts +21 -0
  129. package/lib/utils/navbarUtils.d.ts.map +1 -0
  130. package/lib/utils/navbarUtils.js +36 -0
  131. package/lib/utils/navbarUtils.js.map +1 -0
  132. package/lib/utils/reactUtils.d.ts +42 -0
  133. package/lib/utils/reactUtils.d.ts.map +1 -0
  134. package/lib/utils/reactUtils.js +64 -0
  135. package/lib/utils/reactUtils.js.map +1 -0
  136. package/lib/utils/regexpUtils.d.ts +12 -0
  137. package/lib/utils/regexpUtils.d.ts.map +1 -0
  138. package/lib/utils/regexpUtils.js +18 -0
  139. package/lib/utils/regexpUtils.js.map +1 -0
  140. package/lib/utils/routesUtils.d.ts +26 -0
  141. package/lib/utils/routesUtils.d.ts.map +1 -0
  142. package/lib/utils/routesUtils.js +54 -0
  143. package/lib/utils/routesUtils.js.map +1 -0
  144. package/lib/utils/scrollUtils.d.ts +83 -0
  145. package/lib/utils/scrollUtils.d.ts.map +1 -0
  146. package/lib/utils/scrollUtils.js +200 -0
  147. package/lib/utils/scrollUtils.js.map +1 -0
  148. package/lib/utils/searchUtils.d.ts +13 -0
  149. package/lib/utils/searchUtils.d.ts.map +1 -0
  150. package/lib/utils/searchUtils.js +37 -0
  151. package/lib/utils/searchUtils.js.map +1 -0
  152. package/lib/utils/storageUtils.d.ts +15 -7
  153. package/lib/utils/storageUtils.d.ts.map +1 -0
  154. package/lib/utils/storageUtils.js +55 -22
  155. package/lib/utils/storageUtils.js.map +1 -0
  156. package/lib/utils/tagsUtils.d.ts +18 -0
  157. package/lib/utils/tagsUtils.d.ts.map +1 -0
  158. package/lib/utils/tagsUtils.js +36 -0
  159. package/lib/utils/tagsUtils.js.map +1 -0
  160. package/lib/utils/tocUtils.d.ts +36 -0
  161. package/lib/utils/tocUtils.d.ts.map +1 -0
  162. package/lib/utils/tocUtils.js +84 -0
  163. package/lib/utils/tocUtils.js.map +1 -0
  164. package/lib/utils/useAlternatePageUtils.d.ts +21 -1
  165. package/lib/utils/useAlternatePageUtils.d.ts.map +1 -0
  166. package/lib/utils/useAlternatePageUtils.js +9 -4
  167. package/lib/utils/useAlternatePageUtils.js.map +1 -0
  168. package/lib/utils/useLocalPathname.d.ts +13 -0
  169. package/lib/utils/useLocalPathname.d.ts.map +1 -0
  170. package/lib/utils/useLocalPathname.js +19 -0
  171. package/lib/utils/useLocalPathname.js.map +1 -0
  172. package/lib/utils/useLocationChange.d.ts +9 -6
  173. package/lib/utils/useLocationChange.d.ts.map +1 -0
  174. package/lib/utils/useLocationChange.js +17 -11
  175. package/lib/utils/useLocationChange.js.map +1 -0
  176. package/lib/utils/usePluralForm.d.ts +12 -0
  177. package/lib/utils/usePluralForm.d.ts.map +1 -0
  178. package/lib/utils/usePluralForm.js +36 -37
  179. package/lib/utils/usePluralForm.js.map +1 -0
  180. package/lib/utils/useThemeConfig.d.ts +56 -25
  181. package/lib/utils/useThemeConfig.d.ts.map +1 -0
  182. package/lib/utils/useThemeConfig.js +4 -0
  183. package/lib/utils/useThemeConfig.js.map +1 -0
  184. package/package.json +28 -13
  185. package/src/components/Collapsible/index.tsx +263 -0
  186. package/src/components/Details/index.tsx +109 -0
  187. package/src/components/Details/styles.module.css +62 -0
  188. package/src/{utils/announcementBarUtils.tsx → contexts/announcementBar.tsx} +43 -39
  189. package/src/contexts/colorMode.tsx +198 -0
  190. package/src/contexts/docSidebarItemsExpandedState.tsx +55 -0
  191. package/src/contexts/docsPreferredVersion.tsx +251 -0
  192. package/src/contexts/docsSidebar.tsx +50 -0
  193. package/src/contexts/docsVersion.tsx +36 -0
  194. package/src/contexts/navbarMobileSidebar.tsx +99 -0
  195. package/src/contexts/navbarSecondaryMenu/content.tsx +110 -0
  196. package/src/contexts/navbarSecondaryMenu/display.tsx +102 -0
  197. package/src/contexts/tabGroupChoice.tsx +85 -0
  198. package/{lib/utils/pathUtils.d.ts → src/hooks/styles.css} +4 -1
  199. package/src/hooks/useBackToTopButton.ts +73 -0
  200. package/src/hooks/useCodeWordWrap.ts +56 -0
  201. package/src/hooks/useHideableNavbar.ts +75 -0
  202. package/src/hooks/useKeyboardNavigation.ts +45 -0
  203. package/src/hooks/useLockBodyScroll.ts +21 -0
  204. package/src/hooks/usePrismTheme.ts +24 -0
  205. package/src/hooks/useSearchPage.ts +79 -0
  206. package/src/hooks/useSkipToContent.ts +58 -0
  207. package/src/hooks/useTOCHighlight.ts +189 -0
  208. package/src/hooks/useWindowSize.ts +72 -0
  209. package/src/index.ts +130 -19
  210. package/src/types.d.ts +0 -2
  211. package/src/utils/ThemeClassNames.ts +51 -5
  212. package/src/utils/codeBlockUtils.ts +241 -2
  213. package/src/utils/docsUtils.tsx +334 -0
  214. package/src/utils/footerUtils.ts +18 -0
  215. package/src/utils/generalUtils.ts +9 -5
  216. package/src/utils/historyUtils.ts +45 -0
  217. package/src/utils/jsUtils.ts +36 -0
  218. package/src/utils/metadataUtils.tsx +115 -0
  219. package/src/utils/navbarUtils.tsx +45 -0
  220. package/src/utils/reactUtils.tsx +76 -0
  221. package/src/utils/regexpUtils.ts +24 -0
  222. package/src/utils/routesUtils.ts +75 -0
  223. package/src/utils/scrollUtils.tsx +304 -0
  224. package/src/utils/searchUtils.ts +51 -0
  225. package/src/utils/storageUtils.ts +56 -23
  226. package/src/utils/tagsUtils.ts +50 -0
  227. package/src/utils/tocUtils.ts +119 -0
  228. package/src/utils/useAlternatePageUtils.ts +19 -6
  229. package/src/utils/useLocalPathname.ts +22 -0
  230. package/src/utils/useLocationChange.ts +24 -20
  231. package/src/utils/usePluralForm.ts +50 -38
  232. package/src/utils/useThemeConfig.ts +55 -27
  233. package/lib/.tsbuildinfo +0 -1
  234. package/lib/utils/announcementBarUtils.d.ts +0 -17
  235. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +0 -21
  236. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +0 -94
  237. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts +0 -13
  238. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +0 -20
  239. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +0 -5
  240. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +0 -41
  241. package/lib/utils/pathUtils.js +0 -13
  242. package/lib/utils/usePrevious.js +0 -14
  243. package/src/utils/__tests__/codeBlockUtils.test.ts +0 -54
  244. package/src/utils/__tests__/pathUtils.test.ts +0 -32
  245. package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +0 -165
  246. package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +0 -34
  247. package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +0 -66
  248. package/src/utils/docsUtils.ts +0 -11
  249. package/src/utils/pathUtils.ts +0 -17
  250. package/src/utils/usePrevious.ts +0 -18
  251. package/tsconfig.json +0 -10
@@ -10,13 +10,13 @@ import React, {
10
10
  useEffect,
11
11
  useCallback,
12
12
  useMemo,
13
- ReactNode,
14
13
  useContext,
15
- createContext,
14
+ type ReactNode,
16
15
  } from 'react';
17
- import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
18
- import {createStorageSlot} from './storageUtils';
19
- import {useThemeConfig} from './useThemeConfig';
16
+ import useIsBrowser from '@docusaurus/useIsBrowser';
17
+ import {createStorageSlot} from '../utils/storageUtils';
18
+ import {ReactContextError} from '../utils/reactUtils';
19
+ import {useThemeConfig} from '../utils/useThemeConfig';
20
20
 
21
21
  export const AnnouncementBarDismissStorageKey =
22
22
  'docusaurus.announcement.dismiss';
@@ -32,22 +32,28 @@ const isDismissedInStorage = () =>
32
32
  const setDismissedInStorage = (bool: boolean) =>
33
33
  AnnouncementBarDismissStorage.set(String(bool));
34
34
 
35
- type AnnouncementBarAPI = {
36
- readonly isClosed: boolean;
35
+ type ContextValue = {
36
+ /** Whether the announcement bar should be displayed. */
37
+ readonly isActive: boolean;
38
+ /**
39
+ * Callback fired when the user closes the announcement. Will be saved.
40
+ */
37
41
  readonly close: () => void;
38
42
  };
39
43
 
40
- const useAnnouncementBarContextValue = (): AnnouncementBarAPI => {
44
+ const Context = React.createContext<ContextValue | null>(null);
45
+
46
+ function useContextValue(): ContextValue {
41
47
  const {announcementBar} = useThemeConfig();
42
- const {isClient} = useDocusaurusContext();
48
+ const isBrowser = useIsBrowser();
43
49
 
44
- const [isClosed, setClosed] = useState(() => {
45
- return isClient
46
- ? // On client navigation: init with localstorage value
50
+ const [isClosed, setClosed] = useState(() =>
51
+ isBrowser
52
+ ? // On client navigation: init with local storage value
47
53
  isDismissedInStorage()
48
54
  : // On server/hydration: always visible to prevent layout shifts (will be hidden with css if needed)
49
- false;
50
- });
55
+ false,
56
+ );
51
57
  // Update state after hydration
52
58
  useEffect(() => {
53
59
  setClosed(isDismissedInStorage());
@@ -66,8 +72,9 @@ const useAnnouncementBarContextValue = (): AnnouncementBarAPI => {
66
72
 
67
73
  let viewedId = IdStorage.get();
68
74
 
69
- // retrocompatibility due to spelling mistake of default id
75
+ // Retrocompatibility due to spelling mistake of default id
70
76
  // see https://github.com/facebook/docusaurus/issues/3338
77
+ // cSpell:ignore annoucement
71
78
  if (viewedId === 'annoucement-bar') {
72
79
  viewedId = 'announcement-bar';
73
80
  }
@@ -83,33 +90,30 @@ const useAnnouncementBarContextValue = (): AnnouncementBarAPI => {
83
90
  if (isNewAnnouncement || !isDismissedInStorage()) {
84
91
  setClosed(false);
85
92
  }
86
- }, []);
93
+ }, [announcementBar]);
87
94
 
88
- return useMemo(() => {
89
- return {
90
- isClosed,
95
+ return useMemo(
96
+ () => ({
97
+ isActive: !!announcementBar && !isClosed,
91
98
  close: handleClose,
92
- };
93
- }, [isClosed]);
94
- };
95
-
96
- const AnnouncementBarContext = createContext<AnnouncementBarAPI | null>(null);
97
-
98
- export const AnnouncementBarProvider = ({children}: {children: ReactNode}) => {
99
- const value = useAnnouncementBarContextValue();
100
- return (
101
- <AnnouncementBarContext.Provider value={value}>
102
- {children}
103
- </AnnouncementBarContext.Provider>
99
+ }),
100
+ [announcementBar, isClosed, handleClose],
104
101
  );
105
- };
106
-
107
- export const useAnnouncementBar = (): AnnouncementBarAPI => {
108
- const api = useContext(AnnouncementBarContext);
102
+ }
103
+
104
+ export function AnnouncementBarProvider({
105
+ children,
106
+ }: {
107
+ children: ReactNode;
108
+ }): JSX.Element {
109
+ const value = useContextValue();
110
+ return <Context.Provider value={value}>{children}</Context.Provider>;
111
+ }
112
+
113
+ export function useAnnouncementBar(): ContextValue {
114
+ const api = useContext(Context);
109
115
  if (!api) {
110
- throw new Error(
111
- 'useAnnouncementBar(): AnnouncementBar not found in React context: make sure to use the AnnouncementBarProvider on top of the tree',
112
- );
116
+ throw new ReactContextError('AnnouncementBarProvider');
113
117
  }
114
118
  return api;
115
- };
119
+ }
@@ -0,0 +1,198 @@
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 ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
18
+ import {ReactContextError} from '../utils/reactUtils';
19
+ import {createStorageSlot} from '../utils/storageUtils';
20
+ import {useThemeConfig} from '../utils/useThemeConfig';
21
+
22
+ type ContextValue = {
23
+ /** Current color mode. */
24
+ readonly colorMode: ColorMode;
25
+ /** Set new color mode. */
26
+ readonly setColorMode: (colorMode: ColorMode) => void;
27
+
28
+ // TODO legacy APIs kept for retro-compatibility: deprecate them
29
+ readonly isDarkTheme: boolean;
30
+ readonly setLightTheme: () => void;
31
+ readonly setDarkTheme: () => void;
32
+ };
33
+
34
+ const Context = React.createContext<ContextValue | undefined>(undefined);
35
+
36
+ const ColorModeStorageKey = 'theme';
37
+ const ColorModeStorage = createStorageSlot(ColorModeStorageKey);
38
+
39
+ const ColorModes = {
40
+ light: 'light',
41
+ dark: 'dark',
42
+ } as const;
43
+
44
+ export type ColorMode = typeof ColorModes[keyof typeof ColorModes];
45
+
46
+ // Ensure to always return a valid colorMode even if input is invalid
47
+ const coerceToColorMode = (colorMode?: string | null): ColorMode =>
48
+ colorMode === ColorModes.dark ? ColorModes.dark : ColorModes.light;
49
+
50
+ const getInitialColorMode = (defaultMode: ColorMode | undefined): ColorMode =>
51
+ ExecutionEnvironment.canUseDOM
52
+ ? coerceToColorMode(document.documentElement.getAttribute('data-theme'))
53
+ : coerceToColorMode(defaultMode);
54
+
55
+ const storeColorMode = (newColorMode: ColorMode) => {
56
+ ColorModeStorage.set(coerceToColorMode(newColorMode));
57
+ };
58
+
59
+ function useContextValue(): ContextValue {
60
+ const {
61
+ colorMode: {defaultMode, disableSwitch, respectPrefersColorScheme},
62
+ } = useThemeConfig();
63
+ const [colorMode, setColorModeState] = useState(
64
+ getInitialColorMode(defaultMode),
65
+ );
66
+
67
+ useEffect(() => {
68
+ // A site is deployed without disableSwitch
69
+ // => User visits the site and has a persisted value
70
+ // => Site later enabled disableSwitch
71
+ // => Clear the previously stored value to apply the site's setting
72
+ if (disableSwitch) {
73
+ ColorModeStorage.del();
74
+ }
75
+ }, [disableSwitch]);
76
+
77
+ const setColorMode = useCallback(
78
+ (newColorMode: ColorMode | null, options: {persist?: boolean} = {}) => {
79
+ const {persist = true} = options;
80
+ if (newColorMode) {
81
+ setColorModeState(newColorMode);
82
+ if (persist) {
83
+ storeColorMode(newColorMode);
84
+ }
85
+ } else {
86
+ if (respectPrefersColorScheme) {
87
+ setColorModeState(
88
+ window.matchMedia('(prefers-color-scheme: dark)').matches
89
+ ? ColorModes.dark
90
+ : ColorModes.light,
91
+ );
92
+ } else {
93
+ setColorModeState(defaultMode);
94
+ }
95
+ ColorModeStorage.del();
96
+ }
97
+ },
98
+ [respectPrefersColorScheme, defaultMode],
99
+ );
100
+
101
+ useEffect(() => {
102
+ document.documentElement.setAttribute(
103
+ 'data-theme',
104
+ coerceToColorMode(colorMode),
105
+ );
106
+ }, [colorMode]);
107
+
108
+ useEffect(() => {
109
+ if (disableSwitch) {
110
+ return undefined;
111
+ }
112
+ const onChange = (e: StorageEvent) => {
113
+ if (e.key !== ColorModeStorageKey) {
114
+ return;
115
+ }
116
+ const storedColorMode = ColorModeStorage.get();
117
+ if (storedColorMode !== null) {
118
+ setColorMode(coerceToColorMode(storedColorMode));
119
+ }
120
+ };
121
+ window.addEventListener('storage', onChange);
122
+ return () => window.removeEventListener('storage', onChange);
123
+ }, [disableSwitch, setColorMode]);
124
+
125
+ // PCS is coerced to light mode when printing, which causes the color mode to
126
+ // be reset to dark when exiting print mode, disregarding user settings. When
127
+ // the listener fires only because of a print/screen switch, we don't change
128
+ // color mode. See https://github.com/facebook/docusaurus/pull/6490
129
+ const previousMediaIsPrint = useRef(false);
130
+
131
+ useEffect(() => {
132
+ if (disableSwitch && !respectPrefersColorScheme) {
133
+ return undefined;
134
+ }
135
+ const mql = window.matchMedia('(prefers-color-scheme: dark)');
136
+ const onChange = () => {
137
+ if (window.matchMedia('print').matches || previousMediaIsPrint.current) {
138
+ previousMediaIsPrint.current = window.matchMedia('print').matches;
139
+ return;
140
+ }
141
+ setColorMode(null);
142
+ };
143
+ mql.addListener(onChange);
144
+ return () => mql.removeListener(onChange);
145
+ }, [setColorMode, disableSwitch, respectPrefersColorScheme]);
146
+
147
+ return useMemo(
148
+ () => ({
149
+ colorMode,
150
+ setColorMode,
151
+ get isDarkTheme() {
152
+ if (process.env.NODE_ENV === 'development') {
153
+ console.error(
154
+ '`useColorMode().isDarkTheme` is deprecated. Please use `useColorMode().colorMode === "dark"` instead.',
155
+ );
156
+ }
157
+ return colorMode === ColorModes.dark;
158
+ },
159
+ setLightTheme() {
160
+ if (process.env.NODE_ENV === 'development') {
161
+ console.error(
162
+ '`useColorMode().setLightTheme` is deprecated. Please use `useColorMode().setColorMode("light")` instead.',
163
+ );
164
+ }
165
+ setColorMode(ColorModes.light);
166
+ },
167
+ setDarkTheme() {
168
+ if (process.env.NODE_ENV === 'development') {
169
+ console.error(
170
+ '`useColorMode().setDarkTheme` is deprecated. Please use `useColorMode().setColorMode("dark")` instead.',
171
+ );
172
+ }
173
+ setColorMode(ColorModes.dark);
174
+ },
175
+ }),
176
+ [colorMode, setColorMode],
177
+ );
178
+ }
179
+
180
+ export function ColorModeProvider({
181
+ children,
182
+ }: {
183
+ children: ReactNode;
184
+ }): JSX.Element {
185
+ const value = useContextValue();
186
+ return <Context.Provider value={value}>{children}</Context.Provider>;
187
+ }
188
+
189
+ export function useColorMode(): ContextValue {
190
+ const context = useContext(Context);
191
+ if (context == null) {
192
+ throw new ReactContextError(
193
+ 'ColorModeProvider',
194
+ 'Please see https://docusaurus.io/docs/api/themes/configuration#use-color-mode.',
195
+ );
196
+ }
197
+ return context;
198
+ }
@@ -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,251 @@
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
+ useAllDocsData,
18
+ useDocsData,
19
+ type GlobalPluginData,
20
+ type GlobalVersion,
21
+ } from '@docusaurus/plugin-content-docs/client';
22
+ import {DEFAULT_PLUGIN_ID} from '@docusaurus/constants';
23
+ import {
24
+ useThemeConfig,
25
+ type DocsVersionPersistence,
26
+ } from '../utils/useThemeConfig';
27
+ import {isDocsPluginEnabled} from '../utils/docsUtils';
28
+ import {ReactContextError} from '../utils/reactUtils';
29
+ import {createStorageSlot} from '../utils/storageUtils';
30
+
31
+ const storageKey = (pluginId: string) => `docs-preferred-version-${pluginId}`;
32
+
33
+ const DocsPreferredVersionStorage = {
34
+ save: (
35
+ pluginId: string,
36
+ persistence: DocsVersionPersistence,
37
+ versionName: string,
38
+ ): void => {
39
+ createStorageSlot(storageKey(pluginId), {persistence}).set(versionName);
40
+ },
41
+
42
+ read: (
43
+ pluginId: string,
44
+ persistence: DocsVersionPersistence,
45
+ ): string | null =>
46
+ createStorageSlot(storageKey(pluginId), {persistence}).get(),
47
+
48
+ clear: (pluginId: string, persistence: DocsVersionPersistence): void => {
49
+ createStorageSlot(storageKey(pluginId), {persistence}).del();
50
+ },
51
+ };
52
+
53
+ type DocsPreferredVersionName = string | null;
54
+
55
+ /** State for a single docs plugin instance */
56
+ type DocsPreferredVersionPluginState = {
57
+ preferredVersionName: DocsPreferredVersionName;
58
+ };
59
+
60
+ /**
61
+ * We need to store the state in storage globally, with one preferred version
62
+ * per docs plugin instance.
63
+ */
64
+ type DocsPreferredVersionState = {
65
+ [pluginId: string]: DocsPreferredVersionPluginState;
66
+ };
67
+
68
+ /**
69
+ * Initial state is always null as we can't read local storage from node SSR
70
+ */
71
+ const getInitialState = (pluginIds: string[]): DocsPreferredVersionState =>
72
+ Object.fromEntries(pluginIds.map((id) => [id, {preferredVersionName: null}]));
73
+
74
+ /**
75
+ * Read storage for all docs plugins, assigning each doc plugin a preferred
76
+ * version (if found)
77
+ */
78
+ function readStorageState({
79
+ pluginIds,
80
+ versionPersistence,
81
+ allDocsData,
82
+ }: {
83
+ pluginIds: string[];
84
+ versionPersistence: DocsVersionPersistence;
85
+ allDocsData: {[pluginId: string]: GlobalPluginData};
86
+ }): DocsPreferredVersionState {
87
+ /**
88
+ * The storage value we read might be stale, and belong to a version that does
89
+ * not exist in the site anymore. In such case, we remove the storage value to
90
+ * avoid downstream errors.
91
+ */
92
+ function restorePluginState(
93
+ pluginId: string,
94
+ ): DocsPreferredVersionPluginState {
95
+ const preferredVersionNameUnsafe = DocsPreferredVersionStorage.read(
96
+ pluginId,
97
+ versionPersistence,
98
+ );
99
+ const pluginData = allDocsData[pluginId]!;
100
+ const versionExists = pluginData.versions.some(
101
+ (version) => version.name === preferredVersionNameUnsafe,
102
+ );
103
+ if (versionExists) {
104
+ return {preferredVersionName: preferredVersionNameUnsafe};
105
+ }
106
+ DocsPreferredVersionStorage.clear(pluginId, versionPersistence);
107
+ return {preferredVersionName: null};
108
+ }
109
+ return Object.fromEntries(
110
+ pluginIds.map((id) => [id, restorePluginState(id)]),
111
+ );
112
+ }
113
+
114
+ function useVersionPersistence(): DocsVersionPersistence {
115
+ return useThemeConfig().docs.versionPersistence;
116
+ }
117
+
118
+ type ContextValue = [
119
+ state: DocsPreferredVersionState,
120
+ api: {
121
+ savePreferredVersion: (pluginId: string, versionName: string) => void;
122
+ },
123
+ ];
124
+
125
+ const Context = React.createContext<ContextValue | null>(null);
126
+
127
+ function useContextValue(): ContextValue {
128
+ const allDocsData = useAllDocsData();
129
+ const versionPersistence = useVersionPersistence();
130
+ const pluginIds = useMemo(() => Object.keys(allDocsData), [allDocsData]);
131
+
132
+ // Initial state is empty, as we can't read browser storage in node/SSR
133
+ const [state, setState] = useState(() => getInitialState(pluginIds));
134
+
135
+ // On mount, we set the state read from browser storage
136
+ useEffect(() => {
137
+ setState(readStorageState({allDocsData, versionPersistence, pluginIds}));
138
+ }, [allDocsData, versionPersistence, pluginIds]);
139
+
140
+ // The API that we expose to consumer hooks (memo for constant object)
141
+ const api = useMemo(() => {
142
+ function savePreferredVersion(pluginId: string, versionName: string) {
143
+ DocsPreferredVersionStorage.save(
144
+ pluginId,
145
+ versionPersistence,
146
+ versionName,
147
+ );
148
+ setState((s) => ({
149
+ ...s,
150
+ [pluginId]: {preferredVersionName: versionName},
151
+ }));
152
+ }
153
+
154
+ return {
155
+ savePreferredVersion,
156
+ };
157
+ }, [versionPersistence]);
158
+
159
+ return [state, api];
160
+ }
161
+
162
+ function DocsPreferredVersionContextProviderUnsafe({
163
+ children,
164
+ }: {
165
+ children: ReactNode;
166
+ }): JSX.Element {
167
+ const value = useContextValue();
168
+ return <Context.Provider value={value}>{children}</Context.Provider>;
169
+ }
170
+
171
+ /**
172
+ * This is a maybe-layer. If the docs plugin is not enabled, this provider is a
173
+ * simple pass-through.
174
+ */
175
+ export function DocsPreferredVersionContextProvider({
176
+ children,
177
+ }: {
178
+ children: JSX.Element;
179
+ }): JSX.Element {
180
+ if (isDocsPluginEnabled) {
181
+ return (
182
+ <DocsPreferredVersionContextProviderUnsafe>
183
+ {children}
184
+ </DocsPreferredVersionContextProviderUnsafe>
185
+ );
186
+ }
187
+ return children;
188
+ }
189
+
190
+ function useDocsPreferredVersionContext(): ContextValue {
191
+ const value = useContext(Context);
192
+ if (!value) {
193
+ throw new ReactContextError('DocsPreferredVersionContextProvider');
194
+ }
195
+ return value;
196
+ }
197
+
198
+ /**
199
+ * Returns a read-write interface to a plugin's preferred version. The
200
+ * "preferred version" is defined as the last version that the user visited.
201
+ * For example, if a user is using v3, even when v4 is later published, the user
202
+ * would still be browsing v3 docs when she opens the website next time. Note,
203
+ * the `preferredVersion` attribute will always be `null` before mount.
204
+ */
205
+ export function useDocsPreferredVersion(
206
+ pluginId: string | undefined = DEFAULT_PLUGIN_ID,
207
+ ): {
208
+ preferredVersion: GlobalVersion | null;
209
+ savePreferredVersionName: (versionName: string) => void;
210
+ } {
211
+ const docsData = useDocsData(pluginId);
212
+ const [state, api] = useDocsPreferredVersionContext();
213
+
214
+ const {preferredVersionName} = state[pluginId]!;
215
+
216
+ const preferredVersion =
217
+ docsData.versions.find(
218
+ (version) => version.name === preferredVersionName,
219
+ ) ?? null;
220
+
221
+ const savePreferredVersionName = useCallback(
222
+ (versionName: string) => {
223
+ api.savePreferredVersion(pluginId, versionName);
224
+ },
225
+ [api, pluginId],
226
+ );
227
+
228
+ return {preferredVersion, savePreferredVersionName};
229
+ }
230
+
231
+ export function useDocsPreferredVersionByPluginId(): {
232
+ [pluginId: string]: GlobalVersion | null;
233
+ } {
234
+ const allDocsData = useAllDocsData();
235
+ const [state] = useDocsPreferredVersionContext();
236
+
237
+ function getPluginIdPreferredVersion(pluginId: string) {
238
+ const docsData = allDocsData[pluginId]!;
239
+ const {preferredVersionName} = state[pluginId]!;
240
+
241
+ return (
242
+ docsData.versions.find(
243
+ (version) => version.name === preferredVersionName,
244
+ ) ?? null
245
+ );
246
+ }
247
+ const pluginIds = Object.keys(allDocsData);
248
+ return Object.fromEntries(
249
+ pluginIds.map((id) => [id, getPluginIdPreferredVersion(id)]),
250
+ );
251
+ }
@@ -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 {ReactContextError} from '../utils/reactUtils';
10
+ import type {PropSidebar} from '@docusaurus/plugin-content-docs';
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
+ }