@docusaurus/theme-common 2.0.0-beta.8e9b829d9 → 2.0.0-beta.9

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 (60) hide show
  1. package/lib/.tsbuildinfo +1 -1
  2. package/lib/components/Collapsible/index.js +5 -13
  3. package/lib/components/Details/index.js +3 -3
  4. package/lib/components/Details/styles.module.css +8 -9
  5. package/lib/index.d.ts +11 -1
  6. package/lib/index.js +8 -0
  7. package/lib/utils/ThemeClassNames.d.ts +36 -12
  8. package/lib/utils/ThemeClassNames.js +36 -3
  9. package/lib/utils/announcementBarUtils.d.ts +1 -1
  10. package/lib/utils/announcementBarUtils.js +6 -6
  11. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +1 -1
  12. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +5 -3
  13. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +1 -2
  14. package/lib/utils/generalUtils.js +2 -2
  15. package/lib/utils/historyUtils.d.ts +11 -0
  16. package/lib/utils/historyUtils.js +42 -0
  17. package/lib/utils/jsUtils.d.ts +13 -0
  18. package/lib/utils/jsUtils.js +16 -0
  19. package/lib/utils/mobileSecondaryMenu.js +1 -0
  20. package/lib/utils/reactUtils.d.ts +9 -0
  21. package/lib/utils/reactUtils.js +26 -0
  22. package/lib/utils/regexpUtils.d.ts +10 -0
  23. package/lib/utils/regexpUtils.js +16 -0
  24. package/lib/utils/scrollUtils.d.ts +52 -0
  25. package/lib/utils/scrollUtils.js +137 -0
  26. package/lib/utils/tagsUtils.d.ts +18 -0
  27. package/lib/utils/tagsUtils.js +33 -0
  28. package/lib/utils/tocUtils.d.ts +15 -0
  29. package/lib/utils/tocUtils.js +36 -0
  30. package/lib/utils/useLocationChange.js +5 -9
  31. package/lib/utils/usePrevious.js +3 -2
  32. package/lib/utils/useTOCHighlight.d.ts +14 -0
  33. package/lib/utils/useTOCHighlight.js +124 -0
  34. package/lib/utils/useThemeConfig.d.ts +13 -2
  35. package/package.json +12 -10
  36. package/src/components/Collapsible/index.tsx +6 -18
  37. package/src/components/Details/index.tsx +3 -3
  38. package/src/components/Details/styles.module.css +8 -9
  39. package/src/index.ts +27 -0
  40. package/src/types.d.ts +0 -2
  41. package/src/utils/ThemeClassNames.ts +42 -4
  42. package/src/utils/__tests__/tagUtils.test.ts +66 -0
  43. package/src/utils/__tests__/tocUtils.test.ts +197 -0
  44. package/src/utils/announcementBarUtils.tsx +7 -7
  45. package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +4 -4
  46. package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +13 -14
  47. package/src/utils/generalUtils.ts +2 -2
  48. package/src/utils/historyUtils.ts +50 -0
  49. package/src/utils/jsUtils.ts +23 -0
  50. package/src/utils/mobileSecondaryMenu.tsx +2 -1
  51. package/src/utils/reactUtils.tsx +34 -0
  52. package/src/utils/regexpUtils.ts +23 -0
  53. package/src/utils/scrollUtils.tsx +238 -0
  54. package/src/utils/storageUtils.ts +1 -1
  55. package/src/utils/tagsUtils.ts +48 -0
  56. package/src/utils/tocUtils.ts +54 -0
  57. package/src/utils/useLocationChange.ts +6 -10
  58. package/src/utils/usePrevious.ts +3 -2
  59. package/src/utils/useTOCHighlight.ts +179 -0
  60. package/src/utils/useThemeConfig.ts +17 -2
