@docusaurus/theme-common 2.0.0-beta.2 → 2.0.0-beta.20

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 (253) 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 +152 -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 +71 -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 +128 -0
  26. package/lib/contexts/docsPreferredVersion.js.map +1 -0
  27. package/lib/contexts/docsSidebar.d.ts +26 -0
  28. package/lib/contexts/docsSidebar.d.ts.map +1 -0
  29. package/lib/contexts/docsSidebar.js +30 -0
  30. package/lib/contexts/docsSidebar.js.map +1 -0
  31. package/lib/contexts/docsVersion.d.ts +20 -0
  32. package/lib/contexts/docsVersion.d.ts.map +1 -0
  33. package/lib/contexts/docsVersion.js +26 -0
  34. package/lib/contexts/docsVersion.js.map +1 -0
  35. package/lib/contexts/navbarMobileSidebar.d.ts +31 -0
  36. package/lib/contexts/navbarMobileSidebar.d.ts.map +1 -0
  37. package/lib/contexts/navbarMobileSidebar.js +56 -0
  38. package/lib/contexts/navbarMobileSidebar.js.map +1 -0
  39. package/lib/contexts/navbarSecondaryMenu/content.d.ts +37 -0
  40. package/lib/contexts/navbarSecondaryMenu/content.d.ts.map +1 -0
  41. package/lib/contexts/navbarSecondaryMenu/content.js +56 -0
  42. package/lib/contexts/navbarSecondaryMenu/content.js.map +1 -0
  43. package/lib/contexts/navbarSecondaryMenu/display.d.ts +24 -0
  44. package/lib/contexts/navbarSecondaryMenu/display.d.ts.map +1 -0
  45. package/lib/contexts/navbarSecondaryMenu/display.js +62 -0
  46. package/lib/contexts/navbarSecondaryMenu/display.js.map +1 -0
  47. package/lib/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 +157 -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 +61 -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 +32 -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 +55 -24
  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 +26 -13
  185. package/src/components/Collapsible/index.tsx +265 -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 +199 -0
  190. package/src/contexts/docSidebarItemsExpandedState.tsx +55 -0
  191. package/src/contexts/docsPreferredVersion.tsx +253 -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 +192 -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 +239 -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 +308 -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 +46 -38
  232. package/src/utils/useThemeConfig.ts +54 -26
  233. package/lib/.tsbuildinfo +0 -1
  234. package/lib/utils/announcementBarInlineJavaScript.d.ts +0 -0
  235. package/lib/utils/announcementBarInlineJavaScript.js +0 -1
  236. package/lib/utils/announcementBarUtils.d.ts +0 -17
  237. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +0 -21
  238. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +0 -94
  239. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts +0 -13
  240. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +0 -20
  241. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +0 -5
  242. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +0 -41
  243. package/lib/utils/pathUtils.js +0 -13
  244. package/lib/utils/usePrevious.js +0 -14
  245. package/src/utils/__tests__/codeBlockUtils.test.ts +0 -54
  246. package/src/utils/__tests__/pathUtils.test.ts +0 -32
  247. package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +0 -165
  248. package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +0 -34
  249. package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +0 -66
  250. package/src/utils/docsUtils.ts +0 -11
  251. package/src/utils/pathUtils.ts +0 -17
  252. package/src/utils/usePrevious.ts +0 -18
  253. package/tsconfig.json +0 -10
