@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.
- package/Details.d.ts +14 -0
- package/lib/components/Collapsible/index.d.ts +64 -0
- package/lib/components/Collapsible/index.d.ts.map +1 -0
- package/lib/components/Collapsible/index.js +152 -0
- package/lib/components/Collapsible/index.js.map +1 -0
- package/lib/components/Details/index.d.ts +17 -0
- package/lib/components/Details/index.d.ts.map +1 -0
- package/lib/components/Details/index.js +71 -0
- package/lib/components/Details/index.js.map +1 -0
- package/lib/components/Details/styles.module.css +62 -0
- package/lib/contexts/announcementBar.d.ts +22 -0
- package/lib/contexts/announcementBar.d.ts.map +1 -0
- package/lib/{utils/announcementBarUtils.js → contexts/announcementBar.js} +30 -31
- package/lib/contexts/announcementBar.js.map +1 -0
- package/lib/contexts/colorMode.d.ts +27 -0
- package/lib/contexts/colorMode.d.ts.map +1 -0
- package/lib/contexts/colorMode.js +132 -0
- package/lib/contexts/colorMode.js.map +1 -0
- package/lib/contexts/docSidebarItemsExpandedState.d.ts +31 -0
- package/lib/contexts/docSidebarItemsExpandedState.d.ts.map +1 -0
- package/lib/contexts/docSidebarItemsExpandedState.js +28 -0
- package/lib/contexts/docSidebarItemsExpandedState.js.map +1 -0
- package/lib/contexts/docsPreferredVersion.d.ts +31 -0
- package/lib/contexts/docsPreferredVersion.d.ts.map +1 -0
- package/lib/contexts/docsPreferredVersion.js +128 -0
- package/lib/contexts/docsPreferredVersion.js.map +1 -0
- package/lib/contexts/docsSidebar.d.ts +26 -0
- package/lib/contexts/docsSidebar.d.ts.map +1 -0
- package/lib/contexts/docsSidebar.js +30 -0
- package/lib/contexts/docsSidebar.js.map +1 -0
- package/lib/contexts/docsVersion.d.ts +20 -0
- package/lib/contexts/docsVersion.d.ts.map +1 -0
- package/lib/contexts/docsVersion.js +26 -0
- package/lib/contexts/docsVersion.js.map +1 -0
- package/lib/contexts/navbarMobileSidebar.d.ts +31 -0
- package/lib/contexts/navbarMobileSidebar.d.ts.map +1 -0
- package/lib/contexts/navbarMobileSidebar.js +56 -0
- package/lib/contexts/navbarMobileSidebar.js.map +1 -0
- package/lib/contexts/navbarSecondaryMenu/content.d.ts +37 -0
- package/lib/contexts/navbarSecondaryMenu/content.d.ts.map +1 -0
- package/lib/contexts/navbarSecondaryMenu/content.js +56 -0
- package/lib/contexts/navbarSecondaryMenu/content.js.map +1 -0
- package/lib/contexts/navbarSecondaryMenu/display.d.ts +24 -0
- package/lib/contexts/navbarSecondaryMenu/display.d.ts.map +1 -0
- package/lib/contexts/navbarSecondaryMenu/display.js +62 -0
- package/lib/contexts/navbarSecondaryMenu/display.js.map +1 -0
- package/lib/contexts/tabGroupChoice.d.ts +21 -0
- package/lib/contexts/tabGroupChoice.d.ts.map +1 -0
- package/lib/contexts/tabGroupChoice.js +49 -0
- package/lib/contexts/tabGroupChoice.js.map +1 -0
- package/lib/{utils/usePrevious.d.ts → hooks/styles.css} +4 -1
- package/lib/hooks/useBackToTopButton.d.ts +27 -0
- package/lib/hooks/useBackToTopButton.d.ts.map +1 -0
- package/lib/hooks/useBackToTopButton.js +50 -0
- package/lib/hooks/useBackToTopButton.js.map +1 -0
- package/lib/hooks/useCodeWordWrap.d.ts +14 -0
- package/lib/hooks/useCodeWordWrap.d.ts.map +1 -0
- package/lib/hooks/useCodeWordWrap.js +41 -0
- package/lib/hooks/useCodeWordWrap.js.map +1 -0
- package/lib/hooks/useHideableNavbar.d.ts +17 -0
- package/lib/hooks/useHideableNavbar.d.ts.map +1 -0
- package/lib/hooks/useHideableNavbar.js +60 -0
- package/lib/hooks/useHideableNavbar.js.map +1 -0
- package/lib/hooks/useKeyboardNavigation.d.ts +20 -0
- package/lib/hooks/useKeyboardNavigation.d.ts.map +1 -0
- package/lib/hooks/useKeyboardNavigation.js +39 -0
- package/lib/hooks/useKeyboardNavigation.js.map +1 -0
- package/lib/hooks/useLockBodyScroll.d.ts +12 -0
- package/lib/hooks/useLockBodyScroll.d.ts.map +1 -0
- package/lib/hooks/useLockBodyScroll.js +20 -0
- package/lib/hooks/useLockBodyScroll.js.map +1 -0
- package/lib/hooks/usePrismTheme.d.ts +13 -0
- package/lib/hooks/usePrismTheme.d.ts.map +1 -0
- package/lib/hooks/usePrismTheme.js +21 -0
- package/lib/hooks/usePrismTheme.js.map +1 -0
- package/lib/hooks/useSearchPage.d.ts +25 -0
- package/lib/hooks/useSearchPage.d.ts.map +1 -0
- package/lib/hooks/useSearchPage.js +43 -0
- package/lib/hooks/useSearchPage.js.map +1 -0
- package/lib/hooks/useSkipToContent.d.ts +25 -0
- package/lib/hooks/useSkipToContent.d.ts.map +1 -0
- package/lib/hooks/useSkipToContent.js +35 -0
- package/lib/hooks/useSkipToContent.js.map +1 -0
- package/lib/hooks/useTOCHighlight.d.ts +25 -0
- package/lib/hooks/useTOCHighlight.d.ts.map +1 -0
- package/lib/hooks/useTOCHighlight.js +130 -0
- package/lib/hooks/useTOCHighlight.js.map +1 -0
- package/lib/hooks/useWindowSize.d.ts +28 -0
- package/lib/hooks/useWindowSize.d.ts.map +1 -0
- package/lib/hooks/useWindowSize.js +59 -0
- package/lib/hooks/useWindowSize.js.map +1 -0
- package/lib/index.d.ts +38 -10
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +38 -9
- package/lib/index.js.map +1 -0
- package/lib/utils/ThemeClassNames.d.ts +47 -12
- package/lib/utils/ThemeClassNames.d.ts.map +1 -0
- package/lib/utils/ThemeClassNames.js +45 -4
- package/lib/utils/ThemeClassNames.js.map +1 -0
- package/lib/utils/codeBlockUtils.d.ts +63 -0
- package/lib/utils/codeBlockUtils.d.ts.map +1 -0
- package/lib/utils/codeBlockUtils.js +157 -3
- package/lib/utils/codeBlockUtils.js.map +1 -0
- package/lib/utils/docsUtils.d.ts +91 -0
- package/lib/utils/docsUtils.d.ts.map +1 -0
- package/lib/utils/docsUtils.js +217 -1
- package/lib/utils/docsUtils.js.map +1 -0
- package/lib/utils/footerUtils.d.ts +13 -0
- package/lib/utils/footerUtils.d.ts.map +1 -0
- package/lib/utils/footerUtils.js +14 -0
- package/lib/utils/footerUtils.js.map +1 -0
- package/lib/utils/generalUtils.d.ts +11 -1
- package/lib/utils/generalUtils.d.ts.map +1 -0
- package/lib/utils/generalUtils.js +9 -5
- package/lib/utils/generalUtils.js.map +1 -0
- package/lib/utils/historyUtils.d.ts +17 -0
- package/lib/utils/historyUtils.d.ts.map +1 -0
- package/lib/utils/historyUtils.js +38 -0
- package/lib/utils/historyUtils.js.map +1 -0
- package/lib/utils/jsUtils.d.ts +23 -0
- package/lib/utils/jsUtils.d.ts.map +1 -0
- package/lib/utils/jsUtils.js +29 -0
- package/lib/utils/jsUtils.js.map +1 -0
- package/lib/utils/metadataUtils.d.ts +38 -0
- package/lib/utils/metadataUtils.d.ts.map +1 -0
- package/lib/utils/metadataUtils.js +61 -0
- package/lib/utils/metadataUtils.js.map +1 -0
- package/lib/utils/navbarUtils.d.ts +21 -0
- package/lib/utils/navbarUtils.d.ts.map +1 -0
- package/lib/utils/navbarUtils.js +32 -0
- package/lib/utils/navbarUtils.js.map +1 -0
- package/lib/utils/reactUtils.d.ts +42 -0
- package/lib/utils/reactUtils.d.ts.map +1 -0
- package/lib/utils/reactUtils.js +64 -0
- package/lib/utils/reactUtils.js.map +1 -0
- package/lib/utils/regexpUtils.d.ts +12 -0
- package/lib/utils/regexpUtils.d.ts.map +1 -0
- package/lib/utils/regexpUtils.js +18 -0
- package/lib/utils/regexpUtils.js.map +1 -0
- package/lib/utils/routesUtils.d.ts +26 -0
- package/lib/utils/routesUtils.d.ts.map +1 -0
- package/lib/utils/routesUtils.js +54 -0
- package/lib/utils/routesUtils.js.map +1 -0
- package/lib/utils/scrollUtils.d.ts +83 -0
- package/lib/utils/scrollUtils.d.ts.map +1 -0
- package/lib/utils/scrollUtils.js +200 -0
- package/lib/utils/scrollUtils.js.map +1 -0
- package/lib/utils/searchUtils.d.ts +13 -0
- package/lib/utils/searchUtils.d.ts.map +1 -0
- package/lib/utils/searchUtils.js +37 -0
- package/lib/utils/searchUtils.js.map +1 -0
- package/lib/utils/storageUtils.d.ts +15 -7
- package/lib/utils/storageUtils.d.ts.map +1 -0
- package/lib/utils/storageUtils.js +55 -22
- package/lib/utils/storageUtils.js.map +1 -0
- package/lib/utils/tagsUtils.d.ts +18 -0
- package/lib/utils/tagsUtils.d.ts.map +1 -0
- package/lib/utils/tagsUtils.js +36 -0
- package/lib/utils/tagsUtils.js.map +1 -0
- package/lib/utils/tocUtils.d.ts +36 -0
- package/lib/utils/tocUtils.d.ts.map +1 -0
- package/lib/utils/tocUtils.js +84 -0
- package/lib/utils/tocUtils.js.map +1 -0
- package/lib/utils/useAlternatePageUtils.d.ts +21 -1
- package/lib/utils/useAlternatePageUtils.d.ts.map +1 -0
- package/lib/utils/useAlternatePageUtils.js +9 -4
- package/lib/utils/useAlternatePageUtils.js.map +1 -0
- package/lib/utils/useLocalPathname.d.ts +13 -0
- package/lib/utils/useLocalPathname.d.ts.map +1 -0
- package/lib/utils/useLocalPathname.js +19 -0
- package/lib/utils/useLocalPathname.js.map +1 -0
- package/lib/utils/useLocationChange.d.ts +9 -6
- package/lib/utils/useLocationChange.d.ts.map +1 -0
- package/lib/utils/useLocationChange.js +17 -11
- package/lib/utils/useLocationChange.js.map +1 -0
- package/lib/utils/usePluralForm.d.ts +12 -0
- package/lib/utils/usePluralForm.d.ts.map +1 -0
- package/lib/utils/usePluralForm.js +36 -37
- package/lib/utils/usePluralForm.js.map +1 -0
- package/lib/utils/useThemeConfig.d.ts +55 -24
- package/lib/utils/useThemeConfig.d.ts.map +1 -0
- package/lib/utils/useThemeConfig.js +4 -0
- package/lib/utils/useThemeConfig.js.map +1 -0
- package/package.json +26 -13
- package/src/components/Collapsible/index.tsx +265 -0
- package/src/components/Details/index.tsx +109 -0
- package/src/components/Details/styles.module.css +62 -0
- package/src/{utils/announcementBarUtils.tsx → contexts/announcementBar.tsx} +43 -39
- package/src/contexts/colorMode.tsx +199 -0
- package/src/contexts/docSidebarItemsExpandedState.tsx +55 -0
- package/src/contexts/docsPreferredVersion.tsx +253 -0
- package/src/contexts/docsSidebar.tsx +50 -0
- package/src/contexts/docsVersion.tsx +36 -0
- package/src/contexts/navbarMobileSidebar.tsx +99 -0
- package/src/contexts/navbarSecondaryMenu/content.tsx +110 -0
- package/src/contexts/navbarSecondaryMenu/display.tsx +102 -0
- package/src/contexts/tabGroupChoice.tsx +85 -0
- package/{lib/utils/pathUtils.d.ts → src/hooks/styles.css} +4 -1
- package/src/hooks/useBackToTopButton.ts +73 -0
- package/src/hooks/useCodeWordWrap.ts +56 -0
- package/src/hooks/useHideableNavbar.ts +75 -0
- package/src/hooks/useKeyboardNavigation.ts +45 -0
- package/src/hooks/useLockBodyScroll.ts +21 -0
- package/src/hooks/usePrismTheme.ts +24 -0
- package/src/hooks/useSearchPage.ts +79 -0
- package/src/hooks/useSkipToContent.ts +58 -0
- package/src/hooks/useTOCHighlight.ts +192 -0
- package/src/hooks/useWindowSize.ts +72 -0
- package/src/index.ts +130 -19
- package/src/types.d.ts +0 -2
- package/src/utils/ThemeClassNames.ts +51 -5
- package/src/utils/codeBlockUtils.ts +239 -2
- package/src/utils/docsUtils.tsx +334 -0
- package/src/utils/footerUtils.ts +18 -0
- package/src/utils/generalUtils.ts +9 -5
- package/src/utils/historyUtils.ts +45 -0
- package/src/utils/jsUtils.ts +36 -0
- package/src/utils/metadataUtils.tsx +115 -0
- package/src/utils/navbarUtils.tsx +45 -0
- package/src/utils/reactUtils.tsx +76 -0
- package/src/utils/regexpUtils.ts +24 -0
- package/src/utils/routesUtils.ts +75 -0
- package/src/utils/scrollUtils.tsx +308 -0
- package/src/utils/searchUtils.ts +51 -0
- package/src/utils/storageUtils.ts +56 -23
- package/src/utils/tagsUtils.ts +50 -0
- package/src/utils/tocUtils.ts +119 -0
- package/src/utils/useAlternatePageUtils.ts +19 -6
- package/src/utils/useLocalPathname.ts +22 -0
- package/src/utils/useLocationChange.ts +24 -20
- package/src/utils/usePluralForm.ts +46 -38
- package/src/utils/useThemeConfig.ts +54 -26
- package/lib/.tsbuildinfo +0 -1
- package/lib/utils/announcementBarInlineJavaScript.d.ts +0 -0
- package/lib/utils/announcementBarInlineJavaScript.js +0 -1
- package/lib/utils/announcementBarUtils.d.ts +0 -17
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +0 -21
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +0 -94
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts +0 -13
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +0 -20
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +0 -5
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +0 -41
- package/lib/utils/pathUtils.js +0 -13
- package/lib/utils/usePrevious.js +0 -14
- package/src/utils/__tests__/codeBlockUtils.test.ts +0 -54
- package/src/utils/__tests__/pathUtils.test.ts +0 -32
- package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +0 -165
- package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +0 -34
- package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +0 -66
- package/src/utils/docsUtils.ts +0 -11
- package/src/utils/pathUtils.ts +0 -17
- package/src/utils/usePrevious.ts +0 -18
- package/tsconfig.json +0 -10
|
@@ -1,94 +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
|
-
import React, { createContext, useContext, useEffect, useMemo, useState, } from 'react';
|
|
8
|
-
import { useThemeConfig } from '../useThemeConfig';
|
|
9
|
-
import { isDocsPluginEnabled } from '../docsUtils';
|
|
10
|
-
import { useAllDocsData } from '@theme/hooks/useDocs';
|
|
11
|
-
import DocsPreferredVersionStorage from './DocsPreferredVersionStorage';
|
|
12
|
-
// Initial state is always null as we can't read localstorage from node SSR
|
|
13
|
-
function getInitialState(pluginIds) {
|
|
14
|
-
const initialState = {};
|
|
15
|
-
pluginIds.forEach((pluginId) => {
|
|
16
|
-
initialState[pluginId] = {
|
|
17
|
-
preferredVersionName: null,
|
|
18
|
-
};
|
|
19
|
-
});
|
|
20
|
-
return initialState;
|
|
21
|
-
}
|
|
22
|
-
// Read storage for all docs plugins
|
|
23
|
-
// Assign to each doc plugin a preferred version (if found)
|
|
24
|
-
function readStorageState({ pluginIds, versionPersistence, allDocsData, }) {
|
|
25
|
-
// The storage value we read might be stale,
|
|
26
|
-
// and belong to a version that does not exist in the site anymore
|
|
27
|
-
// In such case, we remove the storage value to avoid downstream errors
|
|
28
|
-
function restorePluginState(pluginId) {
|
|
29
|
-
const preferredVersionNameUnsafe = DocsPreferredVersionStorage.read(pluginId, versionPersistence);
|
|
30
|
-
const pluginData = allDocsData[pluginId];
|
|
31
|
-
const versionExists = pluginData.versions.some((version) => version.name === preferredVersionNameUnsafe);
|
|
32
|
-
if (versionExists) {
|
|
33
|
-
return { preferredVersionName: preferredVersionNameUnsafe };
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
DocsPreferredVersionStorage.clear(pluginId, versionPersistence);
|
|
37
|
-
return { preferredVersionName: null };
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
const initialState = {};
|
|
41
|
-
pluginIds.forEach((pluginId) => {
|
|
42
|
-
initialState[pluginId] = restorePluginState(pluginId);
|
|
43
|
-
});
|
|
44
|
-
return initialState;
|
|
45
|
-
}
|
|
46
|
-
function useVersionPersistence() {
|
|
47
|
-
return useThemeConfig().docs.versionPersistence;
|
|
48
|
-
}
|
|
49
|
-
// Value that will be accessible through context: [state,api]
|
|
50
|
-
function useContextValue() {
|
|
51
|
-
const allDocsData = useAllDocsData();
|
|
52
|
-
const versionPersistence = useVersionPersistence();
|
|
53
|
-
const pluginIds = useMemo(() => Object.keys(allDocsData), [allDocsData]);
|
|
54
|
-
// Initial state is empty, as we can't read browser storage in node/SSR
|
|
55
|
-
const [state, setState] = useState(() => getInitialState(pluginIds));
|
|
56
|
-
// On mount, we set the state read from browser storage
|
|
57
|
-
useEffect(() => {
|
|
58
|
-
setState(readStorageState({ allDocsData, versionPersistence, pluginIds }));
|
|
59
|
-
}, [allDocsData, versionPersistence, pluginIds]);
|
|
60
|
-
// The API that we expose to consumer hooks (memo for constant object)
|
|
61
|
-
const api = useMemo(() => {
|
|
62
|
-
function savePreferredVersion(pluginId, versionName) {
|
|
63
|
-
DocsPreferredVersionStorage.save(pluginId, versionPersistence, versionName);
|
|
64
|
-
setState((s) => ({
|
|
65
|
-
...s,
|
|
66
|
-
[pluginId]: { preferredVersionName: versionName },
|
|
67
|
-
}));
|
|
68
|
-
}
|
|
69
|
-
return {
|
|
70
|
-
savePreferredVersion,
|
|
71
|
-
};
|
|
72
|
-
}, [setState]);
|
|
73
|
-
return [state, api];
|
|
74
|
-
}
|
|
75
|
-
const Context = createContext(null);
|
|
76
|
-
export function DocsPreferredVersionContextProvider({ children, }) {
|
|
77
|
-
if (isDocsPluginEnabled) {
|
|
78
|
-
return (React.createElement(DocsPreferredVersionContextProviderUnsafe, null, children));
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
return React.createElement(React.Fragment, null, children);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
function DocsPreferredVersionContextProviderUnsafe({ children, }) {
|
|
85
|
-
const contextValue = useContextValue();
|
|
86
|
-
return React.createElement(Context.Provider, { value: contextValue }, children);
|
|
87
|
-
}
|
|
88
|
-
export function useDocsPreferredVersionContext() {
|
|
89
|
-
const value = useContext(Context);
|
|
90
|
-
if (!value) {
|
|
91
|
-
throw new Error('Can\'t find docs preferred context, maybe you forgot to use the "DocsPreferredVersionContextProvider"?');
|
|
92
|
-
}
|
|
93
|
-
return value;
|
|
94
|
-
}
|
|
@@ -1,13 +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
|
-
import { DocsVersionPersistence } from '../useThemeConfig';
|
|
8
|
-
declare const DocsPreferredVersionStorage: {
|
|
9
|
-
save: (pluginId: string, persistence: DocsVersionPersistence, versionName: string) => void;
|
|
10
|
-
read: (pluginId: string, persistence: DocsVersionPersistence) => string | null;
|
|
11
|
-
clear: (pluginId: string, persistence: DocsVersionPersistence) => void;
|
|
12
|
-
};
|
|
13
|
-
export default DocsPreferredVersionStorage;
|
|
@@ -1,20 +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
|
-
import { createStorageSlot } from '../storageUtils';
|
|
8
|
-
const storageKey = (pluginId) => `docs-preferred-version-${pluginId}`;
|
|
9
|
-
const DocsPreferredVersionStorage = {
|
|
10
|
-
save: (pluginId, persistence, versionName) => {
|
|
11
|
-
createStorageSlot(storageKey(pluginId), { persistence }).set(versionName);
|
|
12
|
-
},
|
|
13
|
-
read: (pluginId, persistence) => {
|
|
14
|
-
return createStorageSlot(storageKey(pluginId), { persistence }).get();
|
|
15
|
-
},
|
|
16
|
-
clear: (pluginId, persistence) => {
|
|
17
|
-
createStorageSlot(storageKey(pluginId), { persistence }).del();
|
|
18
|
-
},
|
|
19
|
-
};
|
|
20
|
-
export default DocsPreferredVersionStorage;
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export declare function useDocsPreferredVersion(pluginId?: string | undefined): {
|
|
2
|
-
readonly preferredVersion: any;
|
|
3
|
-
readonly savePreferredVersionName: (versionName: string) => void;
|
|
4
|
-
};
|
|
5
|
-
export declare function useDocsPreferredVersionByPluginId(): Record<string, any>;
|
|
@@ -1,41 +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
|
-
import { useCallback } from 'react';
|
|
8
|
-
import { useDocsPreferredVersionContext } from './DocsPreferredVersionProvider';
|
|
9
|
-
import { useAllDocsData, useDocsData } from '@theme/hooks/useDocs';
|
|
10
|
-
import { DEFAULT_PLUGIN_ID } from '@docusaurus/constants';
|
|
11
|
-
// TODO improve typing
|
|
12
|
-
// Note, the preferredVersion attribute will always be null before mount
|
|
13
|
-
export function useDocsPreferredVersion(pluginId = DEFAULT_PLUGIN_ID) {
|
|
14
|
-
const docsData = useDocsData(pluginId);
|
|
15
|
-
const [state, api] = useDocsPreferredVersionContext();
|
|
16
|
-
const { preferredVersionName } = state[pluginId];
|
|
17
|
-
const preferredVersion = preferredVersionName
|
|
18
|
-
? docsData.versions.find((version) => version.name === preferredVersionName)
|
|
19
|
-
: null;
|
|
20
|
-
const savePreferredVersionName = useCallback((versionName) => {
|
|
21
|
-
api.savePreferredVersion(pluginId, versionName);
|
|
22
|
-
}, [api]);
|
|
23
|
-
return { preferredVersion, savePreferredVersionName };
|
|
24
|
-
}
|
|
25
|
-
export function useDocsPreferredVersionByPluginId() {
|
|
26
|
-
const allDocsData = useAllDocsData();
|
|
27
|
-
const [state] = useDocsPreferredVersionContext();
|
|
28
|
-
function getPluginIdPreferredVersion(pluginId) {
|
|
29
|
-
const docsData = allDocsData[pluginId];
|
|
30
|
-
const { preferredVersionName } = state[pluginId];
|
|
31
|
-
return preferredVersionName
|
|
32
|
-
? docsData.versions.find((version) => version.name === preferredVersionName)
|
|
33
|
-
: null;
|
|
34
|
-
}
|
|
35
|
-
const pluginIds = Object.keys(allDocsData);
|
|
36
|
-
const result = {};
|
|
37
|
-
pluginIds.forEach((pluginId) => {
|
|
38
|
-
result[pluginId] = getPluginIdPreferredVersion(pluginId);
|
|
39
|
-
});
|
|
40
|
-
return result;
|
|
41
|
-
}
|
package/lib/utils/pathUtils.js
DELETED
|
@@ -1,13 +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
|
-
// Compare the 2 paths, ignoring trailing /
|
|
8
|
-
export const isSamePath = (path1, path2) => {
|
|
9
|
-
const normalize = (pathname) => {
|
|
10
|
-
return !pathname || (pathname === null || pathname === void 0 ? void 0 : pathname.endsWith('/')) ? pathname : `${pathname}/`;
|
|
11
|
-
};
|
|
12
|
-
return normalize(path1) === normalize(path2);
|
|
13
|
-
};
|
package/lib/utils/usePrevious.js
DELETED
|
@@ -1,14 +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
|
-
import { useRef, useEffect } from 'react';
|
|
8
|
-
export function usePrevious(value) {
|
|
9
|
-
const ref = useRef();
|
|
10
|
-
useEffect(() => {
|
|
11
|
-
ref.current = value;
|
|
12
|
-
});
|
|
13
|
-
return ref.current;
|
|
14
|
-
}
|
|
@@ -1,54 +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 {parseCodeBlockTitle} from '../codeBlockUtils';
|
|
9
|
-
|
|
10
|
-
describe('parseCodeBlockTitle', () => {
|
|
11
|
-
test('should parse double quote delimited title', () => {
|
|
12
|
-
expect(parseCodeBlockTitle(`title="index.js"`)).toEqual(`index.js`);
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
test('should parse single quote delimited title', () => {
|
|
16
|
-
expect(parseCodeBlockTitle(`title='index.js'`)).toEqual(`index.js`);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
test('should not parse mismatched quote delimiters', () => {
|
|
20
|
-
expect(parseCodeBlockTitle(`title="index.js'`)).toEqual(``);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
test('should parse undefined metastring', () => {
|
|
24
|
-
expect(parseCodeBlockTitle(undefined)).toEqual(``);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
test('should parse metastring with no title specified', () => {
|
|
28
|
-
expect(parseCodeBlockTitle(`{1,2-3}`)).toEqual(``);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test('should parse with multiple metadatas title first', () => {
|
|
32
|
-
expect(parseCodeBlockTitle(`title="index.js" label="JavaScript"`)).toEqual(
|
|
33
|
-
`index.js`,
|
|
34
|
-
);
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
test('should parse with multiple metadatas title last', () => {
|
|
38
|
-
expect(parseCodeBlockTitle(`label="JavaScript" title="index.js"`)).toEqual(
|
|
39
|
-
`index.js`,
|
|
40
|
-
);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
test('should parse double quotes when delimited by single quotes', () => {
|
|
44
|
-
expect(parseCodeBlockTitle(`title='console.log("Hello, World!")'`)).toEqual(
|
|
45
|
-
`console.log("Hello, World!")`,
|
|
46
|
-
);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
test('should parse single quotes when delimited by double quotes', () => {
|
|
50
|
-
expect(parseCodeBlockTitle(`title="console.log('Hello, World!')"`)).toEqual(
|
|
51
|
-
`console.log('Hello, World!')`,
|
|
52
|
-
);
|
|
53
|
-
});
|
|
54
|
-
});
|
|
@@ -1,32 +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 {isSamePath} from '../pathUtils';
|
|
9
|
-
|
|
10
|
-
describe('isSamePath', () => {
|
|
11
|
-
test('should be true for compared path without trailing slash', () => {
|
|
12
|
-
expect(isSamePath('/docs', '/docs')).toBeTruthy();
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
test('should be true for compared path with trailing slash', () => {
|
|
16
|
-
expect(isSamePath('/docs', '/docs/')).toBeTruthy();
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
test('should be false for compared path with double trailing slash', () => {
|
|
20
|
-
expect(isSamePath('/docs', '/docs//')).toBeFalsy();
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
test('should be true for twice undefined/null', () => {
|
|
24
|
-
expect(isSamePath(undefined, undefined)).toBeTruthy();
|
|
25
|
-
expect(isSamePath(undefined, undefined)).toBeTruthy();
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
test('should be false when one undefined', () => {
|
|
29
|
-
expect(isSamePath('/docs', undefined)).toBeFalsy();
|
|
30
|
-
expect(isSamePath(undefined, '/docs')).toBeFalsy();
|
|
31
|
-
});
|
|
32
|
-
});
|
|
@@ -1,165 +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
|
-
import React, {
|
|
8
|
-
createContext,
|
|
9
|
-
ReactNode,
|
|
10
|
-
useContext,
|
|
11
|
-
useEffect,
|
|
12
|
-
useMemo,
|
|
13
|
-
useState,
|
|
14
|
-
} from 'react';
|
|
15
|
-
import {useThemeConfig, DocsVersionPersistence} from '../useThemeConfig';
|
|
16
|
-
import {isDocsPluginEnabled} from '../docsUtils';
|
|
17
|
-
|
|
18
|
-
import {useAllDocsData} from '@theme/hooks/useDocs';
|
|
19
|
-
|
|
20
|
-
import DocsPreferredVersionStorage from './DocsPreferredVersionStorage';
|
|
21
|
-
|
|
22
|
-
type DocsPreferredVersionName = string | null;
|
|
23
|
-
|
|
24
|
-
// State for a single docs plugin instance
|
|
25
|
-
type DocsPreferredVersionPluginState = {
|
|
26
|
-
preferredVersionName: DocsPreferredVersionName;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
// We need to store in state/storage globally
|
|
30
|
-
// one preferred version per docs plugin instance
|
|
31
|
-
// pluginId => pluginState
|
|
32
|
-
type DocsPreferredVersionState = Record<
|
|
33
|
-
string,
|
|
34
|
-
DocsPreferredVersionPluginState
|
|
35
|
-
>;
|
|
36
|
-
|
|
37
|
-
// Initial state is always null as we can't read localstorage from node SSR
|
|
38
|
-
function getInitialState(pluginIds: string[]): DocsPreferredVersionState {
|
|
39
|
-
const initialState: DocsPreferredVersionState = {};
|
|
40
|
-
pluginIds.forEach((pluginId) => {
|
|
41
|
-
initialState[pluginId] = {
|
|
42
|
-
preferredVersionName: null,
|
|
43
|
-
};
|
|
44
|
-
});
|
|
45
|
-
return initialState;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// Read storage for all docs plugins
|
|
49
|
-
// Assign to each doc plugin a preferred version (if found)
|
|
50
|
-
function readStorageState({
|
|
51
|
-
pluginIds,
|
|
52
|
-
versionPersistence,
|
|
53
|
-
allDocsData,
|
|
54
|
-
}: {
|
|
55
|
-
pluginIds: string[];
|
|
56
|
-
versionPersistence: DocsVersionPersistence;
|
|
57
|
-
allDocsData: any; // TODO find a way to type it :(
|
|
58
|
-
}): DocsPreferredVersionState {
|
|
59
|
-
// The storage value we read might be stale,
|
|
60
|
-
// and belong to a version that does not exist in the site anymore
|
|
61
|
-
// In such case, we remove the storage value to avoid downstream errors
|
|
62
|
-
function restorePluginState(
|
|
63
|
-
pluginId: string,
|
|
64
|
-
): DocsPreferredVersionPluginState {
|
|
65
|
-
const preferredVersionNameUnsafe = DocsPreferredVersionStorage.read(
|
|
66
|
-
pluginId,
|
|
67
|
-
versionPersistence,
|
|
68
|
-
);
|
|
69
|
-
const pluginData = allDocsData[pluginId];
|
|
70
|
-
const versionExists = pluginData.versions.some(
|
|
71
|
-
(version: any) => version.name === preferredVersionNameUnsafe,
|
|
72
|
-
);
|
|
73
|
-
if (versionExists) {
|
|
74
|
-
return {preferredVersionName: preferredVersionNameUnsafe};
|
|
75
|
-
} else {
|
|
76
|
-
DocsPreferredVersionStorage.clear(pluginId, versionPersistence);
|
|
77
|
-
return {preferredVersionName: null};
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const initialState: DocsPreferredVersionState = {};
|
|
82
|
-
pluginIds.forEach((pluginId) => {
|
|
83
|
-
initialState[pluginId] = restorePluginState(pluginId);
|
|
84
|
-
});
|
|
85
|
-
return initialState;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function useVersionPersistence(): DocsVersionPersistence {
|
|
89
|
-
return useThemeConfig().docs.versionPersistence;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// Value that will be accessible through context: [state,api]
|
|
93
|
-
function useContextValue() {
|
|
94
|
-
const allDocsData = useAllDocsData();
|
|
95
|
-
const versionPersistence = useVersionPersistence();
|
|
96
|
-
const pluginIds = useMemo(() => Object.keys(allDocsData), [allDocsData]);
|
|
97
|
-
|
|
98
|
-
// Initial state is empty, as we can't read browser storage in node/SSR
|
|
99
|
-
const [state, setState] = useState(() => getInitialState(pluginIds));
|
|
100
|
-
|
|
101
|
-
// On mount, we set the state read from browser storage
|
|
102
|
-
useEffect(() => {
|
|
103
|
-
setState(readStorageState({allDocsData, versionPersistence, pluginIds}));
|
|
104
|
-
}, [allDocsData, versionPersistence, pluginIds]);
|
|
105
|
-
|
|
106
|
-
// The API that we expose to consumer hooks (memo for constant object)
|
|
107
|
-
const api = useMemo(() => {
|
|
108
|
-
function savePreferredVersion(pluginId: string, versionName: string) {
|
|
109
|
-
DocsPreferredVersionStorage.save(
|
|
110
|
-
pluginId,
|
|
111
|
-
versionPersistence,
|
|
112
|
-
versionName,
|
|
113
|
-
);
|
|
114
|
-
setState((s) => ({
|
|
115
|
-
...s,
|
|
116
|
-
[pluginId]: {preferredVersionName: versionName},
|
|
117
|
-
}));
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return {
|
|
121
|
-
savePreferredVersion,
|
|
122
|
-
};
|
|
123
|
-
}, [setState]);
|
|
124
|
-
|
|
125
|
-
return [state, api] as const;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
type DocsPreferredVersionContextValue = ReturnType<typeof useContextValue>;
|
|
129
|
-
|
|
130
|
-
const Context = createContext<DocsPreferredVersionContextValue | null>(null);
|
|
131
|
-
|
|
132
|
-
export function DocsPreferredVersionContextProvider({
|
|
133
|
-
children,
|
|
134
|
-
}: {
|
|
135
|
-
children: ReactNode;
|
|
136
|
-
}): JSX.Element {
|
|
137
|
-
if (isDocsPluginEnabled) {
|
|
138
|
-
return (
|
|
139
|
-
<DocsPreferredVersionContextProviderUnsafe>
|
|
140
|
-
{children}
|
|
141
|
-
</DocsPreferredVersionContextProviderUnsafe>
|
|
142
|
-
);
|
|
143
|
-
} else {
|
|
144
|
-
return <>{children}</>;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
function DocsPreferredVersionContextProviderUnsafe({
|
|
149
|
-
children,
|
|
150
|
-
}: {
|
|
151
|
-
children: ReactNode;
|
|
152
|
-
}): JSX.Element {
|
|
153
|
-
const contextValue = useContextValue();
|
|
154
|
-
return <Context.Provider value={contextValue}>{children}</Context.Provider>;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
export function useDocsPreferredVersionContext(): DocsPreferredVersionContextValue {
|
|
158
|
-
const value = useContext(Context);
|
|
159
|
-
if (!value) {
|
|
160
|
-
throw new Error(
|
|
161
|
-
'Can\'t find docs preferred context, maybe you forgot to use the "DocsPreferredVersionContextProvider"?',
|
|
162
|
-
);
|
|
163
|
-
}
|
|
164
|
-
return value;
|
|
165
|
-
}
|
|
@@ -1,34 +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 {createStorageSlot} from '../storageUtils';
|
|
9
|
-
import {DocsVersionPersistence} from '../useThemeConfig';
|
|
10
|
-
|
|
11
|
-
const storageKey = (pluginId: string) => `docs-preferred-version-${pluginId}`;
|
|
12
|
-
|
|
13
|
-
const DocsPreferredVersionStorage = {
|
|
14
|
-
save: (
|
|
15
|
-
pluginId: string,
|
|
16
|
-
persistence: DocsVersionPersistence,
|
|
17
|
-
versionName: string,
|
|
18
|
-
): void => {
|
|
19
|
-
createStorageSlot(storageKey(pluginId), {persistence}).set(versionName);
|
|
20
|
-
},
|
|
21
|
-
|
|
22
|
-
read: (
|
|
23
|
-
pluginId: string,
|
|
24
|
-
persistence: DocsVersionPersistence,
|
|
25
|
-
): string | null => {
|
|
26
|
-
return createStorageSlot(storageKey(pluginId), {persistence}).get();
|
|
27
|
-
},
|
|
28
|
-
|
|
29
|
-
clear: (pluginId: string, persistence: DocsVersionPersistence): void => {
|
|
30
|
-
createStorageSlot(storageKey(pluginId), {persistence}).del();
|
|
31
|
-
},
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export default DocsPreferredVersionStorage;
|
|
@@ -1,66 +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
|
-
import {useCallback} from 'react';
|
|
8
|
-
import {useDocsPreferredVersionContext} from './DocsPreferredVersionProvider';
|
|
9
|
-
import {useAllDocsData, useDocsData} from '@theme/hooks/useDocs';
|
|
10
|
-
|
|
11
|
-
import {DEFAULT_PLUGIN_ID} from '@docusaurus/constants';
|
|
12
|
-
|
|
13
|
-
// TODO improve typing
|
|
14
|
-
|
|
15
|
-
// Note, the preferredVersion attribute will always be null before mount
|
|
16
|
-
export function useDocsPreferredVersion(
|
|
17
|
-
pluginId: string | undefined = DEFAULT_PLUGIN_ID,
|
|
18
|
-
) {
|
|
19
|
-
const docsData = useDocsData(pluginId);
|
|
20
|
-
const [state, api] = useDocsPreferredVersionContext();
|
|
21
|
-
|
|
22
|
-
const {preferredVersionName} = state[pluginId];
|
|
23
|
-
|
|
24
|
-
const preferredVersion = preferredVersionName
|
|
25
|
-
? docsData.versions.find(
|
|
26
|
-
(version: any) => version.name === preferredVersionName,
|
|
27
|
-
)
|
|
28
|
-
: null;
|
|
29
|
-
|
|
30
|
-
const savePreferredVersionName = useCallback(
|
|
31
|
-
(versionName: string) => {
|
|
32
|
-
api.savePreferredVersion(pluginId, versionName);
|
|
33
|
-
},
|
|
34
|
-
[api],
|
|
35
|
-
);
|
|
36
|
-
|
|
37
|
-
return {preferredVersion, savePreferredVersionName} as const;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function useDocsPreferredVersionByPluginId(): Record<string, any> {
|
|
41
|
-
const allDocsData = useAllDocsData();
|
|
42
|
-
const [state] = useDocsPreferredVersionContext();
|
|
43
|
-
|
|
44
|
-
function getPluginIdPreferredVersion(pluginId: string) {
|
|
45
|
-
const docsData = allDocsData[pluginId];
|
|
46
|
-
const {preferredVersionName} = state[pluginId];
|
|
47
|
-
|
|
48
|
-
return preferredVersionName
|
|
49
|
-
? docsData.versions.find(
|
|
50
|
-
(version: any) => version.name === preferredVersionName,
|
|
51
|
-
)
|
|
52
|
-
: null;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const pluginIds = Object.keys(allDocsData);
|
|
56
|
-
|
|
57
|
-
const result: Record<
|
|
58
|
-
string,
|
|
59
|
-
any // TODO find a way to type this properly!
|
|
60
|
-
> = {};
|
|
61
|
-
pluginIds.forEach((pluginId) => {
|
|
62
|
-
result[pluginId] = getPluginIdPreferredVersion(pluginId);
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
return result;
|
|
66
|
-
}
|
package/src/utils/docsUtils.ts
DELETED
|
@@ -1,11 +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 {useAllDocsData} from '@theme/hooks/useDocs';
|
|
9
|
-
|
|
10
|
-
// TODO not ideal, see also "useDocs"
|
|
11
|
-
export const isDocsPluginEnabled: boolean = !!useAllDocsData;
|
package/src/utils/pathUtils.ts
DELETED
|
@@ -1,17 +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
|
-
// Compare the 2 paths, ignoring trailing /
|
|
9
|
-
export const isSamePath = (
|
|
10
|
-
path1: string | undefined,
|
|
11
|
-
path2: string | undefined,
|
|
12
|
-
): boolean => {
|
|
13
|
-
const normalize = (pathname: string | undefined) => {
|
|
14
|
-
return !pathname || pathname?.endsWith('/') ? pathname : `${pathname}/`;
|
|
15
|
-
};
|
|
16
|
-
return normalize(path1) === normalize(path2);
|
|
17
|
-
};
|
package/src/utils/usePrevious.ts
DELETED
|
@@ -1,18 +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 {useRef, useEffect} from 'react';
|
|
9
|
-
|
|
10
|
-
export function usePrevious<T>(value: T): T | undefined {
|
|
11
|
-
const ref = useRef<T>();
|
|
12
|
-
|
|
13
|
-
useEffect(() => {
|
|
14
|
-
ref.current = value;
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
return ref.current;
|
|
18
|
-
}
|