@docusaurus/theme-common 2.0.0-beta.8bda3b2db → 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 (69) hide show
  1. package/copyUntypedFiles.js +20 -0
  2. package/lib/.tsbuildinfo +1 -1
  3. package/lib/components/Collapsible/index.d.ts +35 -0
  4. package/lib/components/Collapsible/index.js +139 -0
  5. package/lib/components/Details/index.d.ts +12 -0
  6. package/lib/components/Details/index.js +64 -0
  7. package/lib/components/Details/styles.module.css +58 -0
  8. package/lib/index.d.ts +19 -1
  9. package/lib/index.js +13 -0
  10. package/lib/utils/ThemeClassNames.d.ts +36 -12
  11. package/lib/utils/ThemeClassNames.js +36 -3
  12. package/lib/utils/announcementBarUtils.d.ts +17 -0
  13. package/lib/utils/announcementBarUtils.js +73 -0
  14. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +1 -1
  15. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +5 -3
  16. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +1 -2
  17. package/lib/utils/generalUtils.js +2 -2
  18. package/lib/utils/historyUtils.d.ts +11 -0
  19. package/lib/utils/historyUtils.js +42 -0
  20. package/lib/utils/jsUtils.d.ts +13 -0
  21. package/lib/utils/jsUtils.js +16 -0
  22. package/lib/utils/mobileSecondaryMenu.d.ts +20 -0
  23. package/lib/utils/mobileSecondaryMenu.js +51 -0
  24. package/lib/utils/reactUtils.d.ts +9 -0
  25. package/lib/utils/reactUtils.js +26 -0
  26. package/lib/utils/regexpUtils.d.ts +10 -0
  27. package/lib/utils/regexpUtils.js +16 -0
  28. package/lib/utils/scrollUtils.d.ts +52 -0
  29. package/lib/utils/scrollUtils.js +137 -0
  30. package/lib/utils/tagsUtils.d.ts +18 -0
  31. package/lib/utils/tagsUtils.js +33 -0
  32. package/lib/utils/tocUtils.d.ts +15 -0
  33. package/lib/utils/tocUtils.js +36 -0
  34. package/lib/utils/useLocalPathname.d.ts +7 -0
  35. package/lib/utils/useLocalPathname.js +16 -0
  36. package/lib/utils/useLocationChange.js +5 -9
  37. package/lib/utils/usePrevious.js +3 -2
  38. package/lib/utils/useTOCHighlight.d.ts +14 -0
  39. package/lib/utils/useTOCHighlight.js +124 -0
  40. package/lib/utils/useThemeConfig.d.ts +14 -2
  41. package/package.json +16 -12
  42. package/src/components/Collapsible/index.tsx +242 -0
  43. package/src/components/Details/index.tsx +94 -0
  44. package/src/components/Details/styles.module.css +58 -0
  45. package/src/index.ts +50 -0
  46. package/src/types.d.ts +0 -2
  47. package/src/utils/ThemeClassNames.ts +42 -4
  48. package/src/utils/__tests__/tagUtils.test.ts +66 -0
  49. package/src/utils/__tests__/tocUtils.test.ts +197 -0
  50. package/src/utils/announcementBarUtils.tsx +119 -0
  51. package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +4 -4
  52. package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +13 -14
  53. package/src/utils/generalUtils.ts +2 -2
  54. package/src/utils/historyUtils.ts +50 -0
  55. package/src/utils/jsUtils.ts +23 -0
  56. package/src/utils/mobileSecondaryMenu.tsx +116 -0
  57. package/src/utils/reactUtils.tsx +34 -0
  58. package/src/utils/regexpUtils.ts +23 -0
  59. package/src/utils/scrollUtils.tsx +238 -0
  60. package/src/utils/storageUtils.ts +1 -1
  61. package/src/utils/tagsUtils.ts +48 -0
  62. package/src/utils/tocUtils.ts +54 -0
  63. package/src/utils/useAlternatePageUtils.ts +9 -1
  64. package/src/utils/useLocalPathname.ts +20 -0
  65. package/src/utils/useLocationChange.ts +6 -10
  66. package/src/utils/usePluralForm.ts +3 -1
  67. package/src/utils/usePrevious.ts +3 -2
  68. package/src/utils/useTOCHighlight.ts +179 -0
  69. package/src/utils/useThemeConfig.ts +18 -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
