@docusaurus/theme-common 2.0.0-beta.1 → 2.0.0-beta.10

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 (94) hide show
  1. package/copyUntypedFiles.js +20 -0
  2. package/lib/.tsbuildinfo +1 -1
  3. package/lib/components/Collapsible/index.d.ts +35 -0
  4. package/lib/components/Collapsible/index.js +139 -0
  5. package/lib/components/Details/index.d.ts +12 -0
  6. package/lib/components/Details/index.js +64 -0
  7. package/lib/components/Details/styles.module.css +58 -0
  8. package/lib/index.d.ts +24 -4
  9. package/lib/index.js +18 -3
  10. package/lib/utils/ThemeClassNames.d.ts +36 -12
  11. package/lib/utils/ThemeClassNames.js +36 -3
  12. package/lib/utils/announcementBarUtils.d.ts +17 -0
  13. package/lib/utils/announcementBarUtils.js +69 -0
  14. package/lib/utils/codeBlockUtils.d.ts +10 -0
  15. package/lib/utils/codeBlockUtils.js +119 -0
  16. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +2 -2
  17. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +2 -2
  18. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +1 -3
  19. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +11 -3
  20. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +1 -2
  21. package/lib/utils/docsUtils.d.ts +20 -0
  22. package/lib/utils/docsUtils.js +106 -0
  23. package/lib/utils/generalUtils.d.ts +6 -0
  24. package/lib/utils/generalUtils.js +2 -2
  25. package/lib/utils/historyUtils.d.ts +11 -0
  26. package/lib/utils/historyUtils.js +39 -0
  27. package/lib/utils/jsUtils.d.ts +19 -0
  28. package/lib/utils/jsUtils.js +25 -0
  29. package/lib/utils/mobileSecondaryMenu.d.ts +20 -0
  30. package/lib/utils/mobileSecondaryMenu.js +49 -0
  31. package/lib/utils/pathUtils.js +1 -3
  32. package/lib/utils/reactUtils.d.ts +9 -0
  33. package/lib/utils/reactUtils.js +26 -0
  34. package/lib/utils/regexpUtils.d.ts +10 -0
  35. package/lib/utils/regexpUtils.js +16 -0
  36. package/lib/utils/scrollUtils.d.ts +52 -0
  37. package/lib/utils/scrollUtils.js +135 -0
  38. package/lib/utils/storageUtils.d.ts +4 -0
  39. package/lib/utils/storageUtils.js +29 -3
  40. package/lib/utils/tagsUtils.d.ts +18 -0
  41. package/lib/utils/tagsUtils.js +33 -0
  42. package/lib/utils/tocUtils.d.ts +15 -0
  43. package/lib/utils/tocUtils.js +34 -0
  44. package/lib/utils/useContextualSearchFilters.d.ts +11 -0
  45. package/lib/utils/useContextualSearchFilters.js +36 -0
  46. package/lib/utils/{useChangeRoute.d.ts → useLocalPathname.d.ts} +1 -1
  47. package/lib/utils/useLocalPathname.js +16 -0
  48. package/lib/utils/useLocationChange.d.ts +14 -0
  49. package/lib/utils/useLocationChange.js +23 -0
  50. package/lib/utils/usePluralForm.js +1 -3
  51. package/{src/utils/docsUtils.ts → lib/utils/usePrevious.d.ts} +1 -5
  52. package/lib/utils/usePrevious.js +15 -0
  53. package/lib/utils/useTOCHighlight.d.ts +14 -0
  54. package/lib/utils/useTOCHighlight.js +124 -0
  55. package/lib/utils/useThemeConfig.d.ts +21 -3
  56. package/package.json +18 -12
  57. package/src/components/Collapsible/index.tsx +242 -0
  58. package/src/components/Details/index.tsx +94 -0
  59. package/src/components/Details/styles.module.css +58 -0
  60. package/src/index.ts +73 -3
  61. package/src/types.d.ts +0 -2
  62. package/src/utils/ThemeClassNames.ts +42 -4
  63. package/src/utils/__tests__/codeBlockUtils.test.ts +2 -2
  64. package/src/utils/__tests__/docsUtils.test.tsx +331 -0
  65. package/src/utils/__tests__/jsUtils.test.ts +33 -0
  66. package/src/utils/__tests__/tagUtils.test.ts +66 -0
  67. package/src/utils/__tests__/tocUtils.test.ts +197 -0
  68. package/src/utils/announcementBarUtils.tsx +120 -0
  69. package/src/utils/codeBlockUtils.ts +151 -0
  70. package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +7 -6
  71. package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +2 -3
  72. package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +14 -14
  73. package/src/utils/docsUtils.tsx +185 -0
  74. package/src/utils/generalUtils.ts +3 -2
  75. package/src/utils/historyUtils.ts +50 -0
  76. package/src/utils/jsUtils.ts +33 -0
  77. package/src/utils/mobileSecondaryMenu.tsx +114 -0
  78. package/src/utils/pathUtils.ts +2 -3
  79. package/src/utils/reactUtils.tsx +34 -0
  80. package/src/utils/regexpUtils.ts +23 -0
  81. package/src/utils/scrollUtils.tsx +237 -0
  82. package/src/utils/storageUtils.ts +27 -4
  83. package/src/utils/tagsUtils.ts +48 -0
  84. package/src/utils/tocUtils.ts +55 -0
  85. package/src/utils/useAlternatePageUtils.ts +9 -1
  86. package/src/utils/useContextualSearchFilters.ts +50 -0
  87. package/src/utils/useLocalPathname.ts +20 -0
  88. package/src/utils/useLocationChange.ts +35 -0
  89. package/src/utils/usePluralForm.ts +5 -4
  90. package/src/utils/usePrevious.ts +19 -0
  91. package/src/utils/useTOCHighlight.ts +179 -0
  92. package/src/utils/useThemeConfig.ts +20 -3
  93. package/lib/utils/useChangeRoute.js +0 -18
  94. package/src/utils/useChangeRoute.ts +0 -21
