@docusaurus/theme-common 2.0.0-beta.12faed89d → 2.0.0-beta.13
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/copyUntypedFiles.js +20 -0
- package/lib/.tsbuildinfo +1 -1
- package/lib/{utils/useCollapsible.d.ts → components/Collapsible/index.d.ts} +9 -3
- package/lib/{utils/useCollapsible.js → components/Collapsible/index.js} +36 -14
- package/lib/components/Details/index.d.ts +12 -0
- package/lib/components/Details/index.js +64 -0
- package/lib/components/Details/styles.module.css +58 -0
- package/lib/index.d.ts +19 -5
- package/lib/index.js +14 -3
- package/lib/utils/ThemeClassNames.d.ts +36 -12
- package/lib/utils/ThemeClassNames.js +36 -3
- package/lib/utils/announcementBarUtils.d.ts +3 -3
- package/lib/utils/announcementBarUtils.js +14 -18
- package/lib/utils/codeBlockUtils.d.ts +10 -0
- package/lib/utils/codeBlockUtils.js +119 -0
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +2 -2
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +2 -2
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +1 -3
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +11 -3
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +1 -2
- package/lib/utils/docsUtils.d.ts +20 -0
- package/lib/utils/docsUtils.js +106 -0
- package/lib/utils/generalUtils.d.ts +6 -0
- package/lib/utils/generalUtils.js +2 -2
- package/lib/utils/historyUtils.d.ts +11 -0
- package/lib/utils/historyUtils.js +39 -0
- package/lib/utils/jsUtils.d.ts +19 -0
- package/lib/utils/jsUtils.js +25 -0
- package/lib/utils/mobileSecondaryMenu.d.ts +2 -2
- package/lib/utils/mobileSecondaryMenu.js +3 -4
- package/lib/utils/pathUtils.js +1 -3
- package/lib/utils/reactUtils.d.ts +9 -0
- package/lib/utils/reactUtils.js +26 -0
- package/lib/utils/regexpUtils.d.ts +10 -0
- package/lib/utils/regexpUtils.js +16 -0
- package/lib/utils/scrollUtils.d.ts +52 -0
- package/lib/utils/scrollUtils.js +135 -0
- package/lib/utils/storageUtils.d.ts +4 -0
- package/lib/utils/storageUtils.js +29 -3
- package/lib/utils/tagsUtils.d.ts +18 -0
- package/lib/utils/tagsUtils.js +33 -0
- package/lib/utils/tocUtils.d.ts +15 -0
- package/lib/utils/tocUtils.js +34 -0
- package/lib/utils/useContextualSearchFilters.d.ts +11 -0
- package/lib/utils/useContextualSearchFilters.js +36 -0
- package/{src/utils/docsUtils.ts → lib/utils/useLocalPathname.d.ts} +1 -5
- package/lib/utils/useLocalPathname.js +16 -0
- package/lib/utils/useLocationChange.js +9 -11
- package/lib/utils/usePluralForm.js +1 -3
- package/lib/utils/usePrevious.js +3 -2
- package/lib/utils/useTOCHighlight.d.ts +14 -0
- package/lib/utils/useTOCHighlight.js +124 -0
- package/lib/utils/useThemeConfig.d.ts +20 -3
- package/package.json +18 -12
- package/src/{utils/useCollapsible.tsx → components/Collapsible/index.tsx} +70 -25
- package/src/components/Details/index.tsx +94 -0
- package/src/components/Details/styles.module.css +58 -0
- package/src/index.ts +54 -4
- package/src/types.d.ts +0 -2
- package/src/utils/ThemeClassNames.ts +42 -4
- package/src/utils/__tests__/codeBlockUtils.test.ts +2 -2
- package/src/utils/__tests__/docsUtils.test.tsx +331 -0
- package/src/utils/__tests__/jsUtils.test.ts +33 -0
- package/src/utils/__tests__/tagUtils.test.ts +66 -0
- package/src/utils/__tests__/tocUtils.test.ts +197 -0
- package/src/utils/announcementBarUtils.tsx +20 -15
- package/src/utils/codeBlockUtils.ts +151 -0
- package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +7 -6
- package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +2 -3
- package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +14 -14
- package/src/utils/docsUtils.tsx +185 -0
- package/src/utils/generalUtils.ts +3 -2
- package/src/utils/historyUtils.ts +50 -0
- package/src/utils/jsUtils.ts +33 -0
- package/src/utils/mobileSecondaryMenu.tsx +9 -6
- package/src/utils/pathUtils.ts +2 -3
- package/src/utils/reactUtils.tsx +34 -0
- package/src/utils/regexpUtils.ts +23 -0
- package/src/utils/scrollUtils.tsx +237 -0
- package/src/utils/storageUtils.ts +27 -4
- package/src/utils/tagsUtils.ts +48 -0
- package/src/utils/tocUtils.ts +55 -0
- package/src/utils/useContextualSearchFilters.ts +50 -0
- package/src/utils/useLocalPathname.ts +20 -0
- package/src/utils/useLocationChange.ts +10 -12
- package/src/utils/usePluralForm.ts +2 -3
- package/src/utils/usePrevious.ts +3 -2
- package/src/utils/useTOCHighlight.ts +179 -0
- package/src/utils/useThemeConfig.ts +19 -3
|
@@ -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
|
+
import { useMemo } from 'react';
|
|
8
|
+
export function filterTOC({ toc, minHeadingLevel, maxHeadingLevel, }) {
|
|
9
|
+
function isValid(item) {
|
|
10
|
+
return item.level >= minHeadingLevel && item.level <= maxHeadingLevel;
|
|
11
|
+
}
|
|
12
|
+
return toc.flatMap((item) => {
|
|
13
|
+
const filteredChildren = filterTOC({
|
|
14
|
+
toc: item.children,
|
|
15
|
+
minHeadingLevel,
|
|
16
|
+
maxHeadingLevel,
|
|
17
|
+
});
|
|
18
|
+
if (isValid(item)) {
|
|
19
|
+
return [
|
|
20
|
+
{
|
|
21
|
+
...item,
|
|
22
|
+
children: filteredChildren,
|
|
23
|
+
},
|
|
24
|
+
];
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
return filteredChildren;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
// Memoize potentially expensive filtering logic
|
|
32
|
+
export function useTOCFilter({ toc, minHeadingLevel, maxHeadingLevel, }) {
|
|
33
|
+
return useMemo(() => filterTOC({ toc, minHeadingLevel, maxHeadingLevel }), [toc, minHeadingLevel, maxHeadingLevel]);
|
|
34
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
export declare type useContextualSearchFiltersReturns = {
|
|
8
|
+
locale: string;
|
|
9
|
+
tags: string[];
|
|
10
|
+
};
|
|
11
|
+
export declare function useContextualSearchFilters(): useContextualSearchFiltersReturns;
|
|
@@ -0,0 +1,36 @@
|
|
|
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 { useAllDocsData, useActivePluginAndVersion } from '@theme/hooks/useDocs';
|
|
8
|
+
import { useDocsPreferredVersionByPluginId } from './docsPreferredVersion/useDocsPreferredVersion';
|
|
9
|
+
import { docVersionSearchTag, DEFAULT_SEARCH_TAG } from './searchUtils';
|
|
10
|
+
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
|
11
|
+
// We may want to support multiple search engines, don't couple that to Algolia/DocSearch
|
|
12
|
+
// Maybe users will want to use its own search engine solution
|
|
13
|
+
export function useContextualSearchFilters() {
|
|
14
|
+
const { i18n } = useDocusaurusContext();
|
|
15
|
+
const allDocsData = useAllDocsData();
|
|
16
|
+
const activePluginAndVersion = useActivePluginAndVersion();
|
|
17
|
+
const docsPreferredVersionByPluginId = useDocsPreferredVersionByPluginId();
|
|
18
|
+
function getDocPluginTags(pluginId) {
|
|
19
|
+
var _a, _b;
|
|
20
|
+
const activeVersion = ((_a = activePluginAndVersion === null || activePluginAndVersion === void 0 ? void 0 : activePluginAndVersion.activePlugin) === null || _a === void 0 ? void 0 : _a.pluginId) === pluginId
|
|
21
|
+
? activePluginAndVersion.activeVersion
|
|
22
|
+
: undefined;
|
|
23
|
+
const preferredVersion = docsPreferredVersionByPluginId[pluginId];
|
|
24
|
+
const latestVersion = allDocsData[pluginId].versions.find((v) => v.isLast);
|
|
25
|
+
const version = (_b = activeVersion !== null && activeVersion !== void 0 ? activeVersion : preferredVersion) !== null && _b !== void 0 ? _b : latestVersion;
|
|
26
|
+
return docVersionSearchTag(pluginId, version.name);
|
|
27
|
+
}
|
|
28
|
+
const tags = [
|
|
29
|
+
DEFAULT_SEARCH_TAG,
|
|
30
|
+
...Object.keys(allDocsData).map(getDocPluginTags),
|
|
31
|
+
];
|
|
32
|
+
return {
|
|
33
|
+
locale: i18n.currentLocale,
|
|
34
|
+
tags,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
@@ -4,8 +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
|
-
|
|
8
|
-
import {useAllDocsData} from '@theme/hooks/useDocs';
|
|
9
|
-
|
|
10
|
-
// TODO not ideal, see also "useDocs"
|
|
11
|
-
export const isDocsPluginEnabled: boolean = !!useAllDocsData;
|
|
7
|
+
export declare function useLocalPathname(): string;
|
|
@@ -0,0 +1,16 @@
|
|
|
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 useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
|
8
|
+
import { useLocation } from '@docusaurus/router';
|
|
9
|
+
// Get the pathname of current route, without the optional site baseUrl
|
|
10
|
+
// - /docs/myDoc => /docs/myDoc
|
|
11
|
+
// - /baseUrl/docs/myDoc => /docs/myDoc
|
|
12
|
+
export function useLocalPathname() {
|
|
13
|
+
const { siteConfig: { baseUrl }, } = useDocusaurusContext();
|
|
14
|
+
const { pathname } = useLocation();
|
|
15
|
+
return pathname.replace(baseUrl, '/');
|
|
16
|
+
}
|
|
@@ -4,22 +4,20 @@
|
|
|
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
|
-
import { useEffect
|
|
7
|
+
import { useEffect } from 'react';
|
|
8
8
|
import { useLocation } from '@docusaurus/router';
|
|
9
9
|
import { usePrevious } from './usePrevious';
|
|
10
|
+
import { useDynamicCallback } from './reactUtils';
|
|
10
11
|
export function useLocationChange(onLocationChange) {
|
|
11
12
|
const location = useLocation();
|
|
12
13
|
const previousLocation = usePrevious(location);
|
|
13
|
-
const
|
|
14
|
+
const onLocationChangeDynamic = useDynamicCallback(onLocationChange);
|
|
14
15
|
useEffect(() => {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
if (location !== previousLocation) {
|
|
17
|
+
onLocationChangeDynamic({
|
|
18
|
+
location,
|
|
19
|
+
previousLocation,
|
|
20
|
+
});
|
|
19
21
|
}
|
|
20
|
-
|
|
21
|
-
location,
|
|
22
|
-
previousLocation,
|
|
23
|
-
});
|
|
24
|
-
}, [location]);
|
|
22
|
+
}, [onLocationChangeDynamic, location, previousLocation]);
|
|
25
23
|
}
|
|
@@ -87,8 +87,6 @@ function selectPluralMessage(pluralMessages, count, localePluralForms) {
|
|
|
87
87
|
export function usePluralForm() {
|
|
88
88
|
const localePluralForm = useLocalePluralForms();
|
|
89
89
|
return {
|
|
90
|
-
selectMessage: (count, pluralMessages) =>
|
|
91
|
-
return selectPluralMessage(pluralMessages, count, localePluralForm);
|
|
92
|
-
},
|
|
90
|
+
selectMessage: (count, pluralMessages) => selectPluralMessage(pluralMessages, count, localePluralForm),
|
|
93
91
|
};
|
|
94
92
|
}
|
package/lib/utils/usePrevious.js
CHANGED
|
@@ -4,10 +4,11 @@
|
|
|
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
|
-
import { useRef
|
|
7
|
+
import { useRef } from 'react';
|
|
8
|
+
import { useIsomorphicLayoutEffect } from './reactUtils';
|
|
8
9
|
export function usePrevious(value) {
|
|
9
10
|
const ref = useRef();
|
|
10
|
-
|
|
11
|
+
useIsomorphicLayoutEffect(() => {
|
|
11
12
|
ref.current = value;
|
|
12
13
|
});
|
|
13
14
|
return ref.current;
|
|
@@ -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
|
+
export declare type TOCHighlightConfig = {
|
|
8
|
+
linkClassName: string;
|
|
9
|
+
linkActiveClassName: string;
|
|
10
|
+
minHeadingLevel: number;
|
|
11
|
+
maxHeadingLevel: number;
|
|
12
|
+
};
|
|
13
|
+
declare function useTOCHighlight(config: TOCHighlightConfig | undefined): void;
|
|
14
|
+
export default useTOCHighlight;
|
|
@@ -0,0 +1,124 @@
|
|
|
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 { useThemeConfig } from './useThemeConfig';
|
|
9
|
+
/*
|
|
10
|
+
TODO make the hardcoded theme-classic classnames configurable
|
|
11
|
+
(or add them to ThemeClassNames?)
|
|
12
|
+
*/
|
|
13
|
+
// If the anchor has no height and is just a "marker" in the dom; we'll use the parent (normally the link text) rect boundaries instead
|
|
14
|
+
function getVisibleBoundingClientRect(element) {
|
|
15
|
+
const rect = element.getBoundingClientRect();
|
|
16
|
+
const hasNoHeight = rect.top === rect.bottom;
|
|
17
|
+
if (hasNoHeight) {
|
|
18
|
+
return getVisibleBoundingClientRect(element.parentNode);
|
|
19
|
+
}
|
|
20
|
+
return rect;
|
|
21
|
+
}
|
|
22
|
+
// Considering we divide viewport into 2 zones of each 50vh
|
|
23
|
+
// This returns true if an element is in the first zone (ie, appear in viewport, near the top)
|
|
24
|
+
function isInViewportTopHalf(boundingRect) {
|
|
25
|
+
return boundingRect.top > 0 && boundingRect.bottom < window.innerHeight / 2;
|
|
26
|
+
}
|
|
27
|
+
function getAnchors({ minHeadingLevel, maxHeadingLevel, }) {
|
|
28
|
+
const selectors = [];
|
|
29
|
+
for (let i = minHeadingLevel; i <= maxHeadingLevel; i += 1) {
|
|
30
|
+
selectors.push(`h${i}.anchor`);
|
|
31
|
+
}
|
|
32
|
+
return Array.from(document.querySelectorAll(selectors.join()));
|
|
33
|
+
}
|
|
34
|
+
function getActiveAnchor(anchors, { anchorTopOffset, }) {
|
|
35
|
+
var _a;
|
|
36
|
+
// Naming is hard
|
|
37
|
+
// The "nextVisibleAnchor" is the first anchor that appear under the viewport top boundary
|
|
38
|
+
// Note: it does not mean this anchor is visible yet, but if user continues scrolling down, it will be the first to become visible
|
|
39
|
+
const nextVisibleAnchor = anchors.find((anchor) => {
|
|
40
|
+
const boundingRect = getVisibleBoundingClientRect(anchor);
|
|
41
|
+
return boundingRect.top >= anchorTopOffset;
|
|
42
|
+
});
|
|
43
|
+
if (nextVisibleAnchor) {
|
|
44
|
+
const boundingRect = getVisibleBoundingClientRect(nextVisibleAnchor);
|
|
45
|
+
// If anchor is in the top half of the viewport: it is the one we consider "active"
|
|
46
|
+
// (unless it's too close to the top and and soon to be scrolled outside viewport)
|
|
47
|
+
if (isInViewportTopHalf(boundingRect)) {
|
|
48
|
+
return nextVisibleAnchor;
|
|
49
|
+
}
|
|
50
|
+
// If anchor is in the bottom half of the viewport, or under the viewport, we consider the active anchor is the previous one
|
|
51
|
+
// This is because the main text appearing in the user screen mostly belong to the previous anchor
|
|
52
|
+
else {
|
|
53
|
+
// Returns null for the first anchor, see https://github.com/facebook/docusaurus/issues/5318
|
|
54
|
+
return (_a = anchors[anchors.indexOf(nextVisibleAnchor) - 1]) !== null && _a !== void 0 ? _a : null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
// no anchor under viewport top? (ie we are at the bottom of the page)
|
|
58
|
+
// => highlight the last anchor found
|
|
59
|
+
else {
|
|
60
|
+
return anchors[anchors.length - 1];
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function getLinkAnchorValue(link) {
|
|
64
|
+
return decodeURIComponent(link.href.substring(link.href.indexOf('#') + 1));
|
|
65
|
+
}
|
|
66
|
+
function getLinks(linkClassName) {
|
|
67
|
+
return Array.from(document.getElementsByClassName(linkClassName));
|
|
68
|
+
}
|
|
69
|
+
function getNavbarHeight() {
|
|
70
|
+
// Not ideal to obtain actual height this way
|
|
71
|
+
// Using TS ! (not ?) because otherwise a bad selector would be un-noticed
|
|
72
|
+
return document.querySelector('.navbar').clientHeight;
|
|
73
|
+
}
|
|
74
|
+
function useAnchorTopOffsetRef() {
|
|
75
|
+
const anchorTopOffsetRef = useRef(0);
|
|
76
|
+
const { navbar: { hideOnScroll }, } = useThemeConfig();
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
anchorTopOffsetRef.current = hideOnScroll ? 0 : getNavbarHeight();
|
|
79
|
+
}, [hideOnScroll]);
|
|
80
|
+
return anchorTopOffsetRef;
|
|
81
|
+
}
|
|
82
|
+
function useTOCHighlight(config) {
|
|
83
|
+
const lastActiveLinkRef = useRef(undefined);
|
|
84
|
+
const anchorTopOffsetRef = useAnchorTopOffsetRef();
|
|
85
|
+
useEffect(() => {
|
|
86
|
+
if (!config) {
|
|
87
|
+
// no-op, highlighting is disabled
|
|
88
|
+
return () => { };
|
|
89
|
+
}
|
|
90
|
+
const { linkClassName, linkActiveClassName, minHeadingLevel, maxHeadingLevel, } = config;
|
|
91
|
+
function updateLinkActiveClass(link, active) {
|
|
92
|
+
var _a;
|
|
93
|
+
if (active) {
|
|
94
|
+
if (lastActiveLinkRef.current && lastActiveLinkRef.current !== link) {
|
|
95
|
+
(_a = lastActiveLinkRef.current) === null || _a === void 0 ? void 0 : _a.classList.remove(linkActiveClassName);
|
|
96
|
+
}
|
|
97
|
+
link.classList.add(linkActiveClassName);
|
|
98
|
+
lastActiveLinkRef.current = link;
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
link.classList.remove(linkActiveClassName);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function updateActiveLink() {
|
|
105
|
+
const links = getLinks(linkClassName);
|
|
106
|
+
const anchors = getAnchors({ minHeadingLevel, maxHeadingLevel });
|
|
107
|
+
const activeAnchor = getActiveAnchor(anchors, {
|
|
108
|
+
anchorTopOffset: anchorTopOffsetRef.current,
|
|
109
|
+
});
|
|
110
|
+
const activeLink = links.find((link) => activeAnchor && activeAnchor.id === getLinkAnchorValue(link));
|
|
111
|
+
links.forEach((link) => {
|
|
112
|
+
updateLinkActiveClass(link, link === activeLink);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
document.addEventListener('scroll', updateActiveLink);
|
|
116
|
+
document.addEventListener('resize', updateActiveLink);
|
|
117
|
+
updateActiveLink();
|
|
118
|
+
return () => {
|
|
119
|
+
document.removeEventListener('scroll', updateActiveLink);
|
|
120
|
+
document.removeEventListener('resize', updateActiveLink);
|
|
121
|
+
};
|
|
122
|
+
}, [config, anchorTopOffsetRef]);
|
|
123
|
+
}
|
|
124
|
+
export default useTOCHighlight;
|
|
@@ -1,15 +1,24 @@
|
|
|
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
|
+
*/
|
|
1
7
|
import { PrismTheme } from 'prism-react-renderer';
|
|
2
8
|
import { CSSProperties } from 'react';
|
|
9
|
+
import { DeepPartial } from 'utility-types';
|
|
3
10
|
export declare type DocsVersionPersistence = 'localStorage' | 'none';
|
|
4
11
|
export declare type NavbarItem = {
|
|
5
12
|
type?: string | undefined;
|
|
6
13
|
items?: NavbarItem[];
|
|
7
14
|
label?: string;
|
|
8
15
|
position?: 'left' | 'right';
|
|
9
|
-
}
|
|
16
|
+
} & Record<string, unknown>;
|
|
10
17
|
export declare type NavbarLogo = {
|
|
11
18
|
src: string;
|
|
12
19
|
srcDark?: string;
|
|
20
|
+
width?: string | number;
|
|
21
|
+
height?: string | number;
|
|
13
22
|
href?: string;
|
|
14
23
|
target?: string;
|
|
15
24
|
alt?: string;
|
|
@@ -62,11 +71,17 @@ export declare type Footer = {
|
|
|
62
71
|
alt?: string;
|
|
63
72
|
src?: string;
|
|
64
73
|
srcDark?: string;
|
|
74
|
+
width?: string | number;
|
|
75
|
+
height?: string | number;
|
|
65
76
|
href?: string;
|
|
66
77
|
};
|
|
67
78
|
copyright?: string;
|
|
68
79
|
links: FooterLinks[];
|
|
69
80
|
};
|
|
81
|
+
export declare type TableOfContents = {
|
|
82
|
+
minHeadingLevel: number;
|
|
83
|
+
maxHeadingLevel: number;
|
|
84
|
+
};
|
|
70
85
|
export declare type ThemeConfig = {
|
|
71
86
|
docs: {
|
|
72
87
|
versionPersistence: DocsVersionPersistence;
|
|
@@ -77,8 +92,10 @@ export declare type ThemeConfig = {
|
|
|
77
92
|
prism: PrismConfig;
|
|
78
93
|
footer?: Footer;
|
|
79
94
|
hideableSidebar: boolean;
|
|
80
|
-
image
|
|
81
|
-
|
|
95
|
+
image?: string;
|
|
96
|
+
metadata: Array<Record<string, string>>;
|
|
82
97
|
sidebarCollapsible: boolean;
|
|
98
|
+
tableOfContents: TableOfContents;
|
|
83
99
|
};
|
|
100
|
+
export declare type UserThemeConfig = DeepPartial<ThemeConfig>;
|
|
84
101
|
export declare function useThemeConfig(): ThemeConfig;
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/theme-common",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.13",
|
|
4
4
|
"description": "Common code for Docusaurus themes.",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"build": "tsc",
|
|
9
|
-
"watch": "tsc --watch"
|
|
8
|
+
"build": "node copyUntypedFiles.js && tsc",
|
|
9
|
+
"watch": "node copyUntypedFiles.js && tsc --watch"
|
|
10
10
|
},
|
|
11
11
|
"publishConfig": {
|
|
12
12
|
"access": "public"
|
|
@@ -18,15 +18,21 @@
|
|
|
18
18
|
},
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@docusaurus/
|
|
22
|
-
"@docusaurus/plugin-content-
|
|
23
|
-
"@docusaurus/plugin-content-
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
21
|
+
"@docusaurus/plugin-content-blog": "2.0.0-beta.13",
|
|
22
|
+
"@docusaurus/plugin-content-docs": "2.0.0-beta.13",
|
|
23
|
+
"@docusaurus/plugin-content-pages": "2.0.0-beta.13",
|
|
24
|
+
"clsx": "^1.1.1",
|
|
25
|
+
"fs-extra": "^10.0.0",
|
|
26
|
+
"parse-numeric-range": "^1.3.0",
|
|
27
|
+
"tslib": "^2.3.1",
|
|
28
|
+
"utility-types": "^3.10.0"
|
|
27
29
|
},
|
|
28
30
|
"devDependencies": {
|
|
29
|
-
"@docusaurus/
|
|
31
|
+
"@docusaurus/core": "2.0.0-beta.13",
|
|
32
|
+
"@docusaurus/module-type-aliases": "2.0.0-beta.13",
|
|
33
|
+
"@docusaurus/types": "2.0.0-beta.13",
|
|
34
|
+
"@testing-library/react-hooks": "^7.0.2",
|
|
35
|
+
"lodash": "^4.17.20"
|
|
30
36
|
},
|
|
31
37
|
"peerDependencies": {
|
|
32
38
|
"prism-react-renderer": "^1.2.1",
|
|
@@ -34,7 +40,7 @@
|
|
|
34
40
|
"react-dom": "^16.8.4 || ^17.0.0"
|
|
35
41
|
},
|
|
36
42
|
"engines": {
|
|
37
|
-
"node": ">=
|
|
43
|
+
"node": ">=14"
|
|
38
44
|
},
|
|
39
|
-
"gitHead": "
|
|
45
|
+
"gitHead": "b6ca12aedf10a10c2375f4f8b7e7380cfd7c7202"
|
|
40
46
|
}
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
+
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
|
|
8
9
|
import React, {
|
|
9
10
|
useState,
|
|
10
11
|
useEffect,
|
|
@@ -14,6 +15,7 @@ import React, {
|
|
|
14
15
|
Dispatch,
|
|
15
16
|
SetStateAction,
|
|
16
17
|
ReactNode,
|
|
18
|
+
useLayoutEffect,
|
|
17
19
|
} from 'react';
|
|
18
20
|
|
|
19
21
|
const DefaultAnimationEasing = 'ease-in-out';
|
|
@@ -118,7 +120,7 @@ function useCollapseAnimation({
|
|
|
118
120
|
|
|
119
121
|
el.style.willChange = 'height';
|
|
120
122
|
|
|
121
|
-
function startAnimation()
|
|
123
|
+
function startAnimation() {
|
|
122
124
|
const animationFrame = requestAnimationFrame(() => {
|
|
123
125
|
// When collapsing
|
|
124
126
|
if (collapsed) {
|
|
@@ -146,52 +148,95 @@ function useCollapseAnimation({
|
|
|
146
148
|
}
|
|
147
149
|
|
|
148
150
|
type CollapsibleElementType = React.ElementType<
|
|
149
|
-
Pick<React.HTMLAttributes<unknown>, 'className' | 'onTransitionEnd'>
|
|
151
|
+
Pick<React.HTMLAttributes<unknown>, 'className' | 'onTransitionEnd' | 'style'>
|
|
150
152
|
>;
|
|
151
153
|
|
|
152
|
-
|
|
154
|
+
// Prevent hydration layout shift before anims are handled imperatively with JS
|
|
155
|
+
function getSSRStyle(collapsed: boolean) {
|
|
156
|
+
if (ExecutionEnvironment.canUseDOM) {
|
|
157
|
+
return undefined;
|
|
158
|
+
}
|
|
159
|
+
return collapsed ? CollapsedStyles : ExpandedStyles;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
type CollapsibleBaseProps = {
|
|
163
|
+
as?: CollapsibleElementType;
|
|
164
|
+
collapsed: boolean;
|
|
165
|
+
children: ReactNode;
|
|
166
|
+
animation?: CollapsibleAnimationConfig;
|
|
167
|
+
onCollapseTransitionEnd?: (collapsed: boolean) => void;
|
|
168
|
+
className?: string;
|
|
169
|
+
|
|
170
|
+
// This is mostly useful for details/summary component where ssrStyle is not needed (as details are hidden natively)
|
|
171
|
+
// and can mess-up with the default native behavior of the browser when JS fails to load or is disabled
|
|
172
|
+
disableSSRStyle?: boolean;
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
function CollapsibleBase({
|
|
153
176
|
as: As = 'div',
|
|
154
177
|
collapsed,
|
|
155
178
|
children,
|
|
156
179
|
animation,
|
|
180
|
+
onCollapseTransitionEnd,
|
|
157
181
|
className,
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
collapsed: boolean;
|
|
161
|
-
children: ReactNode;
|
|
162
|
-
animation?: CollapsibleAnimationConfig;
|
|
163
|
-
className?: string;
|
|
164
|
-
}) {
|
|
182
|
+
disableSSRStyle,
|
|
183
|
+
}: CollapsibleBaseProps) {
|
|
165
184
|
// any because TS is a pain for HTML element refs, see https://twitter.com/sebastienlorber/status/1412784677795110914
|
|
185
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
166
186
|
const collapsibleRef = useRef<any>(null);
|
|
167
187
|
|
|
168
188
|
useCollapseAnimation({collapsibleRef, collapsed, animation});
|
|
169
189
|
|
|
170
190
|
return (
|
|
171
191
|
<As
|
|
172
|
-
// @ts-expect-error:
|
|
192
|
+
// @ts-expect-error: the "too complicated type" is produced from "CollapsibleElementType" being a huge union
|
|
173
193
|
ref={collapsibleRef}
|
|
174
|
-
|
|
194
|
+
style={disableSSRStyle ? undefined : getSSRStyle(collapsed)}
|
|
195
|
+
onTransitionEnd={(e: React.TransitionEvent) => {
|
|
175
196
|
if (e.propertyName !== 'height') {
|
|
176
197
|
return;
|
|
177
198
|
}
|
|
178
199
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
if (
|
|
183
|
-
!collapsed &&
|
|
184
|
-
parseInt(currentCollapsibleElementHeight, 10) === el.scrollHeight
|
|
185
|
-
) {
|
|
186
|
-
applyCollapsedStyle(el, false);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
if (currentCollapsibleElementHeight === CollapsedStyles.height) {
|
|
190
|
-
applyCollapsedStyle(el, true);
|
|
191
|
-
}
|
|
200
|
+
applyCollapsedStyle(collapsibleRef.current!, collapsed);
|
|
201
|
+
onCollapseTransitionEnd?.(collapsed);
|
|
192
202
|
}}
|
|
193
203
|
className={className}>
|
|
194
204
|
{children}
|
|
195
205
|
</As>
|
|
196
206
|
);
|
|
197
207
|
}
|
|
208
|
+
|
|
209
|
+
function CollapsibleLazy({collapsed, ...props}: CollapsibleBaseProps) {
|
|
210
|
+
const [mounted, setMounted] = useState(!collapsed);
|
|
211
|
+
|
|
212
|
+
useLayoutEffect(() => {
|
|
213
|
+
if (!collapsed) {
|
|
214
|
+
setMounted(true);
|
|
215
|
+
}
|
|
216
|
+
}, [collapsed]);
|
|
217
|
+
|
|
218
|
+
// lazyCollapsed updated in effect so that the first expansion transition can work
|
|
219
|
+
const [lazyCollapsed, setLazyCollapsed] = useState(collapsed);
|
|
220
|
+
useLayoutEffect(() => {
|
|
221
|
+
if (mounted) {
|
|
222
|
+
setLazyCollapsed(collapsed);
|
|
223
|
+
}
|
|
224
|
+
}, [mounted, collapsed]);
|
|
225
|
+
|
|
226
|
+
return mounted ? (
|
|
227
|
+
<CollapsibleBase {...props} collapsed={lazyCollapsed} />
|
|
228
|
+
) : null;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
type CollapsibleProps = CollapsibleBaseProps & {
|
|
232
|
+
// Lazy allows to delay the rendering when collapsed => it will render children only after hydration, on first expansion
|
|
233
|
+
// Required prop: it forces to think if content should be server-rendered or not!
|
|
234
|
+
// This has perf impact on the SSR output and html file sizes
|
|
235
|
+
// See https://github.com/facebook/docusaurus/issues/4753
|
|
236
|
+
lazy: boolean;
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
export function Collapsible({lazy, ...props}: CollapsibleProps): JSX.Element {
|
|
240
|
+
const Comp = lazy ? CollapsibleLazy : CollapsibleBase;
|
|
241
|
+
return <Comp {...props} />;
|
|
242
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
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, {ComponentProps, ReactElement, useRef, useState} from 'react';
|
|
9
|
+
import useIsBrowser from '@docusaurus/useIsBrowser';
|
|
10
|
+
import clsx from 'clsx';
|
|
11
|
+
import {useCollapsible, Collapsible} from '../Collapsible';
|
|
12
|
+
import styles from './styles.module.css';
|
|
13
|
+
|
|
14
|
+
function isInSummary(node: HTMLElement | null): boolean {
|
|
15
|
+
if (!node) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
return node.tagName === 'SUMMARY' || isInSummary(node.parentElement);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function hasParent(node: HTMLElement | null, parent: HTMLElement): boolean {
|
|
22
|
+
if (!node) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
return node === parent || hasParent(node.parentElement, parent);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type DetailsProps = {
|
|
29
|
+
summary?: ReactElement;
|
|
30
|
+
} & ComponentProps<'details'>;
|
|
31
|
+
|
|
32
|
+
function Details({summary, children, ...props}: DetailsProps): JSX.Element {
|
|
33
|
+
const isBrowser = useIsBrowser();
|
|
34
|
+
const detailsRef = useRef<HTMLDetailsElement>(null);
|
|
35
|
+
|
|
36
|
+
const {collapsed, setCollapsed} = useCollapsible({
|
|
37
|
+
initialState: !props.open,
|
|
38
|
+
});
|
|
39
|
+
// We use a separate prop because it must be set only after animation completes
|
|
40
|
+
// Otherwise close anim won't work
|
|
41
|
+
const [open, setOpen] = useState(props.open);
|
|
42
|
+
|
|
43
|
+
return (
|
|
44
|
+
<details
|
|
45
|
+
{...props}
|
|
46
|
+
ref={detailsRef}
|
|
47
|
+
open={open}
|
|
48
|
+
data-collapsed={collapsed}
|
|
49
|
+
className={clsx(
|
|
50
|
+
styles.details,
|
|
51
|
+
{[styles.isBrowser]: isBrowser},
|
|
52
|
+
props.className,
|
|
53
|
+
)}
|
|
54
|
+
onMouseDown={(e) => {
|
|
55
|
+
const target = e.target as HTMLElement;
|
|
56
|
+
// Prevent a double-click to highlight summary text
|
|
57
|
+
if (isInSummary(target) && e.detail > 1) {
|
|
58
|
+
e.preventDefault();
|
|
59
|
+
}
|
|
60
|
+
}}
|
|
61
|
+
onClick={(e) => {
|
|
62
|
+
e.stopPropagation(); // For isolation of multiple nested details/summary
|
|
63
|
+
const target = e.target as HTMLElement;
|
|
64
|
+
const shouldToggle =
|
|
65
|
+
isInSummary(target) && hasParent(target, detailsRef.current!);
|
|
66
|
+
if (!shouldToggle) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
e.preventDefault();
|
|
70
|
+
if (collapsed) {
|
|
71
|
+
setCollapsed(false);
|
|
72
|
+
setOpen(true);
|
|
73
|
+
} else {
|
|
74
|
+
setCollapsed(true);
|
|
75
|
+
// setOpen(false); // Don't do this, it breaks close animation!
|
|
76
|
+
}
|
|
77
|
+
}}>
|
|
78
|
+
{summary}
|
|
79
|
+
|
|
80
|
+
<Collapsible
|
|
81
|
+
lazy={false} // Content might matter for SEO in this case
|
|
82
|
+
collapsed={collapsed}
|
|
83
|
+
disableSSRStyle // Allows component to work fine even with JS disabled!
|
|
84
|
+
onCollapseTransitionEnd={(newCollapsed) => {
|
|
85
|
+
setCollapsed(newCollapsed);
|
|
86
|
+
setOpen(!newCollapsed);
|
|
87
|
+
}}>
|
|
88
|
+
<div className={styles.collapsibleContent}>{children}</div>
|
|
89
|
+
</Collapsible>
|
|
90
|
+
</details>
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export default Details;
|