+ });
@@ -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 React, {
9
+ useState,
10
+ useEffect,
11
+ useCallback,
12
+ useMemo,
13
+ ReactNode,
14
+ useContext,
15
+ createContext,
16
+ } from 'react';
17
+ import useIsBrowser from '@docusaurus/useIsBrowser';
18
+ import {createStorageSlot} from './storageUtils';
19
+ import {useThemeConfig} from './useThemeConfig';
20
+
21
+ export const AnnouncementBarDismissStorageKey =
22
+ 'docusaurus.announcement.dismiss';
23
+ const AnnouncementBarIdStorageKey = 'docusaurus.announcement.id';
24
+
25
+ const AnnouncementBarDismissStorage = createStorageSlot(
26
+ AnnouncementBarDismissStorageKey,
27
+ );
28
+ const IdStorage = createStorageSlot(AnnouncementBarIdStorageKey);
29
+
30
+ const isDismissedInStorage = () =>
31
+ AnnouncementBarDismissStorage.get() === 'true';
32
+ const setDismissedInStorage = (bool: boolean) =>
33
+ AnnouncementBarDismissStorage.set(String(bool));
34
+
35
+ type AnnouncementBarAPI = {
36
+ readonly isActive: boolean;
37
+ readonly close: () => void;
38
+ };
39
+
40
+ const useAnnouncementBarContextValue = (): AnnouncementBarAPI => {
41
+ const {announcementBar} = useThemeConfig();
42
+ const isBrowser = useIsBrowser();
43
+
44
+ const [isClosed, setClosed] = useState(() => {
45
+ return isBrowser
46
+ ? // On client navigation: init with localstorage value
47
+ isDismissedInStorage()
48
+ : // On server/hydration: always visible to prevent layout shifts (will be hidden with css if needed)
49
+ false;
50
+ });
51
+ // Update state after hydration
52
+ useEffect(() => {
53
+ setClosed(isDismissedInStorage());
54
+ }, []);
55
+
56
+ const handleClose = useCallback(() => {
57
+ setDismissedInStorage(true);
58
+ setClosed(true);
59
+ }, []);
60
+
61
+ useEffect(() => {
62
+ if (!announcementBar) {
63
+ return;
64
+ }
65
+ const {id} = announcementBar;
66
+
67
+ let viewedId = IdStorage.get();
68
+
69
+ // retrocompatibility due to spelling mistake of default id
70
+ // see https://github.com/facebook/docusaurus/issues/3338
71
+ if (viewedId === 'annoucement-bar') {
72
+ viewedId = 'announcement-bar';
73
+ }
74
+
75
+ const isNewAnnouncement = id !== viewedId;
76
+
77
+ IdStorage.set(id);
78
+
79
+ if (isNewAnnouncement) {
80
+ setDismissedInStorage(false);
81
+ }
82
+
83
+ if (isNewAnnouncement || !isDismissedInStorage()) {
84
+ setClosed(false);
85
+ }
86
+ }, [announcementBar]);
87
+
88
+ return useMemo(() => {
89
+ return {
90
+ isActive: !!announcementBar && !isClosed,
91
+ close: handleClose,
92
+ };
93
+ }, [announcementBar, isClosed, handleClose]);
94
+ };
95
+
96
+ const AnnouncementBarContext = createContext<AnnouncementBarAPI | null>(null);
97
+
98
+ export const AnnouncementBarProvider = ({
99
+ children,
100
+ }: {
101
+ children: ReactNode;
102
+ }): JSX.Element => {
103
+ const value = useAnnouncementBarContextValue();
104
+ return (
105
+ <AnnouncementBarContext.Provider value={value}>
106
+ {children}
107
+ </AnnouncementBarContext.Provider>
108
+ );
109
+ };
110
+
111
+ export const useAnnouncementBar = (): AnnouncementBarAPI => {
112
+ const api = useContext(AnnouncementBarContext);
113
+ if (!api) {
114
+ throw new Error(
115
+ 'useAnnouncementBar(): AnnouncementBar not found in React context: make sure to use the AnnouncementBarProvider on top of the tree',
116
+ );
117
+ }
118
+ return api;
119
+ };
@@ -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
+ }
@@ -0,0 +1,116 @@
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
+ ReactNode,
11
+ useContext,
12
+ createContext,
13
+ useEffect,
14
+ ComponentType,
15
+ useMemo,
16
+ } from 'react';
17
+
18
+ /*
19
+ The idea behind all this is that a specific component must be able to fill a placeholder in the generic layout
20
+ The doc page should be able to fill the secondary menu of the main mobile navbar.
21
+ This permits to reduce coupling between the main layout and the specific page.
22
+
23
+ This kind of feature is often called portal/teleport/gateway... various unmaintained React libs exist
24
+ Most up-to-date one: https://github.com/gregberge/react-teleporter
25
+ Not sure any of those is safe regarding concurrent mode.
26
+ */
27
+
28
+ type ExtraProps = {
29
+ toggleSidebar: () => void;
30
+ };
31
+
32
+ export type MobileSecondaryMenuComponent<Props extends unknown> = ComponentType<
33
+ Props & ExtraProps
34
+ >;
35
+
36
+ type State = {
37
+ component: MobileSecondaryMenuComponent<unknown>;
38
+ props: unknown;
39
+ } | null;
40
+
41
+ function useContextValue() {
42
+ return useState<State>(null);
43
+ }
44
+
45
+ type ContextValue = ReturnType<typeof useContextValue>;
46
+
47
+ const Context = createContext<ContextValue | null>(null);
48
+
49
+ export function MobileSecondaryMenuProvider({
50
+ children,
51
+ }: {
52
+ children: ReactNode;
53
+ }): JSX.Element {
54
+ return (
55
+ <Context.Provider value={useContextValue()}>{children}</Context.Provider>
56
+ );
57
+ }
58
+
59
+ function useMobileSecondaryMenuContext(): ContextValue {
60
+ const value = useContext(Context);
61
+ if (value === null) {
62
+ throw new Error(
63
+ 'MobileSecondaryMenuProvider was not used correctly, context value is null',
64
+ );
65
+ }
66
+ return value;
67
+ }
68
+
69
+ export function useMobileSecondaryMenuRenderer(): (
70
+ extraProps: ExtraProps,
71
+ ) => ReactNode | undefined {
72
+ const [state] = useMobileSecondaryMenuContext();
73
+ if (state) {
74
+ const Comp = state.component;
75
+ return function render(extraProps) {
76
+ return <Comp {...state.props} {...extraProps} />;
77
+ };
78
+ }
79
+ return () => undefined;
80
+ }
81
+
82
+ function useShallowMemoizedObject<O extends Record<string, unknown>>(obj: O) {
83
+ return useMemo(
84
+ () => obj,
85
+ // Is this safe?
86
+ // eslint-disable-next-line react-hooks/exhaustive-deps
87
+ [...Object.keys(obj), ...Object.values(obj)],
88
+ );
89
+ }
90
+
91
+ // Fill the secondary menu placeholder with some real content
92
+ export function MobileSecondaryMenuFiller<
93
+ Props extends Record<string, unknown>,
94
+ >({
95
+ component,
96
+ props,
97
+ }: {
98
+ component: MobileSecondaryMenuComponent<Props & ExtraProps>;
99
+ props: Props;
100
+ }): JSX.Element | null {
101
+ const [, setState] = useMobileSecondaryMenuContext();
102
+
103
+ // To avoid useless context re-renders, props are memoized shallowly
104
+ const memoizedProps = useShallowMemoizedObject(props);
105
+
106
+ useEffect(() => {
107
+ // @ts-expect-error: context is not 100% typesafe but it's ok
108
+ setState({component, props: memoizedProps});
109
+ }, [setState, component, memoizedProps]);
110
+
111
+ useEffect(() => {
112
+ return () => setState(null);
113
+ }, [setState]);
114
+
115
+ return null;
116
+ }
@@ -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
+ }