@docusaurus/theme-common 2.2.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/hooks/useSearchPage.d.ts.map +1 -1
- package/lib/hooks/useSearchPage.js +4 -2
- package/lib/hooks/useSearchPage.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/internal.d.ts +3 -2
- package/lib/internal.d.ts.map +1 -1
- package/lib/internal.js +2 -2
- package/lib/internal.js.map +1 -1
- package/lib/utils/historyUtils.d.ts +12 -1
- package/lib/utils/historyUtils.d.ts.map +1 -1
- package/lib/utils/historyUtils.js +23 -0
- package/lib/utils/historyUtils.js.map +1 -1
- package/lib/utils/storageUtils.d.ts +4 -0
- package/lib/utils/storageUtils.d.ts.map +1 -1
- package/lib/utils/storageUtils.js +70 -7
- package/lib/utils/storageUtils.js.map +1 -1
- package/lib/utils/tabsUtils.d.ts +46 -0
- package/lib/utils/tabsUtils.d.ts.map +1 -0
- package/lib/utils/tabsUtils.js +151 -0
- package/lib/utils/tabsUtils.js.map +1 -0
- package/package.json +11 -10
- package/src/hooks/useSearchPage.ts +10 -5
- package/src/index.ts +5 -1
- package/src/internal.ts +7 -5
- package/src/utils/historyUtils.ts +29 -1
- package/src/utils/storageUtils.ts +108 -7
- package/src/utils/tabsUtils.tsx +266 -0
- package/lib/contexts/tabGroupChoice.d.ts +0 -21
- package/lib/contexts/tabGroupChoice.d.ts.map +0 -1
- package/lib/contexts/tabGroupChoice.js +0 -49
- package/lib/contexts/tabGroupChoice.js.map +0 -1
- package/src/contexts/tabGroupChoice.tsx +0 -85
|
@@ -1,85 +0,0 @@
|
|
|
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
|
-
useMemo,
|
|
13
|
-
useContext,
|
|
14
|
-
type ReactNode,
|
|
15
|
-
} from 'react';
|
|
16
|
-
import {createStorageSlot, listStorageKeys} from '../utils/storageUtils';
|
|
17
|
-
import {ReactContextError} from '../utils/reactUtils';
|
|
18
|
-
|
|
19
|
-
const TAB_CHOICE_PREFIX = 'docusaurus.tab.';
|
|
20
|
-
|
|
21
|
-
type ContextValue = {
|
|
22
|
-
/** A map from `groupId` to the `value` of the saved choice. */
|
|
23
|
-
readonly tabGroupChoices: {readonly [groupId: string]: string};
|
|
24
|
-
/** Set the new choice value of a group. */
|
|
25
|
-
readonly setTabGroupChoices: (groupId: string, newChoice: string) => void;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const Context = React.createContext<ContextValue | undefined>(undefined);
|
|
29
|
-
|
|
30
|
-
function useContextValue(): ContextValue {
|
|
31
|
-
const [tabGroupChoices, setChoices] = useState<{
|
|
32
|
-
readonly [groupId: string]: string;
|
|
33
|
-
}>({});
|
|
34
|
-
const setChoiceSyncWithLocalStorage = useCallback(
|
|
35
|
-
(groupId: string, newChoice: string) => {
|
|
36
|
-
createStorageSlot(`${TAB_CHOICE_PREFIX}${groupId}`).set(newChoice);
|
|
37
|
-
},
|
|
38
|
-
[],
|
|
39
|
-
);
|
|
40
|
-
|
|
41
|
-
useEffect(() => {
|
|
42
|
-
try {
|
|
43
|
-
const localStorageChoices: {[groupId: string]: string} = {};
|
|
44
|
-
listStorageKeys().forEach((storageKey) => {
|
|
45
|
-
if (storageKey.startsWith(TAB_CHOICE_PREFIX)) {
|
|
46
|
-
const groupId = storageKey.substring(TAB_CHOICE_PREFIX.length);
|
|
47
|
-
localStorageChoices[groupId] = createStorageSlot(storageKey).get()!;
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
setChoices(localStorageChoices);
|
|
51
|
-
} catch (err) {
|
|
52
|
-
console.error(err);
|
|
53
|
-
}
|
|
54
|
-
}, []);
|
|
55
|
-
|
|
56
|
-
const setTabGroupChoices = useCallback(
|
|
57
|
-
(groupId: string, newChoice: string) => {
|
|
58
|
-
setChoices((oldChoices) => ({...oldChoices, [groupId]: newChoice}));
|
|
59
|
-
setChoiceSyncWithLocalStorage(groupId, newChoice);
|
|
60
|
-
},
|
|
61
|
-
[setChoiceSyncWithLocalStorage],
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
return useMemo(
|
|
65
|
-
() => ({tabGroupChoices, setTabGroupChoices}),
|
|
66
|
-
[tabGroupChoices, setTabGroupChoices],
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export function TabGroupChoiceProvider({
|
|
71
|
-
children,
|
|
72
|
-
}: {
|
|
73
|
-
children: ReactNode;
|
|
74
|
-
}): JSX.Element {
|
|
75
|
-
const value = useContextValue();
|
|
76
|
-
return <Context.Provider value={value}>{children}</Context.Provider>;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export function useTabGroupChoice(): ContextValue {
|
|
80
|
-
const context = useContext(Context);
|
|
81
|
-
if (context == null) {
|
|
82
|
-
throw new ReactContextError('TabGroupChoiceProvider');
|
|
83
|
-
}
|
|
84
|
-
return context;
|
|
85
|
-
}
|