@docusaurus/theme-common 2.0.0-beta.8bda3b2db → 2.0.0-beta.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/copyUntypedFiles.js +20 -0
- package/lib/.tsbuildinfo +1 -1
- package/lib/components/Collapsible/index.d.ts +35 -0
- package/lib/components/Collapsible/index.js +139 -0
- 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 -1
- package/lib/index.js +13 -0
- package/lib/utils/ThemeClassNames.d.ts +36 -12
- package/lib/utils/ThemeClassNames.js +36 -3
- 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/docsPreferredVersion/useDocsPreferredVersion.d.ts +5 -3
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +1 -2
- package/lib/utils/generalUtils.js +2 -2
- package/lib/utils/historyUtils.d.ts +11 -0
- package/lib/utils/historyUtils.js +42 -0
- package/lib/utils/jsUtils.d.ts +13 -0
- package/lib/utils/jsUtils.js +16 -0
- package/lib/utils/mobileSecondaryMenu.d.ts +20 -0
- package/lib/utils/mobileSecondaryMenu.js +51 -0
- 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 +137 -0
- 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 +36 -0
- package/lib/utils/useLocalPathname.d.ts +7 -0
- package/lib/utils/useLocalPathname.js +16 -0
- package/lib/utils/useLocationChange.js +5 -9
- 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 +14 -2
- package/package.json +16 -12
- package/src/components/Collapsible/index.tsx +242 -0
- package/src/components/Details/index.tsx +94 -0
- package/src/components/Details/styles.module.css +58 -0
- package/src/index.ts +50 -0
- package/src/types.d.ts +0 -2
- package/src/utils/ThemeClassNames.ts +42 -4
- package/src/utils/__tests__/tagUtils.test.ts +66 -0
- package/src/utils/__tests__/tocUtils.test.ts +197 -0
- package/src/utils/announcementBarUtils.tsx +119 -0
- package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +4 -4
- package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +13 -14
- package/src/utils/generalUtils.ts +2 -2
- package/src/utils/historyUtils.ts +50 -0
- package/src/utils/jsUtils.ts +23 -0
- package/src/utils/mobileSecondaryMenu.tsx +116 -0
- package/src/utils/reactUtils.tsx +34 -0
- package/src/utils/regexpUtils.ts +23 -0
- package/src/utils/scrollUtils.tsx +238 -0
- package/src/utils/storageUtils.ts +1 -1
- package/src/utils/tagsUtils.ts +48 -0
- package/src/utils/tocUtils.ts +54 -0
- package/src/utils/useAlternatePageUtils.ts +9 -1
- package/src/utils/useLocalPathname.ts +20 -0
- package/src/utils/useLocationChange.ts +6 -10
- package/src/utils/usePluralForm.ts +3 -1
- package/src/utils/usePrevious.ts +3 -2
- package/src/utils/useTOCHighlight.ts +179 -0
- package/src/utils/useThemeConfig.ts +18 -2
|
@@ -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
|
+
/**
|
|
8
|
+
* Utility to convert an optional string into a Regex case sensitive and global
|
|
9
|
+
*/
|
|
10
|
+
export function isRegexpStringMatch(regexAsString, valueToTest) {
|
|
11
|
+
if (typeof regexAsString === 'undefined' ||
|
|
12
|
+
typeof valueToTest === 'undefined') {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
return new RegExp(regexAsString, 'gi').test(valueToTest);
|
|
16
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
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, { ReactNode } from 'react';
|
|
8
|
+
/**
|
|
9
|
+
* We need a way to update the scroll position while ignoring scroll events
|
|
10
|
+
* without affecting Navbar/BackToTop visibility
|
|
11
|
+
*
|
|
12
|
+
* This API permits to temporarily disable/ignore scroll events
|
|
13
|
+
* Motivated by https://github.com/facebook/docusaurus/pull/5618
|
|
14
|
+
*/
|
|
15
|
+
declare type ScrollController = {
|
|
16
|
+
/**
|
|
17
|
+
* A boolean ref tracking whether scroll events are enabled
|
|
18
|
+
*/
|
|
19
|
+
scrollEventsEnabledRef: React.MutableRefObject<boolean>;
|
|
20
|
+
/**
|
|
21
|
+
* Enables scroll events in `useScrollPosition`
|
|
22
|
+
*/
|
|
23
|
+
enableScrollEvents: () => void;
|
|
24
|
+
/**
|
|
25
|
+
* Disables scroll events in `useScrollPosition`
|
|
26
|
+
*/
|
|
27
|
+
disableScrollEvents: () => void;
|
|
28
|
+
};
|
|
29
|
+
export declare function ScrollControllerProvider({ children, }: {
|
|
30
|
+
children: ReactNode;
|
|
31
|
+
}): JSX.Element;
|
|
32
|
+
export declare function useScrollController(): ScrollController;
|
|
33
|
+
declare type ScrollPosition = {
|
|
34
|
+
scrollX: number;
|
|
35
|
+
scrollY: number;
|
|
36
|
+
};
|
|
37
|
+
export declare function useScrollPosition(effect: (position: ScrollPosition, lastPosition: ScrollPosition | null) => void, deps?: unknown[]): void;
|
|
38
|
+
declare type UseScrollPositionBlockerReturn = {
|
|
39
|
+
blockElementScrollPositionUntilNextRender: (el: HTMLElement) => void;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* This hook permits to "block" the scroll position of a dom element
|
|
43
|
+
* The idea is that we should be able to update DOM content above this element
|
|
44
|
+
* but the screen position of this element should not change
|
|
45
|
+
*
|
|
46
|
+
* Feature motivated by the Tabs groups:
|
|
47
|
+
* clicking on a tab may affect tabs of the same group upper in the tree
|
|
48
|
+
* Yet to avoid a bad UX, the clicked tab must remain under the user mouse!
|
|
49
|
+
* See GIF here: https://github.com/facebook/docusaurus/pull/5618
|
|
50
|
+
*/
|
|
51
|
+
export declare function useScrollPositionBlocker(): UseScrollPositionBlockerReturn;
|
|
52
|
+
export {};
|
|
@@ -0,0 +1,137 @@
|
|
|
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, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, } from 'react';
|
|
8
|
+
import { useDynamicCallback } from './reactUtils';
|
|
9
|
+
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
|
|
10
|
+
function useScrollControllerContextValue() {
|
|
11
|
+
const scrollEventsEnabledRef = useRef(true);
|
|
12
|
+
return useMemo(() => ({
|
|
13
|
+
scrollEventsEnabledRef,
|
|
14
|
+
enableScrollEvents: () => {
|
|
15
|
+
scrollEventsEnabledRef.current = true;
|
|
16
|
+
},
|
|
17
|
+
disableScrollEvents: () => {
|
|
18
|
+
scrollEventsEnabledRef.current = false;
|
|
19
|
+
},
|
|
20
|
+
}), []);
|
|
21
|
+
}
|
|
22
|
+
const ScrollMonitorContext = createContext(undefined);
|
|
23
|
+
export function ScrollControllerProvider({ children, }) {
|
|
24
|
+
return (React.createElement(ScrollMonitorContext.Provider, { value: useScrollControllerContextValue() }, children));
|
|
25
|
+
}
|
|
26
|
+
export function useScrollController() {
|
|
27
|
+
const context = useContext(ScrollMonitorContext);
|
|
28
|
+
if (context == null) {
|
|
29
|
+
throw new Error('"useScrollController" is used but no context provider was found in the React tree.');
|
|
30
|
+
}
|
|
31
|
+
return context;
|
|
32
|
+
}
|
|
33
|
+
const getScrollPosition = () => {
|
|
34
|
+
return ExecutionEnvironment.canUseDOM
|
|
35
|
+
? {
|
|
36
|
+
scrollX: window.pageXOffset,
|
|
37
|
+
scrollY: window.pageYOffset,
|
|
38
|
+
}
|
|
39
|
+
: null;
|
|
40
|
+
};
|
|
41
|
+
export function useScrollPosition(effect, deps = []) {
|
|
42
|
+
const { scrollEventsEnabledRef } = useScrollController();
|
|
43
|
+
const lastPositionRef = useRef(getScrollPosition());
|
|
44
|
+
const dynamicEffect = useDynamicCallback(effect);
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
const handleScroll = () => {
|
|
47
|
+
if (!scrollEventsEnabledRef.current) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const currentPosition = getScrollPosition();
|
|
51
|
+
if (dynamicEffect) {
|
|
52
|
+
dynamicEffect(currentPosition, lastPositionRef.current);
|
|
53
|
+
}
|
|
54
|
+
lastPositionRef.current = currentPosition;
|
|
55
|
+
};
|
|
56
|
+
const opts = {
|
|
57
|
+
passive: true,
|
|
58
|
+
};
|
|
59
|
+
handleScroll();
|
|
60
|
+
window.addEventListener('scroll', handleScroll, opts);
|
|
61
|
+
return () => window.removeEventListener('scroll', handleScroll, opts);
|
|
62
|
+
}, [
|
|
63
|
+
dynamicEffect,
|
|
64
|
+
scrollEventsEnabledRef,
|
|
65
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
66
|
+
...deps,
|
|
67
|
+
]);
|
|
68
|
+
}
|
|
69
|
+
function useScrollPositionSaver() {
|
|
70
|
+
const lastElementRef = useRef({
|
|
71
|
+
elem: null,
|
|
72
|
+
top: 0,
|
|
73
|
+
});
|
|
74
|
+
const save = useCallback((elem) => {
|
|
75
|
+
lastElementRef.current = {
|
|
76
|
+
elem,
|
|
77
|
+
top: elem.getBoundingClientRect().top,
|
|
78
|
+
};
|
|
79
|
+
}, []);
|
|
80
|
+
const restore = useCallback(() => {
|
|
81
|
+
const { current: { elem, top }, } = lastElementRef;
|
|
82
|
+
if (!elem) {
|
|
83
|
+
return { restored: false };
|
|
84
|
+
}
|
|
85
|
+
const newTop = elem.getBoundingClientRect().top;
|
|
86
|
+
const heightDiff = newTop - top;
|
|
87
|
+
if (heightDiff) {
|
|
88
|
+
window.scrollBy({ left: 0, top: heightDiff });
|
|
89
|
+
}
|
|
90
|
+
lastElementRef.current = { elem: null, top: 0 };
|
|
91
|
+
return { restored: heightDiff !== 0 };
|
|
92
|
+
}, []);
|
|
93
|
+
return useMemo(() => ({ save, restore }), [restore, save]);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* This hook permits to "block" the scroll position of a dom element
|
|
97
|
+
* The idea is that we should be able to update DOM content above this element
|
|
98
|
+
* but the screen position of this element should not change
|
|
99
|
+
*
|
|
100
|
+
* Feature motivated by the Tabs groups:
|
|
101
|
+
* clicking on a tab may affect tabs of the same group upper in the tree
|
|
102
|
+
* Yet to avoid a bad UX, the clicked tab must remain under the user mouse!
|
|
103
|
+
* See GIF here: https://github.com/facebook/docusaurus/pull/5618
|
|
104
|
+
*/
|
|
105
|
+
export function useScrollPositionBlocker() {
|
|
106
|
+
const scrollController = useScrollController();
|
|
107
|
+
const scrollPositionSaver = useScrollPositionSaver();
|
|
108
|
+
const nextLayoutEffectCallbackRef = useRef(undefined);
|
|
109
|
+
const blockElementScrollPositionUntilNextRender = useCallback((el) => {
|
|
110
|
+
scrollPositionSaver.save(el);
|
|
111
|
+
scrollController.disableScrollEvents();
|
|
112
|
+
nextLayoutEffectCallbackRef.current = () => {
|
|
113
|
+
const { restored } = scrollPositionSaver.restore();
|
|
114
|
+
nextLayoutEffectCallbackRef.current = undefined;
|
|
115
|
+
// Restoring the former scroll position will trigger a scroll event
|
|
116
|
+
// We need to wait for next scroll event to happen
|
|
117
|
+
// before enabling again the scrollController events
|
|
118
|
+
if (restored) {
|
|
119
|
+
const handleScrollRestoreEvent = () => {
|
|
120
|
+
scrollController.enableScrollEvents();
|
|
121
|
+
window.removeEventListener('scroll', handleScrollRestoreEvent);
|
|
122
|
+
};
|
|
123
|
+
window.addEventListener('scroll', handleScrollRestoreEvent);
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
scrollController.enableScrollEvents();
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}, [scrollController, scrollPositionSaver]);
|
|
130
|
+
useLayoutEffect(() => {
|
|
131
|
+
var _a;
|
|
132
|
+
(_a = nextLayoutEffectCallbackRef.current) === null || _a === void 0 ? void 0 : _a.call(nextLayoutEffectCallbackRef);
|
|
133
|
+
});
|
|
134
|
+
return {
|
|
135
|
+
blockElementScrollPositionUntilNextRender,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
@@ -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
|
+
export declare const translateTagsPageTitle: () => string;
|
|
8
|
+
declare type TagsListItem = Readonly<{
|
|
9
|
+
name: string;
|
|
10
|
+
permalink: string;
|
|
11
|
+
count: number;
|
|
12
|
+
}>;
|
|
13
|
+
export declare type TagLetterEntry = Readonly<{
|
|
14
|
+
letter: string;
|
|
15
|
+
tags: TagsListItem[];
|
|
16
|
+
}>;
|
|
17
|
+
export declare function listTagsByLetters(tags: readonly TagsListItem[]): TagLetterEntry[];
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
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 { translate } from '@docusaurus/Translate';
|
|
8
|
+
export const translateTagsPageTitle = () => translate({
|
|
9
|
+
id: 'theme.tags.tagsPageTitle',
|
|
10
|
+
message: 'Tags',
|
|
11
|
+
description: 'The title of the tag list page',
|
|
12
|
+
});
|
|
13
|
+
function getTagLetter(tag) {
|
|
14
|
+
return tag[0].toUpperCase();
|
|
15
|
+
}
|
|
16
|
+
export function listTagsByLetters(tags) {
|
|
17
|
+
// Group by letters
|
|
18
|
+
const groups = {};
|
|
19
|
+
Object.values(tags).forEach((tag) => {
|
|
20
|
+
var _a;
|
|
21
|
+
const letter = getTagLetter(tag.name);
|
|
22
|
+
groups[letter] = (_a = groups[letter]) !== null && _a !== void 0 ? _a : [];
|
|
23
|
+
groups[letter].push(tag);
|
|
24
|
+
});
|
|
25
|
+
return (Object.entries(groups)
|
|
26
|
+
// Sort letters
|
|
27
|
+
.sort(([letter1], [letter2]) => letter1.localeCompare(letter2))
|
|
28
|
+
.map(([letter, letterTags]) => {
|
|
29
|
+
// Sort tags inside a letter
|
|
30
|
+
const sortedTags = letterTags.sort((tag1, tag2) => tag1.name.localeCompare(tag2.name));
|
|
31
|
+
return { letter, tags: sortedTags };
|
|
32
|
+
}));
|
|
33
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
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 { TOCItem } from '@docusaurus/types';
|
|
8
|
+
declare type FilterTOCParam = {
|
|
9
|
+
toc: readonly TOCItem[];
|
|
10
|
+
minHeadingLevel: number;
|
|
11
|
+
maxHeadingLevel: number;
|
|
12
|
+
};
|
|
13
|
+
export declare function filterTOC({ toc, minHeadingLevel, maxHeadingLevel, }: FilterTOCParam): TOCItem[];
|
|
14
|
+
export declare function useTOCFilter({ toc, minHeadingLevel, maxHeadingLevel, }: FilterTOCParam): readonly TOCItem[];
|
|
15
|
+
export {};
|
|
@@ -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 { 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(() => {
|
|
34
|
+
return filterTOC({ toc, minHeadingLevel, maxHeadingLevel });
|
|
35
|
+
}, [toc, minHeadingLevel, maxHeadingLevel]);
|
|
36
|
+
}
|
|
@@ -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,18 @@
|
|
|
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
|
-
if (isFirst.current) {
|
|
17
|
-
isFirst.current = false;
|
|
18
|
-
return;
|
|
19
|
-
}
|
|
20
|
-
onLocationChange({
|
|
16
|
+
onLocationChangeDynamic({
|
|
21
17
|
location,
|
|
22
18
|
previousLocation,
|
|
23
19
|
});
|
|
24
|
-
}, [location]);
|
|
20
|
+
}, [onLocationChangeDynamic, location, previousLocation]);
|
|
25
21
|
}
|
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,18 @@
|
|
|
1
1
|
import { PrismTheme } from 'prism-react-renderer';
|
|
2
2
|
import { CSSProperties } from 'react';
|
|
3
|
+
import { DeepPartial } from 'utility-types';
|
|
3
4
|
export declare type DocsVersionPersistence = 'localStorage' | 'none';
|
|
4
5
|
export declare type NavbarItem = {
|
|
5
6
|
type?: string | undefined;
|
|
6
7
|
items?: NavbarItem[];
|
|
7
8
|
label?: string;
|
|
8
9
|
position?: 'left' | 'right';
|
|
9
|
-
}
|
|
10
|
+
} & Record<string, unknown>;
|
|
10
11
|
export declare type NavbarLogo = {
|
|
11
12
|
src: string;
|
|
12
13
|
srcDark?: string;
|
|
14
|
+
width?: string | number;
|
|
15
|
+
height?: string | number;
|
|
13
16
|
href?: string;
|
|
14
17
|
target?: string;
|
|
15
18
|
alt?: string;
|
|
@@ -62,11 +65,17 @@ export declare type Footer = {
|
|
|
62
65
|
alt?: string;
|
|
63
66
|
src?: string;
|
|
64
67
|
srcDark?: string;
|
|
68
|
+
width?: string | number;
|
|
69
|
+
height?: string | number;
|
|
65
70
|
href?: string;
|
|
66
71
|
};
|
|
67
72
|
copyright?: string;
|
|
68
73
|
links: FooterLinks[];
|
|
69
74
|
};
|
|
75
|
+
export declare type TableOfContents = {
|
|
76
|
+
minHeadingLevel: number;
|
|
77
|
+
maxHeadingLevel: number;
|
|
78
|
+
};
|
|
70
79
|
export declare type ThemeConfig = {
|
|
71
80
|
docs: {
|
|
72
81
|
versionPersistence: DocsVersionPersistence;
|
|
@@ -77,7 +86,10 @@ export declare type ThemeConfig = {
|
|
|
77
86
|
prism: PrismConfig;
|
|
78
87
|
footer?: Footer;
|
|
79
88
|
hideableSidebar: boolean;
|
|
80
|
-
image
|
|
89
|
+
image?: string;
|
|
81
90
|
metadatas: Array<Record<string, string>>;
|
|
91
|
+
sidebarCollapsible: boolean;
|
|
92
|
+
tableOfContents: TableOfContents;
|
|
82
93
|
};
|
|
94
|
+
export declare type UserThemeConfig = DeepPartial<ThemeConfig>;
|
|
83
95
|
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.9",
|
|
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,19 @@
|
|
|
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.
|
|
26
|
-
"
|
|
21
|
+
"@docusaurus/core": "2.0.0-beta.9",
|
|
22
|
+
"@docusaurus/plugin-content-blog": "2.0.0-beta.9",
|
|
23
|
+
"@docusaurus/plugin-content-docs": "2.0.0-beta.9",
|
|
24
|
+
"@docusaurus/plugin-content-pages": "2.0.0-beta.9",
|
|
25
|
+
"@docusaurus/types": "2.0.0-beta.9",
|
|
26
|
+
"clsx": "^1.1.1",
|
|
27
|
+
"fs-extra": "^10.0.0",
|
|
28
|
+
"tslib": "^2.3.1",
|
|
29
|
+
"utility-types": "^3.10.0"
|
|
27
30
|
},
|
|
28
31
|
"devDependencies": {
|
|
29
|
-
"@docusaurus/module-type-aliases": "2.0.0-beta.
|
|
32
|
+
"@docusaurus/module-type-aliases": "2.0.0-beta.9",
|
|
33
|
+
"lodash": "^4.17.20"
|
|
30
34
|
},
|
|
31
35
|
"peerDependencies": {
|
|
32
36
|
"prism-react-renderer": "^1.2.1",
|
|
@@ -34,7 +38,7 @@
|
|
|
34
38
|
"react-dom": "^16.8.4 || ^17.0.0"
|
|
35
39
|
},
|
|
36
40
|
"engines": {
|
|
37
|
-
"node": ">=
|
|
41
|
+
"node": ">=14"
|
|
38
42
|
},
|
|
39
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "8a491fc29ad002f90e97f5b5fe4178ac8fa0c4d7"
|
|
40
44
|
}
|