@@ -0,0 +1,35 @@
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} from 'react';
9
+ import {useLocation} from '@docusaurus/router';
10
+ import {Location} from '@docusaurus/history';
11
+ import {usePrevious} from './usePrevious';
12
+ import {useDynamicCallback} from './reactUtils';
13
+
14
+ type LocationChangeEvent = {
15
+ location: Location;
16
+ previousLocation: Location | undefined;
17
+ };
18
+
19
+ type OnLocationChange = (locationChangeEvent: LocationChangeEvent) => void;
20
+
21
+ export function useLocationChange(onLocationChange: OnLocationChange): void {
22
+ const location = useLocation();
23
+ const previousLocation = usePrevious(location);
24
+
25
+ const onLocationChangeDynamic = useDynamicCallback(onLocationChange);
26
+
27
+ useEffect(() => {
28
+ if (location !== previousLocation) {
29
+ onLocationChangeDynamic({
30
+ location,
31
+ previousLocation,
32
+ });
33
+ }
34
+ }, [onLocationChangeDynamic, location, previousLocation]);
35
+ }
@@ -107,11 +107,12 @@ 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
- selectMessage: (count: number, pluralMessages: string): string => {
114
- return selectPluralMessage(pluralMessages, count, localePluralForm);
115
- },
115
+ selectMessage: (count: number, pluralMessages: string): string =>
116
+ selectPluralMessage(pluralMessages, count, localePluralForm),
116
117
  };
117
118
  }
@@ -0,0 +1,19 @@
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 {useRef} from 'react';
9
+ import {useIsomorphicLayoutEffect} from './reactUtils';
10
+
11
+ export function usePrevious<T>(value: T): T | undefined {
12
+ const ref = useRef<T>();
13
+
14
+ useIsomorphicLayoutEffect(() => {
15
+ ref.current = value;
16
+ });
17
+
18
+ return ref.current;
19
+ }
@@ -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;
@@ -4,9 +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
+
7
8
  import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
8
9
  import {PrismTheme} from 'prism-react-renderer';
9
10
  import {CSSProperties} from 'react';
11
+ import {DeepPartial} from 'utility-types';
10
12
 
11
13
  export type DocsVersionPersistence = 'localStorage' | 'none';
12
14
 
@@ -16,11 +18,13 @@ export type NavbarItem = {
16
18
  items?: NavbarItem[];
17
19
  label?: string;
18
20
  position?: 'left' | 'right';
19
- };
21
+ } & Record<string, unknown>;
20
22
 
21
23
  export type NavbarLogo = {
22
24
  src: string;
23
25
  srcDark?: string;
26
+ width?: string | number;
27
+ height?: string | number;
24
28
  href?: string;
25
29
  target?: string;
26
30
  alt?: string;
@@ -79,12 +83,20 @@ export type Footer = {
79
83
  alt?: string;
80
84
  src?: string;
81
85
  srcDark?: string;
86
+ width?: string | number;
87
+ height?: string | number;
82
88
  href?: string;
83
89
  };
84
90
  copyright?: string;
85
91
  links: FooterLinks[];
86
92
  };
87
93
 
94
+ export type TableOfContents = {
95
+ minHeadingLevel: number;
96
+ maxHeadingLevel: number;
97
+ };
98
+
99
+ // Theme config after validation/normalization
88
100
  export type ThemeConfig = {
89
101
  docs: {
90
102
  versionPersistence: DocsVersionPersistence;
@@ -101,10 +113,15 @@ export type ThemeConfig = {
101
113
  prism: PrismConfig;
102
114
  footer?: Footer;
103
115
  hideableSidebar: boolean;
104
- image: string;
105
- metadatas: Array<Record<string, string>>;
116
+ image?: string;
117
+ metadata: Array<Record<string, string>>;
118
+ sidebarCollapsible: boolean;
119
+ tableOfContents: TableOfContents;
106
120
  };
107
121
 
122
+ // User-provided theme config, unnormalized
123
+ export type UserThemeConfig = DeepPartial<ThemeConfig>;
124
+
108
125
  export function useThemeConfig(): ThemeConfig {
109
126
  return useDocusaurusContext().siteConfig.themeConfig as ThemeConfig;
110
127
  }
@@ -1,18 +0,0 @@
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 { useRef, useEffect } from 'react';
8
- import { useLocation } from '@docusaurus/router';
9
- export function useChangeRoute(onRouteChange) {
10
- const { pathname } = useLocation();
11
- const latestPathnameRef = useRef(pathname);
12
- useEffect(() => {
13
- if (pathname !== latestPathnameRef.current) {
14
- latestPathnameRef.current = pathname;
15
- onRouteChange();
16
- }
17
- }, [pathname, latestPathnameRef, onRouteChange]);
18
- }
@@ -1,21 +0,0 @@
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 {useRef, useEffect} from 'react';
9
- import {useLocation} from '@docusaurus/router';
10
-
11
- export function useChangeRoute(onRouteChange: () => void): void {
12
- const {pathname} = useLocation();
13
- const latestPathnameRef = useRef(pathname);
14
-
15
- useEffect(() => {
16
- if (pathname !== latestPathnameRef.current) {
17
- latestPathnameRef.current = pathname;
18
- onRouteChange();
19
- }
20
- }, [pathname, latestPathnameRef, onRouteChange]);
21
- }