@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,238 @@
|
|
|
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
|
+
createContext,
|
|
10
|
+
ReactNode,
|
|
11
|
+
useCallback,
|
|
12
|
+
useContext,
|
|
13
|
+
useEffect,
|
|
14
|
+
useLayoutEffect,
|
|
15
|
+
useMemo,
|
|
16
|
+
useRef,
|
|
17
|
+
} from 'react';
|
|
18
|
+
import {useDynamicCallback} from './reactUtils';
|
|
19
|
+
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* We need a way to update the scroll position while ignoring scroll events
|
|
23
|
+
* without affecting Navbar/BackToTop visibility
|
|
24
|
+
*
|
|
25
|
+
* This API permits to temporarily disable/ignore scroll events
|
|
26
|
+
* Motivated by https://github.com/facebook/docusaurus/pull/5618
|
|
27
|
+
*/
|
|
28
|
+
type ScrollController = {
|
|
29
|
+
/**
|
|
30
|
+
* A boolean ref tracking whether scroll events are enabled
|
|
31
|
+
*/
|
|
32
|
+
scrollEventsEnabledRef: React.MutableRefObject<boolean>;
|
|
33
|
+
/**
|
|
34
|
+
* Enables scroll events in `useScrollPosition`
|
|
35
|
+
*/
|
|
36
|
+
enableScrollEvents: () => void;
|
|
37
|
+
/**
|
|
38
|
+
* Disables scroll events in `useScrollPosition`
|
|
39
|
+
*/
|
|
40
|
+
disableScrollEvents: () => void;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
function useScrollControllerContextValue(): ScrollController {
|
|
44
|
+
const scrollEventsEnabledRef = useRef(true);
|
|
45
|
+
|
|
46
|
+
return useMemo(
|
|
47
|
+
() => ({
|
|
48
|
+
scrollEventsEnabledRef,
|
|
49
|
+
enableScrollEvents: () => {
|
|
50
|
+
scrollEventsEnabledRef.current = true;
|
|
51
|
+
},
|
|
52
|
+
disableScrollEvents: () => {
|
|
53
|
+
scrollEventsEnabledRef.current = false;
|
|
54
|
+
},
|
|
55
|
+
}),
|
|
56
|
+
[],
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const ScrollMonitorContext = createContext<ScrollController | undefined>(
|
|
61
|
+
undefined,
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
export function ScrollControllerProvider({
|
|
65
|
+
children,
|
|
66
|
+
}: {
|
|
67
|
+
children: ReactNode;
|
|
68
|
+
}): JSX.Element {
|
|
69
|
+
return (
|
|
70
|
+
<ScrollMonitorContext.Provider value={useScrollControllerContextValue()}>
|
|
71
|
+
{children}
|
|
72
|
+
</ScrollMonitorContext.Provider>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function useScrollController(): ScrollController {
|
|
77
|
+
const context = useContext(ScrollMonitorContext);
|
|
78
|
+
if (context == null) {
|
|
79
|
+
throw new Error(
|
|
80
|
+
'"useScrollController" is used but no context provider was found in the React tree.',
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
return context;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const getScrollPosition = (): ScrollPosition | null => {
|
|
87
|
+
return ExecutionEnvironment.canUseDOM
|
|
88
|
+
? {
|
|
89
|
+
scrollX: window.pageXOffset,
|
|
90
|
+
scrollY: window.pageYOffset,
|
|
91
|
+
}
|
|
92
|
+
: null;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
type ScrollPosition = {scrollX: number; scrollY: number};
|
|
96
|
+
|
|
97
|
+
export function useScrollPosition(
|
|
98
|
+
effect: (
|
|
99
|
+
position: ScrollPosition,
|
|
100
|
+
lastPosition: ScrollPosition | null,
|
|
101
|
+
) => void,
|
|
102
|
+
deps: unknown[] = [],
|
|
103
|
+
): void {
|
|
104
|
+
const {scrollEventsEnabledRef} = useScrollController();
|
|
105
|
+
const lastPositionRef = useRef<ScrollPosition | null>(getScrollPosition());
|
|
106
|
+
|
|
107
|
+
const dynamicEffect = useDynamicCallback(effect);
|
|
108
|
+
|
|
109
|
+
useEffect(() => {
|
|
110
|
+
const handleScroll = () => {
|
|
111
|
+
if (!scrollEventsEnabledRef.current) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const currentPosition = getScrollPosition()!;
|
|
115
|
+
|
|
116
|
+
if (dynamicEffect) {
|
|
117
|
+
dynamicEffect(currentPosition, lastPositionRef.current);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
lastPositionRef.current = currentPosition;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const opts: AddEventListenerOptions & EventListenerOptions = {
|
|
124
|
+
passive: true,
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
handleScroll();
|
|
128
|
+
window.addEventListener('scroll', handleScroll, opts);
|
|
129
|
+
|
|
130
|
+
return () => window.removeEventListener('scroll', handleScroll, opts);
|
|
131
|
+
}, [
|
|
132
|
+
dynamicEffect,
|
|
133
|
+
scrollEventsEnabledRef,
|
|
134
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
135
|
+
...deps,
|
|
136
|
+
]);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
type UseScrollPositionSaver = {
|
|
140
|
+
/**
|
|
141
|
+
* Measure the top of an element, and store the details
|
|
142
|
+
*/
|
|
143
|
+
save: (elem: HTMLElement) => void;
|
|
144
|
+
/**
|
|
145
|
+
* Restore the page position to keep the stored element's position from
|
|
146
|
+
* the top of the viewport, and remove the stored details
|
|
147
|
+
*/
|
|
148
|
+
restore: () => {restored: boolean};
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
function useScrollPositionSaver(): UseScrollPositionSaver {
|
|
152
|
+
const lastElementRef = useRef<{elem: HTMLElement | null; top: number}>({
|
|
153
|
+
elem: null,
|
|
154
|
+
top: 0,
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
const save = useCallback((elem: HTMLElement) => {
|
|
158
|
+
lastElementRef.current = {
|
|
159
|
+
elem,
|
|
160
|
+
top: elem.getBoundingClientRect().top,
|
|
161
|
+
};
|
|
162
|
+
}, []);
|
|
163
|
+
|
|
164
|
+
const restore = useCallback(() => {
|
|
165
|
+
const {
|
|
166
|
+
current: {elem, top},
|
|
167
|
+
} = lastElementRef;
|
|
168
|
+
if (!elem) {
|
|
169
|
+
return {restored: false};
|
|
170
|
+
}
|
|
171
|
+
const newTop = elem.getBoundingClientRect().top;
|
|
172
|
+
const heightDiff = newTop - top;
|
|
173
|
+
if (heightDiff) {
|
|
174
|
+
window.scrollBy({left: 0, top: heightDiff});
|
|
175
|
+
}
|
|
176
|
+
lastElementRef.current = {elem: null, top: 0};
|
|
177
|
+
|
|
178
|
+
return {restored: heightDiff !== 0};
|
|
179
|
+
}, []);
|
|
180
|
+
|
|
181
|
+
return useMemo(() => ({save, restore}), [restore, save]);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
type UseScrollPositionBlockerReturn = {
|
|
185
|
+
blockElementScrollPositionUntilNextRender: (el: HTMLElement) => void;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* This hook permits to "block" the scroll position of a dom element
|
|
190
|
+
* The idea is that we should be able to update DOM content above this element
|
|
191
|
+
* but the screen position of this element should not change
|
|
192
|
+
*
|
|
193
|
+
* Feature motivated by the Tabs groups:
|
|
194
|
+
* clicking on a tab may affect tabs of the same group upper in the tree
|
|
195
|
+
* Yet to avoid a bad UX, the clicked tab must remain under the user mouse!
|
|
196
|
+
* See GIF here: https://github.com/facebook/docusaurus/pull/5618
|
|
197
|
+
*/
|
|
198
|
+
export function useScrollPositionBlocker(): UseScrollPositionBlockerReturn {
|
|
199
|
+
const scrollController = useScrollController();
|
|
200
|
+
const scrollPositionSaver = useScrollPositionSaver();
|
|
201
|
+
|
|
202
|
+
const nextLayoutEffectCallbackRef = useRef<(() => void) | undefined>(
|
|
203
|
+
undefined,
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
const blockElementScrollPositionUntilNextRender = useCallback(
|
|
207
|
+
(el: HTMLElement) => {
|
|
208
|
+
scrollPositionSaver.save(el);
|
|
209
|
+
scrollController.disableScrollEvents();
|
|
210
|
+
nextLayoutEffectCallbackRef.current = () => {
|
|
211
|
+
const {restored} = scrollPositionSaver.restore();
|
|
212
|
+
nextLayoutEffectCallbackRef.current = undefined;
|
|
213
|
+
|
|
214
|
+
// Restoring the former scroll position will trigger a scroll event
|
|
215
|
+
// We need to wait for next scroll event to happen
|
|
216
|
+
// before enabling again the scrollController events
|
|
217
|
+
if (restored) {
|
|
218
|
+
const handleScrollRestoreEvent = () => {
|
|
219
|
+
scrollController.enableScrollEvents();
|
|
220
|
+
window.removeEventListener('scroll', handleScrollRestoreEvent);
|
|
221
|
+
};
|
|
222
|
+
window.addEventListener('scroll', handleScrollRestoreEvent);
|
|
223
|
+
} else {
|
|
224
|
+
scrollController.enableScrollEvents();
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
},
|
|
228
|
+
[scrollController, scrollPositionSaver],
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
useLayoutEffect(() => {
|
|
232
|
+
nextLayoutEffectCallbackRef.current?.();
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
return {
|
|
236
|
+
blockElementScrollPositionUntilNextRender,
|
|
237
|
+
};
|
|
238
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
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 {translate} from '@docusaurus/Translate';
|
|
9
|
+
|
|
10
|
+
export const translateTagsPageTitle = (): string =>
|
|
11
|
+
translate({
|
|
12
|
+
id: 'theme.tags.tagsPageTitle',
|
|
13
|
+
message: 'Tags',
|
|
14
|
+
description: 'The title of the tag list page',
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
type TagsListItem = Readonly<{name: string; permalink: string; count: number}>; // TODO remove duplicated type :s
|
|
18
|
+
|
|
19
|
+
export type TagLetterEntry = Readonly<{letter: string; tags: TagsListItem[]}>;
|
|
20
|
+
|
|
21
|
+
function getTagLetter(tag: string): string {
|
|
22
|
+
return tag[0].toUpperCase();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function listTagsByLetters(
|
|
26
|
+
tags: readonly TagsListItem[],
|
|
27
|
+
): TagLetterEntry[] {
|
|
28
|
+
// Group by letters
|
|
29
|
+
const groups: Record<string, TagsListItem[]> = {};
|
|
30
|
+
Object.values(tags).forEach((tag) => {
|
|
31
|
+
const letter = getTagLetter(tag.name);
|
|
32
|
+
groups[letter] = groups[letter] ?? [];
|
|
33
|
+
groups[letter].push(tag);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
Object.entries(groups)
|
|
38
|
+
// Sort letters
|
|
39
|
+
.sort(([letter1], [letter2]) => letter1.localeCompare(letter2))
|
|
40
|
+
.map(([letter, letterTags]) => {
|
|
41
|
+
// Sort tags inside a letter
|
|
42
|
+
const sortedTags = letterTags.sort((tag1, tag2) =>
|
|
43
|
+
tag1.name.localeCompare(tag2.name),
|
|
44
|
+
);
|
|
45
|
+
return {letter, tags: sortedTags};
|
|
46
|
+
})
|
|
47
|
+
);
|
|
48
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
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 {useMemo} from 'react';
|
|
9
|
+
import {TOCItem} from '@docusaurus/types';
|
|
10
|
+
|
|
11
|
+
type FilterTOCParam = {
|
|
12
|
+
toc: readonly TOCItem[];
|
|
13
|
+
minHeadingLevel: number;
|
|
14
|
+
maxHeadingLevel: number;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export function filterTOC({
|
|
18
|
+
toc,
|
|
19
|
+
minHeadingLevel,
|
|
20
|
+
maxHeadingLevel,
|
|
21
|
+
}: FilterTOCParam): TOCItem[] {
|
|
22
|
+
function isValid(item: TOCItem) {
|
|
23
|
+
return item.level >= minHeadingLevel && item.level <= maxHeadingLevel;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return toc.flatMap((item) => {
|
|
27
|
+
const filteredChildren = filterTOC({
|
|
28
|
+
toc: item.children,
|
|
29
|
+
minHeadingLevel,
|
|
30
|
+
maxHeadingLevel,
|
|
31
|
+
});
|
|
32
|
+
if (isValid(item)) {
|
|
33
|
+
return [
|
|
34
|
+
{
|
|
35
|
+
...item,
|
|
36
|
+
children: filteredChildren,
|
|
37
|
+
},
|
|
38
|
+
];
|
|
39
|
+
} else {
|
|
40
|
+
return filteredChildren;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Memoize potentially expensive filtering logic
|
|
46
|
+
export function useTOCFilter({
|
|
47
|
+
toc,
|
|
48
|
+
minHeadingLevel,
|
|
49
|
+
maxHeadingLevel,
|
|
50
|
+
}: FilterTOCParam): readonly TOCItem[] {
|
|
51
|
+
return useMemo(() => {
|
|
52
|
+
return filterTOC({toc, minHeadingLevel, maxHeadingLevel});
|
|
53
|
+
}, [toc, minHeadingLevel, maxHeadingLevel]);
|
|
54
|
+
}
|
|
@@ -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,20 @@
|
|
|
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 useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
|
9
|
+
import {useLocation} from '@docusaurus/router';
|
|
10
|
+
|
|
11
|
+
// Get the pathname of current route, without the optional site baseUrl
|
|
12
|
+
// - /docs/myDoc => /docs/myDoc
|
|
13
|
+
// - /baseUrl/docs/myDoc => /docs/myDoc
|
|
14
|
+
export function useLocalPathname(): string {
|
|
15
|
+
const {
|
|
16
|
+
siteConfig: {baseUrl},
|
|
17
|
+
} = useDocusaurusContext();
|
|
18
|
+
const {pathname} = useLocation();
|
|
19
|
+
return pathname.replace(baseUrl, '/');
|
|
20
|
+
}
|
|
@@ -5,10 +5,11 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import {useEffect
|
|
8
|
+
import {useEffect} from 'react';
|
|
9
9
|
import {useLocation} from '@docusaurus/router';
|
|
10
10
|
import {Location} from '@docusaurus/history';
|
|
11
11
|
import {usePrevious} from './usePrevious';
|
|
12
|
+
import {useDynamicCallback} from './reactUtils';
|
|
12
13
|
|
|
13
14
|
type LocationChangeEvent = {
|
|
14
15
|
location: Location;
|
|
@@ -20,18 +21,13 @@ type OnLocationChange = (locationChangeEvent: LocationChangeEvent) => void;
|
|
|
20
21
|
export function useLocationChange(onLocationChange: OnLocationChange): void {
|
|
21
22
|
const location = useLocation();
|
|
22
23
|
const previousLocation = usePrevious(location);
|
|
23
|
-
const isFirst = useRef<boolean>(true);
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
// Prevent first effect to trigger the listener on mount
|
|
27
|
-
if (isFirst.current) {
|
|
28
|
-
isFirst.current = false;
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
25
|
+
const onLocationChangeDynamic = useDynamicCallback(onLocationChange);
|
|
31
26
|
|
|
32
|
-
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
onLocationChangeDynamic({
|
|
33
29
|
location,
|
|
34
30
|
previousLocation,
|
|
35
31
|
});
|
|
36
|
-
}, [location]);
|
|
32
|
+
}, [onLocationChangeDynamic, location, previousLocation]);
|
|
37
33
|
}
|
|
@@ -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 => {
|
package/src/utils/usePrevious.ts
CHANGED
|
@@ -5,12 +5,13 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import {useRef
|
|
8
|
+
import {useRef} from 'react';
|
|
9
|
+
import {useIsomorphicLayoutEffect} from './reactUtils';
|
|
9
10
|
|
|
10
11
|
export function usePrevious<T>(value: T): T | undefined {
|
|
11
12
|
const ref = useRef<T>();
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
useIsomorphicLayoutEffect(() => {
|
|
14
15
|
ref.current = value;
|
|
15
16
|
});
|
|
16
17
|
|
|
@@ -0,0 +1,179 @@
|
|
|
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 {useThemeConfig} from './useThemeConfig';
|
|
10
|
+
|
|
11
|
+
/*
|
|
12
|
+
TODO make the hardcoded theme-classic classnames configurable
|
|
13
|
+
(or add them to ThemeClassNames?)
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
// 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
|
|
17
|
+
function getVisibleBoundingClientRect(element: HTMLElement): DOMRect {
|
|
18
|
+
const rect = element.getBoundingClientRect();
|
|
19
|
+
const hasNoHeight = rect.top === rect.bottom;
|
|
20
|
+
if (hasNoHeight) {
|
|
21
|
+
return getVisibleBoundingClientRect(element.parentNode as HTMLElement);
|
|
22
|
+
}
|
|
23
|
+
return rect;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Considering we divide viewport into 2 zones of each 50vh
|
|
27
|
+
// This returns true if an element is in the first zone (ie, appear in viewport, near the top)
|
|
28
|
+
function isInViewportTopHalf(boundingRect: DOMRect) {
|
|
29
|
+
return boundingRect.top > 0 && boundingRect.bottom < window.innerHeight / 2;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function getAnchors({
|
|
33
|
+
minHeadingLevel,
|
|
34
|
+
maxHeadingLevel,
|
|
35
|
+
}: {
|
|
36
|
+
minHeadingLevel: number;
|
|
37
|
+
maxHeadingLevel: number;
|
|
38
|
+
}) {
|
|
39
|
+
const selectors = [];
|
|
40
|
+
for (let i = minHeadingLevel; i <= maxHeadingLevel; i += 1) {
|
|
41
|
+
selectors.push(`h${i}.anchor`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return Array.from(
|
|
45
|
+
document.querySelectorAll(selectors.join()),
|
|
46
|
+
) as HTMLElement[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function getActiveAnchor(
|
|
50
|
+
anchors: HTMLElement[],
|
|
51
|
+
{
|
|
52
|
+
anchorTopOffset,
|
|
53
|
+
}: {
|
|
54
|
+
anchorTopOffset: number;
|
|
55
|
+
},
|
|
56
|
+
): Element | null {
|
|
57
|
+
// Naming is hard
|
|
58
|
+
// The "nextVisibleAnchor" is the first anchor that appear under the viewport top boundary
|
|
59
|
+
// Note: it does not mean this anchor is visible yet, but if user continues scrolling down, it will be the first to become visible
|
|
60
|
+
const nextVisibleAnchor = anchors.find((anchor) => {
|
|
61
|
+
const boundingRect = getVisibleBoundingClientRect(anchor);
|
|
62
|
+
return boundingRect.top >= anchorTopOffset;
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
if (nextVisibleAnchor) {
|
|
66
|
+
const boundingRect = getVisibleBoundingClientRect(nextVisibleAnchor);
|
|
67
|
+
// If anchor is in the top half of the viewport: it is the one we consider "active"
|
|
68
|
+
// (unless it's too close to the top and and soon to be scrolled outside viewport)
|
|
69
|
+
if (isInViewportTopHalf(boundingRect)) {
|
|
70
|
+
return nextVisibleAnchor;
|
|
71
|
+
}
|
|
72
|
+
// If anchor is in the bottom half of the viewport, or under the viewport, we consider the active anchor is the previous one
|
|
73
|
+
// This is because the main text appearing in the user screen mostly belong to the previous anchor
|
|
74
|
+
else {
|
|
75
|
+
// Returns null for the first anchor, see https://github.com/facebook/docusaurus/issues/5318
|
|
76
|
+
return anchors[anchors.indexOf(nextVisibleAnchor) - 1] ?? null;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
// no anchor under viewport top? (ie we are at the bottom of the page)
|
|
80
|
+
// => highlight the last anchor found
|
|
81
|
+
else {
|
|
82
|
+
return anchors[anchors.length - 1];
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function getLinkAnchorValue(link: HTMLAnchorElement): string {
|
|
87
|
+
return decodeURIComponent(link.href.substring(link.href.indexOf('#') + 1));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function getLinks(linkClassName: string) {
|
|
91
|
+
return Array.from(
|
|
92
|
+
document.getElementsByClassName(linkClassName),
|
|
93
|
+
) as HTMLAnchorElement[];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function getNavbarHeight(): number {
|
|
97
|
+
// Not ideal to obtain actual height this way
|
|
98
|
+
// Using TS ! (not ?) because otherwise a bad selector would be un-noticed
|
|
99
|
+
return document.querySelector('.navbar')!.clientHeight;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function useAnchorTopOffsetRef() {
|
|
103
|
+
const anchorTopOffsetRef = useRef<number>(0);
|
|
104
|
+
const {
|
|
105
|
+
navbar: {hideOnScroll},
|
|
106
|
+
} = useThemeConfig();
|
|
107
|
+
|
|
108
|
+
useEffect(() => {
|
|
109
|
+
anchorTopOffsetRef.current = hideOnScroll ? 0 : getNavbarHeight();
|
|
110
|
+
}, [hideOnScroll]);
|
|
111
|
+
|
|
112
|
+
return anchorTopOffsetRef;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export type TOCHighlightConfig = {
|
|
116
|
+
linkClassName: string;
|
|
117
|
+
linkActiveClassName: string;
|
|
118
|
+
minHeadingLevel: number;
|
|
119
|
+
maxHeadingLevel: number;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
function useTOCHighlight(config: TOCHighlightConfig | undefined): void {
|
|
123
|
+
const lastActiveLinkRef = useRef<HTMLAnchorElement | undefined>(undefined);
|
|
124
|
+
|
|
125
|
+
const anchorTopOffsetRef = useAnchorTopOffsetRef();
|
|
126
|
+
|
|
127
|
+
useEffect(() => {
|
|
128
|
+
if (!config) {
|
|
129
|
+
// no-op, highlighting is disabled
|
|
130
|
+
return () => {};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const {
|
|
134
|
+
linkClassName,
|
|
135
|
+
linkActiveClassName,
|
|
136
|
+
minHeadingLevel,
|
|
137
|
+
maxHeadingLevel,
|
|
138
|
+
} = config;
|
|
139
|
+
|
|
140
|
+
function updateLinkActiveClass(link: HTMLAnchorElement, active: boolean) {
|
|
141
|
+
if (active) {
|
|
142
|
+
if (lastActiveLinkRef.current && lastActiveLinkRef.current !== link) {
|
|
143
|
+
lastActiveLinkRef.current?.classList.remove(linkActiveClassName);
|
|
144
|
+
}
|
|
145
|
+
link.classList.add(linkActiveClassName);
|
|
146
|
+
lastActiveLinkRef.current = link;
|
|
147
|
+
} else {
|
|
148
|
+
link.classList.remove(linkActiveClassName);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function updateActiveLink() {
|
|
153
|
+
const links = getLinks(linkClassName);
|
|
154
|
+
const anchors = getAnchors({minHeadingLevel, maxHeadingLevel});
|
|
155
|
+
const activeAnchor = getActiveAnchor(anchors, {
|
|
156
|
+
anchorTopOffset: anchorTopOffsetRef.current,
|
|
157
|
+
});
|
|
158
|
+
const activeLink = links.find(
|
|
159
|
+
(link) => activeAnchor && activeAnchor.id === getLinkAnchorValue(link),
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
links.forEach((link) => {
|
|
163
|
+
updateLinkActiveClass(link, link === activeLink);
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
document.addEventListener('scroll', updateActiveLink);
|
|
168
|
+
document.addEventListener('resize', updateActiveLink);
|
|
169
|
+
|
|
170
|
+
updateActiveLink();
|
|
171
|
+
|
|
172
|
+
return () => {
|
|
173
|
+
document.removeEventListener('scroll', updateActiveLink);
|
|
174
|
+
document.removeEventListener('resize', updateActiveLink);
|
|
175
|
+
};
|
|
176
|
+
}, [config, anchorTopOffsetRef]);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export default useTOCHighlight;
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
|
8
8
|
import {PrismTheme} from 'prism-react-renderer';
|
|
9
9
|
import {CSSProperties} from 'react';
|
|
10
|
+
import {DeepPartial} from 'utility-types';
|
|
10
11
|
|
|
11
12
|
export type DocsVersionPersistence = 'localStorage' | 'none';
|
|
12
13
|
|
|
@@ -16,11 +17,13 @@ export type NavbarItem = {
|
|
|
16
17
|
items?: NavbarItem[];
|
|
17
18
|
label?: string;
|
|
18
19
|
position?: 'left' | 'right';
|
|
19
|
-
}
|
|
20
|
+
} & Record<string, unknown>;
|
|
20
21
|
|
|
21
22
|
export type NavbarLogo = {
|
|
22
23
|
src: string;
|
|
23
24
|
srcDark?: string;
|
|
25
|
+
width?: string | number;
|
|
26
|
+
height?: string | number;
|
|
24
27
|
href?: string;
|
|
25
28
|
target?: string;
|
|
26
29
|
alt?: string;
|
|
@@ -79,12 +82,20 @@ export type Footer = {
|
|
|
79
82
|
alt?: string;
|
|
80
83
|
src?: string;
|
|
81
84
|
srcDark?: string;
|
|
85
|
+
width?: string | number;
|
|
86
|
+
height?: string | number;
|
|
82
87
|
href?: string;
|
|
83
88
|
};
|
|
84
89
|
copyright?: string;
|
|
85
90
|
links: FooterLinks[];
|
|
86
91
|
};
|
|
87
92
|
|
|
93
|
+
export type TableOfContents = {
|
|
94
|
+
minHeadingLevel: number;
|
|
95
|
+
maxHeadingLevel: number;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// Theme config after validation/normalization
|
|
88
99
|
export type ThemeConfig = {
|
|
89
100
|
docs: {
|
|
90
101
|
versionPersistence: DocsVersionPersistence;
|
|
@@ -101,10 +112,15 @@ export type ThemeConfig = {
|
|
|
101
112
|
prism: PrismConfig;
|
|
102
113
|
footer?: Footer;
|
|
103
114
|
hideableSidebar: boolean;
|
|
104
|
-
image
|
|
115
|
+
image?: string;
|
|
105
116
|
metadatas: Array<Record<string, string>>;
|
|
117
|
+
sidebarCollapsible: boolean;
|
|
118
|
+
tableOfContents: TableOfContents;
|
|
106
119
|
};
|
|
107
120
|
|
|
121
|
+
// User-provided theme config, unnormalized
|
|
122
|
+
export type UserThemeConfig = DeepPartial<ThemeConfig>;
|
|
123
|
+
|
|
108
124
|
export function useThemeConfig(): ThemeConfig {
|
|
109
125
|
return useDocusaurusContext().siteConfig.themeConfig as ThemeConfig;
|
|
110
126
|
}
|