@docusaurus/theme-common 2.0.0-beta.13 → 2.0.0-beta.15

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 (197) hide show
  1. package/lib/components/Collapsible/index.d.ts +2 -1
  2. package/lib/components/Collapsible/index.d.ts.map +1 -0
  3. package/lib/components/Collapsible/index.js +2 -1
  4. package/lib/components/Collapsible/index.js.map +1 -0
  5. package/lib/components/Details/index.d.ts +2 -1
  6. package/lib/components/Details/index.d.ts.map +1 -0
  7. package/lib/components/Details/index.js +2 -1
  8. package/lib/components/Details/index.js.map +1 -0
  9. package/lib/components/Details/styles.module.css +4 -4
  10. package/lib/hooks/styles.css +10 -0
  11. package/lib/hooks/useHideableNavbar.d.ts +13 -0
  12. package/lib/hooks/useHideableNavbar.d.ts.map +1 -0
  13. package/lib/hooks/useHideableNavbar.js +59 -0
  14. package/lib/hooks/useHideableNavbar.js.map +1 -0
  15. package/lib/hooks/useKeyboardNavigation.d.ts +9 -0
  16. package/lib/hooks/useKeyboardNavigation.d.ts.map +1 -0
  17. package/lib/hooks/useKeyboardNavigation.js +31 -0
  18. package/lib/hooks/useKeyboardNavigation.js.map +1 -0
  19. package/lib/hooks/useLockBodyScroll.d.ts +8 -0
  20. package/lib/hooks/useLockBodyScroll.d.ts.map +1 -0
  21. package/lib/hooks/useLockBodyScroll.js +16 -0
  22. package/lib/hooks/useLockBodyScroll.js.map +1 -0
  23. package/lib/hooks/usePrismTheme.d.ts +9 -0
  24. package/lib/hooks/usePrismTheme.d.ts.map +1 -0
  25. package/lib/hooks/usePrismTheme.js +18 -0
  26. package/lib/hooks/usePrismTheme.js.map +1 -0
  27. package/lib/hooks/useSearchPage.d.ts +14 -0
  28. package/lib/hooks/useSearchPage.d.ts.map +1 -0
  29. package/lib/hooks/useSearchPage.js +43 -0
  30. package/lib/hooks/useSearchPage.js.map +1 -0
  31. package/lib/hooks/useWindowSize.d.ts +15 -0
  32. package/lib/hooks/useWindowSize.d.ts.map +1 -0
  33. package/lib/hooks/useWindowSize.js +54 -0
  34. package/lib/hooks/useWindowSize.js.map +1 -0
  35. package/lib/index.d.ts +11 -1
  36. package/lib/index.d.ts.map +1 -0
  37. package/lib/index.js +10 -0
  38. package/lib/index.js.map +1 -0
  39. package/lib/utils/ThemeClassNames.d.ts +3 -0
  40. package/lib/utils/ThemeClassNames.d.ts.map +1 -0
  41. package/lib/utils/ThemeClassNames.js +3 -0
  42. package/lib/utils/ThemeClassNames.js.map +1 -0
  43. package/lib/utils/announcementBarUtils.d.ts +2 -1
  44. package/lib/utils/announcementBarUtils.d.ts.map +1 -0
  45. package/lib/utils/announcementBarUtils.js +3 -1
  46. package/lib/utils/announcementBarUtils.js.map +1 -0
  47. package/lib/utils/codeBlockUtils.d.ts +3 -3
  48. package/lib/utils/codeBlockUtils.d.ts.map +1 -0
  49. package/lib/utils/codeBlockUtils.js +4 -1
  50. package/lib/utils/codeBlockUtils.js.map +1 -0
  51. package/lib/utils/colorModeUtils.d.ts +18 -0
  52. package/lib/utils/colorModeUtils.d.ts.map +1 -0
  53. package/lib/utils/colorModeUtils.js +84 -0
  54. package/lib/utils/colorModeUtils.js.map +1 -0
  55. package/lib/utils/docSidebarItemsExpandedState.d.ts +17 -0
  56. package/lib/utils/docSidebarItemsExpandedState.d.ts.map +1 -0
  57. package/lib/utils/docSidebarItemsExpandedState.js +22 -0
  58. package/lib/utils/docSidebarItemsExpandedState.js.map +1 -0
  59. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +1 -0
  60. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts.map +1 -0
  61. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +3 -2
  62. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js.map +1 -0
  63. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts +2 -1
  64. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts.map +1 -0
  65. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +1 -0
  66. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js.map +1 -0
  67. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +2 -1
  68. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts.map +1 -0
  69. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +2 -1
  70. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js.map +1 -0
  71. package/lib/utils/docsUtils.d.ts +3 -2
  72. package/lib/utils/docsUtils.d.ts.map +1 -0
  73. package/lib/utils/docsUtils.js +2 -1
  74. package/lib/utils/docsUtils.js.map +1 -0
  75. package/lib/utils/generalUtils.d.ts +1 -0
  76. package/lib/utils/generalUtils.d.ts.map +1 -0
  77. package/lib/utils/generalUtils.js +1 -0
  78. package/lib/utils/generalUtils.js.map +1 -0
  79. package/lib/utils/historyUtils.d.ts +1 -0
  80. package/lib/utils/historyUtils.d.ts.map +1 -0
  81. package/lib/utils/historyUtils.js +1 -0
  82. package/lib/utils/historyUtils.js.map +1 -0
  83. package/lib/utils/jsUtils.d.ts +1 -0
  84. package/lib/utils/jsUtils.d.ts.map +1 -0
  85. package/lib/utils/jsUtils.js +1 -0
  86. package/lib/utils/jsUtils.js.map +1 -0
  87. package/lib/utils/mobileSecondaryMenu.d.ts +2 -1
  88. package/lib/utils/mobileSecondaryMenu.d.ts.map +1 -0
  89. package/lib/utils/mobileSecondaryMenu.js +2 -1
  90. package/lib/utils/mobileSecondaryMenu.js.map +1 -0
  91. package/lib/utils/pathUtils.d.ts +1 -0
  92. package/lib/utils/pathUtils.d.ts.map +1 -0
  93. package/lib/utils/pathUtils.js +1 -0
  94. package/lib/utils/pathUtils.js.map +1 -0
  95. package/lib/utils/reactUtils.d.ts +1 -0
  96. package/lib/utils/reactUtils.d.ts.map +1 -0
  97. package/lib/utils/reactUtils.js +1 -0
  98. package/lib/utils/reactUtils.js.map +1 -0
  99. package/lib/utils/regexpUtils.d.ts +2 -1
  100. package/lib/utils/regexpUtils.d.ts.map +1 -0
  101. package/lib/utils/regexpUtils.js +2 -1
  102. package/lib/utils/regexpUtils.js.map +1 -0
  103. package/lib/utils/scrollUtils.d.ts +2 -1
  104. package/lib/utils/scrollUtils.d.ts.map +1 -0
  105. package/lib/utils/scrollUtils.js +1 -0
  106. package/lib/utils/scrollUtils.js.map +1 -0
  107. package/lib/utils/searchUtils.d.ts +1 -0
  108. package/lib/utils/searchUtils.d.ts.map +1 -0
  109. package/lib/utils/searchUtils.js +1 -0
  110. package/lib/utils/searchUtils.js.map +1 -0
  111. package/lib/utils/storageUtils.d.ts +1 -0
  112. package/lib/utils/storageUtils.d.ts.map +1 -0
  113. package/lib/utils/storageUtils.js +1 -0
  114. package/lib/utils/storageUtils.js.map +1 -0
  115. package/lib/utils/tabGroupChoiceUtils.d.ts +19 -0
  116. package/lib/utils/tabGroupChoiceUtils.d.ts.map +1 -0
  117. package/lib/utils/tabGroupChoiceUtils.js +54 -0
  118. package/lib/utils/tabGroupChoiceUtils.js.map +1 -0
  119. package/lib/utils/tagsUtils.d.ts +1 -0
  120. package/lib/utils/tagsUtils.d.ts.map +1 -0
  121. package/lib/utils/tagsUtils.js +1 -0
  122. package/lib/utils/tagsUtils.js.map +1 -0
  123. package/lib/utils/tocUtils.d.ts +2 -1
  124. package/lib/utils/tocUtils.d.ts.map +1 -0
  125. package/lib/utils/tocUtils.js +1 -0
  126. package/lib/utils/tocUtils.js.map +1 -0
  127. package/lib/utils/useAlternatePageUtils.d.ts +1 -0
  128. package/lib/utils/useAlternatePageUtils.d.ts.map +1 -0
  129. package/lib/utils/useAlternatePageUtils.js +1 -0
  130. package/lib/utils/useAlternatePageUtils.js.map +1 -0
  131. package/lib/utils/useContextualSearchFilters.d.ts +1 -0
  132. package/lib/utils/useContextualSearchFilters.d.ts.map +1 -0
  133. package/lib/utils/useContextualSearchFilters.js +2 -1
  134. package/lib/utils/useContextualSearchFilters.js.map +1 -0
  135. package/lib/utils/useLocalPathname.d.ts +1 -0
  136. package/lib/utils/useLocalPathname.d.ts.map +1 -0
  137. package/lib/utils/useLocalPathname.js +1 -0
  138. package/lib/utils/useLocalPathname.js.map +1 -0
  139. package/lib/utils/useLocationChange.d.ts +2 -1
  140. package/lib/utils/useLocationChange.d.ts.map +1 -0
  141. package/lib/utils/useLocationChange.js +1 -0
  142. package/lib/utils/useLocationChange.js.map +1 -0
  143. package/lib/utils/usePluralForm.d.ts +1 -0
  144. package/lib/utils/usePluralForm.d.ts.map +1 -0
  145. package/lib/utils/usePluralForm.js +1 -0
  146. package/lib/utils/usePluralForm.js.map +1 -0
  147. package/lib/utils/usePrevious.d.ts +1 -0
  148. package/lib/utils/usePrevious.d.ts.map +1 -0
  149. package/lib/utils/usePrevious.js +1 -0
  150. package/lib/utils/usePrevious.js.map +1 -0
  151. package/lib/utils/useTOCHighlight.d.ts +1 -0
  152. package/lib/utils/useTOCHighlight.d.ts.map +1 -0
  153. package/lib/utils/useTOCHighlight.js +2 -0
  154. package/lib/utils/useTOCHighlight.js.map +1 -0
  155. package/lib/utils/useThemeConfig.d.ts +16 -9
  156. package/lib/utils/useThemeConfig.d.ts.map +1 -0
  157. package/lib/utils/useThemeConfig.js +1 -0
  158. package/lib/utils/useThemeConfig.js.map +1 -0
  159. package/package.json +11 -11
  160. package/src/components/Collapsible/index.tsx +5 -5
  161. package/src/components/Details/index.tsx +6 -1
  162. package/src/components/Details/styles.module.css +4 -4
  163. package/src/hooks/styles.css +10 -0
  164. package/src/hooks/useHideableNavbar.ts +77 -0
  165. package/src/hooks/useKeyboardNavigation.ts +37 -0
  166. package/src/hooks/useLockBodyScroll.ts +18 -0
  167. package/src/hooks/usePrismTheme.ts +20 -0
  168. package/src/hooks/useSearchPage.ts +66 -0
  169. package/src/hooks/useWindowSize.ts +68 -0
  170. package/src/index.ts +19 -1
  171. package/src/utils/ThemeClassNames.ts +2 -0
  172. package/src/utils/announcementBarUtils.tsx +3 -2
  173. package/src/utils/codeBlockUtils.ts +4 -5
  174. package/src/utils/colorModeUtils.tsx +135 -0
  175. package/src/utils/docSidebarItemsExpandedState.tsx +41 -0
  176. package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +7 -4
  177. package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +1 -1
  178. package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +5 -1
  179. package/src/utils/docsUtils.tsx +3 -3
  180. package/src/utils/mobileSecondaryMenu.tsx +3 -3
  181. package/src/utils/regexpUtils.ts +1 -1
  182. package/src/utils/scrollUtils.tsx +1 -1
  183. package/src/utils/tabGroupChoiceUtils.tsx +90 -0
  184. package/src/utils/tocUtils.ts +1 -1
  185. package/src/utils/useContextualSearchFilters.ts +4 -1
  186. package/src/utils/useLocationChange.ts +1 -1
  187. package/src/utils/useTOCHighlight.ts +1 -0
  188. package/src/utils/useThemeConfig.ts +19 -9
  189. package/copyUntypedFiles.js +0 -20
  190. package/lib/.tsbuildinfo +0 -1
  191. package/src/utils/__tests__/codeBlockUtils.test.ts +0 -54
  192. package/src/utils/__tests__/docsUtils.test.tsx +0 -331
  193. package/src/utils/__tests__/jsUtils.test.ts +0 -33
  194. package/src/utils/__tests__/pathUtils.test.ts +0 -32
  195. package/src/utils/__tests__/tagUtils.test.ts +0 -66
  196. package/src/utils/__tests__/tocUtils.test.ts +0 -197
  197. package/tsconfig.json +0 -10
