@docusaurus/theme-common 2.0.0-beta.15 → 2.0.0-beta.16
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/components/Collapsible/index.d.ts.map +1 -1
- package/lib/components/Collapsible/index.js +9 -5
- package/lib/components/Collapsible/index.js.map +1 -1
- package/lib/components/Details/index.d.ts +1 -2
- package/lib/components/Details/index.d.ts.map +1 -1
- package/lib/components/Details/index.js +5 -4
- package/lib/components/Details/index.js.map +1 -1
- package/lib/hooks/useKeyboardNavigation.d.ts +1 -0
- package/lib/hooks/useKeyboardNavigation.d.ts.map +1 -1
- package/lib/hooks/useKeyboardNavigation.js +1 -1
- package/lib/hooks/useKeyboardNavigation.js.map +1 -1
- package/lib/hooks/useWindowSize.d.ts.map +1 -1
- package/lib/hooks/useWindowSize.js +4 -2
- package/lib/hooks/useWindowSize.js.map +1 -1
- package/lib/index.d.ts +4 -3
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +4 -3
- package/lib/index.js.map +1 -1
- package/lib/utils/ThemeClassNames.d.ts +1 -0
- package/lib/utils/ThemeClassNames.d.ts.map +1 -1
- package/lib/utils/ThemeClassNames.js +3 -1
- package/lib/utils/ThemeClassNames.js.map +1 -1
- package/lib/utils/codeBlockUtils.d.ts.map +1 -1
- package/lib/utils/codeBlockUtils.js +5 -4
- package/lib/utils/codeBlockUtils.js.map +1 -1
- package/lib/utils/colorModeUtils.d.ts.map +1 -1
- package/lib/utils/colorModeUtils.js +38 -16
- package/lib/utils/colorModeUtils.js.map +1 -1
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts.map +1 -1
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +3 -7
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js.map +1 -1
- package/lib/utils/docsUtils.d.ts +6 -1
- package/lib/utils/docsUtils.d.ts.map +1 -1
- package/lib/utils/docsUtils.js +38 -10
- package/lib/utils/docsUtils.js.map +1 -1
- package/lib/utils/historyUtils.d.ts +12 -1
- package/lib/utils/historyUtils.d.ts.map +1 -1
- package/lib/utils/historyUtils.js +9 -8
- package/lib/utils/historyUtils.js.map +1 -1
- package/lib/utils/jsUtils.d.ts +5 -2
- package/lib/utils/jsUtils.d.ts.map +1 -1
- package/lib/utils/jsUtils.js +5 -2
- package/lib/utils/jsUtils.js.map +1 -1
- package/lib/utils/mobileSecondaryMenu.d.ts.map +1 -1
- package/lib/utils/mobileSecondaryMenu.js.map +1 -1
- package/lib/utils/pathUtils.d.ts.map +1 -1
- package/lib/utils/pathUtils.js +7 -2
- package/lib/utils/pathUtils.js.map +1 -1
- package/lib/utils/reactUtils.d.ts +18 -0
- package/lib/utils/reactUtils.d.ts.map +1 -1
- package/lib/utils/reactUtils.js +20 -11
- package/lib/utils/reactUtils.js.map +1 -1
- package/lib/utils/regexpUtils.d.ts +1 -1
- package/lib/utils/regexpUtils.js +1 -1
- package/lib/utils/routesUtils.d.ts +11 -0
- package/lib/utils/routesUtils.d.ts.map +1 -0
- package/lib/utils/routesUtils.js +33 -0
- package/lib/utils/routesUtils.js.map +1 -0
- package/lib/utils/storageUtils.d.ts +4 -4
- package/lib/utils/storageUtils.d.ts.map +1 -1
- package/lib/utils/storageUtils.js +18 -20
- package/lib/utils/storageUtils.js.map +1 -1
- package/lib/utils/tocUtils.d.ts +9 -5
- package/lib/utils/tocUtils.d.ts.map +1 -1
- package/lib/utils/tocUtils.js +45 -7
- package/lib/utils/tocUtils.js.map +1 -1
- package/lib/utils/useAlternatePageUtils.d.ts.map +1 -1
- package/lib/utils/useAlternatePageUtils.js +2 -1
- package/lib/utils/useAlternatePageUtils.js.map +1 -1
- package/lib/utils/useContextualSearchFilters.js +2 -2
- package/lib/utils/useContextualSearchFilters.js.map +1 -1
- package/lib/utils/useLocationChange.d.ts +1 -1
- package/lib/utils/useLocationChange.d.ts.map +1 -1
- package/lib/utils/useLocationChange.js +3 -0
- package/lib/utils/useLocationChange.js.map +1 -1
- package/lib/utils/usePluralForm.d.ts.map +1 -1
- package/lib/utils/usePluralForm.js +26 -21
- package/lib/utils/usePluralForm.js.map +1 -1
- package/lib/utils/useTOCHighlight.d.ts +1 -2
- package/lib/utils/useTOCHighlight.d.ts.map +1 -1
- package/lib/utils/useTOCHighlight.js +22 -20
- package/lib/utils/useTOCHighlight.js.map +1 -1
- package/package.json +11 -11
- package/src/components/Collapsible/index.tsx +14 -9
- package/src/components/Details/index.tsx +7 -4
- package/src/hooks/useKeyboardNavigation.ts +2 -2
- package/src/hooks/useWindowSize.ts +4 -2
- package/src/index.ts +12 -2
- package/src/utils/ThemeClassNames.ts +3 -1
- package/src/utils/codeBlockUtils.ts +5 -4
- package/src/utils/colorModeUtils.tsx +39 -18
- package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +3 -5
- package/src/utils/docsUtils.tsx +56 -11
- package/src/utils/historyUtils.ts +10 -9
- package/src/utils/jsUtils.ts +5 -2
- package/src/utils/mobileSecondaryMenu.tsx +6 -5
- package/src/utils/pathUtils.ts +5 -2
- package/src/utils/reactUtils.tsx +20 -11
- package/src/utils/regexpUtils.ts +1 -1
- package/src/utils/routesUtils.ts +39 -0
- package/src/utils/storageUtils.ts +21 -19
- package/src/utils/tocUtils.ts +66 -13
- package/src/utils/useAlternatePageUtils.ts +4 -3
- package/src/utils/useContextualSearchFilters.ts +2 -2
- package/src/utils/useLocationChange.ts +5 -1
- package/src/utils/usePluralForm.ts +29 -23
- package/src/utils/useTOCHighlight.ts +24 -21
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
import {useEffect} from 'react';
|
|
9
9
|
import {useLocation} from '@docusaurus/router';
|
|
10
|
-
import type {Location} from '
|
|
10
|
+
import type {Location} from 'history';
|
|
11
11
|
import {usePrevious} from './usePrevious';
|
|
12
12
|
import {useDynamicCallback} from './reactUtils';
|
|
13
13
|
|
|
@@ -25,6 +25,10 @@ export function useLocationChange(onLocationChange: OnLocationChange): void {
|
|
|
25
25
|
const onLocationChangeDynamic = useDynamicCallback(onLocationChange);
|
|
26
26
|
|
|
27
27
|
useEffect(() => {
|
|
28
|
+
if (!previousLocation) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
28
32
|
if (location !== previousLocation) {
|
|
29
33
|
onLocationChangeDynamic({
|
|
30
34
|
location,
|
|
@@ -49,27 +49,33 @@ function createLocalePluralForms(locale: string): LocalePluralForms {
|
|
|
49
49
|
};
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Poor man's PluralSelector implementation, using an english fallback. We want
|
|
54
|
+
* a lightweight, future-proof and good-enough solution. We don't want a perfect
|
|
55
|
+
* and heavy solution.
|
|
56
|
+
*
|
|
57
|
+
* Docusaurus classic theme has only 2 deeply nested labels requiring complex
|
|
58
|
+
* plural rules. We don't want to use Intl + PluralRules polyfills + full ICU
|
|
59
|
+
* syntax (react-intl) just for that.
|
|
60
|
+
*
|
|
61
|
+
* Notes:
|
|
62
|
+
* - 2021: 92+% Browsers support Intl.PluralRules, and support will increase in
|
|
63
|
+
* the future
|
|
64
|
+
* - NodeJS >= 13 has full ICU support by default
|
|
65
|
+
* - In case of "mismatch" between SSR and Browser ICU support, React keeps
|
|
66
|
+
* working!
|
|
67
|
+
*/
|
|
63
68
|
function useLocalePluralForms(): LocalePluralForms {
|
|
64
69
|
const {
|
|
65
70
|
i18n: {currentLocale},
|
|
66
71
|
} = useDocusaurusContext();
|
|
67
72
|
return useMemo(() => {
|
|
68
|
-
// @ts-expect-error checking Intl.PluralRules in case browser doesn't
|
|
73
|
+
// @ts-expect-error checking Intl.PluralRules in case browser doesn't
|
|
74
|
+
// have it (e.g Safari 12-)
|
|
69
75
|
if (Intl.PluralRules) {
|
|
70
76
|
try {
|
|
71
77
|
return createLocalePluralForms(currentLocale);
|
|
72
|
-
} catch
|
|
78
|
+
} catch {
|
|
73
79
|
console.error(`Failed to use Intl.PluralRules for locale "${currentLocale}".
|
|
74
80
|
Docusaurus will fallback to a default/fallback (English) Intl.PluralRules implementation.
|
|
75
81
|
`);
|
|
@@ -94,17 +100,17 @@ function selectPluralMessage(
|
|
|
94
100
|
|
|
95
101
|
if (parts.length === 1) {
|
|
96
102
|
return parts[0];
|
|
97
|
-
} else {
|
|
98
|
-
if (parts.length > localePluralForms.pluralForms.length) {
|
|
99
|
-
console.error(
|
|
100
|
-
`For locale=${localePluralForms.locale}, a maximum of ${localePluralForms.pluralForms.length} plural forms are expected (${localePluralForms.pluralForms}), but the message contains ${parts.length} plural forms: ${pluralMessages} `,
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
const pluralForm = localePluralForms.select(count);
|
|
104
|
-
const pluralFormIndex = localePluralForms.pluralForms.indexOf(pluralForm);
|
|
105
|
-
// In case of not enough plural form messages, we take the last one (other) instead of returning undefined
|
|
106
|
-
return parts[Math.min(pluralFormIndex, parts.length - 1)];
|
|
107
103
|
}
|
|
104
|
+
if (parts.length > localePluralForms.pluralForms.length) {
|
|
105
|
+
console.error(
|
|
106
|
+
`For locale=${localePluralForms.locale}, a maximum of ${localePluralForms.pluralForms.length} plural forms are expected (${localePluralForms.pluralForms}), but the message contains ${parts.length} plural forms: ${pluralMessages} `,
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
const pluralForm = localePluralForms.select(count);
|
|
110
|
+
const pluralFormIndex = localePluralForms.pluralForms.indexOf(pluralForm);
|
|
111
|
+
// In case of not enough plural form messages, we take the last one (other)
|
|
112
|
+
// instead of returning undefined
|
|
113
|
+
return parts[Math.min(pluralFormIndex, parts.length - 1)];
|
|
108
114
|
}
|
|
109
115
|
|
|
110
116
|
export function usePluralForm(): {
|
|
@@ -13,7 +13,8 @@ TODO make the hardcoded theme-classic classnames configurable
|
|
|
13
13
|
(or add them to ThemeClassNames?)
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
|
-
// If the anchor has no height and is just a "marker" in the dom; we'll use the
|
|
16
|
+
// If the anchor has no height and is just a "marker" in the dom; we'll use the
|
|
17
|
+
// parent (normally the link text) rect boundaries instead
|
|
17
18
|
function getVisibleBoundingClientRect(element: HTMLElement): DOMRect {
|
|
18
19
|
const rect = element.getBoundingClientRect();
|
|
19
20
|
const hasNoHeight = rect.top === rect.bottom;
|
|
@@ -23,8 +24,10 @@ function getVisibleBoundingClientRect(element: HTMLElement): DOMRect {
|
|
|
23
24
|
return rect;
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Considering we divide viewport into 2 zones of each 50vh, this returns true
|
|
29
|
+
* if an element is in the first zone (ie, appear in viewport, near the top)
|
|
30
|
+
*/
|
|
28
31
|
function isInViewportTopHalf(boundingRect: DOMRect) {
|
|
29
32
|
return boundingRect.top > 0 && boundingRect.bottom < window.innerHeight / 2;
|
|
30
33
|
}
|
|
@@ -54,9 +57,10 @@ function getActiveAnchor(
|
|
|
54
57
|
anchorTopOffset: number;
|
|
55
58
|
},
|
|
56
59
|
): Element | null {
|
|
57
|
-
// Naming is hard
|
|
58
|
-
//
|
|
59
|
-
//
|
|
60
|
+
// Naming is hard: The "nextVisibleAnchor" is the first anchor that appear
|
|
61
|
+
// under the viewport top boundary. It does not mean this anchor is visible
|
|
62
|
+
// yet, but if user continues scrolling down, it will be the first to become
|
|
63
|
+
// visible
|
|
60
64
|
const nextVisibleAnchor = anchors.find((anchor) => {
|
|
61
65
|
const boundingRect = getVisibleBoundingClientRect(anchor);
|
|
62
66
|
return boundingRect.top >= anchorTopOffset;
|
|
@@ -64,23 +68,22 @@ function getActiveAnchor(
|
|
|
64
68
|
|
|
65
69
|
if (nextVisibleAnchor) {
|
|
66
70
|
const boundingRect = getVisibleBoundingClientRect(nextVisibleAnchor);
|
|
67
|
-
// If anchor is in the top half of the viewport: it is the one we consider
|
|
68
|
-
// (unless it's too close to the top and and soon to be scrolled
|
|
71
|
+
// If anchor is in the top half of the viewport: it is the one we consider
|
|
72
|
+
// "active" (unless it's too close to the top and and soon to be scrolled
|
|
73
|
+
// outside viewport)
|
|
69
74
|
if (isInViewportTopHalf(boundingRect)) {
|
|
70
75
|
return nextVisibleAnchor;
|
|
71
76
|
}
|
|
72
|
-
// If anchor is in the bottom half of the viewport, or under the viewport,
|
|
73
|
-
//
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
77
|
+
// If anchor is in the bottom half of the viewport, or under the viewport,
|
|
78
|
+
// we consider the active anchor is the previous one. This is because the
|
|
79
|
+
// main text appearing in the user screen mostly belong to the previous
|
|
80
|
+
// anchor. Returns null for the first anchor, see
|
|
81
|
+
// https://github.com/facebook/docusaurus/issues/5318
|
|
82
|
+
return anchors[anchors.indexOf(nextVisibleAnchor) - 1] ?? null;
|
|
78
83
|
}
|
|
79
84
|
// no anchor under viewport top? (ie we are at the bottom of the page)
|
|
80
85
|
// => highlight the last anchor found
|
|
81
|
-
|
|
82
|
-
return anchors[anchors.length - 1];
|
|
83
|
-
}
|
|
86
|
+
return anchors[anchors.length - 1];
|
|
84
87
|
}
|
|
85
88
|
|
|
86
89
|
function getLinkAnchorValue(link: HTMLAnchorElement): string {
|
|
@@ -119,7 +122,9 @@ export type TOCHighlightConfig = {
|
|
|
119
122
|
maxHeadingLevel: number;
|
|
120
123
|
};
|
|
121
124
|
|
|
122
|
-
function useTOCHighlight(
|
|
125
|
+
export default function useTOCHighlight(
|
|
126
|
+
config: TOCHighlightConfig | undefined,
|
|
127
|
+
): void {
|
|
123
128
|
const lastActiveLinkRef = useRef<HTMLAnchorElement | undefined>(undefined);
|
|
124
129
|
|
|
125
130
|
const anchorTopOffsetRef = useAnchorTopOffsetRef();
|
|
@@ -144,7 +149,7 @@ function useTOCHighlight(config: TOCHighlightConfig | undefined): void {
|
|
|
144
149
|
}
|
|
145
150
|
link.classList.add(linkActiveClassName);
|
|
146
151
|
lastActiveLinkRef.current = link;
|
|
147
|
-
link.scrollIntoView({block: 'nearest'});
|
|
152
|
+
// link.scrollIntoView({block: 'nearest'});
|
|
148
153
|
} else {
|
|
149
154
|
link.classList.remove(linkActiveClassName);
|
|
150
155
|
}
|
|
@@ -176,5 +181,3 @@ function useTOCHighlight(config: TOCHighlightConfig | undefined): void {
|
|
|
176
181
|
};
|
|
177
182
|
}, [config, anchorTopOffsetRef]);
|
|
178
183
|
}
|
|
179
|
-
|
|
180
|
-
export default useTOCHighlight;
|