@docusaurus/theme-common 2.0.0-beta.15a2b59f9 → 2.0.0-beta.17

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 (209) hide show
  1. package/lib/components/Collapsible/index.d.ts +36 -0
  2. package/lib/components/Collapsible/index.d.ts.map +1 -0
  3. package/lib/components/Collapsible/index.js +143 -0
  4. package/lib/components/Collapsible/index.js.map +1 -0
  5. package/lib/components/Details/index.d.ts +12 -0
  6. package/lib/components/Details/index.d.ts.map +1 -0
  7. package/lib/components/Details/index.js +66 -0
  8. package/lib/components/Details/index.js.map +1 -0
  9. package/lib/components/Details/styles.module.css +58 -0
  10. package/{src/utils/docsUtils.ts → lib/hooks/styles.css} +3 -4
  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 +10 -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 +42 -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 +56 -0
  34. package/lib/hooks/useWindowSize.js.map +1 -0
  35. package/lib/index.d.ts +32 -3
  36. package/lib/index.d.ts.map +1 -0
  37. package/lib/index.js +26 -2
  38. package/lib/index.js.map +1 -0
  39. package/lib/utils/ThemeClassNames.d.ts +40 -12
  40. package/lib/utils/ThemeClassNames.d.ts.map +1 -0
  41. package/lib/utils/ThemeClassNames.js +41 -3
  42. package/lib/utils/ThemeClassNames.js.map +1 -0
  43. package/lib/utils/announcementBarUtils.d.ts +6 -5
  44. package/lib/utils/announcementBarUtils.d.ts.map +1 -0
  45. package/lib/utils/announcementBarUtils.js +20 -21
  46. package/lib/utils/announcementBarUtils.js.map +1 -0
  47. package/lib/utils/codeBlockUtils.d.ts +10 -0
  48. package/lib/utils/codeBlockUtils.d.ts.map +1 -0
  49. package/lib/utils/codeBlockUtils.js +125 -3
  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 +107 -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 +23 -0
  58. package/lib/utils/docSidebarItemsExpandedState.js.map +1 -0
  59. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +3 -2
  60. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts.map +1 -0
  61. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +9 -11
  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 +2 -3
  66. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js.map +1 -0
  67. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +12 -3
  68. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts.map +1 -0
  69. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +3 -3
  70. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js.map +1 -0
  71. package/lib/utils/docsUtils.d.ts +26 -0
  72. package/lib/utils/docsUtils.d.ts.map +1 -0
  73. package/lib/utils/docsUtils.js +136 -1
  74. package/lib/utils/docsUtils.js.map +1 -0
  75. package/lib/utils/generalUtils.d.ts +7 -0
  76. package/lib/utils/generalUtils.d.ts.map +1 -0
  77. package/lib/utils/generalUtils.js +3 -2
  78. package/lib/utils/generalUtils.js.map +1 -0
  79. package/lib/utils/historyUtils.d.ts +23 -0
  80. package/lib/utils/historyUtils.d.ts.map +1 -0
  81. package/lib/utils/historyUtils.js +41 -0
  82. package/lib/utils/historyUtils.js.map +1 -0
  83. package/lib/utils/jsUtils.d.ts +23 -0
  84. package/lib/utils/jsUtils.d.ts.map +1 -0
  85. package/lib/utils/jsUtils.js +29 -0
  86. package/lib/utils/jsUtils.js.map +1 -0
  87. package/lib/utils/mobileSecondaryMenu.d.ts +21 -0
  88. package/lib/utils/mobileSecondaryMenu.d.ts.map +1 -0
  89. package/lib/utils/mobileSecondaryMenu.js +51 -0
  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 +5 -4
  94. package/lib/utils/pathUtils.js.map +1 -0
  95. package/lib/utils/reactUtils.d.ts +31 -0
  96. package/lib/utils/reactUtils.d.ts.map +1 -0
  97. package/lib/utils/reactUtils.js +43 -0
  98. package/lib/utils/reactUtils.js.map +1 -0
  99. package/lib/utils/regexpUtils.d.ts +11 -0
  100. package/lib/utils/regexpUtils.d.ts.map +1 -0
  101. package/lib/utils/regexpUtils.js +17 -0
  102. package/lib/utils/regexpUtils.js.map +1 -0
  103. package/lib/utils/routesUtils.d.ts +14 -0
  104. package/lib/utils/routesUtils.d.ts.map +1 -0
  105. package/lib/utils/routesUtils.js +41 -0
  106. package/lib/utils/routesUtils.js.map +1 -0
  107. package/lib/utils/scrollUtils.d.ts +53 -0
  108. package/lib/utils/scrollUtils.d.ts.map +1 -0
  109. package/lib/utils/scrollUtils.js +135 -0
  110. package/lib/utils/scrollUtils.js.map +1 -0
  111. package/lib/utils/searchUtils.d.ts +1 -0
  112. package/lib/utils/searchUtils.d.ts.map +1 -0
  113. package/lib/utils/searchUtils.js +1 -0
  114. package/lib/utils/searchUtils.js.map +1 -0
  115. package/lib/utils/storageUtils.d.ts +5 -0
  116. package/lib/utils/storageUtils.d.ts.map +1 -0
  117. package/lib/utils/storageUtils.js +39 -14
  118. package/lib/utils/storageUtils.js.map +1 -0
  119. package/lib/utils/tabGroupChoiceUtils.d.ts +19 -0
  120. package/lib/utils/tabGroupChoiceUtils.d.ts.map +1 -0
  121. package/lib/utils/tabGroupChoiceUtils.js +55 -0
  122. package/lib/utils/tabGroupChoiceUtils.js.map +1 -0
  123. package/lib/utils/tagsUtils.d.ts +19 -0
  124. package/lib/utils/tagsUtils.d.ts.map +1 -0
  125. package/lib/utils/tagsUtils.js +33 -0
  126. package/lib/utils/tagsUtils.js.map +1 -0
  127. package/lib/utils/tocUtils.d.ts +20 -0
  128. package/lib/utils/tocUtils.d.ts.map +1 -0
  129. package/lib/utils/tocUtils.js +73 -0
  130. package/lib/utils/tocUtils.js.map +1 -0
  131. package/lib/utils/useAlternatePageUtils.d.ts +1 -0
  132. package/lib/utils/useAlternatePageUtils.d.ts.map +1 -0
  133. package/lib/utils/useAlternatePageUtils.js +3 -1
  134. package/lib/utils/useAlternatePageUtils.js.map +1 -0
  135. package/lib/utils/useContextualSearchFilters.d.ts +12 -0
  136. package/lib/utils/useContextualSearchFilters.d.ts.map +1 -0
  137. package/lib/utils/useContextualSearchFilters.js +36 -0
  138. package/lib/utils/useContextualSearchFilters.js.map +1 -0
  139. package/lib/utils/useLocalPathname.d.ts +8 -0
  140. package/lib/utils/useLocalPathname.d.ts.map +1 -0
  141. package/lib/utils/useLocalPathname.js +17 -0
  142. package/lib/utils/useLocalPathname.js.map +1 -0
  143. package/lib/utils/useLocationChange.d.ts +2 -1
  144. package/lib/utils/useLocationChange.d.ts.map +1 -0
  145. package/lib/utils/useLocationChange.js +12 -10
  146. package/lib/utils/useLocationChange.js.map +1 -0
  147. package/lib/utils/usePluralForm.d.ts +1 -0
  148. package/lib/utils/usePluralForm.d.ts.map +1 -0
  149. package/lib/utils/usePluralForm.js +28 -24
  150. package/lib/utils/usePluralForm.js.map +1 -0
  151. package/lib/utils/usePrevious.d.ts +1 -0
  152. package/lib/utils/usePrevious.d.ts.map +1 -0
  153. package/lib/utils/usePrevious.js +4 -2
  154. package/lib/utils/usePrevious.js.map +1 -0
  155. package/lib/utils/useTOCHighlight.d.ts +14 -0
  156. package/lib/utils/useTOCHighlight.d.ts.map +1 -0
  157. package/lib/utils/useTOCHighlight.js +126 -0
  158. package/lib/utils/useTOCHighlight.js.map +1 -0
  159. package/lib/utils/useThemeConfig.d.ts +35 -17
  160. package/lib/utils/useThemeConfig.d.ts.map +1 -0
  161. package/lib/utils/useThemeConfig.js +1 -0
  162. package/lib/utils/useThemeConfig.js.map +1 -0
  163. package/package.json +19 -13
  164. package/src/components/Collapsible/index.tsx +247 -0
  165. package/src/components/Details/index.tsx +102 -0
  166. package/src/components/Details/styles.module.css +58 -0
  167. package/src/hooks/styles.css +10 -0
  168. package/src/hooks/useHideableNavbar.ts +77 -0
  169. package/src/hooks/useKeyboardNavigation.ts +37 -0
  170. package/src/hooks/useLockBodyScroll.ts +18 -0
  171. package/src/hooks/usePrismTheme.ts +20 -0
  172. package/src/hooks/useSearchPage.ts +66 -0
  173. package/src/hooks/useWindowSize.ts +70 -0
  174. package/src/index.ts +95 -3
  175. package/src/types.d.ts +0 -2
  176. package/src/utils/ThemeClassNames.ts +46 -4
  177. package/src/utils/announcementBarUtils.tsx +27 -22
  178. package/src/utils/codeBlockUtils.ts +153 -2
  179. package/src/utils/colorModeUtils.tsx +158 -0
  180. package/src/utils/docSidebarItemsExpandedState.tsx +40 -0
  181. package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +17 -16
  182. package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +3 -4
  183. package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +18 -14
  184. package/src/utils/docsUtils.tsx +231 -0
  185. package/src/utils/generalUtils.ts +3 -2
  186. package/src/utils/historyUtils.ts +51 -0
  187. package/src/utils/jsUtils.ts +36 -0
  188. package/src/utils/mobileSecondaryMenu.tsx +114 -0
  189. package/src/utils/pathUtils.ts +6 -4
  190. package/src/utils/reactUtils.tsx +53 -0
  191. package/src/utils/regexpUtils.ts +23 -0
  192. package/src/utils/routesUtils.ts +56 -0
  193. package/src/utils/scrollUtils.tsx +235 -0
  194. package/src/utils/storageUtils.ts +37 -12
  195. package/src/utils/tabGroupChoiceUtils.tsx +89 -0
  196. package/src/utils/tagsUtils.ts +48 -0
  197. package/src/utils/tocUtils.ts +108 -0
  198. package/src/utils/useAlternatePageUtils.ts +4 -3
  199. package/src/utils/useContextualSearchFilters.ts +53 -0
  200. package/src/utils/useLocalPathname.ts +20 -0
  201. package/src/utils/useLocationChange.ts +13 -11
  202. package/src/utils/usePluralForm.ts +31 -26
  203. package/src/utils/usePrevious.ts +3 -2
  204. package/src/utils/useTOCHighlight.ts +183 -0
  205. package/src/utils/useThemeConfig.ts +37 -17
  206. package/lib/.tsbuildinfo +0 -1
  207. package/src/utils/__tests__/codeBlockUtils.test.ts +0 -54
  208. package/src/utils/__tests__/pathUtils.test.ts +0 -32
  209. package/tsconfig.json +0 -10