@@ -0,0 +1,197 @@
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 {TOCItem} from '@docusaurus/types';
9
+ import {filterTOC} from '../tocUtils';
10
+
11
+ describe('filterTOC', () => {
12
+ test('filter a toc with all heading levels', () => {
13
+ const toc: TOCItem[] = [
14
+ {
15
+ id: 'alpha',
16
+ level: 1,
17
+ value: 'alpha',
18
+ children: [
19
+ {
20
+ id: 'bravo',
21
+ level: 2,
22
+ value: 'Bravo',
23
+ children: [
24
+ {
25
+ id: 'charlie',
26
+ level: 3,
27
+ value: 'Charlie',
28
+ children: [
29
+ {
30
+ id: 'delta',
31
+ level: 4,
32
+ value: 'Delta',
33
+ children: [
34
+ {
35
+ id: 'echo',
36
+ level: 5,
37
+ value: 'Echo',
38
+ children: [
39
+ {
40
+ id: 'foxtrot',
41
+ level: 6,
42
+ value: 'Foxtrot',
43
+ children: [],
44
+ },
45
+ ],
46
+ },
47
+ ],
48
+ },
49
+ ],
50
+ },
51
+ ],
52
+ },
53
+ ],
54
+ },
55
+ ];
56
+
57
+ expect(filterTOC({toc, minHeadingLevel: 2, maxHeadingLevel: 2})).toEqual([
58
+ {
59
+ id: 'bravo',
60
+ level: 2,
61
+ value: 'Bravo',
62
+ children: [],
63
+ },
64
+ ]);
65
+
66
+ expect(filterTOC({toc, minHeadingLevel: 3, maxHeadingLevel: 3})).toEqual([
67
+ {
68
+ id: 'charlie',
69
+ level: 3,
70
+ value: 'Charlie',
71
+ children: [],
72
+ },
73
+ ]);
74
+
75
+ expect(filterTOC({toc, minHeadingLevel: 2, maxHeadingLevel: 3})).toEqual([
76
+ {
77
+ id: 'bravo',
78
+ level: 2,
79
+ value: 'Bravo',
80
+ children: [
81
+ {
82
+ id: 'charlie',
83
+ level: 3,
84
+ value: 'Charlie',
85
+ children: [],
86
+ },
87
+ ],
88
+ },
89
+ ]);
90
+
91
+ expect(filterTOC({toc, minHeadingLevel: 2, maxHeadingLevel: 4})).toEqual([
92
+ {
93
+ id: 'bravo',
94
+ level: 2,
95
+ value: 'Bravo',
96
+ children: [
97
+ {
98
+ id: 'charlie',
99
+ level: 3,
100
+ value: 'Charlie',
101
+ children: [
102
+ {
103
+ id: 'delta',
104
+ level: 4,
105
+ value: 'Delta',
106
+ children: [],
107
+ },
108
+ ],
109
+ },
110
+ ],
111
+ },
112
+ ]);
113
+ });
114
+
115
+ // It's not 100% clear exactly how the TOC should behave under weird heading levels provided by the user
116
+ // Adding a test so that behavior stays the same over time
117
+ test('filter invalid heading levels (but possible) TOC', () => {
118
+ const toc: TOCItem[] = [
119
+ {
120
+ id: 'charlie',
121
+ level: 3,
122
+ value: 'Charlie',
123
+ children: [],
124
+ },
125
+ {
126
+ id: 'bravo',
127
+ level: 2,
128
+ value: 'Bravo',
129
+ children: [
130
+ {
131
+ id: 'delta',
132
+ level: 4,
133
+ value: 'Delta',
134
+ children: [],
135
+ },
136
+ ],
137
+ },
138
+ ];
139
+
140
+ expect(filterTOC({toc, minHeadingLevel: 2, maxHeadingLevel: 2})).toEqual([
141
+ {
142
+ id: 'bravo',
143
+ level: 2,
144
+ value: 'Bravo',
145
+ children: [],
146
+ },
147
+ ]);
148
+
149
+ expect(filterTOC({toc, minHeadingLevel: 3, maxHeadingLevel: 3})).toEqual([
150
+ {
151
+ id: 'charlie',
152
+ level: 3,
153
+ value: 'Charlie',
154
+ children: [],
155
+ },
156
+ ]);
157
+
158
+ expect(filterTOC({toc, minHeadingLevel: 4, maxHeadingLevel: 4})).toEqual([
159
+ {
160
+ id: 'delta',
161
+ level: 4,
162
+ value: 'Delta',
163
+ children: [],
164
+ },
165
+ ]);
166
+
167
+ expect(filterTOC({toc, minHeadingLevel: 2, maxHeadingLevel: 3})).toEqual([
168
+ {
169
+ id: 'charlie',
170
+ level: 3,
171
+ value: 'Charlie',
172
+ children: [],
173
+ },
174
+ {
175
+ id: 'bravo',
176
+ level: 2,
177
+ value: 'Bravo',
178
+ children: [],
179
+ },
180
+ ]);
181
+
182
+ expect(filterTOC({toc, minHeadingLevel: 3, maxHeadingLevel: 4})).toEqual([
183
+ {
184
+ id: 'charlie',
185
+ level: 3,
186
+ value: 'Charlie',
187
+ children: [],
188
+ },
189
+ {
190
+ id: 'delta',
191
+ level: 4,
192
+ value: 'Delta',
193
+ children: [],
194
+ },
195
+ ]);
196
+ });
197
+ });
@@ -14,7 +14,7 @@ import React, {
14
14
  useContext,
15
15
  createContext,
16
16
  } from 'react';
