@docusaurus/theme-common 2.0.0-beta.1ab8aa0af → 2.0.0-beta.2
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/.tsbuildinfo +1 -4103
- package/lib/index.d.ts +3 -1
- package/lib/index.js +3 -1
- package/lib/utils/announcementBarInlineJavaScript.d.ts +0 -0
- package/lib/utils/announcementBarInlineJavaScript.js +1 -0
- package/lib/utils/announcementBarUtils.d.ts +17 -0
- package/lib/utils/announcementBarUtils.js +73 -0
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +1 -1
- package/lib/utils/storageUtils.js +3 -3
- package/lib/utils/useLocationChange.d.ts +14 -0
- package/lib/utils/useLocationChange.js +25 -0
- package/lib/utils/usePluralForm.js +1 -1
- package/lib/utils/{useChangeRoute.d.ts → usePrevious.d.ts} +1 -1
- package/lib/utils/usePrevious.js +14 -0
- package/lib/utils/useThemeConfig.d.ts +1 -0
- package/package.json +9 -9
- package/src/index.ts +8 -1
- package/src/utils/announcementBarUtils.tsx +115 -0
- package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +1 -1
- package/src/utils/storageUtils.ts +3 -3
- package/src/utils/useAlternatePageUtils.ts +9 -1
- package/src/utils/useLocationChange.ts +37 -0
- package/src/utils/usePluralForm.ts +4 -2
- package/src/utils/usePrevious.ts +18 -0
- package/src/utils/useThemeConfig.ts +1 -0
- package/lib/utils/useChangeRoute.js +0 -18
- package/src/utils/useChangeRoute.ts +0 -21
package/lib/index.d.ts
CHANGED
|
@@ -14,7 +14,9 @@ export { isDocsPluginEnabled } from './utils/docsUtils';
|
|
|
14
14
|
export { isSamePath } from './utils/pathUtils';
|
|
15
15
|
export { useTitleFormatter } from './utils/generalUtils';
|
|
16
16
|
export { usePluralForm } from './utils/usePluralForm';
|
|
17
|
-
export {
|
|
17
|
+
export { useLocationChange } from './utils/useLocationChange';
|
|
18
|
+
export { usePrevious } from './utils/usePrevious';
|
|
18
19
|
export { useDocsPreferredVersion, useDocsPreferredVersionByPluginId, } from './utils/docsPreferredVersion/useDocsPreferredVersion';
|
|
19
20
|
export { DocsPreferredVersionContextProvider } from './utils/docsPreferredVersion/DocsPreferredVersionProvider';
|
|
20
21
|
export { ThemeClassNames } from './utils/ThemeClassNames';
|
|
22
|
+
export { AnnouncementBarProvider, useAnnouncementBar, } from './utils/announcementBarUtils';
|
package/lib/index.js
CHANGED
|
@@ -13,7 +13,9 @@ export { isDocsPluginEnabled } from './utils/docsUtils';
|
|
|
13
13
|
export { isSamePath } from './utils/pathUtils';
|
|
14
14
|
export { useTitleFormatter } from './utils/generalUtils';
|
|
15
15
|
export { usePluralForm } from './utils/usePluralForm';
|
|
16
|
-
export {
|
|
16
|
+
export { useLocationChange } from './utils/useLocationChange';
|
|
17
|
+
export { usePrevious } from './utils/usePrevious';
|
|
17
18
|
export { useDocsPreferredVersion, useDocsPreferredVersionByPluginId, } from './utils/docsPreferredVersion/useDocsPreferredVersion';
|
|
18
19
|
export { DocsPreferredVersionContextProvider } from './utils/docsPreferredVersion/DocsPreferredVersionProvider';
|
|
19
20
|
export { ThemeClassNames } from './utils/ThemeClassNames';
|
|
21
|
+
export { AnnouncementBarProvider, useAnnouncementBar, } from './utils/announcementBarUtils';
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1,17 @@
|
|
|
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 { ReactNode } from 'react';
|
|
8
|
+
export declare const AnnouncementBarDismissStorageKey = "docusaurus.announcement.dismiss";
|
|
9
|
+
declare type AnnouncementBarAPI = {
|
|
10
|
+
readonly isClosed: boolean;
|
|
11
|
+
readonly close: () => void;
|
|
12
|
+
};
|
|
13
|
+
export declare const AnnouncementBarProvider: ({ children }: {
|
|
14
|
+
children: ReactNode;
|
|
15
|
+
}) => JSX.Element;
|
|
16
|
+
export declare const useAnnouncementBar: () => AnnouncementBarAPI;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,73 @@
|
|
|
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, { useState, useEffect, useCallback, useMemo, useContext, createContext, } from 'react';
|
|
8
|
+
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
|
9
|
+
import { createStorageSlot } from './storageUtils';
|
|
10
|
+
import { useThemeConfig } from './useThemeConfig';
|
|
11
|
+
export const AnnouncementBarDismissStorageKey = 'docusaurus.announcement.dismiss';
|
|
12
|
+
const AnnouncementBarIdStorageKey = 'docusaurus.announcement.id';
|
|
13
|
+
const AnnouncementBarDismissStorage = createStorageSlot(AnnouncementBarDismissStorageKey);
|
|
14
|
+
const IdStorage = createStorageSlot(AnnouncementBarIdStorageKey);
|
|
15
|
+
const isDismissedInStorage = () => AnnouncementBarDismissStorage.get() === 'true';
|
|
16
|
+
const setDismissedInStorage = (bool) => AnnouncementBarDismissStorage.set(String(bool));
|
|
17
|
+
const useAnnouncementBarContextValue = () => {
|
|
18
|
+
const { announcementBar } = useThemeConfig();
|
|
19
|
+
const { isClient } = useDocusaurusContext();
|
|
20
|
+
const [isClosed, setClosed] = useState(() => {
|
|
21
|
+
return isClient
|
|
22
|
+
? // On client navigation: init with localstorage value
|
|
23
|
+
isDismissedInStorage()
|
|
24
|
+
: // On server/hydration: always visible to prevent layout shifts (will be hidden with css if needed)
|
|
25
|
+
false;
|
|
26
|
+
});
|
|
27
|
+
// Update state after hydration
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
setClosed(isDismissedInStorage());
|
|
30
|
+
}, []);
|
|
31
|
+
const handleClose = useCallback(() => {
|
|
32
|
+
setDismissedInStorage(true);
|
|
33
|
+
setClosed(true);
|
|
34
|
+
}, []);
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
if (!announcementBar) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const { id } = announcementBar;
|
|
40
|
+
let viewedId = IdStorage.get();
|
|
41
|
+
// retrocompatibility due to spelling mistake of default id
|
|
42
|
+
// see https://github.com/facebook/docusaurus/issues/3338
|
|
43
|
+
if (viewedId === 'annoucement-bar') {
|
|
44
|
+
viewedId = 'announcement-bar';
|
|
45
|
+
}
|
|
46
|
+
const isNewAnnouncement = id !== viewedId;
|
|
47
|
+
IdStorage.set(id);
|
|
48
|
+
if (isNewAnnouncement) {
|
|
49
|
+
setDismissedInStorage(false);
|
|
50
|
+
}
|
|
51
|
+
if (isNewAnnouncement || !isDismissedInStorage()) {
|
|
52
|
+
setClosed(false);
|
|
53
|
+
}
|
|
54
|
+
}, []);
|
|
55
|
+
return useMemo(() => {
|
|
56
|
+
return {
|
|
57
|
+
isClosed,
|
|
58
|
+
close: handleClose,
|
|
59
|
+
};
|
|
60
|
+
}, [isClosed]);
|
|
61
|
+
};
|
|
62
|
+
const AnnouncementBarContext = createContext(null);
|
|
63
|
+
export const AnnouncementBarProvider = ({ children }) => {
|
|
64
|
+
const value = useAnnouncementBarContextValue();
|
|
65
|
+
return (React.createElement(AnnouncementBarContext.Provider, { value: value }, children));
|
|
66
|
+
};
|
|
67
|
+
export const useAnnouncementBar = () => {
|
|
68
|
+
const api = useContext(AnnouncementBarContext);
|
|
69
|
+
if (!api) {
|
|
70
|
+
throw new Error('useAnnouncementBar(): AnnouncementBar not found in React context: make sure to use the AnnouncementBarProvider on top of the tree');
|
|
71
|
+
}
|
|
72
|
+
return api;
|
|
73
|
+
};
|
|
@@ -88,7 +88,7 @@ function DocsPreferredVersionContextProviderUnsafe({ children, }) {
|
|
|
88
88
|
export function useDocsPreferredVersionContext() {
|
|
89
89
|
const value = useContext(Context);
|
|
90
90
|
if (!value) {
|
|
91
|
-
throw new Error(
|
|
91
|
+
throw new Error('Can\'t find docs preferred context, maybe you forgot to use the "DocsPreferredVersionContextProvider"?');
|
|
92
92
|
}
|
|
93
93
|
return value;
|
|
94
94
|
}
|
|
@@ -10,7 +10,7 @@ const DefaultStorageType = 'localStorage';
|
|
|
10
10
|
// See https://github.com/facebook/docusaurus/pull/4501
|
|
11
11
|
function getBrowserStorage(storageType = DefaultStorageType) {
|
|
12
12
|
if (typeof window === 'undefined') {
|
|
13
|
-
throw new Error('Browser storage is not available on
|
|
13
|
+
throw new Error('Browser storage is not available on Node.js/Docusaurus SSR process.');
|
|
14
14
|
}
|
|
15
15
|
if (storageType === 'none') {
|
|
16
16
|
return null;
|
|
@@ -33,7 +33,7 @@ let hasLoggedBrowserStorageNotAvailableWarning = false;
|
|
|
33
33
|
function logOnceBrowserStorageNotAvailableWarning(error) {
|
|
34
34
|
if (!hasLoggedBrowserStorageNotAvailableWarning) {
|
|
35
35
|
console.warn(`Docusaurus browser storage is not available.
|
|
36
|
-
Possible reasons: running Docusaurus in an
|
|
36
|
+
Possible reasons: running Docusaurus in an iframe, in an incognito browser session, or using too strict browser privacy settings.`, error);
|
|
37
37
|
hasLoggedBrowserStorageNotAvailableWarning = true;
|
|
38
38
|
}
|
|
39
39
|
}
|
|
@@ -45,7 +45,7 @@ const NoopStorageSlot = {
|
|
|
45
45
|
// Fail-fast, as storage APIs should not be used during the SSR process
|
|
46
46
|
function createServerStorageSlot(key) {
|
|
47
47
|
function throwError() {
|
|
48
|
-
throw new Error(`Illegal storage API usage for storage key
|
|
48
|
+
throw new Error(`Illegal storage API usage for storage key "${key}".
|
|
49
49
|
Docusaurus storage APIs are not supposed to be called on the server-rendering process.
|
|
50
50
|
Please only call storage APIs in effects and event handlers.`);
|
|
51
51
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
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 { Location } from '@docusaurus/history';
|
|
8
|
+
declare type LocationChangeEvent = {
|
|
9
|
+
location: Location;
|
|
10
|
+
previousLocation: Location | undefined;
|
|
11
|
+
};
|
|
12
|
+
declare type OnLocationChange = (locationChangeEvent: LocationChangeEvent) => void;
|
|
13
|
+
export declare function useLocationChange(onLocationChange: OnLocationChange): void;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
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 { useEffect, useRef } from 'react';
|
|
8
|
+
import { useLocation } from '@docusaurus/router';
|
|
9
|
+
import { usePrevious } from './usePrevious';
|
|
10
|
+
export function useLocationChange(onLocationChange) {
|
|
11
|
+
const location = useLocation();
|
|
12
|
+
const previousLocation = usePrevious(location);
|
|
13
|
+
const isFirst = useRef(true);
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
// Prevent first effect to trigger the listener on mount
|
|
16
|
+
if (isFirst.current) {
|
|
17
|
+
isFirst.current = false;
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
onLocationChange({
|
|
21
|
+
location,
|
|
22
|
+
previousLocation,
|
|
23
|
+
});
|
|
24
|
+
}, [location]);
|
|
25
|
+
}
|
|
@@ -54,7 +54,7 @@ function useLocalePluralForms() {
|
|
|
54
54
|
return createLocalePluralForms(currentLocale);
|
|
55
55
|
}
|
|
56
56
|
catch (e) {
|
|
57
|
-
console.error(`Failed to use Intl.PluralRules for locale
|
|
57
|
+
console.error(`Failed to use Intl.PluralRules for locale "${currentLocale}".
|
|
58
58
|
Docusaurus will fallback to a default/fallback (English) Intl.PluralRules implementation.
|
|
59
59
|
`);
|
|
60
60
|
return EnglishPluralForms;
|
|
@@ -4,4 +4,4 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
export declare function
|
|
7
|
+
export declare function usePrevious<T>(value: T): T | undefined;
|
|
@@ -0,0 +1,14 @@
|
|
|
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
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/theme-common",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.2",
|
|
4
4
|
"description": "Common code for Docusaurus themes.",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
@@ -18,23 +18,23 @@
|
|
|
18
18
|
},
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@docusaurus/core": "2.0.0-beta.
|
|
22
|
-
"@docusaurus/plugin-content-blog": "2.0.0-beta.
|
|
23
|
-
"@docusaurus/plugin-content-docs": "2.0.0-beta.
|
|
24
|
-
"@docusaurus/plugin-content-pages": "2.0.0-beta.
|
|
25
|
-
"@docusaurus/types": "2.0.0-beta.
|
|
21
|
+
"@docusaurus/core": "2.0.0-beta.2",
|
|
22
|
+
"@docusaurus/plugin-content-blog": "2.0.0-beta.2",
|
|
23
|
+
"@docusaurus/plugin-content-docs": "2.0.0-beta.2",
|
|
24
|
+
"@docusaurus/plugin-content-pages": "2.0.0-beta.2",
|
|
25
|
+
"@docusaurus/types": "2.0.0-beta.2",
|
|
26
26
|
"tslib": "^2.1.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@docusaurus/module-type-aliases": "2.0.0-beta.
|
|
29
|
+
"@docusaurus/module-type-aliases": "2.0.0-beta.2"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"prism-react-renderer": "^1.
|
|
32
|
+
"prism-react-renderer": "^1.2.1",
|
|
33
33
|
"react": "^16.8.4 || ^17.0.0",
|
|
34
34
|
"react-dom": "^16.8.4 || ^17.0.0"
|
|
35
35
|
},
|
|
36
36
|
"engines": {
|
|
37
37
|
"node": ">=12.13.0"
|
|
38
38
|
},
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "883f07fddffaf1657407c8e202e370cc436e25f7"
|
|
40
40
|
}
|
package/src/index.ts
CHANGED
|
@@ -33,7 +33,9 @@ export {useTitleFormatter} from './utils/generalUtils';
|
|
|
33
33
|
|
|
34
34
|
export {usePluralForm} from './utils/usePluralForm';
|
|
35
35
|
|
|
36
|
-
export {
|
|
36
|
+
export {useLocationChange} from './utils/useLocationChange';
|
|
37
|
+
|
|
38
|
+
export {usePrevious} from './utils/usePrevious';
|
|
37
39
|
|
|
38
40
|
export {
|
|
39
41
|
useDocsPreferredVersion,
|
|
@@ -43,3 +45,8 @@ export {
|
|
|
43
45
|
export {DocsPreferredVersionContextProvider} from './utils/docsPreferredVersion/DocsPreferredVersionProvider';
|
|
44
46
|
|
|
45
47
|
export {ThemeClassNames} from './utils/ThemeClassNames';
|
|
48
|
+
|
|
49
|
+
export {
|
|
50
|
+
AnnouncementBarProvider,
|
|
51
|
+
useAnnouncementBar,
|
|
52
|
+
} from './utils/announcementBarUtils';
|
|
@@ -0,0 +1,115 @@
|
|
|
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 useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
|
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 isClosed: boolean;
|
|
37
|
+
readonly close: () => void;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const useAnnouncementBarContextValue = (): AnnouncementBarAPI => {
|
|
41
|
+
const {announcementBar} = useThemeConfig();
|
|
42
|
+
const {isClient} = useDocusaurusContext();
|
|
43
|
+
|
|
44
|
+
const [isClosed, setClosed] = useState(() => {
|
|
45
|
+
return isClient
|
|
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
|
+
}, []);
|
|
87
|
+
|
|
88
|
+
return useMemo(() => {
|
|
89
|
+
return {
|
|
90
|
+
isClosed,
|
|
91
|
+
close: handleClose,
|
|
92
|
+
};
|
|
93
|
+
}, [isClosed]);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const AnnouncementBarContext = createContext<AnnouncementBarAPI | null>(null);
|
|
97
|
+
|
|
98
|
+
export const AnnouncementBarProvider = ({children}: {children: ReactNode}) => {
|
|
99
|
+
const value = useAnnouncementBarContextValue();
|
|
100
|
+
return (
|
|
101
|
+
<AnnouncementBarContext.Provider value={value}>
|
|
102
|
+
{children}
|
|
103
|
+
</AnnouncementBarContext.Provider>
|
|
104
|
+
);
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export const useAnnouncementBar = (): AnnouncementBarAPI => {
|
|
108
|
+
const api = useContext(AnnouncementBarContext);
|
|
109
|
+
if (!api) {
|
|
110
|
+
throw new Error(
|
|
111
|
+
'useAnnouncementBar(): AnnouncementBar not found in React context: make sure to use the AnnouncementBarProvider on top of the tree',
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
return api;
|
|
115
|
+
};
|
|
@@ -158,7 +158,7 @@ export function useDocsPreferredVersionContext(): DocsPreferredVersionContextVal
|
|
|
158
158
|
const value = useContext(Context);
|
|
159
159
|
if (!value) {
|
|
160
160
|
throw new Error(
|
|
161
|
-
|
|
161
|
+
'Can\'t find docs preferred context, maybe you forgot to use the "DocsPreferredVersionContextProvider"?',
|
|
162
162
|
);
|
|
163
163
|
}
|
|
164
164
|
return value;
|
|
@@ -18,7 +18,7 @@ function getBrowserStorage(
|
|
|
18
18
|
): Storage | null {
|
|
19
19
|
if (typeof window === 'undefined') {
|
|
20
20
|
throw new Error(
|
|
21
|
-
'Browser storage is not available on
|
|
21
|
+
'Browser storage is not available on Node.js/Docusaurus SSR process.',
|
|
22
22
|
);
|
|
23
23
|
}
|
|
24
24
|
if (storageType === 'none') {
|
|
@@ -42,7 +42,7 @@ function logOnceBrowserStorageNotAvailableWarning(error: Error) {
|
|
|
42
42
|
if (!hasLoggedBrowserStorageNotAvailableWarning) {
|
|
43
43
|
console.warn(
|
|
44
44
|
`Docusaurus browser storage is not available.
|
|
45
|
-
Possible reasons: running Docusaurus in an
|
|
45
|
+
Possible reasons: running Docusaurus in an iframe, in an incognito browser session, or using too strict browser privacy settings.`,
|
|
46
46
|
error,
|
|
47
47
|
);
|
|
48
48
|
hasLoggedBrowserStorageNotAvailableWarning = true;
|
|
@@ -65,7 +65,7 @@ const NoopStorageSlot: StorageSlot = {
|
|
|
65
65
|
// Fail-fast, as storage APIs should not be used during the SSR process
|
|
66
66
|
function createServerStorageSlot(key: string): StorageSlot {
|
|
67
67
|
function throwError(): never {
|
|
68
|
-
throw new Error(`Illegal storage API usage for storage key
|
|
68
|
+
throw new Error(`Illegal storage API usage for storage key "${key}".
|
|
69
69
|
Docusaurus storage APIs are not supposed to be called on the server-rendering process.
|
|
70
70
|
Please only call storage APIs in effects and event handlers.`);
|
|
71
71
|
}
|
|
@@ -11,7 +11,15 @@ import {useLocation} from '@docusaurus/router';
|
|
|
11
11
|
// Permits to obtain the url of the current page in another locale
|
|
12
12
|
// Useful to generate hreflang meta headers etc...
|
|
13
13
|
// See https://developers.google.com/search/docs/advanced/crawling/localized-versions
|
|
14
|
-
export function useAlternatePageUtils() {
|
|
14
|
+
export function useAlternatePageUtils(): {
|
|
15
|
+
createUrl: ({
|
|
16
|
+
locale,
|
|
17
|
+
fullyQualified,
|
|
18
|
+
}: {
|
|
19
|
+
locale: string;
|
|
20
|
+
fullyQualified: boolean;
|
|
21
|
+
}) => string;
|
|
22
|
+
} {
|
|
15
23
|
const {
|
|
16
24
|
siteConfig: {baseUrl, url},
|
|
17
25
|
i18n: {defaultLocale, currentLocale},
|
|
@@ -0,0 +1,37 @@
|
|
|
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 {useLocation} from '@docusaurus/router';
|
|
10
|
+
import {Location} from '@docusaurus/history';
|
|
11
|
+
import {usePrevious} from './usePrevious';
|
|
12
|
+
|
|
13
|
+
type LocationChangeEvent = {
|
|
14
|
+
location: Location;
|
|
15
|
+
previousLocation: Location | undefined;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
type OnLocationChange = (locationChangeEvent: LocationChangeEvent) => void;
|
|
19
|
+
|
|
20
|
+
export function useLocationChange(onLocationChange: OnLocationChange): void {
|
|
21
|
+
const location = useLocation();
|
|
22
|
+
const previousLocation = usePrevious(location);
|
|
23
|
+
const isFirst = useRef<boolean>(true);
|
|
24
|
+
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
// Prevent first effect to trigger the listener on mount
|
|
27
|
+
if (isFirst.current) {
|
|
28
|
+
isFirst.current = false;
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
onLocationChange({
|
|
33
|
+
location,
|
|
34
|
+
previousLocation,
|
|
35
|
+
});
|
|
36
|
+
}, [location]);
|
|
37
|
+
}
|
|
@@ -70,7 +70,7 @@ function useLocalePluralForms(): LocalePluralForms {
|
|
|
70
70
|
try {
|
|
71
71
|
return createLocalePluralForms(currentLocale);
|
|
72
72
|
} catch (e) {
|
|
73
|
-
console.error(`Failed to use Intl.PluralRules for locale
|
|
73
|
+
console.error(`Failed to use Intl.PluralRules for locale "${currentLocale}".
|
|
74
74
|
Docusaurus will fallback to a default/fallback (English) Intl.PluralRules implementation.
|
|
75
75
|
`);
|
|
76
76
|
return EnglishPluralForms;
|
|
@@ -107,7 +107,9 @@ function selectPluralMessage(
|
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
export function usePluralForm() {
|
|
110
|
+
export function usePluralForm(): {
|
|
111
|
+
selectMessage: (count: number, pluralMessages: string) => string;
|
|
112
|
+
} {
|
|
111
113
|
const localePluralForm = useLocalePluralForms();
|
|
112
114
|
return {
|
|
113
115
|
selectMessage: (count: number, pluralMessages: string): string => {
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
}
|
|
@@ -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
|
-
import { useRef, useEffect } from 'react';
|
|
8
|
-
import { useLocation } from '@docusaurus/router';
|
|
9
|
-
export function useChangeRoute(onRouteChange) {
|
|
10
|
-
const { pathname } = useLocation();
|
|
11
|
-
const latestPathnameRef = useRef(pathname);
|
|
12
|
-
useEffect(() => {
|
|
13
|
-
if (pathname !== latestPathnameRef.current) {
|
|
14
|
-
latestPathnameRef.current = pathname;
|
|
15
|
-
onRouteChange();
|
|
16
|
-
}
|
|
17
|
-
}, [pathname, latestPathnameRef, onRouteChange]);
|
|
18
|
-
}
|
|
@@ -1,21 +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
|
-
import {useLocation} from '@docusaurus/router';
|
|
10
|
-
|
|
11
|
-
export function useChangeRoute(onRouteChange: () => void): void {
|
|
12
|
-
const {pathname} = useLocation();
|
|
13
|
-
const latestPathnameRef = useRef(pathname);
|
|
14
|
-
|
|
15
|
-
useEffect(() => {
|
|
16
|
-
if (pathname !== latestPathnameRef.current) {
|
|
17
|
-
latestPathnameRef.current = pathname;
|
|
18
|
-
onRouteChange();
|
|
19
|
-
}
|
|
20
|
-
}, [pathname, latestPathnameRef, onRouteChange]);
|
|
21
|
-
}
|