@@ -0,0 +1,235 @@
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
+ createContext,
10
+ type ReactNode,
11
+ useCallback,
12
+ useContext,
13
+ useEffect,
14
+ useLayoutEffect,
15
+ useMemo,
16
+ useRef,
17
+ } from 'react';
18
+ import {useDynamicCallback, ReactContextError} from './reactUtils';
19
+ import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
20
+
21
+ /**
22
+ * We need a way to update the scroll position while ignoring scroll events
23
+ * without affecting Navbar/BackToTop visibility
24
+ *
25
+ * This API permits to temporarily disable/ignore scroll events
26
+ * Motivated by https://github.com/facebook/docusaurus/pull/5618
27
+ */
28
+ type ScrollController = {
29
+ /**
30
+ * A boolean ref tracking whether scroll events are enabled
31
+ */
32
+ scrollEventsEnabledRef: React.MutableRefObject<boolean>;
33
+ /**
34
+ * Enables scroll events in `useScrollPosition`
35
+ */
36
+ enableScrollEvents: () => void;
37
+ /**
38
+ * Disables scroll events in `useScrollPosition`
39
+ */
40
+ disableScrollEvents: () => void;
41
+ };
42
+
43
+ function useScrollControllerContextValue(): ScrollController {
44
+ const scrollEventsEnabledRef = useRef(true);
45
+
46
+ return useMemo(
47
+ () => ({
48
+ scrollEventsEnabledRef,
49
+ enableScrollEvents: () => {
50
+ scrollEventsEnabledRef.current = true;
51
+ },
52
+ disableScrollEvents: () => {
53
+ scrollEventsEnabledRef.current = false;
54
+ },
55
+ }),
56
+ [],
57
+ );
58
+ }
59
+
60
+ const ScrollMonitorContext = createContext<ScrollController | undefined>(
61
+ undefined,
62
+ );
63
+
64
+ export function ScrollControllerProvider({
65
+ children,
66
+ }: {
67
+ children: ReactNode;
68
+ }): JSX.Element {
69
+ return (
70
+ <ScrollMonitorContext.Provider value={useScrollControllerContextValue()}>
71
+ {children}
72
+ </ScrollMonitorContext.Provider>
73
+ );
74
+ }
75
+
76
+ export function useScrollController(): ScrollController {
77
+ const context = useContext(ScrollMonitorContext);
78
+ if (context == null) {
79
+ throw new ReactContextError('ScrollControllerProvider');
80
+ }
81
+ return context;
82
+ }
83
+
84
+ const getScrollPosition = (): ScrollPosition | null =>
85
+ ExecutionEnvironment.canUseDOM
86
+ ? {
87
+ scrollX: window.pageXOffset,
88
+ scrollY: window.pageYOffset,
89
+ }
90
+ : null;
91
+
92
+ type ScrollPosition = {scrollX: number; scrollY: number};
93
+
94
+ export function useScrollPosition(
95
+ effect: (
96
+ position: ScrollPosition,
97
+ lastPosition: ScrollPosition | null,
98
+ ) => void,
99
+ deps: unknown[] = [],
100
+ ): void {
101
+ const {scrollEventsEnabledRef} = useScrollController();
102
+ const lastPositionRef = useRef<ScrollPosition | null>(getScrollPosition());
103
+
104
+ const dynamicEffect = useDynamicCallback(effect);
105
+
106
+ useEffect(() => {
107
+ const handleScroll = () => {
108
+ if (!scrollEventsEnabledRef.current) {
109
+ return;
110
+ }
111
+ const currentPosition = getScrollPosition()!;
112
+
113
+ if (dynamicEffect) {
114
+ dynamicEffect(currentPosition, lastPositionRef.current);
115
+ }
116
+
117
+ lastPositionRef.current = currentPosition;
118
+ };
119
+
120
+ const opts: AddEventListenerOptions & EventListenerOptions = {
121
+ passive: true,
122
+ };
123
+
124
+ handleScroll();
125
+ window.addEventListener('scroll', handleScroll, opts);
126
+
127
+ return () => window.removeEventListener('scroll', handleScroll, opts);
128
+ }, [
129
+ dynamicEffect,
130
+ scrollEventsEnabledRef,
131
+ // eslint-disable-next-line react-hooks/exhaustive-deps
132
+ ...deps,
133
+ ]);
134
+ }
135
+
136
+ type UseScrollPositionSaver = {
137
+ /**
138
+ * Measure the top of an element, and store the details
139
+ */
140
+ save: (elem: HTMLElement) => void;
141
+ /**
142
+ * Restore the page position to keep the stored element's position from
143
+ * the top of the viewport, and remove the stored details
144
+ */
145
+ restore: () => {restored: boolean};
146
+ };
147
+
148
+ function useScrollPositionSaver(): UseScrollPositionSaver {
149
+ const lastElementRef = useRef<{elem: HTMLElement | null; top: number}>({
150
+ elem: null,
151
+ top: 0,
152
+ });
153
+
154
+ const save = useCallback((elem: HTMLElement) => {
155
+ lastElementRef.current = {
156
+ elem,
157
+ top: elem.getBoundingClientRect().top,
158
+ };
159
+ }, []);
160
+
161
+ const restore = useCallback(() => {
162
+ const {
163
+ current: {elem, top},
164
+ } = lastElementRef;
165
+ if (!elem) {
166
+ return {restored: false};
167
+ }
168
+ const newTop = elem.getBoundingClientRect().top;
169
+ const heightDiff = newTop - top;
170
+ if (heightDiff) {
171
+ window.scrollBy({left: 0, top: heightDiff});
172
+ }
173
+ lastElementRef.current = {elem: null, top: 0};
174
+
175
+ return {restored: heightDiff !== 0};
176
+ }, []);
177
+
178
+ return useMemo(() => ({save, restore}), [restore, save]);
179
+ }
180
+
181
+ type UseScrollPositionBlockerReturn = {
182
+ blockElementScrollPositionUntilNextRender: (el: HTMLElement) => void;
183
+ };
184
+
185
+ /**
186
+ * This hook permits to "block" the scroll position of a dom element
187
+ * The idea is that we should be able to update DOM content above this element
188
+ * but the screen position of this element should not change
189
+ *
190
+ * Feature motivated by the Tabs groups:
191
+ * clicking on a tab may affect tabs of the same group upper in the tree
192
+ * Yet to avoid a bad UX, the clicked tab must remain under the user mouse!
193
+ * See GIF here: https://github.com/facebook/docusaurus/pull/5618
194
+ */
195
+ export function useScrollPositionBlocker(): UseScrollPositionBlockerReturn {
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
212
+ // We need to wait for next scroll event to happen
213
+ // before enabling again the scrollController events
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
+ }
@@ -11,8 +11,8 @@ 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
+ // Will return null browser storage is unavailable (like running Docusaurus in
15
+ // iframe) See https://github.com/facebook/docusaurus/pull/4501
16
16
  function getBrowserStorage(
17
17
  storageType: StorageType = DefaultStorageType,
18
18
  ): Storage | null {
@@ -23,13 +23,12 @@ function getBrowserStorage(
23
23
  }
24
24
  if (storageType === 'none') {
25
25
  return null;
26
- } else {
27
- try {
28
- return window[storageType];
29
- } catch (e) {
30
- logOnceBrowserStorageNotAvailableWarning(e);
31
- return null;
32
- }
26
+ }
27
+ try {
28
+ return window[storageType];
29
+ } catch (err) {
30
+ logOnceBrowserStorageNotAvailableWarning(err as Error);
31
+ return null;
33
32
  }
34
33
  }