@@ -0,0 +1,308 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import React, {
9
+ useCallback,
10
+ useContext,
11
+ useEffect,
12
+ useLayoutEffect,
13
+ useMemo,
14
+ useRef,
15
+ type ReactNode,
16
+ } from 'react';
17
+ import {useDynamicCallback, ReactContextError} from './reactUtils';
18
+ import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
19
+ import useIsBrowser from '@docusaurus/useIsBrowser';
20
+
21
+ type ScrollController = {
22
+ /** A boolean ref tracking whether scroll events are enabled. */
23
+ scrollEventsEnabledRef: React.MutableRefObject<boolean>;
24
+ /** Enable scroll events in `useScrollPosition`. */
25
+ enableScrollEvents: () => void;
26
+ /** Disable scroll events in `useScrollPosition`. */
27
+ disableScrollEvents: () => void;
28
+ };
29
+
30
+ function useScrollControllerContextValue(): ScrollController {
31
+ const scrollEventsEnabledRef = useRef(true);
32
+
33
+ return useMemo(
34
+ () => ({
35
+ scrollEventsEnabledRef,
36
+ enableScrollEvents: () => {
37
+ scrollEventsEnabledRef.current = true;
38
+ },
39
+ disableScrollEvents: () => {
40
+ scrollEventsEnabledRef.current = false;
41
+ },
42
+ }),
43
+ [],
44
+ );
45
+ }
46
+
47
+ const ScrollMonitorContext = React.createContext<ScrollController | undefined>(
48
+ undefined,
49
+ );
50
+
51
+ export function ScrollControllerProvider({
52
+ children,
53
+ }: {
54
+ children: ReactNode;
55
+ }): JSX.Element {
56
+ const value = useScrollControllerContextValue();
57
+ return (
58
+ <ScrollMonitorContext.Provider value={value}>
59
+ {children}
60
+ </ScrollMonitorContext.Provider>
61
+ );
62
+ }
63
+
64
+ /**
65
+ * We need a way to update the scroll position while ignoring scroll events
66
+ * so as not to toggle Navbar/BackToTop visibility.
67
+ *
68
+ * This API permits to temporarily disable/ignore scroll events. Motivated by
69
+ * https://github.com/facebook/docusaurus/pull/5618
70
+ */
71
+ export function useScrollController(): ScrollController {
72
+ const context = useContext(ScrollMonitorContext);
73
+ if (context == null) {
74
+ throw new ReactContextError('ScrollControllerProvider');
75
+ }
76
+ return context;
77
+ }
78
+
79
+ type ScrollPosition = {scrollX: number; scrollY: number};
80
+
81
+ const getScrollPosition = (): ScrollPosition | null =>
82
+ ExecutionEnvironment.canUseDOM
83
+ ? {
84
+ scrollX: window.pageXOffset,
85
+ scrollY: window.pageYOffset,
86
+ }
87
+ : null;
88
+
89
+ /**
90
+ * This hook fires an effect when the scroll position changes. The effect will
91
+ * be provided with the before/after scroll positions. Note that the effect may
92
+ * not be always run: if scrolling is disabled through `useScrollController`, it
93
+ * will be a no-op.
94
+ *
95
+ * @see {@link useScrollController}
96
+ */
97
+ export function useScrollPosition(
98
+ effect: (
99
+ position: ScrollPosition,
100
+ lastPosition: ScrollPosition | null,
101
+ ) => void,
102
+ deps: unknown[] = [],
103
+ ): void {
104
+ const {scrollEventsEnabledRef} = useScrollController();
105
+ const lastPositionRef = useRef<ScrollPosition | null>(getScrollPosition());
106
+
107
+ const dynamicEffect = useDynamicCallback(effect);
108
+
109
+ useEffect(() => {
110
+ const handleScroll = () => {
111
+ if (!scrollEventsEnabledRef.current) {
112
+ return;
113
+ }
114
+ const currentPosition = getScrollPosition()!;
115
+
116
+ if (dynamicEffect) {
117
+ dynamicEffect(currentPosition, lastPositionRef.current);
118
+ }
119
+
120
+ lastPositionRef.current = currentPosition;
121
+ };
122
+
123
+ const opts: AddEventListenerOptions & EventListenerOptions = {
124
+ passive: true,
125
+ };
126
+
127
+ handleScroll();
128
+ window.addEventListener('scroll', handleScroll, opts);
129
+
130
+ return () => window.removeEventListener('scroll', handleScroll, opts);
131
+ // eslint-disable-next-line react-hooks/exhaustive-deps
132
+ }, [dynamicEffect, scrollEventsEnabledRef, ...deps]);
133
+ }
134
+
135
+ type UseScrollPositionSaver = {
136
+ /** Measure the top of an element, and store the details. */
137
+ save: (elem: HTMLElement) => void;
138
+ /**
139
+ * Restore the page position to keep the stored element's position from
140
+ * the top of the viewport, and remove the stored details.
141
+ */
142
+ restore: () => {restored: boolean};
143
+ };
144
+
145
+ function useScrollPositionSaver(): UseScrollPositionSaver {
146
+ const lastElementRef = useRef<{elem: HTMLElement | null; top: number}>({
147
+ elem: null,
148
+ top: 0,
149
+ });
150
+
151
+ const save = useCallback((elem: HTMLElement) => {
152
+ lastElementRef.current = {
153
+ elem,
154
+ top: elem.getBoundingClientRect().top,
155
+ };
156
+ }, []);
157
+
158
+ const restore = useCallback(() => {
159
+ const {
160
+ current: {elem, top},
161
+ } = lastElementRef;
162
+ if (!elem) {
163
+ return {restored: false};
164
+ }
165
+ const newTop = elem.getBoundingClientRect().top;
166
+ const heightDiff = newTop - top;
167
+ if (heightDiff) {
168
+ window.scrollBy({left: 0, top: heightDiff});
169
+ }
170
+ lastElementRef.current = {elem: null, top: 0};
171
+
172
+ return {restored: heightDiff !== 0};
173
+ }, []);
174
+
175
+ return useMemo(() => ({save, restore}), [restore, save]);
176
+ }
177
+
178
+ /**
179
+ * This hook permits to "block" the scroll position of a DOM element.
180
+ * The idea is that we should be able to update DOM content above this element
181
+ * but the screen position of this element should not change.
182
+ *
183
+ * Feature motivated by the Tabs groups: clicking on a tab may affect tabs of
184
+ * the same group upper in the tree, yet to avoid a bad UX, the clicked tab must
185
+ * remain under the user mouse.
186
+ *
187
+ * @see https://github.com/facebook/docusaurus/pull/5618
188
+ */
189
+ export function useScrollPositionBlocker(): {
190
+ /**
191
+ * Takes an element, and keeps its screen position no matter what's getting
192
+ * rendered above it, until the next render.
193
+ */
194
+ blockElementScrollPositionUntilNextRender: (el: HTMLElement) => void;
195
+ } {
196
+ const scrollController = useScrollController();
197
+ const scrollPositionSaver = useScrollPositionSaver();
198
+
199
+ const nextLayoutEffectCallbackRef = useRef<(() => void) | undefined>(
200
+ undefined,
201
+ );
202
+
203
+ const blockElementScrollPositionUntilNextRender = useCallback(
204
+ (el: HTMLElement) => {
205
+ scrollPositionSaver.save(el);
206
+ scrollController.disableScrollEvents();
207
+ nextLayoutEffectCallbackRef.current = () => {
208
+ const {restored} = scrollPositionSaver.restore();
209
+ nextLayoutEffectCallbackRef.current = undefined;
210
+
211
+ // Restoring the former scroll position will trigger a scroll event. We
212
+ // need to wait for next scroll event to happen before enabling the
213
+ // scrollController events again.
214
+ if (restored) {
215
+ const handleScrollRestoreEvent = () => {
216
+ scrollController.enableScrollEvents();
217
+ window.removeEventListener('scroll', handleScrollRestoreEvent);
218
+ };
219
+ window.addEventListener('scroll', handleScrollRestoreEvent);
220
+ } else {
221
+ scrollController.enableScrollEvents();
222
+ }
223
+ };
224
+ },
225
+ [scrollController, scrollPositionSaver],
226
+ );
227
+
228
+ useLayoutEffect(() => {
229
+ nextLayoutEffectCallbackRef.current?.();
230
+ });
231
+
232
+ return {
233
+ blockElementScrollPositionUntilNextRender,
234
+ };
235
+ }
236
+
237
+ type CancelScrollTop = () => void;
238
+
239
+ function smoothScrollNative(top: number): CancelScrollTop {
240
+ window.scrollTo({top, behavior: 'smooth'});
241
+ return () => {
242
+ // Nothing to cancel, it's natively cancelled if user tries to scroll down
243
+ };
244
+ }
245
+
246
+ function smoothScrollPolyfill(top: number): CancelScrollTop {
247
+ let raf: number | null = null;
248
+ const isUpScroll = document.documentElement.scrollTop > top;
249
+ function rafRecursion() {
250
+ const currentScroll = document.documentElement.scrollTop;
251
+ if (
252
+ (isUpScroll && currentScroll > top) ||
253
+ (!isUpScroll && currentScroll < top)
254
+ ) {
255
+ raf = requestAnimationFrame(rafRecursion);
256
+ window.scrollTo(0, Math.floor((currentScroll - top) * 0.85) + top);
257
+ }
258
+ }
259
+ rafRecursion();
260
+
261
+ // Break the recursion. Prevents the user from "fighting" against that
262
+ // recursion producing a weird UX
263
+ return () => raf && cancelAnimationFrame(raf);
264
+ }
265
+
266
+ /**
267
+ * A "smart polyfill" of `window.scrollTo({ top, behavior: "smooth" })`.
268
+ * This currently always uses a polyfilled implementation unless
269
+ * `scroll-behavior: smooth` has been set in CSS, because native support
270
+ * detection for scroll behavior seems unreliable.
271
+ *
272
+ * This hook does not do anything by itself: it returns a start and a stop
273
+ * handle. You can execute either handle at any time.
274
+ */
275
+ export function useSmoothScrollTo(): {
276
+ /**
277
+ * Start the scroll.
278
+ *
279
+ * @param top The final scroll top position.
280
+ */
281
+ startScroll: (top: number) => void;
282
+ /**
283
+ * A cancel function, because the non-native smooth scroll-top
284
+ * implementation must be interrupted if user scrolls down. If there's no
285
+ * existing animation or the scroll is using native behavior, this is a no-op.
286
+ */
287
+ cancelScroll: CancelScrollTop;
288
+ } {
289
+ const cancelRef = useRef<CancelScrollTop | null>(null);
290
+ const isBrowser = useIsBrowser();
291
+ // Not all have support for smooth scrolling (particularly Safari mobile iOS)
292
+ // TODO proper detection is currently unreliable!
293
+ // see https://github.com/wessberg/scroll-behavior-polyfill/issues/16
294
+ // For now, we only use native scroll behavior if smooth is already set,
295
+ // because otherwise the polyfill produces a weird UX when both CSS and JS try
296
+ // to scroll a page, and they cancel each other.
297
+ const supportsNativeSmoothScrolling =
298
+ isBrowser &&
299
+ getComputedStyle(document.documentElement).scrollBehavior === 'smooth';
300
+ return {
301
+ startScroll: (top: number) => {
302
+ cancelRef.current = supportsNativeSmoothScrolling
303
+ ? smoothScrollNative(top)
304
+ : smoothScrollPolyfill(top);
305
+ },
306
+ cancelScroll: () => cancelRef.current?.(),
307
+ };
308
+ }
@@ -5,11 +5,62 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
+ import {
9
+ useAllDocsData,
10
+ useActivePluginAndVersion,
11
+ } from '@docusaurus/plugin-content-docs/client';
12
+ import {useDocsPreferredVersionByPluginId} from '../contexts/docsPreferredVersion';
13
+ import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
14
+
8
15
  export const DEFAULT_SEARCH_TAG = 'default';