17
- import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
17
+ import useIsBrowser from '@docusaurus/useIsBrowser';
18
18
  import {createStorageSlot} from './storageUtils';
19
19
  import {useThemeConfig} from './useThemeConfig';
20
20
 
@@ -33,16 +33,16 @@ const setDismissedInStorage = (bool: boolean) =>
33
33
  AnnouncementBarDismissStorage.set(String(bool));
34
34
 
35
35
  type AnnouncementBarAPI = {
36
- readonly isClosed: boolean;
36
+ readonly isActive: boolean;
37
37
  readonly close: () => void;
38
38
  };
39
39
 
40
40
  const useAnnouncementBarContextValue = (): AnnouncementBarAPI => {
41
41
  const {announcementBar} = useThemeConfig();
42
- const {isClient} = useDocusaurusContext();
42
+ const isBrowser = useIsBrowser();
43
43
 
44
44
  const [isClosed, setClosed] = useState(() => {
45
- return isClient
45
+ return isBrowser
46
46
  ? // On client navigation: init with localstorage value
47
47
  isDismissedInStorage()
48
48
  : // On server/hydration: always visible to prevent layout shifts (will be hidden with css if needed)
@@ -83,14 +83,14 @@ const useAnnouncementBarContextValue = (): AnnouncementBarAPI => {
83
83
  if (isNewAnnouncement || !isDismissedInStorage()) {
84
84
  setClosed(false);
85
85
  }
86
- }, []);
86
+ }, [announcementBar]);
87
87
 
88
88
  return useMemo(() => {
89
89
  return {
90
- isClosed,
90
+ isActive: !!announcementBar && !isClosed,
91
91
  close: handleClose,
92
92
  };
93
- }, [isClosed]);
93
+ }, [announcementBar, isClosed, handleClose]);
94
94
  };
95
95
 
96
96
  const AnnouncementBarContext = createContext<AnnouncementBarAPI | null>(null);
@@ -15,7 +15,7 @@ import React, {
15
15
  import {useThemeConfig, DocsVersionPersistence} from '../useThemeConfig';
16
16
  import {isDocsPluginEnabled} from '../docsUtils';
17
17
 
18
- import {useAllDocsData} from '@theme/hooks/useDocs';
18
+ import {useAllDocsData, GlobalPluginData} from '@theme/hooks/useDocs';
19
19
 
20
20
  import DocsPreferredVersionStorage from './DocsPreferredVersionStorage';
21
21
 
@@ -54,7 +54,7 @@ function readStorageState({
54
54
  }: {
55
55
  pluginIds: string[];
56
56
  versionPersistence: DocsVersionPersistence;
57
- allDocsData: any; // TODO find a way to type it :(
57
+ allDocsData: Record<string, GlobalPluginData>;
58
58
  }): DocsPreferredVersionState {
59
59
  // The storage value we read might be stale,
60
60
  // and belong to a version that does not exist in the site anymore
@@ -68,7 +68,7 @@ function readStorageState({
68
68
  );
69
69
  const pluginData = allDocsData[pluginId];
70
70
  const versionExists = pluginData.versions.some(
71
- (version: any) => version.name === preferredVersionNameUnsafe,
71
+ (version) => version.name === preferredVersionNameUnsafe,
72
72
  );
73
73
  if (versionExists) {
74
74
  return {preferredVersionName: preferredVersionNameUnsafe};
@@ -120,7 +120,7 @@ function useContextValue() {
120
120
  return {
121
121
  savePreferredVersion,
122
122
  };
123
- }, [setState]);
123
+ }, [versionPersistence]);
124
124
 
125
125
  return [state, api] as const;
126
126
  }
@@ -6,38 +6,40 @@
6
6
  */
7
7
  import {useCallback} from 'react';
