@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.
Files changed (107) hide show
  1. package/lib/components/Collapsible/index.d.ts.map +1 -1
  2. package/lib/components/Collapsible/index.js +9 -5
  3. package/lib/components/Collapsible/index.js.map +1 -1
  4. package/lib/components/Details/index.d.ts +1 -2
  5. package/lib/components/Details/index.d.ts.map +1 -1
  6. package/lib/components/Details/index.js +5 -4
  7. package/lib/components/Details/index.js.map +1 -1
  8. package/lib/hooks/useKeyboardNavigation.d.ts +1 -0
  9. package/lib/hooks/useKeyboardNavigation.d.ts.map +1 -1
  10. package/lib/hooks/useKeyboardNavigation.js +1 -1
  11. package/lib/hooks/useKeyboardNavigation.js.map +1 -1
  12. package/lib/hooks/useWindowSize.d.ts.map +1 -1
  13. package/lib/hooks/useWindowSize.js +4 -2
  14. package/lib/hooks/useWindowSize.js.map +1 -1
  15. package/lib/index.d.ts +4 -3
  16. package/lib/index.d.ts.map +1 -1
  17. package/lib/index.js +4 -3
  18. package/lib/index.js.map +1 -1
  19. package/lib/utils/ThemeClassNames.d.ts +1 -0
  20. package/lib/utils/ThemeClassNames.d.ts.map +1 -1
  21. package/lib/utils/ThemeClassNames.js +3 -1
  22. package/lib/utils/ThemeClassNames.js.map +1 -1
  23. package/lib/utils/codeBlockUtils.d.ts.map +1 -1
  24. package/lib/utils/codeBlockUtils.js +5 -4
  25. package/lib/utils/codeBlockUtils.js.map +1 -1
  26. package/lib/utils/colorModeUtils.d.ts.map +1 -1
  27. package/lib/utils/colorModeUtils.js +38 -16
  28. package/lib/utils/colorModeUtils.js.map +1 -1
  29. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts.map +1 -1
  30. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +3 -7
  31. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js.map +1 -1
  32. package/lib/utils/docsUtils.d.ts +6 -1
  33. package/lib/utils/docsUtils.d.ts.map +1 -1
  34. package/lib/utils/docsUtils.js +38 -10
  35. package/lib/utils/docsUtils.js.map +1 -1
  36. package/lib/utils/historyUtils.d.ts +12 -1
  37. package/lib/utils/historyUtils.d.ts.map +1 -1
  38. package/lib/utils/historyUtils.js +9 -8
  39. package/lib/utils/historyUtils.js.map +1 -1
  40. package/lib/utils/jsUtils.d.ts +5 -2
  41. package/lib/utils/jsUtils.d.ts.map +1 -1
  42. package/lib/utils/jsUtils.js +5 -2
  43. package/lib/utils/jsUtils.js.map +1 -1
  44. package/lib/utils/mobileSecondaryMenu.d.ts.map +1 -1
  45. package/lib/utils/mobileSecondaryMenu.js.map +1 -1
  46. package/lib/utils/pathUtils.d.ts.map +1 -1
  47. package/lib/utils/pathUtils.js +7 -2
  48. package/lib/utils/pathUtils.js.map +1 -1
  49. package/lib/utils/reactUtils.d.ts +18 -0
  50. package/lib/utils/reactUtils.d.ts.map +1 -1
  51. package/lib/utils/reactUtils.js +20 -11
  52. package/lib/utils/reactUtils.js.map +1 -1
  53. package/lib/utils/regexpUtils.d.ts +1 -1
  54. package/lib/utils/regexpUtils.js +1 -1
  55. package/lib/utils/routesUtils.d.ts +11 -0
  56. package/lib/utils/routesUtils.d.ts.map +1 -0
  57. package/lib/utils/routesUtils.js +33 -0
  58. package/lib/utils/routesUtils.js.map +1 -0
  59. package/lib/utils/storageUtils.d.ts +4 -4
  60. package/lib/utils/storageUtils.d.ts.map +1 -1
  61. package/lib/utils/storageUtils.js +18 -20
  62. package/lib/utils/storageUtils.js.map +1 -1
  63. package/lib/utils/tocUtils.d.ts +9 -5
  64. package/lib/utils/tocUtils.d.ts.map +1 -1
  65. package/lib/utils/tocUtils.js +45 -7
  66. package/lib/utils/tocUtils.js.map +1 -1
  67. package/lib/utils/useAlternatePageUtils.d.ts.map +1 -1
  68. package/lib/utils/useAlternatePageUtils.js +2 -1
  69. package/lib/utils/useAlternatePageUtils.js.map +1 -1
  70. package/lib/utils/useContextualSearchFilters.js +2 -2
  71. package/lib/utils/useContextualSearchFilters.js.map +1 -1
  72. package/lib/utils/useLocationChange.d.ts +1 -1
  73. package/lib/utils/useLocationChange.d.ts.map +1 -1
  74. package/lib/utils/useLocationChange.js +3 -0
  75. package/lib/utils/useLocationChange.js.map +1 -1
  76. package/lib/utils/usePluralForm.d.ts.map +1 -1
  77. package/lib/utils/usePluralForm.js +26 -21
  78. package/lib/utils/usePluralForm.js.map +1 -1
  79. package/lib/utils/useTOCHighlight.d.ts +1 -2
  80. package/lib/utils/useTOCHighlight.d.ts.map +1 -1
  81. package/lib/utils/useTOCHighlight.js +22 -20
  82. package/lib/utils/useTOCHighlight.js.map +1 -1
  83. package/package.json +11 -11
  84. package/src/components/Collapsible/index.tsx +14 -9
  85. package/src/components/Details/index.tsx +7 -4
  86. package/src/hooks/useKeyboardNavigation.ts +2 -2
  87. package/src/hooks/useWindowSize.ts +4 -2
  88. package/src/index.ts +12 -2
  89. package/src/utils/ThemeClassNames.ts +3 -1
  90. package/src/utils/codeBlockUtils.ts +5 -4
  91. package/src/utils/colorModeUtils.tsx +39 -18
  92. package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +3 -5
  93. package/src/utils/docsUtils.tsx +56 -11
  94. package/src/utils/historyUtils.ts +10 -9
  95. package/src/utils/jsUtils.ts +5 -2
  96. package/src/utils/mobileSecondaryMenu.tsx +6 -5
  97. package/src/utils/pathUtils.ts +5 -2
  98. package/src/utils/reactUtils.tsx +20 -11
  99. package/src/utils/regexpUtils.ts +1 -1
  100. package/src/utils/routesUtils.ts +39 -0
  101. package/src/utils/storageUtils.ts +21 -19
  102. package/src/utils/tocUtils.ts +66 -13
  103. package/src/utils/useAlternatePageUtils.ts +4 -3
  104. package/src/utils/useContextualSearchFilters.ts +2 -2
  105. package/src/utils/useLocationChange.ts +5 -1
  106. package/src/utils/usePluralForm.ts +29 -23
  107. 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 '@docusaurus/history';
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
- // Poor man's PluralSelector implementation, using an english fallback.
53
- // We want a lightweight, future-proof and good-enough solution.
54
- // We don't want a perfect and heavy solution.
55
- //
56
- // Docusaurus classic theme has only 2 deeply nested labels requiring complex plural rules
57
- // We don't want to use Intl + PluralRules polyfills + full ICU syntax (react-intl) just for that.
58
- //
59
- // Notes:
60
- // - 2021: 92+% Browsers support Intl.PluralRules, and support will increase in the future
61
- // - NodeJS >= 13 has full ICU support by default
62
- // - In case of "mismatch" between SSR and Browser ICU support, React keeps working!
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 have it (e.g Safari 12-)
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 (e) {
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 parent (normally the link text) rect boundaries instead
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
- // 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)
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
- // 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
+ // 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 "active"
68
- // (unless it's too close to the top and and soon to be scrolled outside viewport)
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, 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
- }
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
- else {
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(config: TOCHighlightConfig | undefined): void {
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;