@@ -0,0 +1,77 @@
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 {useState, useCallback, useRef} from 'react';
9
+ import {useLocationChange} from '../utils/useLocationChange';
10
+ import {useScrollPosition} from '../utils/scrollUtils';
11
+
12
+ type UseHideableNavbarReturns = {
13
+ readonly navbarRef: (node: HTMLElement | null) => void;
14
+ readonly isNavbarVisible: boolean;
15
+ };
16
+
17
+ export default function useHideableNavbar(
18
+ hideOnScroll: boolean,
19
+ ): UseHideableNavbarReturns {
20
+ const [isNavbarVisible, setIsNavbarVisible] = useState(hideOnScroll);
21
+ const isFocusedAnchor = useRef(false);
22
+ const navbarHeight = useRef(0);
23
+ const navbarRef = useCallback((node: HTMLElement | null) => {
24
+ if (node !== null) {
25
+ navbarHeight.current = node.getBoundingClientRect().height;
26
+ }
27
+ }, []);
28
+
29
+ useScrollPosition((currentPosition, lastPosition) => {
30
+ if (!hideOnScroll) {
31
+ return;
32
+ }
33
+
34
+ const scrollTop = currentPosition.scrollY;
35
+
36
+ // It needed for mostly to handle rubber band scrolling
37
+ if (scrollTop < navbarHeight.current) {
38
+ setIsNavbarVisible(true);
39
+ return;
40
+ }
41
+
42
+ if (isFocusedAnchor.current) {
43
+ isFocusedAnchor.current = false;
44
+ return;
45
+ }
46
+
47
+ const lastScrollTop = lastPosition?.scrollY;
48
+ const documentHeight =
49
+ document.documentElement.scrollHeight - navbarHeight.current;
50
+ const windowHeight = window.innerHeight;
51
+
52
+ if (lastScrollTop && scrollTop >= lastScrollTop) {
53
+ setIsNavbarVisible(false);
54
+ } else if (scrollTop + windowHeight < documentHeight) {
55
+ setIsNavbarVisible(true);
56
+ }
57
+ });
58
+
59
+ useLocationChange((locationChangeEvent) => {
60
+ if (!hideOnScroll) {
61
+ return;
62
+ }
63
+
64
+ if (locationChangeEvent.location.hash) {
65
+ isFocusedAnchor.current = true;
66
+ setIsNavbarVisible(false);
67
+ return;
68
+ }
69
+
70
+ setIsNavbarVisible(true);
71
+ });
72
+
73
+ return {
74
+ navbarRef,
75
+ isNavbarVisible,
76
+ };
77
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import {useEffect} from 'react';
9
+
10
+ import './styles.css';
11
+
12
+ // This hook detect keyboard focus indicator to not show outline for mouse users
13
+ // Inspired by https://hackernoon.com/removing-that-ugly-focus-ring-and-keeping-it-too-6c8727fefcd2
14
+ export default function useKeyboardNavigation(): void {
15
+ useEffect(() => {
16
+ const keyboardFocusedClassName = 'navigation-with-keyboard';
17
+
18
+ function handleOutlineStyles(e: MouseEvent | KeyboardEvent) {
19
+ if (e.type === 'keydown' && (e as KeyboardEvent).key === 'Tab') {
20
+ document.body.classList.add(keyboardFocusedClassName);
21
+ }
22
+
23
+ if (e.type === 'mousedown') {
24
+ document.body.classList.remove(keyboardFocusedClassName);
25
+ }
26
+ }
27
+
28
+ document.addEventListener('keydown', handleOutlineStyles);
29
+ document.addEventListener('mousedown', handleOutlineStyles);
30
+
31
+ return () => {
32
+ document.body.classList.remove(keyboardFocusedClassName);
33
+ document.removeEventListener('keydown', handleOutlineStyles);
34
+ document.removeEventListener('mousedown', handleOutlineStyles);
35
+ };
36
+ }, []);
37
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import {useEffect} from 'react';
9
+
10
+ export default function useLockBodyScroll(lock: boolean = true): void {
11
+ useEffect(() => {
12
+ document.body.style.overflow = lock ? 'hidden' : 'visible';
13
+
14
+ return () => {
15
+ document.body.style.overflow = 'visible';
16
+ };
17
+ }, [lock]);
18
+ }
@@ -0,0 +1,20 @@
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 defaultTheme from 'prism-react-renderer/themes/palenight';
9
+ import {useColorMode} from '../utils/colorModeUtils';
10
+ import {useThemeConfig} from '../utils/useThemeConfig';
11
+
12
+ export default function usePrismTheme(): typeof defaultTheme {
13
+ const {prism} = useThemeConfig();
14
+ const {isDarkTheme} = useColorMode();
15
+ const lightModeTheme = prism.theme || defaultTheme;
16
+ const darkModeTheme = prism.darkTheme || lightModeTheme;
17
+ const prismTheme = isDarkTheme ? darkModeTheme : lightModeTheme;
18
+
19
+ return prismTheme;
20
+ }
@@ -0,0 +1,66 @@
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 {useHistory} from '@docusaurus/router';
9
+ import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
10
+ import {useCallback, useEffect, useState} from 'react';
11
+
12
+ const SEARCH_PARAM_QUERY = 'q';
13
+
14
+ interface UseSearchPageReturn {
15
+ searchQuery: string;
16
+ setSearchQuery: (newSearchQuery: string) => void;
17
+ generateSearchPageLink: (targetSearchQuery: string) => string;
18
+ }
19
+
20
+ export default function useSearchPage(): UseSearchPageReturn {
21
+ const history = useHistory();
22
+ const {
23
+ siteConfig: {baseUrl},
24
+ } = useDocusaurusContext();
25
+
26
+ const [searchQuery, setSearchQueryState] = useState('');
27
+
28
+ // Init search query just after React hydration
29
+ useEffect(() => {
30
+ const searchQueryStringValue =
31
+ new URLSearchParams(window.location.search).get(SEARCH_PARAM_QUERY) ?? '';
32
+
33
+ setSearchQueryState(searchQueryStringValue);
34
+ }, []);
35
+
36
+ const setSearchQuery = useCallback(
37
+ (newSearchQuery: string) => {
38
+ const searchParams = new URLSearchParams(window.location.search);
39
+
40
+ if (newSearchQuery) {
41
+ searchParams.set(SEARCH_PARAM_QUERY, newSearchQuery);
42
+ } else {
43
+ searchParams.delete(SEARCH_PARAM_QUERY);
44
+ }
45
+
46
+ history.replace({
47
+ search: searchParams.toString(),
48
+ });
49
+ setSearchQueryState(newSearchQuery);
50
+ },
51
+ [history],
52
+ );
53
+
54
+ const generateSearchPageLink = useCallback(
55
+ (targetSearchQuery: string) =>
56
+ // Refer to https://github.com/facebook/docusaurus/pull/2838
57
+ `${baseUrl}search?q=${encodeURIComponent(targetSearchQuery)}`,
58
+ [baseUrl],
59
+ );
60
+
61
+ return {
62
+ searchQuery,
63
+ setSearchQuery,
64
+ generateSearchPageLink,
65
+ };
66
+ }
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import {useEffect, useState} from 'react';
9
+
10
+ import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
11
+
12
+ const windowSizes = {
13
+ desktop: 'desktop',
14
+ mobile: 'mobile',
15
+
16
+ // This "ssr" value is very important to handle hydration FOUC / layout shifts
17
+ // You have to handle server-rendering explicitly on the call-site
18
+ // On the server, you may need to render BOTH the mobile/desktop elements (and hide one of them with mediaquery)
19
+ // We don't return "undefined" on purpose, to make it more explicit
20
+ ssr: 'ssr',
21
+ } as const;
22
+
23
+ type WindowSize = keyof typeof windowSizes;
24
+
25
+ const DesktopThresholdWidth = 996;
26
+
27
+ function getWindowSize() {
28
+ if (!ExecutionEnvironment.canUseDOM) {
29
+ return windowSizes.ssr;
30
+ }
31
+ return window.innerWidth > DesktopThresholdWidth
32
+ ? windowSizes.desktop
33
+ : windowSizes.mobile;
34
+ }
35
+
36
+ // Simulate the SSR window size in dev, so that potential hydration FOUC/layout shift problems can be seen in dev too!
37
+ const DevSimulateSSR = process.env.NODE_ENV === 'development' && true;
38
+
39
+ // This hook returns an enum value on purpose!
40
+ // We don't want it to return the actual width value, for resize perf reasons
41
+ // We only want to re-render once a breakpoint is crossed
42
+ export default function useWindowSize(): WindowSize {
43
+ const [windowSize, setWindowSize] = useState<WindowSize>(() => {
44
+ if (DevSimulateSSR) {
45
+ return 'ssr';
46
+ }
47
+ return getWindowSize();
48
+ });
49
+
50
+ useEffect(() => {
51
+ function updateWindowSize() {
52
+ setWindowSize(getWindowSize());
53
+ }
54
+
55
+ const timeout = DevSimulateSSR
56
+ ? window.setTimeout(updateWindowSize, 1000)
57
+ : undefined;
58
+
59
+ window.addEventListener('resize', updateWindowSize);
60
+
61
+ return () => {
62
+ window.removeEventListener('resize', updateWindowSize);
63
+ clearTimeout(timeout);
64
+ };
65
+ }, []);
66
+
67
+ return windowSize;
68
+ }
package/src/index.ts CHANGED
@@ -6,6 +6,10 @@
6
6
  */
7
7
 
8
8
  export {useThemeConfig} from './utils/useThemeConfig';
9
+ export {
10
+ DocSidebarItemsExpandedStateProvider,
11
+ useDocSidebarItemsExpandedState,
12
+ } from './utils/docSidebarItemsExpandedState';
9
13
 
10
14
  export type {
11
15
  ThemeConfig,
@@ -13,8 +17,9 @@ export type {
13
17
  Navbar,
14
18
  NavbarItem,
15
19
  NavbarLogo,
20
+ MultiColumnFooter,
21
+ SimpleFooter,
16
22
  Footer,
17
- FooterLinks,
18
23
  FooterLinkItem,
19
24
  ColorModeConfig,
20
25
  } from './utils/useThemeConfig';
@@ -113,3 +118,16 @@ export {
113
118
  } from './utils/reactUtils';
114
119
 
115
120
  export {isRegexpStringMatch} from './utils/regexpUtils';
121
+
122
+ export {useColorMode, ColorModeProvider} from './utils/colorModeUtils';
123
+ export {
124
+ useTabGroupChoice,
125
+ TabGroupChoiceProvider,
126
+ } from './utils/tabGroupChoiceUtils';
127
+
128
+ export {default as useHideableNavbar} from './hooks/useHideableNavbar';
129
+ export {default as useKeyboardNavigation} from './hooks/useKeyboardNavigation';
130
+ export {default as usePrismTheme} from './hooks/usePrismTheme';
131
+ export {default as useLockBodyScroll} from './hooks/useLockBodyScroll';
132
+ export {default as useWindowSize} from './hooks/useWindowSize';
133
+ export {default as useSearchPage} from './hooks/useSearchPage';
@@ -34,6 +34,7 @@ export const ThemeClassNames = {
34
34
  editThisPage: 'theme-edit-this-page',
35
35
  lastUpdated: 'theme-last-updated',
36
36
  backToTopButton: 'theme-back-to-top-button',
37
+ codeBlock: 'theme-code-block',
37
38
  },
38
39
  layout: {
39
40
  // TODO add other stable classNames here
@@ -47,6 +48,7 @@ export const ThemeClassNames = {
47
48
  docFooter: 'theme-doc-footer',
48
49
  docFooterTagsRow: 'theme-doc-footer-tags-row',
49
50
  docFooterEditMetaRow: 'theme-doc-footer-edit-meta-row',
51
+ docSidebarContainer: 'theme-doc-sidebar-container',
50
52
  docSidebarMenu: 'theme-doc-sidebar-menu',
51
53
  docSidebarItemCategory: 'theme-doc-sidebar-item-category',
52
54
  docSidebarItemLink: 'theme-doc-sidebar-item-link',
@@ -10,7 +10,7 @@ import React, {
10
10
  useEffect,
11
11
  useCallback,
12
12
  useMemo,
13
- ReactNode,
13
+ type ReactNode,
14
14
  useContext,
15
15
  createContext,
16
16
  } from 'react';
@@ -43,7 +43,7 @@ const useAnnouncementBarContextValue = (): AnnouncementBarAPI => {
43
43
 
44
44
  const [isClosed, setClosed] = useState(() =>
45
45
  isBrowser
46
- ? // On client navigation: init with localstorage value
46
+ ? // On client navigation: init with local storage value
47
47
  isDismissedInStorage()
48
48
  : // On server/hydration: always visible to prevent layout shifts (will be hidden with css if needed)
49
49
  false,
@@ -68,6 +68,7 @@ const useAnnouncementBarContextValue = (): AnnouncementBarAPI => {
68
68
 
69
69
  // retrocompatibility due to spelling mistake of default id
70
70
  // see https://github.com/facebook/docusaurus/issues/3338
71
+ // cSpell:ignore annoucement
71
72
  if (viewedId === 'annoucement-bar') {
72
73
  viewedId = 'announcement-bar';
73
74
  }
@@ -6,7 +6,6 @@
6
6
  */
7
7
 
8
8
  import rangeParser from 'parse-numeric-range';
9
- import type {Language} from 'prism-react-renderer';
10
9
 
11
10
  const codeBlockTitleRegex = /title=(["'])(.*?)\1/;
12
11
  const highlightLinesRangeRegex = /{([\d,-]+)}/;
@@ -93,11 +92,11 @@ export function parseCodeBlockTitle(metastring?: string): string {
93
92
  return metastring?.match(codeBlockTitleRegex)?.[2] ?? '';
94
93
  }
95
94
 
96
- export function parseLanguage(className?: string): Language | undefined {
95
+ export function parseLanguage(className: string): string | undefined {
97
96
  const languageClassName = className
98
- ?.split(' ')
97
+ .split(' ')
99
98
  .find((str) => str.startsWith('language-'));
100
- return languageClassName?.replace(/language-/, '') as Language | undefined;
99
+ return languageClassName?.replace(/language-/, '');
101
100
  }
102
101
 
103
102
  /**
@@ -107,7 +106,7 @@ export function parseLanguage(className?: string): Language | undefined {
107
106
  export function parseLines(
108
107
  content: string,
109
108
  metastring?: string,
110
- language?: Language,
109
+ language?: string,
111
110
  ): {
112
111
  highlightLines: number[];
113
112
  code: string;
@@ -0,0 +1,135 @@
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 {ReactNode} from 'react';
9
+ import React, {
10
+ useState,
11
+ useCallback,
12
+ useEffect,
13
+ useContext,
14
+ useMemo,
15
+ } from 'react';
16
+
17
+ import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
18
+ import {createStorageSlot} from './storageUtils';
19
+ import {useThemeConfig} from './useThemeConfig';
20
+
21
+ type ColorModeContextValue = {
22
+ readonly isDarkTheme: boolean;
23
+ readonly setLightTheme: () => void;
24
+ readonly setDarkTheme: () => void;
25
+ };
26
+
27
+ const ThemeStorage = createStorageSlot('theme');
28
+
29
+ const themes = {
30
+ light: 'light',
31
+ dark: 'dark',
32
+ } as const;
33
+
34
+ type Themes = typeof themes[keyof typeof themes];
35
+
36
+ // Ensure to always return a valid theme even if input is invalid
37
+ const coerceToTheme = (theme?: string | null): Themes =>
38
+ theme === themes.dark ? themes.dark : themes.light;
39
+
40
+ const getInitialTheme = (defaultMode: Themes | undefined): Themes => {
41
+ if (!ExecutionEnvironment.canUseDOM) {
42
+ return coerceToTheme(defaultMode);
43
+ }
44
+ return coerceToTheme(document.documentElement.getAttribute('data-theme'));
45
+ };
46
+
47
+ const storeTheme = (newTheme: Themes) => {
48
+ createStorageSlot('theme').set(coerceToTheme(newTheme));
49
+ };
50
+
51
+ function useColorModeContextValue(): ColorModeContextValue {
52
+ const {
53
+ colorMode: {defaultMode, disableSwitch, respectPrefersColorScheme},
54
+ } = useThemeConfig();
55
+ const [theme, setTheme] = useState(getInitialTheme(defaultMode));
56
+
57
+ const setLightTheme = useCallback(() => {
58
+ setTheme(themes.light);
59
+ storeTheme(themes.light);
60
+ }, []);
61
+ const setDarkTheme = useCallback(() => {
62
+ setTheme(themes.dark);
63
+ storeTheme(themes.dark);
64
+ }, []);
65
+
66
+ useEffect(() => {
67
+ document.documentElement.setAttribute('data-theme', coerceToTheme(theme));
68
+ }, [theme]);
69
+
70
+ useEffect(() => {
71
+ if (disableSwitch) {
72
+ return;
73
+ }
74
+
75
+ try {
76
+ const storedTheme = ThemeStorage.get();
77
+ if (storedTheme !== null) {
78
+ setTheme(coerceToTheme(storedTheme));
79
+ }
80
+ } catch (err) {
81
+ console.error(err);
82
+ }
83
+ }, [disableSwitch, setTheme]);
84
+
85
+ useEffect(() => {
86
+ if (disableSwitch && !respectPrefersColorScheme) {
87
+ return;
88
+ }
89
+
90
+ window
91
+ .matchMedia('(prefers-color-scheme: dark)')
92
+ .addListener(({matches}) => {
93
+ setTheme(matches ? themes.dark : themes.light);
94
+ });
95
+ }, [disableSwitch, respectPrefersColorScheme]);
96
+
97
+ return {
98
+ isDarkTheme: theme === themes.dark,
99
+ setLightTheme,
100
+ setDarkTheme,
101
+ };
102
+ }
103
+
104
+ const ColorModeContext = React.createContext<ColorModeContextValue | undefined>(
105
+ undefined,
106
+ );
107
+
108
+ export function ColorModeProvider({
109
+ children,
110
+ }: {
111
+ children: ReactNode;
112
+ }): JSX.Element {
113
+ const {isDarkTheme, setLightTheme, setDarkTheme} = useColorModeContextValue();
114
+ const contextValue = useMemo(
115
+ () => ({isDarkTheme, setLightTheme, setDarkTheme}),
116
+ [isDarkTheme, setLightTheme, setDarkTheme],
117
+ );
118
+ return (
119
+ <ColorModeContext.Provider value={contextValue}>
120
+ {children}
121
+ </ColorModeContext.Provider>
122
+ );
123
+ }
124
+
125
+ export function useColorMode(): ColorModeContextValue {
126
+ const context = useContext<ColorModeContextValue | undefined>(
127
+ ColorModeContext,
128
+ );
129
+ if (context == null) {
130
+ throw new Error(
131
+ '"useColorMode()" is used outside of "Layout" component. Please see https://docusaurus.io/docs/api/themes/configuration#use-color-mode.',
132
+ );
133
+ }
134
+ return context;
135
+ }
@@ -0,0 +1,41 @@
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
+
10
+ const EmptyContext: unique symbol = Symbol('EmptyContext');
11
+ const Context = React.createContext<
12
+ DocSidebarItemsExpandedState | typeof EmptyContext
13
+ >(EmptyContext);
14
+ type DocSidebarItemsExpandedState = {
15
+ expandedItem: number | null;
16
+ setExpandedItem: (a: number | null) => void;
17
+ };
18
+
19
+ export function DocSidebarItemsExpandedStateProvider({
20
+ children,
21
+ }: {
22
+ children: ReactNode;
23
+ }): JSX.Element {
24
+ const [expandedItem, setExpandedItem] = useState<number | null>(null);
25
+ const contextValue = useMemo(
26
+ () => ({expandedItem, setExpandedItem}),
27
+ [expandedItem],
28
+ );
29
+
30
+ return <Context.Provider value={contextValue}>{children}</Context.Provider>;
31
+ }
32
+
33
+ export function useDocSidebarItemsExpandedState(): DocSidebarItemsExpandedState {
34
+ const contextValue = useContext(Context);
35
+ if (contextValue === EmptyContext) {
36
+ throw new Error(
37
+ 'This hook requires usage of <DocSidebarItemsExpandedStateProvider>',
38
+ );
39
+ }
40
+ return contextValue;
41
+ }
@@ -7,16 +7,19 @@
7
7
 
8
8
  import React, {
9
9
  createContext,
10
- ReactNode,
10
+ type ReactNode,
11
11
  useContext,
12
12
  useEffect,
13
13
  useMemo,
14
14
  useState,
15
15
  } from 'react';
16
- import {useThemeConfig, DocsVersionPersistence} from '../useThemeConfig';
16
+ import {useThemeConfig, type DocsVersionPersistence} from '../useThemeConfig';
17
17
  import {isDocsPluginEnabled} from '../docsUtils';
18
18
 
19
- import {useAllDocsData, GlobalPluginData} from '@theme/hooks/useDocs';
19
+ import {
20
+ useAllDocsData,
21
+ type GlobalPluginData,
22
+ } from '@docusaurus/plugin-content-docs/client';
20
23
 
21
24
  import DocsPreferredVersionStorage from './DocsPreferredVersionStorage';
22
25
 
@@ -35,7 +38,7 @@ type DocsPreferredVersionState = Record<
35
38
  DocsPreferredVersionPluginState
36
39
  >;
37
40
 
38
- // Initial state is always null as we can't read localstorage from node SSR
41
+ // Initial state is always null as we can't read local storage from node SSR
39
42
  function getInitialState(pluginIds: string[]): DocsPreferredVersionState {
40
43
  const initialState: DocsPreferredVersionState = {};
41
44
  pluginIds.forEach((pluginId) => {
@@ -6,7 +6,7 @@
6
6
  */
7
7
 
8
8
  import {createStorageSlot} from '../storageUtils';
9
- import {DocsVersionPersistence} from '../useThemeConfig';
9
+ import type {DocsVersionPersistence} from '../useThemeConfig';
10
10
 
11
11
  const storageKey = (pluginId: string) => `docs-preferred-version-${pluginId}`;
12
12
 
@@ -7,7 +7,11 @@
7
7
 
8
8
  import {useCallback} from 'react';
9
9
  import {useDocsPreferredVersionContext} from './DocsPreferredVersionProvider';
10
- import {useAllDocsData, useDocsData, GlobalVersion} from '@theme/hooks/useDocs';
10
+ import {
11
+ useAllDocsData,
12
+ useDocsData,
13
+ type GlobalVersion,
14
+ } from '@docusaurus/plugin-content-docs/client';
11
15
 
12
16
  import {DEFAULT_PLUGIN_ID} from '@docusaurus/constants';
13
17
 
@@ -5,9 +5,9 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import React, {createContext, ReactNode, useContext} from 'react';
9
- import {useAllDocsData} from '@theme/hooks/useDocs';
10
- import {
8
+ import React, {createContext, type ReactNode, useContext} from 'react';
9
+ import {useAllDocsData} from '@docusaurus/plugin-content-docs/client';
10
+ import type {
11
11
  PropSidebar,
12
12
  PropSidebarItem,
13
13
  PropSidebarItemCategory,
@@ -7,11 +7,11 @@
7
7
 
8
8
  import React, {
9
9
  useState,
10
- ReactNode,
10
+ type ReactNode,
11
11
  useContext,
12
12
  createContext,
13
13
  useEffect,
14
- ComponentType,
14
+ type ComponentType,
15
15
  useMemo,
16
16
  } from 'react';
17
17
 
@@ -104,7 +104,7 @@ export function MobileSecondaryMenuFiller<
104
104
  const memoizedProps = useShallowMemoizedObject(props);
105
105
 
106
106
  useEffect(() => {
107
- // @ts-expect-error: context is not 100% typesafe but it's ok
107
+ // @ts-expect-error: context is not 100% type-safe but it's ok
108
108
  setState({component, props: memoizedProps});
109
109
  }, [setState, component, memoizedProps]);
110
110
 
@@ -6,7 +6,7 @@
6
6
  */
7
7
 
8
8
  /**
9
- * Utility to convert an optional string into a Regex case sensitive and global
9
+ * Utility to convert an optional string into a Regex case insensitive and global
10
10
  */
11
11
  export function isRegexpStringMatch(
12
12
  regexAsString?: string,