8
8
  import {useDocsPreferredVersionContext} from './DocsPreferredVersionProvider';
9
- import {useAllDocsData, useDocsData} from '@theme/hooks/useDocs';
9
+ import {useAllDocsData, useDocsData, GlobalVersion} from '@theme/hooks/useDocs';
10
10
 
11
11
  import {DEFAULT_PLUGIN_ID} from '@docusaurus/constants';
12
12
 
13
- // TODO improve typing
14
-
15
13
  // Note, the preferredVersion attribute will always be null before mount
16
14
  export function useDocsPreferredVersion(
17
15
  pluginId: string | undefined = DEFAULT_PLUGIN_ID,
18
- ) {
16
+ ): {
17
+ preferredVersion: GlobalVersion | null | undefined;
18
+ savePreferredVersionName: (versionName: string) => void;
19
+ } {
19
20
  const docsData = useDocsData(pluginId);
20
21
  const [state, api] = useDocsPreferredVersionContext();
21
22
 
22
23
  const {preferredVersionName} = state[pluginId];
23
24
 
24
25
  const preferredVersion = preferredVersionName
25
- ? docsData.versions.find(
26
- (version: any) => version.name === preferredVersionName,
27
- )
26
+ ? docsData.versions.find((version) => version.name === preferredVersionName)
28
27
  : null;
29
28
 
30
29
  const savePreferredVersionName = useCallback(
31
30
  (versionName: string) => {
32
31
  api.savePreferredVersion(pluginId, versionName);
33
32
  },
34
- [api],
33
+ [api, pluginId],
35
34
  );
36
35
 
37
36
  return {preferredVersion, savePreferredVersionName} as const;
38
37
  }
39
38
 
