@docusaurus/theme-common 2.0.0-beta.fc64c12e4 → 2.0.0

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 (274) hide show
  1. package/lib/components/Collapsible/index.d.ts +35 -6
  2. package/lib/components/Collapsible/index.d.ts.map +1 -0
  3. package/lib/components/Collapsible/index.js +33 -30
  4. package/lib/components/Collapsible/index.js.map +1 -0
  5. package/lib/components/Details/index.d.ts +8 -3
  6. package/lib/components/Details/index.d.ts.map +1 -0
  7. package/lib/components/Details/index.js +31 -19
  8. package/lib/components/Details/index.js.map +1 -0
  9. package/lib/components/Details/styles.module.css +14 -11
  10. package/lib/contexts/announcementBar.d.ts +22 -0
  11. package/lib/contexts/announcementBar.d.ts.map +1 -0
  12. package/lib/{utils/announcementBarUtils.js → contexts/announcementBar.js} +30 -31
  13. package/lib/contexts/announcementBar.js.map +1 -0
  14. package/lib/contexts/blogPost.d.ts +33 -0
  15. package/lib/contexts/blogPost.d.ts.map +1 -0
  16. package/lib/contexts/blogPost.js +46 -0
  17. package/lib/contexts/blogPost.js.map +1 -0
  18. package/lib/contexts/colorMode.d.ts +27 -0
  19. package/lib/contexts/colorMode.d.ts.map +1 -0
  20. package/lib/contexts/colorMode.js +132 -0
  21. package/lib/contexts/colorMode.js.map +1 -0
  22. package/lib/contexts/doc.d.ts +30 -0
  23. package/lib/contexts/doc.d.ts.map +1 -0
  24. package/lib/contexts/doc.js +48 -0
  25. package/lib/contexts/doc.js.map +1 -0
  26. package/lib/contexts/docSidebarItemsExpandedState.d.ts +31 -0
  27. package/lib/contexts/docSidebarItemsExpandedState.d.ts.map +1 -0
  28. package/lib/contexts/docSidebarItemsExpandedState.js +28 -0
  29. package/lib/contexts/docSidebarItemsExpandedState.js.map +1 -0
  30. package/lib/contexts/docsPreferredVersion.d.ts +30 -0
  31. package/lib/contexts/docsPreferredVersion.d.ts.map +1 -0
  32. package/lib/contexts/docsPreferredVersion.js +130 -0
  33. package/lib/contexts/docsPreferredVersion.js.map +1 -0
  34. package/lib/contexts/docsSidebar.d.ts +26 -0
  35. package/lib/contexts/docsSidebar.d.ts.map +1 -0
  36. package/lib/contexts/docsSidebar.js +30 -0
  37. package/lib/contexts/docsSidebar.js.map +1 -0
  38. package/lib/contexts/docsVersion.d.ts +20 -0
  39. package/lib/contexts/docsVersion.d.ts.map +1 -0
  40. package/lib/contexts/docsVersion.js +26 -0
  41. package/lib/contexts/docsVersion.js.map +1 -0
  42. package/lib/contexts/navbarMobileSidebar.d.ts +31 -0
  43. package/lib/contexts/navbarMobileSidebar.d.ts.map +1 -0
  44. package/lib/contexts/navbarMobileSidebar.js +56 -0
  45. package/lib/contexts/navbarMobileSidebar.js.map +1 -0
  46. package/lib/contexts/navbarSecondaryMenu/content.d.ts +37 -0
  47. package/lib/contexts/navbarSecondaryMenu/content.d.ts.map +1 -0
  48. package/lib/contexts/navbarSecondaryMenu/content.js +50 -0
  49. package/lib/contexts/navbarSecondaryMenu/content.js.map +1 -0
  50. package/lib/contexts/navbarSecondaryMenu/display.d.ts +24 -0
  51. package/lib/contexts/navbarSecondaryMenu/display.d.ts.map +1 -0
  52. package/lib/contexts/navbarSecondaryMenu/display.js +62 -0
  53. package/lib/contexts/navbarSecondaryMenu/display.js.map +1 -0
  54. package/lib/contexts/tabGroupChoice.d.ts +21 -0
  55. package/lib/contexts/tabGroupChoice.d.ts.map +1 -0
  56. package/lib/contexts/tabGroupChoice.js +49 -0
  57. package/lib/contexts/tabGroupChoice.js.map +1 -0
  58. package/lib/{utils/usePrevious.d.ts → hooks/styles.css} +4 -1
  59. package/lib/hooks/useBackToTopButton.d.ts +27 -0
  60. package/lib/hooks/useBackToTopButton.d.ts.map +1 -0
  61. package/lib/hooks/useBackToTopButton.js +50 -0
  62. package/lib/hooks/useBackToTopButton.js.map +1 -0
  63. package/lib/hooks/useCodeWordWrap.d.ts +14 -0
  64. package/lib/hooks/useCodeWordWrap.d.ts.map +1 -0
  65. package/lib/hooks/useCodeWordWrap.js +67 -0
  66. package/lib/hooks/useCodeWordWrap.js.map +1 -0
  67. package/lib/hooks/useHideableNavbar.d.ts +17 -0
  68. package/lib/hooks/useHideableNavbar.d.ts.map +1 -0
  69. package/lib/hooks/useHideableNavbar.js +60 -0
  70. package/lib/hooks/useHideableNavbar.js.map +1 -0
  71. package/lib/hooks/useKeyboardNavigation.d.ts +20 -0
  72. package/lib/hooks/useKeyboardNavigation.d.ts.map +1 -0
  73. package/lib/hooks/useKeyboardNavigation.js +39 -0
  74. package/lib/hooks/useKeyboardNavigation.js.map +1 -0
  75. package/lib/hooks/useLockBodyScroll.d.ts +12 -0
  76. package/lib/hooks/useLockBodyScroll.d.ts.map +1 -0
  77. package/lib/hooks/useLockBodyScroll.js +20 -0
  78. package/lib/hooks/useLockBodyScroll.js.map +1 -0
  79. package/lib/hooks/useMutationObserver.d.ts +4 -0
  80. package/lib/hooks/useMutationObserver.d.ts.map +1 -0
  81. package/lib/hooks/useMutationObserver.js +29 -0
  82. package/lib/hooks/useMutationObserver.js.map +1 -0
  83. package/lib/hooks/usePrismTheme.d.ts +13 -0
  84. package/lib/hooks/usePrismTheme.d.ts.map +1 -0
  85. package/lib/hooks/usePrismTheme.js +21 -0
  86. package/lib/hooks/usePrismTheme.js.map +1 -0
  87. package/lib/hooks/useSearchPage.d.ts +25 -0
  88. package/lib/hooks/useSearchPage.d.ts.map +1 -0
  89. package/lib/hooks/useSearchPage.js +43 -0
  90. package/lib/hooks/useSearchPage.js.map +1 -0
  91. package/lib/hooks/useSkipToContent.d.ts +25 -0
  92. package/lib/hooks/useSkipToContent.d.ts.map +1 -0
  93. package/lib/hooks/useSkipToContent.js +35 -0
  94. package/lib/hooks/useSkipToContent.js.map +1 -0
  95. package/lib/hooks/useTOCHighlight.d.ts +25 -0
  96. package/lib/hooks/useTOCHighlight.d.ts.map +1 -0
  97. package/lib/hooks/useTOCHighlight.js +130 -0
  98. package/lib/hooks/useTOCHighlight.js.map +1 -0
  99. package/lib/hooks/useWindowSize.d.ts +28 -0
  100. package/lib/hooks/useWindowSize.d.ts.map +1 -0
  101. package/lib/hooks/useWindowSize.js +59 -0
  102. package/lib/hooks/useWindowSize.js.map +1 -0
  103. package/lib/index.d.ts +15 -19
  104. package/lib/index.d.ts.map +1 -0
  105. package/lib/index.js +22 -15
  106. package/lib/index.js.map +1 -0
  107. package/lib/internal.d.ts +41 -0
  108. package/lib/internal.d.ts.map +1 -0
  109. package/lib/internal.js +52 -0
  110. package/lib/internal.js.map +1 -0
  111. package/lib/utils/ThemeClassNames.d.ts +49 -12
  112. package/lib/utils/ThemeClassNames.d.ts.map +1 -0
  113. package/lib/utils/ThemeClassNames.js +47 -4
  114. package/lib/utils/ThemeClassNames.js.map +1 -0
  115. package/lib/utils/codeBlockUtils.d.ts +63 -0
  116. package/lib/utils/codeBlockUtils.d.ts.map +1 -0
  117. package/lib/utils/codeBlockUtils.js +159 -3
  118. package/lib/utils/codeBlockUtils.js.map +1 -0
  119. package/lib/utils/docsUtils.d.ts +91 -0
  120. package/lib/utils/docsUtils.d.ts.map +1 -0
  121. package/lib/utils/docsUtils.js +217 -1
  122. package/lib/utils/docsUtils.js.map +1 -0
  123. package/lib/utils/footerUtils.d.ts +13 -0
  124. package/lib/utils/footerUtils.d.ts.map +1 -0
  125. package/lib/utils/footerUtils.js +14 -0
  126. package/lib/utils/footerUtils.js.map +1 -0
  127. package/lib/utils/generalUtils.d.ts +11 -1
  128. package/lib/utils/generalUtils.d.ts.map +1 -0
  129. package/lib/utils/generalUtils.js +9 -5
  130. package/lib/utils/generalUtils.js.map +1 -0
  131. package/lib/utils/historyUtils.d.ts +17 -0
  132. package/lib/utils/historyUtils.d.ts.map +1 -0
  133. package/lib/utils/historyUtils.js +38 -0
  134. package/lib/utils/historyUtils.js.map +1 -0
  135. package/lib/utils/jsUtils.d.ts +23 -0
  136. package/lib/utils/jsUtils.d.ts.map +1 -0
  137. package/lib/utils/jsUtils.js +29 -0
  138. package/lib/utils/jsUtils.js.map +1 -0
  139. package/lib/utils/metadataUtils.d.ts +38 -0
  140. package/lib/utils/metadataUtils.d.ts.map +1 -0
  141. package/lib/utils/metadataUtils.js +70 -0
  142. package/lib/utils/metadataUtils.js.map +1 -0
  143. package/lib/utils/navbarUtils.d.ts +21 -0
  144. package/lib/utils/navbarUtils.d.ts.map +1 -0
  145. package/lib/utils/navbarUtils.js +36 -0
  146. package/lib/utils/navbarUtils.js.map +1 -0
  147. package/lib/utils/reactUtils.d.ts +69 -0
  148. package/lib/utils/reactUtils.d.ts.map +1 -0
  149. package/lib/utils/reactUtils.js +98 -0
  150. package/lib/utils/reactUtils.js.map +1 -0
  151. package/lib/utils/regexpUtils.d.ts +12 -0
  152. package/lib/utils/regexpUtils.d.ts.map +1 -0
  153. package/lib/utils/regexpUtils.js +18 -0
  154. package/lib/utils/regexpUtils.js.map +1 -0
  155. package/lib/utils/routesUtils.d.ts +26 -0
  156. package/lib/utils/routesUtils.d.ts.map +1 -0
  157. package/lib/utils/routesUtils.js +54 -0
  158. package/lib/utils/routesUtils.js.map +1 -0
  159. package/lib/utils/scrollUtils.d.ts +83 -0
  160. package/lib/utils/scrollUtils.d.ts.map +1 -0
  161. package/lib/utils/scrollUtils.js +200 -0
  162. package/lib/utils/scrollUtils.js.map +1 -0
  163. package/lib/utils/searchUtils.d.ts +13 -0
  164. package/lib/utils/searchUtils.d.ts.map +1 -0
  165. package/lib/utils/searchUtils.js +37 -0
  166. package/lib/utils/searchUtils.js.map +1 -0
  167. package/lib/utils/storageUtils.d.ts +15 -7
  168. package/lib/utils/storageUtils.d.ts.map +1 -0
  169. package/lib/utils/storageUtils.js +55 -22
  170. package/lib/utils/storageUtils.js.map +1 -0
  171. package/lib/utils/tagsUtils.d.ts +18 -0
  172. package/lib/utils/tagsUtils.d.ts.map +1 -0
  173. package/lib/utils/tagsUtils.js +36 -0
  174. package/lib/utils/tagsUtils.js.map +1 -0
  175. package/lib/utils/tocUtils.d.ts +36 -0
  176. package/lib/utils/tocUtils.d.ts.map +1 -0
  177. package/lib/utils/tocUtils.js +84 -0
  178. package/lib/utils/tocUtils.js.map +1 -0
  179. package/lib/utils/useAlternatePageUtils.d.ts +21 -1
  180. package/lib/utils/useAlternatePageUtils.d.ts.map +1 -0
  181. package/lib/utils/useAlternatePageUtils.js +9 -4
  182. package/lib/utils/useAlternatePageUtils.js.map +1 -0
  183. package/lib/utils/useLocalPathname.d.ts +6 -0
  184. package/lib/utils/useLocalPathname.d.ts.map +1 -0
  185. package/lib/utils/useLocalPathname.js +7 -4
  186. package/lib/utils/useLocalPathname.js.map +1 -0
  187. package/lib/utils/useLocationChange.d.ts +9 -6
  188. package/lib/utils/useLocationChange.d.ts.map +1 -0
  189. package/lib/utils/useLocationChange.js +17 -11
  190. package/lib/utils/useLocationChange.js.map +1 -0
  191. package/lib/utils/usePluralForm.d.ts +12 -0
  192. package/lib/utils/usePluralForm.d.ts.map +1 -0
  193. package/lib/utils/usePluralForm.js +36 -37
  194. package/lib/utils/usePluralForm.js.map +1 -0
  195. package/lib/utils/useThemeConfig.d.ts +56 -29
  196. package/lib/utils/useThemeConfig.d.ts.map +1 -0
  197. package/lib/utils/useThemeConfig.js +4 -0
  198. package/lib/utils/useThemeConfig.js.map +1 -0
  199. package/package.json +35 -15
  200. package/src/components/Collapsible/index.tsx +57 -48
  201. package/src/components/Details/index.tsx +27 -12
  202. package/src/components/Details/styles.module.css +14 -11
  203. package/src/{utils/announcementBarUtils.tsx → contexts/announcementBar.tsx} +43 -39
  204. package/src/contexts/blogPost.tsx +80 -0
  205. package/src/contexts/colorMode.tsx +198 -0
  206. package/src/contexts/doc.tsx +71 -0
  207. package/src/contexts/docSidebarItemsExpandedState.tsx +55 -0
  208. package/src/contexts/docsPreferredVersion.tsx +251 -0
  209. package/src/contexts/docsSidebar.tsx +50 -0
  210. package/src/contexts/docsVersion.tsx +36 -0
  211. package/src/contexts/navbarMobileSidebar.tsx +99 -0
  212. package/src/contexts/navbarSecondaryMenu/content.tsx +100 -0
  213. package/src/contexts/navbarSecondaryMenu/display.tsx +102 -0
  214. package/src/contexts/tabGroupChoice.tsx +85 -0
  215. package/{lib/utils/pathUtils.d.ts → src/hooks/styles.css} +4 -1
  216. package/src/hooks/useBackToTopButton.ts +73 -0
  217. package/src/hooks/useCodeWordWrap.ts +105 -0
  218. package/src/hooks/useHideableNavbar.ts +75 -0
  219. package/src/hooks/useKeyboardNavigation.ts +45 -0
  220. package/src/hooks/useLockBodyScroll.ts +21 -0
  221. package/src/hooks/useMutationObserver.ts +38 -0
  222. package/src/hooks/usePrismTheme.ts +24 -0
  223. package/src/hooks/useSearchPage.ts +79 -0
  224. package/src/hooks/useSkipToContent.ts +58 -0
  225. package/src/hooks/useTOCHighlight.ts +189 -0
  226. package/src/hooks/useWindowSize.ts +72 -0
  227. package/src/index.ts +49 -42
  228. package/src/internal.ts +122 -0
  229. package/src/types.d.ts +0 -2
  230. package/src/utils/ThemeClassNames.ts +54 -5
  231. package/src/utils/codeBlockUtils.ts +241 -2
  232. package/src/utils/docsUtils.tsx +334 -0
  233. package/src/utils/footerUtils.ts +18 -0
  234. package/src/utils/generalUtils.ts +9 -5
  235. package/src/utils/historyUtils.ts +45 -0
  236. package/src/utils/jsUtils.ts +36 -0
  237. package/src/utils/metadataUtils.tsx +115 -0
  238. package/src/utils/navbarUtils.tsx +45 -0
  239. package/src/utils/reactUtils.tsx +129 -0
  240. package/src/utils/regexpUtils.ts +24 -0
  241. package/src/utils/routesUtils.ts +75 -0
  242. package/src/utils/scrollUtils.tsx +304 -0
  243. package/src/utils/searchUtils.ts +51 -0
  244. package/src/utils/storageUtils.ts +56 -23
  245. package/src/utils/tagsUtils.ts +50 -0
  246. package/src/utils/tocUtils.ts +119 -0
  247. package/src/utils/useAlternatePageUtils.ts +19 -6
  248. package/src/utils/useLocalPathname.ts +6 -4
  249. package/src/utils/useLocationChange.ts +24 -20
  250. package/src/utils/usePluralForm.ts +50 -38
  251. package/src/utils/useThemeConfig.ts +55 -31
  252. package/copyUntypedFiles.js +0 -20
  253. package/lib/.tsbuildinfo +0 -1
  254. package/lib/utils/announcementBarUtils.d.ts +0 -17
  255. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +0 -21
  256. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +0 -94
  257. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts +0 -13
  258. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +0 -20
  259. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +0 -5
  260. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +0 -41
  261. package/lib/utils/mobileSecondaryMenu.d.ts +0 -20
  262. package/lib/utils/mobileSecondaryMenu.js +0 -50
  263. package/lib/utils/pathUtils.js +0 -13
  264. package/lib/utils/usePrevious.js +0 -14
  265. package/src/utils/__tests__/codeBlockUtils.test.ts +0 -54
  266. package/src/utils/__tests__/pathUtils.test.ts +0 -32
  267. package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +0 -165
  268. package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +0 -34
  269. package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +0 -66
  270. package/src/utils/docsUtils.ts +0 -11
  271. package/src/utils/mobileSecondaryMenu.tsx +0 -115
  272. package/src/utils/pathUtils.ts +0 -17
  273. package/src/utils/usePrevious.ts +0 -18
  274. package/tsconfig.json +0 -10