9
16
 
17
+ /** The search tag to append as each doc's metadata. */
10
18
  export function docVersionSearchTag(
11
19
  pluginId: string,
12
20
  versionName: string,
13
21
  ): string {
14
22
  return `docs-${pluginId}-${versionName}`;
15
23
  }
24
+
25
+ /**
26
+ * Gets the relevant context information for contextual search.
27
+ *
28
+ * The value is generic and not coupled to Algolia/DocSearch, since we may want
29
+ * to support multiple search engines, or allowing users to use their own search
30
+ * engine solution.
31
+ */
32
+ export function useContextualSearchFilters(): {locale: string; tags: string[]} {
33
+ const {i18n} = useDocusaurusContext();
34
+ const allDocsData = useAllDocsData();
35
+ const activePluginAndVersion = useActivePluginAndVersion();
36
+ const docsPreferredVersionByPluginId = useDocsPreferredVersionByPluginId();
37
+
38
+ // This can't use more specialized hooks because we are mapping over all
39
+ // plugin instances.
40
+ function getDocPluginTags(pluginId: string) {
41
+ const activeVersion =
42
+ activePluginAndVersion?.activePlugin?.pluginId === pluginId
43
+ ? activePluginAndVersion.activeVersion
44
+ : undefined;
45
+
46
+ const preferredVersion = docsPreferredVersionByPluginId[pluginId];
47
+
48
+ const latestVersion = allDocsData[pluginId]!.versions.find(
49
+ (v) => v.isLast,
50
+ )!;
51
+
52
+ const version = activeVersion ?? preferredVersion ?? latestVersion;
53
+
54
+ return docVersionSearchTag(pluginId, version.name);
55
+ }
56
+
57
+ const tags = [
58
+ DEFAULT_SEARCH_TAG,
59
+ ...Object.keys(allDocsData).map(getDocPluginTags),
60
+ ];
61
+
62
+ return {
63
+ locale: i18n.currentLocale,
64
+ tags,
65
+ };
66
+ }
@@ -11,8 +11,12 @@ export type StorageType = typeof StorageTypes[number];
11
11
 