40
- export function useDocsPreferredVersionByPluginId(): Record<string, any> {
39
+ export function useDocsPreferredVersionByPluginId(): Record<
40
+ string,
41
+ GlobalVersion | null | undefined
42
+ > {
41
43
  const allDocsData = useAllDocsData();
42
44
  const [state] = useDocsPreferredVersionContext();
43
45
 
@@ -47,17 +49,14 @@ export function useDocsPreferredVersionByPluginId(): Record<string, any> {
47
49
 
48
50
  return preferredVersionName
49
51
  ? docsData.versions.find(
50
- (version: any) => version.name === preferredVersionName,
52
+ (version) => version.name === preferredVersionName,
51
53
  )
52
54
  : null;
53
55
  }
54
56
 
55
57
  const pluginIds = Object.keys(allDocsData);
56
58
 
57
- const result: Record<
58
- string,
59
- any // TODO find a way to type this properly!
60
- > = {};
59
+ const result: Record<string, GlobalVersion | null | undefined> = {};
61
60
  pluginIds.forEach((pluginId) => {
62
61
  result[pluginId] = getPluginIdPreferredVersion(pluginId);
63
62
  });
@@ -7,8 +7,8 @@
7
7
  import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
8
8
 
9
9
  export const useTitleFormatter = (title?: string | undefined): string => {
10
- const {siteConfig = {}} = useDocusaurusContext();
11
- const {title: siteTitle, titleDelimiter = '|'} = siteConfig;
10
+ const {siteConfig} = useDocusaurusContext();
11
+ const {title: siteTitle, titleDelimiter} = siteConfig;
12
12
  return title && title.trim().length
13
13
  ? `${title.trim()} ${titleDelimiter} ${siteTitle}`
14
14
  : siteTitle;
@@ -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 {useEffect, useRef} from 'react';
9
+ import {useHistory} from '@docusaurus/router';
10
+ import type {Location, Action} from '@docusaurus/history';
11
+
12
+ type HistoryBlockHandler = (location: Location, action: Action) => void | false;
13
+
14
+ /*
15
+ Permits to register a handler that will be called on history actions (pop,push,replace)
16
+ If the handler returns false, the navigation transition will be blocked/cancelled
17
+ */
18
+ export function useHistoryActionHandler(handler: HistoryBlockHandler): void {
19
+ const {block} = useHistory();
20
+
21
+ // Avoid stale closure issues without triggering useless re-renders
22
+ const lastHandlerRef = useRef(handler);
23
+ useEffect(() => {
24
+ lastHandlerRef.current = handler;
25
+ }, [handler]);
26
+
27
+ useEffect(() => {
28
+ // See https://github.com/remix-run/history/blob/main/docs/blocking-transitions.md
29
+ return block((location, action) => {
30
+ return lastHandlerRef.current(location, action);
31
+ });
32
+ }, [block, lastHandlerRef]);
33
+ }
34
+
35
+ /*
36
+ Permits to register a handler that will be called on history pop navigation (backward/forward)
37
+ If the handler returns false, the backward/forward transition will be blocked
38
+
39
+ Unfortunately there's no good way to detect the "direction" (backward/forward) of the POP event.
40
+ */
41
+ export function useHistoryPopHandler(handler: HistoryBlockHandler): void {
42
+ useHistoryActionHandler((location, action) => {
43
+ if (action === 'POP') {
44
+ // Eventually block navigation if handler returns false
45
+ return handler(location, action);
46
+ }
47
+ // Don't block other navigation actions
48
+ return undefined;
49
+ });
50
+ }
@@ -0,0 +1,23 @@
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
+ // A replacement of lodash in client code
9
+
10
+ /**
11
+ * Gets the duplicate values in an array.
12
+ * @param arr The array.
13
+ * @param comparator Compares two values and returns `true` if they are equal (duplicated).
14
+ * @returns Value of the elements `v` that have a preceding element `u` where `comparator(u, v) === true`. Values within the returned array are not guaranteed to be unique.
15
+ */
16
+ export function duplicates<T>(
17
+ arr: readonly T[],
18
+ comparator: (a: T, b: T) => boolean = (a, b) => a === b,
19
+ ): T[] {
20
+ return arr.filter(
21
+ (v, vIndex) => arr.findIndex((u) => comparator(u, v)) !== vIndex,
22
+ );
23
+ }
@@ -83,13 +83,14 @@ function useShallowMemoizedObject<O extends Record<string, unknown>>(obj: O) {
83
83
  return useMemo(
84
84
  () => obj,
85
85
  // Is this safe?
86
+ // eslint-disable-next-line react-hooks/exhaustive-deps
86
87
  [...Object.keys(obj), ...Object.values(obj)],
87
88
  );
88
89
  }
89
90
 
90
91
  // Fill the secondary menu placeholder with some real content
91
92
  export function MobileSecondaryMenuFiller<
92
- Props extends Record<string, unknown>
93
+ Props extends Record<string, unknown>,
93
94
  >({
94
95
  component,
95
96
  props,
@@ -0,0 +1,34 @@
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, useLayoutEffect, useRef} from 'react';
9
+
10
+ // This hook is like useLayoutEffect, but without the SSR warning
11
+ // It seems hacky but it's used in many React libs (Redux, Formik...)
12
+ // Also mentioned here: https://github.com/facebook/react/issues/16956
13
+ // It is useful when you need to update a ref as soon as possible after a React render (before useEffect)
14
+ export const useIsomorphicLayoutEffect =
15
+ typeof window !== 'undefined' ? useLayoutEffect : useEffect;
16
+
17
+ // Permits to transform an unstable callback (like an arrow function provided as props)
18
+ // to a "stable" callback that is safe to use in a useEffect dependency array
19
+ // Useful to avoid React stale closure problems + avoid useless effect re-executions
20
+ //
21
+ // Workaround until the React team recommends a good solution, see https://github.com/facebook/react/issues/16956
22
+ // This generally works has some potential drawbacks, such as https://github.com/facebook/react/issues/16956#issuecomment-536636418
23
+ export function useDynamicCallback<T extends (...args: never[]) => unknown>(
24
+ callback: T,
25
+ ): T {
26
+ const ref = useRef<T>(callback);
27
+
28
+ useIsomorphicLayoutEffect(() => {
29
+ ref.current = callback;
30
+ }, [callback]);
31
+
32
+ // @ts-expect-error: TODO, not sure how to fix this TS error
33
+ return useCallback<T>((...args) => ref.current(...args), []);
34
+ }
@@ -0,0 +1,23 @@
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
+ /**
9
+ * Utility to convert an optional string into a Regex case sensitive and global
10
+ */
11
+ export function isRegexpStringMatch(
12
+ regexAsString?: string,
13
+ valueToTest?: string,
14
+ ): boolean {
15
+ if (
16
+ typeof regexAsString === 'undefined' ||
17
+ typeof valueToTest === 'undefined'
18
+ ) {
19
+ return false;
20
+ }
21
+
22
+ return new RegExp(regexAsString, 'gi').test(valueToTest);
23
+ }