35
34
 
@@ -79,6 +78,10 @@ Please only call storage APIs in effects and event handlers.`);
79
78
 
80
79
  /**
81
80
  * Creates an object for accessing a particular key in localStorage.
81
+ * The API is fail-safe, and usage of browser storage should be considered
82
+ * unreliable. Local storage might simply be unavailable (iframe + browser
83
+ * security) or operations might fail individually. Please assume that using
84
+ * this API can be a NO-OP. See also https://github.com/facebook/docusaurus/issues/6036
82
85
  */
83
86
  export const createStorageSlot = (
84
87
  key: string,
@@ -92,9 +95,31 @@ export const createStorageSlot = (
92
95
  return NoopStorageSlot;
93
96
  }
94
97
  return {
95
- get: () => browserStorage.getItem(key),
96
- set: (value) => browserStorage.setItem(key, value),
97
- del: () => browserStorage.removeItem(key),
98
+ get: () => {
99
+ try {
100
+ return browserStorage.getItem(key);
101
+ } catch (err) {
102
+ console.error(`Docusaurus storage error, can't get key=${key}`, err);
103
+ return null;
104
+ }
105
+ },
106
+ set: (value) => {
107
+ try {
108
+ browserStorage.setItem(key, value);
109
+ } catch (err) {
110
+ console.error(
111
+ `Docusaurus storage error, can't set ${key}=${value}`,
112
+ err,
113
+ );
114
+ }
115
+ },
116
+ del: () => {
117
+ try {
118
+ browserStorage.removeItem(key);
119
+ } catch (err) {
120
+ console.error(`Docusaurus storage error, can't delete key=${key}`, err);
121
+ }
122
+ },
98
123
  };