12
12
  const DefaultStorageType: StorageType = 'localStorage';
13
13
 
14
- // Will return null browser storage is unavailable (like running Docusaurus in iframe)
15
- // See https://github.com/facebook/docusaurus/pull/4501
14
+ /**
15
+ * Will return `null` if browser storage is unavailable (like running Docusaurus
16
+ * in an iframe). This should NOT be called in SSR.
17
+ *
18
+ * @see https://github.com/facebook/docusaurus/pull/4501
19
+ */
16
20
  function getBrowserStorage(
17
21
  storageType: StorageType = DefaultStorageType,
18
22
  ): Storage | null {
@@ -23,21 +27,21 @@ function getBrowserStorage(
23
27
  }
24
28
  if (storageType === 'none') {
25
29
  return null;
26
- } else {
27
- try {
28
- return window[storageType];
29
- } catch (e) {
30
- logOnceBrowserStorageNotAvailableWarning(e);
31
- return null;
32
- }
30
+ }
31
+ try {
32
+ return window[storageType];
33
+ } catch (err) {
34
+ logOnceBrowserStorageNotAvailableWarning(err as Error);
35
+ return null;
33
36
  }
34
37
  }
35
38
 
39
+ let hasLoggedBrowserStorageNotAvailableWarning = false;
36
40
  /**
37
- * Poor man's memoization to avoid logging multiple times the same warning
38
- * Sometimes, localStorage/sessionStorage is unavailable due to browser policies
41
+ * Poor man's memoization to avoid logging multiple times the same warning.
42
+ * Sometimes, `localStorage`/`sessionStorage` is unavailable due to browser
43
+ * policies.
39
44
  */