@@ -0,0 +1,21 @@
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
+ /**
11
+ * Side-effect that locks the document body's scroll throughout the lifetime of
12
+ * the containing component. e.g. when the mobile sidebar is expanded.
13
+ */
14
+ export function useLockBodyScroll(lock: boolean = true): void {
15
+ useEffect(() => {
16
+ document.body.style.overflow = lock ? 'hidden' : 'visible';
17
+ return () => {
18
+ document.body.style.overflow = 'visible';
19
+ };
20
+ }, [lock]);
21
+ }
@@ -0,0 +1,38 @@
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
+ import {useEffect} from 'react';
8
+ import {useEvent, useShallowMemoObject} from '../utils/reactUtils';
9
+
10
+ type Options = MutationObserverInit;
11
+
12
+ const DefaultOptions: Options = {
13
+ attributes: true,
14
+ characterData: true,
15
+ childList: true,
16
+ subtree: true,
17
+ };
18
+
19
+ export function useMutationObserver(
20
+ target: Element | undefined | null,
21
+ callback: MutationCallback,
22
+ options: Options = DefaultOptions,
23
+ ): void {
24
+ const stableCallback = useEvent(callback);
25
+
26
+ // MutationObserver options are not nested much
27
+ // so this should be to memo options in 99%
28
+ // TODO handle options.attributeFilter array
29
+ const stableOptions: Options = useShallowMemoObject(options);
30
+
31
+ useEffect(() => {
32
+ const observer = new MutationObserver(stableCallback);
33
+ if (target) {
34
+ observer.observe(target, stableOptions);
35
+ }
36
+ return () => observer.disconnect();
37
+ }, [target, stableCallback, stableOptions]);
38
+ }
@@ -0,0 +1,24 @@
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 {useColorMode} from '../contexts/colorMode';
9
+ import {useThemeConfig} from '../utils/useThemeConfig';
10
+ import type {PrismTheme} from 'prism-react-renderer';
11
+
12
+ /**
13
+ * Returns a color-mode-dependent Prism theme: whatever the user specified in
14
+ * the config. Falls back to `palenight`.
15
+ */
16
+ export function usePrismTheme(): PrismTheme {
17
+ const {prism} = useThemeConfig();
18
+ const {colorMode} = useColorMode();
19
+ const lightModeTheme = prism.theme;
20
+ const darkModeTheme = prism.darkTheme || lightModeTheme;
21
+ const prismTheme = colorMode === 'dark' ? darkModeTheme : lightModeTheme;
22
+
23
+ return prismTheme;
24
+ }
@@ -0,0 +1,79 @@
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 {useCallback, useEffect, useState} from 'react';
9
+ import {useHistory} from '@docusaurus/router';
10
+ import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
11
+
12
+ const SEARCH_PARAM_QUERY = 'q';
13
+
14
+ /** Some utility functions around search queries. */
15
+ export function useSearchPage(): {
16
+ /**
17
+ * Works hand-in-hand with `setSearchQuery`; whatever the user has inputted
18
+ * into the search box.
19
+ */
20
+ searchQuery: string;
21
+ /**
22
+ * Set a new search query. In addition to updating `searchQuery`, this handle
23
+ * also mutates the location and appends the query.
24
+ */
25
+ setSearchQuery: (newSearchQuery: string) => void;
26
+ /**
27
+ * Given a query, this handle generates the corresponding search page link,
28
+ * with base URL prepended.
29
+ */
30
+ generateSearchPageLink: (targetSearchQuery: string) => string;
31
+ } {
32
+ const history = useHistory();
33
+ const {
34
+ siteConfig: {baseUrl},
35
+ } = useDocusaurusContext();
36
+
37
+ const [searchQuery, setSearchQueryState] = useState('');
38
+
39
+ // Init search query just after React hydration
40
+ useEffect(() => {
41
+ const searchQueryStringValue =
42
+ new URLSearchParams(window.location.search).get(SEARCH_PARAM_QUERY) ?? '';
43
+
44
+ setSearchQueryState(searchQueryStringValue);
45
+ }, []);
46
+
47
+ const setSearchQuery = useCallback(
48
+ (newSearchQuery: string) => {
49
+ const searchParams = new URLSearchParams(window.location.search);
50
+
51
+ if (newSearchQuery) {
52
+ searchParams.set(SEARCH_PARAM_QUERY, newSearchQuery);
53
+ } else {
54
+ searchParams.delete(SEARCH_PARAM_QUERY);
55
+ }
56
+
57
+ history.replace({
58
+ search: searchParams.toString(),
59
+ });
60
+ setSearchQueryState(newSearchQuery);
61
+ },
62
+ [history],
63
+ );
64
+
65
+ const generateSearchPageLink = useCallback(
66
+ (targetSearchQuery: string) =>
67
+ // Refer to https://github.com/facebook/docusaurus/pull/2838
68
+ `${baseUrl}search?${SEARCH_PARAM_QUERY}=${encodeURIComponent(
69
+ targetSearchQuery,
70
+ )}`,
71
+ [baseUrl],
72
+ );
73
+
74
+ return {
75
+ searchQuery,
76
+ setSearchQuery,
77
+ generateSearchPageLink,
78
+ };
79
+ }
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import type React from 'react';
9
+ import {useCallback, useRef} from 'react';
10
+ import {useHistory} from '@docusaurus/router';
11
+ import {useLocationChange} from '../utils/useLocationChange';
12
+ import {ThemeClassNames} from '../utils/ThemeClassNames';
13
+
14
+ function programmaticFocus(el: HTMLElement) {
15
+ el.setAttribute('tabindex', '-1');
16
+ el.focus();
17
+ el.removeAttribute('tabindex');
18
+ }
19
+
20
+ /** This hook wires the logic for a skip-to-content link. */
21
+ export function useSkipToContent(): {
22
+ /**
23
+ * The ref to the container. On page transition, the container will be focused
24
+ * so that keyboard navigators can instantly interact with the link and jump
25
+ * to content. **Note:** the type is `RefObject<HTMLDivElement>` only because
26
+ * the typing for refs don't reflect that the `ref` prop is contravariant, so
27
+ * using `HTMLElement` causes type-checking to fail. You can plug the ref into
28
+ * any HTML element, as long as it can be focused.
29
+ */
30
+ containerRef: React.RefObject<HTMLDivElement>;
31
+ /**
32
+ * Callback fired when the skip to content link has been interacted with. It
33
+ * will programmatically focus the main content.
34
+ */
35
+ handleSkip: (e: React.MouseEvent<HTMLAnchorElement>) => void;
36
+ } {
37
+ const containerRef = useRef<HTMLDivElement>(null);
38
+ const {action} = useHistory();
39
+ const handleSkip = useCallback((e: React.MouseEvent<HTMLAnchorElement>) => {
40
+ e.preventDefault();
41
+
42
+ const targetElement: HTMLElement | null =
43
+ document.querySelector('main:first-of-type') ??
44
+ document.querySelector(`.${ThemeClassNames.wrapper.main}`);
45
+
46
+ if (targetElement) {
47
+ programmaticFocus(targetElement);
48
+ }
49
+ }, []);
50
+
51
+ useLocationChange(({location}) => {
52
+ if (containerRef.current && !location.hash && action === 'PUSH') {
53
+ programmaticFocus(containerRef.current);
54
+ }
55
+ });
56
+
57
+ return {containerRef, handleSkip};
58
+ }
@@ -0,0 +1,189 @@
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, useRef} from 'react';
9
+ import {useThemeConfig} from '../utils/useThemeConfig';
10
+
11
+ // TODO make the hardcoded theme-classic classnames configurable (or add them
12
+ // to ThemeClassNames?)
13
+
14
+ /**
15
+ * If the anchor has no height and is just a "marker" in the DOM; we'll use the
16
+ * parent (normally the link text) rect boundaries instead
17
+ */
18
+ function getVisibleBoundingClientRect(element: HTMLElement): DOMRect {
19
+ const rect = element.getBoundingClientRect();
20
+ const hasNoHeight = rect.top === rect.bottom;
21
+ if (hasNoHeight) {
22
+ return getVisibleBoundingClientRect(element.parentNode as HTMLElement);
23
+ }
24
+ return rect;
25
+ }
26
+
27
+ /**
28
+ * Considering we divide viewport into 2 zones of each 50vh, this returns true
29
+ * if an element is in the first zone (i.e., appear in viewport, near the top)
30
+ */
31
+ function isInViewportTopHalf(boundingRect: DOMRect) {
32
+ return boundingRect.top > 0 && boundingRect.bottom < window.innerHeight / 2;
33
+ }
34
+
35
+ function getAnchors({
36
+ minHeadingLevel,
37
+ maxHeadingLevel,
38
+ }: {
39
+ minHeadingLevel: number;
40
+ maxHeadingLevel: number;
41
+ }): HTMLElement[] {
42
+ const selectors = [];
43
+ for (let i = minHeadingLevel; i <= maxHeadingLevel; i += 1) {
44
+ selectors.push(`h${i}.anchor`);
45
+ }
46
+ return Array.from(document.querySelectorAll(selectors.join()));
47
+ }
48
+
49
+ function getActiveAnchor(
50
+ anchors: HTMLElement[],
51
+ {
52
+ anchorTopOffset,
53
+ }: {
54
+ anchorTopOffset: number;
55
+ },
56
+ ): Element | null {
57
+ // Naming is hard: The "nextVisibleAnchor" is the first anchor that appear
58
+ // under the viewport top boundary. It does not mean this anchor is visible
59
+ // yet, but if user continues scrolling down, it will be the first to become
60
+ // visible
61
+ const nextVisibleAnchor = anchors.find((anchor) => {
62
+ const boundingRect = getVisibleBoundingClientRect(anchor);
63
+ return boundingRect.top >= anchorTopOffset;
64
+ });
65
+
66
+ if (nextVisibleAnchor) {
67
+ const boundingRect = getVisibleBoundingClientRect(nextVisibleAnchor);
68
+ // If anchor is in the top half of the viewport: it is the one we consider
69
+ // "active" (unless it's too close to the top and and soon to be scrolled
70
+ // outside viewport)
71
+ if (isInViewportTopHalf(boundingRect)) {
72
+ return nextVisibleAnchor;
73
+ }
74
+ // If anchor is in the bottom half of the viewport, or under the viewport,
75
+ // we consider the active anchor is the previous one. This is because the
76
+ // main text appearing in the user screen mostly belong to the previous
77
+ // anchor. Returns null for the first anchor, see
78
+ // https://github.com/facebook/docusaurus/issues/5318
79
+ return anchors[anchors.indexOf(nextVisibleAnchor) - 1] ?? null;
80
+ }
81
+ // No anchor under viewport top (i.e. we are at the bottom of the page),
82
+ // highlight the last anchor found
83
+ return anchors[anchors.length - 1] ?? null;
84
+ }
85
+
86
+ function getLinkAnchorValue(link: HTMLAnchorElement): string {
87
+ return decodeURIComponent(link.href.substring(link.href.indexOf('#') + 1));
88
+ }
89
+
90
+ function getLinks(linkClassName: string) {
91
+ return Array.from(
92
+ document.getElementsByClassName(linkClassName),
93
+ ) as HTMLAnchorElement[];
94
+ }
95
+
96
+ function getNavbarHeight(): number {
97
+ // Not ideal to obtain actual height this way
98
+ // Using TS ! (not ?) because otherwise a bad selector would be un-noticed
99
+ return document.querySelector('.navbar')!.clientHeight;
100
+ }
101
+
102
+ function useAnchorTopOffsetRef() {
103
+ const anchorTopOffsetRef = useRef<number>(0);
104
+ const {
105
+ navbar: {hideOnScroll},
106
+ } = useThemeConfig();
107
+
108
+ useEffect(() => {
109
+ anchorTopOffsetRef.current = hideOnScroll ? 0 : getNavbarHeight();
110
+ }, [hideOnScroll]);
111
+
112
+ return anchorTopOffsetRef;
113
+ }
114
+
115
+ export type TOCHighlightConfig = {
116
+ /** A class name that all TOC links share. */
117
+ linkClassName: string;
118
+ /** The class name applied to the active (highlighted) link. */
119
+ linkActiveClassName: string;
120
+ /**
121
+ * The minimum heading level that the TOC includes. Only headings that are in
122
+ * this range will be eligible as "active heading".
123
+ */
124
+ minHeadingLevel: number;
125
+ /** @see {@link TOCHighlightConfig.minHeadingLevel} */
126
+ maxHeadingLevel: number;
127
+ };
128
+
129
+ /**
130
+ * Side-effect that applies the active class name to the TOC heading that the
131
+ * user is currently viewing. Disabled when `config` is undefined.
132
+ */
133
+ export function useTOCHighlight(config: TOCHighlightConfig | undefined): void {
134
+ const lastActiveLinkRef = useRef<HTMLAnchorElement | undefined>(undefined);
135
+
136
+ const anchorTopOffsetRef = useAnchorTopOffsetRef();
137
+
138
+ useEffect(() => {
139
+ if (!config) {
140
+ // No-op, highlighting is disabled
141
+ return () => {};
142
+ }
143
+
144
+ const {
145
+ linkClassName,
146
+ linkActiveClassName,
147
+ minHeadingLevel,
148
+ maxHeadingLevel,
149
+ } = config;
150
+
151
+ function updateLinkActiveClass(link: HTMLAnchorElement, active: boolean) {
152
+ if (active) {
153
+ if (lastActiveLinkRef.current && lastActiveLinkRef.current !== link) {
154
+ lastActiveLinkRef.current.classList.remove(linkActiveClassName);
155
+ }
156
+ link.classList.add(linkActiveClassName);
157
+ lastActiveLinkRef.current = link;
158
+ // link.scrollIntoView({block: 'nearest'});
159
+ } else {
160
+ link.classList.remove(linkActiveClassName);
161
+ }
162
+ }
163
+
164
+ function updateActiveLink() {
165
+ const links = getLinks(linkClassName);
166
+ const anchors = getAnchors({minHeadingLevel, maxHeadingLevel});
167
+ const activeAnchor = getActiveAnchor(anchors, {
168
+ anchorTopOffset: anchorTopOffsetRef.current,
169
+ });
170
+ const activeLink = links.find(
171
+ (link) => activeAnchor && activeAnchor.id === getLinkAnchorValue(link),
172
+ );
173
+
174
+ links.forEach((link) => {
175
+ updateLinkActiveClass(link, link === activeLink);
176
+ });
177
+ }
178
+
179
+ document.addEventListener('scroll', updateActiveLink);
180
+ document.addEventListener('resize', updateActiveLink);
181
+
182
+ updateActiveLink();
183
+
184
+ return () => {
185
+ document.removeEventListener('scroll', updateActiveLink);
186
+ document.removeEventListener('resize', updateActiveLink);
187
+ };
188
+ }, [config, anchorTopOffsetRef]);
189
+ }
@@ -0,0 +1,72 @@
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
+ ssr: 'ssr',
16
+ } as const;
17
+
18
+ type WindowSize = keyof typeof windowSizes;
19
+
20
+ const DesktopThresholdWidth = 996;
21
+
22
+ function getWindowSize() {
23
+ if (!ExecutionEnvironment.canUseDOM) {
24
+ return windowSizes.ssr;
25
+ }
26
+ return window.innerWidth > DesktopThresholdWidth
27
+ ? windowSizes.desktop
28
+ : windowSizes.mobile;
29
+ }
30
+
31
+ const DevSimulateSSR = process.env.NODE_ENV === 'development' && true;
32
+
33
+ /**
34
+ * Gets the current window size as an enum value. We don't want it to return the
35
+ * actual width value, so that it only re-renders once a breakpoint is crossed.
36
+ *
37
+ * It may return `"ssr"`, which is very important to handle hydration FOUC or
38
+ * layout shifts. You have to handle it explicitly upfront. On the server, you
39
+ * may need to render BOTH the mobile/desktop elements (and hide one of them
40
+ * with mediaquery). We don't return `undefined` on purpose, to make it more
41
+ * explicit.
42
+ *
43
+ * In development mode, this hook will still return `"ssr"` for one second, to
44
+ * catch potential layout shifts, similar to strict mode calling effects twice.
45
+ */
46
+ export function useWindowSize(): WindowSize {
47
+ const [windowSize, setWindowSize] = useState<WindowSize>(() => {
48
+ if (DevSimulateSSR) {
49
+ return 'ssr';
50
+ }
51
+ return getWindowSize();
52
+ });
53
+
54
+ useEffect(() => {
55
+ function updateWindowSize() {
56
+ setWindowSize(getWindowSize());
57
+ }
58
+
59
+ const timeout = DevSimulateSSR
60
+ ? window.setTimeout(updateWindowSize, 1000)
61
+ : undefined;
62
+
63
+ window.addEventListener('resize', updateWindowSize);
64
+
65
+ return () => {
66
+ window.removeEventListener('resize', updateWindowSize);
67
+ clearTimeout(timeout);
68
+ };
69
+ }, []);
70
+
71
+ return windowSize;
72
+ }
package/src/index.ts CHANGED
@@ -5,66 +5,73 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- export {useThemeConfig} from './utils/useThemeConfig';
9
-
10
- export type {
11
- ThemeConfig,
12
- Navbar,
13
- NavbarItem,
14
- NavbarLogo,
15
- Footer,
16
- FooterLinks,
17
- FooterLinkItem,
8
+ /*
9
+ * APIs to document
10
+ */
11
+
12
+ export {
13
+ useThemeConfig,
14
+ type ThemeConfig,
15
+ type UserThemeConfig,
16
+ type Navbar,
17
+ type NavbarItem,
18
+ type NavbarLogo,
19
+ type MultiColumnFooter,
20
+ type SimpleFooter,
21
+ type Footer,
22
+ type FooterLogo,
23
+ type FooterLinkItem,
24
+ type ColorModeConfig,
18
25
  } from './utils/useThemeConfig';
19
26
 
20
27
  export {createStorageSlot, listStorageKeys} from './utils/storageUtils';
21
28
 
22
- export {useAlternatePageUtils} from './utils/useAlternatePageUtils';
29
+ export {useContextualSearchFilters} from './utils/searchUtils';
23
30
 
24
- export {parseCodeBlockTitle} from './utils/codeBlockUtils';
31
+ export {useCurrentSidebarCategory} from './utils/docsUtils';
25
32
 
26
- export {docVersionSearchTag, DEFAULT_SEARCH_TAG} from './utils/searchUtils';
33
+ export {usePluralForm} from './utils/usePluralForm';
27
34
 
28
- export {isDocsPluginEnabled} from './utils/docsUtils';
35
+ export {useCollapsible, Collapsible} from './components/Collapsible';
29
36
 
30
- export {isSamePath} from './utils/pathUtils';
37
+ export {ThemeClassNames} from './utils/ThemeClassNames';
31
38
 
32
- export {useTitleFormatter} from './utils/generalUtils';
39
+ export {
40
+ useIsomorphicLayoutEffect,
41
+ useEvent,
42
+ usePrevious,
43
+ composeProviders,
44
+ ReactContextError,
45
+ } from './utils/reactUtils';
33
46
 
34
- export {usePluralForm} from './utils/usePluralForm';
47
+ export {PageMetadata, HtmlClassNameProvider} from './utils/metadataUtils';
35
48
 
36
- export {useLocationChange} from './utils/useLocationChange';
49
+ export {useColorMode, type ColorMode} from './contexts/colorMode';
37
50
 
38
- export {usePrevious} from './utils/usePrevious';
51
+ export {
52
+ NavbarSecondaryMenuFiller,
53
+ type NavbarSecondaryMenuComponent,
54
+ } from './contexts/navbarSecondaryMenu/content';
39
55
 
40
- export {useCollapsible, Collapsible} from './components/Collapsible';
41
- export type {
42
- UseCollapsibleConfig,
43
- UseCollapsibleReturns,
44
- } from './components/Collapsible';
56
+ export {useWindowSize} from './hooks/useWindowSize';
45
57
 
46
- export {default as Details} from './components/Details';
47
- export type {DetailsProps} from './components/Details';
58
+ /*
59
+ * APIs kept undocumented, on purpose
60
+ * Note: we still guarantee retro-compatibility on those
61
+ */
48
62
 
49
63
  export {
50
- MobileSecondaryMenuProvider,
51
- MobileSecondaryMenuFiller,
52
- useMobileSecondaryMenuRenderer,
53
- } from './utils/mobileSecondaryMenu';
54
- export type {MobileSecondaryMenuComponent} from './utils/mobileSecondaryMenu';
64
+ translateTagsPageTitle,
65
+ listTagsByLetters,
66
+ type TagLetterEntry,
67
+ } from './utils/tagsUtils';
55
68
 
56
- export {
57
- useDocsPreferredVersion,
58
- useDocsPreferredVersionByPluginId,
59
- } from './utils/docsPreferredVersion/useDocsPreferredVersion';
69
+ export {isMultiColumnFooterLinks} from './utils/footerUtils';
60
70
 
61
- export {DocsPreferredVersionContextProvider} from './utils/docsPreferredVersion/DocsPreferredVersionProvider';
71
+ export {isRegexpStringMatch} from './utils/regexpUtils';
62
72
 
63
- export {ThemeClassNames} from './utils/ThemeClassNames';
73
+ export {duplicates, uniq} from './utils/jsUtils';
64
74
 
65
- export {
66
- AnnouncementBarProvider,
67
- useAnnouncementBar,
68
- } from './utils/announcementBarUtils';
75
+ export {usePrismTheme} from './hooks/usePrismTheme';
69
76
 
70
- export {useLocalPathname} from './utils/useLocalPathname';
77
+ export {useDocsPreferredVersion} from './contexts/docsPreferredVersion';