99
124
  };
100
125
 
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import React, {
9
+ useState,
10
+ useCallback,
11
+ useEffect,
12
+ createContext,
13
+ useMemo,
14
+ useContext,
15
+ type ReactNode,
16
+ } from 'react';
17
+ import {createStorageSlot, listStorageKeys} from './storageUtils';
18
+ import {ReactContextError} from './reactUtils';
19
+
20
+ const TAB_CHOICE_PREFIX = 'docusaurus.tab.';
21
+
22
+ type TabGroupChoiceContextValue = {
23
+ readonly tabGroupChoices: {readonly [groupId: string]: string};
24
+ readonly setTabGroupChoices: (groupId: string, newChoice: string) => void;
25
+ };
26
+
27
+ const TabGroupChoiceContext = createContext<
28
+ TabGroupChoiceContextValue | undefined
29
+ >(undefined);
30
+
31
+ function useTabGroupChoiceContextValue(): TabGroupChoiceContextValue {
32
+ const [tabGroupChoices, setChoices] = useState<{
33
+ readonly [groupId: string]: string;
34
+ }>({});
35
+ const setChoiceSyncWithLocalStorage = useCallback((groupId, newChoice) => {
36
+ createStorageSlot(`${TAB_CHOICE_PREFIX}${groupId}`).set(newChoice);
37
+ }, []);
38
+
39
+ useEffect(() => {
40
+ try {
41
+ const localStorageChoices: Record<string, string> = {};
42
+ listStorageKeys().forEach((storageKey) => {
43
+ if (storageKey.startsWith(TAB_CHOICE_PREFIX)) {
44
+ const groupId = storageKey.substring(TAB_CHOICE_PREFIX.length);
45
+ localStorageChoices[groupId] = createStorageSlot(storageKey).get()!;
46
+ }
47
+ });
48
+ setChoices(localStorageChoices);
49
+ } catch (err) {
50
+ console.error(err);
51
+ }
52
+ }, []);
53
+
54
+ return {
55
+ tabGroupChoices,
56
+ setTabGroupChoices: (groupId: string, newChoice: string) => {
57
+ setChoices((oldChoices) => ({...oldChoices, [groupId]: newChoice}));
58
+ setChoiceSyncWithLocalStorage(groupId, newChoice);
59
+ },
60
+ };
61
+ }
62
+
63
+ export function TabGroupChoiceProvider({
64
+ children,
65
+ }: {
66
+ children: ReactNode;
67
+ }): JSX.Element {
68
+ const {tabGroupChoices, setTabGroupChoices} = useTabGroupChoiceContextValue();
69
+ const contextValue = useMemo(
70
+ () => ({
71
+ tabGroupChoices,
72
+ setTabGroupChoices,
73
+ }),
74
+ [tabGroupChoices, setTabGroupChoices],
75
+ );
76
+ return (
77
+ <TabGroupChoiceContext.Provider value={contextValue}>
78
+ {children}
79
+ </TabGroupChoiceContext.Provider>
80
+ );
81
+ }
82
+
83
+ export function useTabGroupChoice(): TabGroupChoiceContextValue {
84
+ const context = useContext(TabGroupChoiceContext);
85
+ if (context == null) {
86
+ throw new ReactContextError('TabGroupChoiceProvider');
87
+ }
88
+ return context;
89
+ }
@@ -0,0 +1,48 @@
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
+
10
+ export const translateTagsPageTitle = (): string =>
11
+ translate({
12
+ id: 'theme.tags.tagsPageTitle',
13
+ message: 'Tags',
14
+ description: 'The title of the tag list page',
15
+ });
16
+
17
+ type TagsListItem = Readonly<{name: string; permalink: string; count: number}>; // TODO remove duplicated type :s
18
+
19
+ export type TagLetterEntry = Readonly<{letter: string; tags: TagsListItem[]}>;
20
+
21
+ function getTagLetter(tag: string): string {
22
+ return tag[0].toUpperCase();
23
+ }
24
+
25
+ export function listTagsByLetters(
26
+ tags: readonly TagsListItem[],
27
+ ): TagLetterEntry[] {
28
+ // Group by letters
29
+ const groups: Record<string, TagsListItem[]> = {};
30
+ Object.values(tags).forEach((tag) => {
31
+ const letter = getTagLetter(tag.name);
32
+ groups[letter] = groups[letter] ?? [];
33
+ groups[letter].push(tag);
34
+ });
35
+
36
+ return (
37
+ Object.entries(groups)
38
+ // Sort letters
39
+ .sort(([letter1], [letter2]) => letter1.localeCompare(letter2))
40
+ .map(([letter, letterTags]) => {
41
+ // Sort tags inside a letter
42
+ const sortedTags = letterTags.sort((tag1, tag2) =>
43
+ tag1.name.localeCompare(tag2.name),
44
+ );
45
+ return {letter, tags: sortedTags};
46
+ })
47
+ );
48
+ }
@@ -0,0 +1,108 @@
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/types';
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
34
+ // index will 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
+ export function useTreeifiedTOC(toc: TOCItem[]): readonly TOCTreeNode[] {
56
+ return useMemo(() => treeifyTOC(toc), [toc]);
57
+ }
58
+
59
+ function filterTOC({
60
+ toc,
61
+ minHeadingLevel,
62
+ maxHeadingLevel,
63
+ }: {
64
+ toc: readonly TOCTreeNode[];
65
+ minHeadingLevel: number;
66
+ maxHeadingLevel: number;
67
+ }): TOCTreeNode[] {
68
+ function isValid(item: TOCTreeNode) {
69
+ return item.level >= minHeadingLevel && item.level <= maxHeadingLevel;
70
+ }
71
+
72
+ return toc.flatMap((item) => {
73
+ const filteredChildren = filterTOC({
74
+ toc: item.children,
75
+ minHeadingLevel,
76
+ maxHeadingLevel,
77
+ });
78
+ if (isValid(item)) {
79
+ return [
80
+ {
81
+ ...item,
82
+ children: filteredChildren,
83
+ },
84
+ ];
85
+ }
86
+ return filteredChildren;
87
+ });
88
+ }
89
+
90
+ export function useFilteredAndTreeifiedTOC({
91
+ toc,
92
+ minHeadingLevel,
93
+ maxHeadingLevel,
94
+ }: {
95
+ toc: readonly TOCItem[];
96
+ minHeadingLevel: number;
97
+ maxHeadingLevel: number;
98
+ }): readonly TOCTreeNode[] {
99
+ return useMemo(
100
+ () =>
101
+ // Note: we have to filter the TOC after it has been treeified. This is
102
+ // mostly to ensure that weird TOC structures preserve their semantics.
103
+ // For example, an h3-h2-h4 sequence should not be treeified as an h3 > h4
104
+ // hierarchy with min=3, max=4, but should rather be [h3, h4]
105
+ filterTOC({toc: treeifyTOC(toc), minHeadingLevel, maxHeadingLevel}),
106
+ [toc, minHeadingLevel, maxHeadingLevel],
107
+ );
108
+ }
@@ -39,14 +39,15 @@ export function useAlternatePageUtils(): {
39
39
  : `${baseUrlUnlocalized}${locale}/`;
40
40
  }