40
- let hasLoggedBrowserStorageNotAvailableWarning = false;
41
45
  function logOnceBrowserStorageNotAvailableWarning(error: Error) {
42
46
  if (!hasLoggedBrowserStorageNotAvailableWarning) {
43
47
  console.warn(
@@ -50,11 +54,11 @@ Possible reasons: running Docusaurus in an iframe, in an incognito browser sessi
50
54
  }
51
55
 
52
56
  // Convenient storage interface for a single storage key
53
- export interface StorageSlot {
57
+ export type StorageSlot = {
54
58
  get: () => string | null;
55
59
  set: (value: string) => void;
56
60
  del: () => void;
57
- }
61
+ };
58
62
 
59
63
  const NoopStorageSlot: StorageSlot = {
60
64
  get: () => null,
@@ -62,7 +66,7 @@ const NoopStorageSlot: StorageSlot = {
62
66
  del: () => {},
63
67
  };
64
68
 
65
- // Fail-fast, as storage APIs should not be used during the SSR process
69
+ // Fail-fast, as storage APIs should not be used during the SSR process
66
70
  function createServerStorageSlot(key: string): StorageSlot {
67
71
  function throwError(): never {
68
72
  throw new Error(`Illegal storage API usage for storage key "${key}".
@@ -78,12 +82,19 @@ Please only call storage APIs in effects and event handlers.`);
78
82
  }
79
83
 
80
84
  /**
81
- * Creates an object for accessing a particular key in localStorage.
85
+ * Creates an interface to work on a particular key in the storage model.
86
+ * Note that this function only initializes the interface, but doesn't allocate
87
+ * anything by itself (i.e. no side-effects).
88
+ *
89
+ * The API is fail-safe, since usage of browser storage should be considered
90
+ * unreliable. Local storage might simply be unavailable (iframe + browser
91
+ * security) or operations might fail individually. Please assume that using
92
+ * this API can be a no-op. See also https://github.com/facebook/docusaurus/issues/6036
82
93
  */
83
- export const createStorageSlot = (
94
+ export function createStorageSlot(
84
95
  key: string,
85
96
  options?: {persistence?: StorageType},
86
- ): StorageSlot => {
97
+ ): StorageSlot {
87
98
  if (typeof window === 'undefined') {
88
99
  return createServerStorageSlot(key);
89
100
  }
@@ -92,14 +103,36 @@ export const createStorageSlot = (
92
103
  return NoopStorageSlot;
93
104
  }
94
105
  return {
95
- get: () => browserStorage.getItem(key),
96
- set: (value) => browserStorage.setItem(key, value),
97
- del: () => browserStorage.removeItem(key),
106
+ get: () => {
107
+ try {
108
+ return browserStorage.getItem(key);
109
+ } catch (err) {
110
+ console.error(`Docusaurus storage error, can't get key=${key}`, err);
111
+ return null;
112
+ }
113
+ },
114
+ set: (value) => {
115
+ try {
116
+ browserStorage.setItem(key, value);
117
+ } catch (err) {
118
+ console.error(
119
+ `Docusaurus storage error, can't set ${key}=${value}`,
120
+ err,
121
+ );
122
+ }
123
+ },
124
+ del: () => {
125
+ try {
126
+ browserStorage.removeItem(key);
127
+ } catch (err) {
128
+ console.error(`Docusaurus storage error, can't delete key=${key}`, err);
129
+ }
130
+ },
98
131
  };
99
- };
132
+ }
100
133
 
101
134
  /**
102
- * Returns a list of all the keys currently stored in browser storage
135
+ * Returns a list of all the keys currently stored in browser storage,
103
136
  * or an empty list if browser storage can't be accessed.
104
137
  */
105
138
  export function listStorageKeys(
@@ -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 {translate} from '@docusaurus/Translate';
9
+ import type {TagsListItem} from '@docusaurus/utils';
10
+
11
+ export const translateTagsPageTitle = (): string =>
12
+ translate({
13
+ id: 'theme.tags.tagsPageTitle',
14
+ message: 'Tags',
15
+ description: 'The title of the tag list page',
16
+ });
17
+
18
+ export type TagLetterEntry = {letter: string; tags: TagsListItem[]};
19
+
20
+ function getTagLetter(tag: string): string {
21
+ return tag[0]!.toUpperCase();
22
+ }
23
+
24
+ /**
25
+ * Takes a list of tags (as provided by the content plugins), and groups them by
26
+ * their initials.
27
+ */
28
+ export function listTagsByLetters(
29
+ tags: readonly TagsListItem[],
30
+ ): TagLetterEntry[] {
31
+ const groups: {[initial: string]: TagsListItem[]} = {};
32
+ Object.values(tags).forEach((tag) => {
33
+ const initial = getTagLetter(tag.label);
34
+ groups[initial] ??= [];
35
+ groups[initial]!.push(tag);
36
+ });
37
+
38
+ return (
39
+ Object.entries(groups)
40
+ // Sort letters
41
+ .sort(([letter1], [letter2]) => letter1.localeCompare(letter2))
42
+ .map(([letter, letterTags]) => {
43
+ // Sort tags inside a letter
44
+ const sortedTags = letterTags.sort((tag1, tag2) =>
45
+ tag1.label.localeCompare(tag2.label),
46
+ );
47
+ return {letter, tags: sortedTags};
48
+ })
49
+ );
50
+ }
@@ -0,0 +1,119 @@
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 {useMemo} from 'react';
9
+ import type {TOCItem} from '@docusaurus/mdx-loader';
10
+
11
+ export type TOCTreeNode = {
12
+ readonly value: string;
13
+ readonly id: string;
14
+ readonly level: number;
15
+ readonly children: readonly TOCTreeNode[];
16
+ };
17
+
18
+ function treeifyTOC(flatTOC: readonly TOCItem[]): TOCTreeNode[] {
19
+ const headings = flatTOC.map((heading) => ({
20
+ ...heading,
21
+ parentIndex: -1,
22
+ children: [] as TOCTreeNode[],
23
+ }));
24
+
25
+ // Keep track of which previous index would be the current heading's direct
26
+ // parent. Each entry <i> is the last index of the `headings` array at heading
27
+ // level <i>. We will modify these indices as we iterate through all headings.
28
+ // e.g. if an ### H3 was last seen at index 2, then prevIndexForLevel[3] === 2
29
+ // indices 0 and 1 will remain unused.
30
+ const prevIndexForLevel = Array(7).fill(-1);
31
+
32
+ headings.forEach((curr, currIndex) => {
33
+ // Take the last seen index for each ancestor level. the highest index will
34
+ // be the direct ancestor of the current heading.
35
+ const ancestorLevelIndexes = prevIndexForLevel.slice(2, curr.level);
36
+ curr.parentIndex = Math.max(...ancestorLevelIndexes);
37
+ // Mark that curr.level was last seen at the current index.
38
+ prevIndexForLevel[curr.level] = currIndex;
39
+ });
40
+
41
+ const rootNodes: TOCTreeNode[] = [];
42
+
43
+ // For a given parentIndex, add each Node into that parent's `children` array
44
+ headings.forEach((heading) => {
45
+ const {parentIndex, ...rest} = heading;
46
+ if (parentIndex >= 0) {
47
+ headings[parentIndex]!.children.push(rest);
48
+ } else {
49
+ rootNodes.push(rest);
50
+ }
51
+ });
52
+ return rootNodes;
53
+ }
54
+
55
+ /**
56
+ * Takes a flat TOC list (from the MDX loader) and treeifies it into what the
57
+ * TOC components expect. Memoized for performance.
58
+ */
59
+ export function useTreeifiedTOC(toc: TOCItem[]): readonly TOCTreeNode[] {
60
+ return useMemo(() => treeifyTOC(toc), [toc]);
61
+ }
62
+
63
+ function filterTOC({
64
+ toc,
65
+ minHeadingLevel,
66
+ maxHeadingLevel,
67
+ }: {
68
+ toc: readonly TOCTreeNode[];
69
+ minHeadingLevel: number;
70
+ maxHeadingLevel: number;
71
+ }): TOCTreeNode[] {
72
+ function isValid(item: TOCTreeNode) {
73
+ return item.level >= minHeadingLevel && item.level <= maxHeadingLevel;
74
+ }
75
+
76
+ return toc.flatMap((item) => {
77
+ const filteredChildren = filterTOC({
78
+ toc: item.children,
79
+ minHeadingLevel,
80
+ maxHeadingLevel,
81
+ });
82
+ if (isValid(item)) {
83
+ return [
84
+ {
85
+ ...item,
86
+ children: filteredChildren,
87
+ },
88
+ ];
89
+ }
90
+ return filteredChildren;
91
+ });
92
+ }
93
+
94
+ /**
95
+ * Takes a flat TOC list (from the MDX loader) and treeifies it into what the
96
+ * TOC components expect, applying the `minHeadingLevel` and `maxHeadingLevel`.
97
+ * Memoized for performance.
98
+ *
99
+ * **Important**: this is not the same as `useTreeifiedTOC(toc.filter(...))`,
100
+ * because we have to filter the TOC after it has been treeified. This is mostly
101
+ * to ensure that weird TOC structures preserve their semantics. For example, an
102
+ * h3-h2-h4 sequence should not be treeified as an "h3 > h4" hierarchy with
103
+ * min=3, max=4, but should rather be "[h3, h4]" (since the h2 heading has split
104
+ * the two headings and they are not parent-children)
105
+ */
106
+ export function useFilteredAndTreeifiedTOC({
107
+ toc,
108
+ minHeadingLevel,
109
+ maxHeadingLevel,
110
+ }: {
111
+ toc: readonly TOCItem[];
112
+ minHeadingLevel: number;
113
+ maxHeadingLevel: number;
114
+ }): readonly TOCTreeNode[] {
115
+ return useMemo(
116
+ () => filterTOC({toc: treeifyTOC(toc), minHeadingLevel, maxHeadingLevel}),
117
+ [toc, minHeadingLevel, maxHeadingLevel],
118
+ );
119
+ }