41
41
 
42
- // TODO support correct alternate url when localized site is deployed on another domain
42
+ // TODO support correct alternate url when localized site is deployed on
43
+ // another domain
43
44
  function createUrl({
44
45
  locale,
45
46
  fullyQualified,
46
47
  }: {
47
48
  locale: string;
48
- // For hreflang SEO headers, we need it to be fully qualified (full protocol/domain/path...)
49
- // For locale dropdown, using a path is good enough
49
+ // For hreflang SEO headers, we need it to be fully qualified (full
50
+ // protocol/domain/path...) or locale dropdown, using a path is good enough
50
51
  fullyQualified: boolean;
51
52
  }) {
52
53
  return `${fullyQualified ? url : ''}${getLocalizedBaseUrl(
@@ -0,0 +1,53 @@
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 {
9
+ useAllDocsData,
10
+ useActivePluginAndVersion,
11
+ } from '@docusaurus/plugin-content-docs/client';
12
+ import {useDocsPreferredVersionByPluginId} from './docsPreferredVersion/useDocsPreferredVersion';
13
+ import {docVersionSearchTag, DEFAULT_SEARCH_TAG} from './searchUtils';
14
+ import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
15
+
16
+ export type useContextualSearchFiltersReturns = {
17
+ locale: string;
18
+ tags: string[];
19
+ };
20
+
21
+ // We may want to support multiple search engines, don't couple that to
22
+ // Algolia/DocSearch. Maybe users want to use their own search engine solution
23
+ export function useContextualSearchFilters(): useContextualSearchFiltersReturns {
24
+ const {i18n} = useDocusaurusContext();
25
+ const allDocsData = useAllDocsData();
26
+ const activePluginAndVersion = useActivePluginAndVersion();
27
+ const docsPreferredVersionByPluginId = useDocsPreferredVersionByPluginId();
28
+
29
+ function getDocPluginTags(pluginId: string) {
30
+ const activeVersion =
31
+ activePluginAndVersion?.activePlugin?.pluginId === pluginId
32
+ ? activePluginAndVersion.activeVersion
33
+ : undefined;
34
+
35
+ const preferredVersion = docsPreferredVersionByPluginId[pluginId];
36
+
37
+ const latestVersion = allDocsData[pluginId].versions.find((v) => v.isLast)!;
38
+
39
+ const version = activeVersion ?? preferredVersion ?? latestVersion;
40
+
41
+ return docVersionSearchTag(pluginId, version.name);
42
+ }
43
+
44
+ const tags = [
45
+ DEFAULT_SEARCH_TAG,
46
+ ...Object.keys(allDocsData).map(getDocPluginTags),
47
+ ];
48
+
49
+ return {
50
+ locale: i18n.currentLocale,
51
+ tags,
52
+ };
53
+ }
@@ -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 useDocusaurusContext from '@docusaurus/useDocusaurusContext';
9
+ import {useLocation} from '@docusaurus/router';
10
+
11
+ // Get the pathname of current route, without the optional site baseUrl
12
+ // - /docs/myDoc => /docs/myDoc
13
+ // - /baseUrl/docs/myDoc => /docs/myDoc
14
+ export function useLocalPathname(): string {
15
+ const {
16
+ siteConfig: {baseUrl},
17
+ } = useDocusaurusContext();
18
+ const {pathname} = useLocation();
19
+ return pathname.replace(baseUrl, '/');
20
+ }
@@ -5,10 +5,11 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import {useEffect, useRef} from 'react';
8
+ import {useEffect} from 'react';
9
9
  import {useLocation} from '@docusaurus/router';
10
- import {Location} from '@docusaurus/history';
10
+ import type {Location} from 'history';
11
11
  import {usePrevious} from './usePrevious';
12
+ import {useDynamicCallback} from './reactUtils';
12
13
 
13
14
  type LocationChangeEvent = {
14
15
  location: Location;
@@ -20,18 +21,19 @@ type OnLocationChange = (locationChangeEvent: LocationChangeEvent) => void;
20
21
  export function useLocationChange(onLocationChange: OnLocationChange): void {
21
22
  const location = useLocation();
22
23
  const previousLocation = usePrevious(location);
23
- const isFirst = useRef<boolean>(true);
24
+
25
+ const onLocationChangeDynamic = useDynamicCallback(onLocationChange);
24
26
 
25
27
  useEffect(() => {
26
- // Prevent first effect to trigger the listener on mount
27
- if (isFirst.current) {
28
- isFirst.current = false;
28
+ if (!previousLocation) {
29
29
  return;
30
30
  }
31
31
 
32
- onLocationChange({
33
- location,
34
- previousLocation,
35
- });
36
- }, [location]);
32
+ if (location !== previousLocation) {
33
+ onLocationChangeDynamic({
34
+ location,
35
+ previousLocation,
36
+ });
37
+ }
38
+ }, [onLocationChangeDynamic, location, previousLocation]);
37
39
  }