@docusaurus/theme-classic 0.0.0-4776 → 0.0.0-4781

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.
@@ -235,6 +235,40 @@ declare module '@theme/DocSidebarItem' {
235
235
  export default function DocSidebarItem(props: Props): JSX.Element;
236
236
  }
237
237
 
238
+ declare module '@theme/DocSidebarItem/Link' {
239
+ import type {Props as DocSidebarItemProps} from '@theme/DocSidebarItem';
240
+
241
+ import type {PropSidebarItemLink} from '@docusaurus/plugin-content-docs';
242
+
243
+ export interface Props extends DocSidebarItemProps {
244
+ item: PropSidebarItemLink;
245
+ }
246
+
247
+ export default function DocSidebarItemLink(props: Props): JSX.Element;
248
+ }
249
+
250
+ declare module '@theme/DocSidebarItem/Html' {
251
+ import type {Props as DocSidebarItemProps} from '@theme/DocSidebarItem';
252
+ import type {PropSidebarItemHtml} from '@docusaurus/plugin-content-docs';
253
+
254
+ export interface Props extends DocSidebarItemProps {
255
+ item: PropSidebarItemHtml;
256
+ }
257
+
258
+ export default function DocSidebarItemHtml(props: Props): JSX.Element;
259
+ }
260
+
261
+ declare module '@theme/DocSidebarItem/Category' {
262
+ import type {Props as DocSidebarItemProps} from '@theme/DocSidebarItem';
263
+ import type {PropSidebarItemCategory} from '@docusaurus/plugin-content-docs';
264
+
265
+ export interface Props extends DocSidebarItemProps {
266
+ item: PropSidebarItemCategory;
267
+ }
268
+
269
+ export default function DocSidebarItemCategory(props: Props): JSX.Element;
270
+ }
271
+
238
272
  declare module '@theme/DocSidebarItems' {
239
273
  import type {Props as DocSidebarItemProps} from '@theme/DocSidebarItem';
240
274
  import type {PropSidebarItem} from '@docusaurus/plugin-content-docs';
@@ -0,0 +1,199 @@
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, {useEffect, useMemo} from 'react';
8
+ import clsx from 'clsx';
9
+ import {
10
+ isActiveSidebarItem,
11
+ usePrevious,
12
+ Collapsible,
13
+ useCollapsible,
14
+ findFirstCategoryLink,
15
+ ThemeClassNames,
16
+ useThemeConfig,
17
+ useDocSidebarItemsExpandedState,
18
+ isSamePath,
19
+ } from '@docusaurus/theme-common';
20
+ import Link from '@docusaurus/Link';
21
+ import {translate} from '@docusaurus/Translate';
22
+ import DocSidebarItems from '@theme/DocSidebarItems';
23
+ import useIsBrowser from '@docusaurus/useIsBrowser'; // If we navigate to a category and it becomes active, it should automatically
24
+ // expand itself
25
+
26
+ function useAutoExpandActiveCategory({isActive, collapsed, setCollapsed}) {
27
+ const wasActive = usePrevious(isActive);
28
+ useEffect(() => {
29
+ const justBecameActive = isActive && !wasActive;
30
+
31
+ if (justBecameActive && collapsed) {
32
+ setCollapsed(false);
33
+ }
34
+ }, [isActive, wasActive, collapsed, setCollapsed]);
35
+ }
36
+ /**
37
+ * When a collapsible category has no link, we still link it to its first child
38
+ * during SSR as a temporary fallback. This allows to be able to navigate inside
39
+ * the category even when JS fails to load, is delayed or simply disabled
40
+ * React hydration becomes an optional progressive enhancement
41
+ * see https://github.com/facebookincubator/infima/issues/36#issuecomment-772543188
42
+ * see https://github.com/facebook/docusaurus/issues/3030
43
+ */
44
+
45
+ function useCategoryHrefWithSSRFallback(item) {
46
+ const isBrowser = useIsBrowser();
47
+ return useMemo(() => {
48
+ if (item.href) {
49
+ return item.href;
50
+ } // In these cases, it's not necessary to render a fallback
51
+ // We skip the "findFirstCategoryLink" computation
52
+
53
+ if (isBrowser || !item.collapsible) {
54
+ return undefined;
55
+ }
56
+
57
+ return findFirstCategoryLink(item);
58
+ }, [item, isBrowser]);
59
+ }
60
+
61
+ function CollapseButton({categoryLabel, onClick}) {
62
+ return (
63
+ <button
64
+ aria-label={translate(
65
+ {
66
+ id: 'theme.DocSidebarItem.toggleCollapsedCategoryAriaLabel',
67
+ message: "Toggle the collapsible sidebar category '{label}'",
68
+ description:
69
+ 'The ARIA label to toggle the collapsible sidebar category',
70
+ },
71
+ {
72
+ label: categoryLabel,
73
+ },
74
+ )}
75
+ type="button"
76
+ className="clean-btn menu__caret"
77
+ onClick={onClick}
78
+ />
79
+ );
80
+ }
81
+
82
+ export default function DocSidebarItemCategory({
83
+ item,
84
+ onItemClick,
85
+ activePath,
86
+ level,
87
+ index,
88
+ ...props
89
+ }) {
90
+ const {items, label, collapsible, className, href} = item;
91
+ const hrefWithSSRFallback = useCategoryHrefWithSSRFallback(item);
92
+ const isActive = isActiveSidebarItem(item, activePath);
93
+ const isCurrentPage = isSamePath(href, activePath);
94
+ const {collapsed, setCollapsed} = useCollapsible({
95
+ // active categories are always initialized as expanded
96
+ // the default (item.collapsed) is only used for non-active categories
97
+ initialState: () => {
98
+ if (!collapsible) {
99
+ return false;
100
+ }
101
+
102
+ return isActive ? false : item.collapsed;
103
+ },
104
+ });
105
+ useAutoExpandActiveCategory({
106
+ isActive,
107
+ collapsed,
108
+ setCollapsed,
109
+ });
110
+ const {expandedItem, setExpandedItem} = useDocSidebarItemsExpandedState();
111
+
112
+ function updateCollapsed(toCollapsed = !collapsed) {
113
+ setExpandedItem(toCollapsed ? null : index);
114
+ setCollapsed(toCollapsed);
115
+ }
116
+
117
+ const {autoCollapseSidebarCategories} = useThemeConfig();
118
+ useEffect(() => {
119
+ if (
120
+ collapsible &&
121
+ expandedItem &&
122
+ expandedItem !== index &&
123
+ autoCollapseSidebarCategories
124
+ ) {
125
+ setCollapsed(true);
126
+ }
127
+ }, [
128
+ collapsible,
129
+ expandedItem,
130
+ index,
131
+ setCollapsed,
132
+ autoCollapseSidebarCategories,
133
+ ]);
134
+ return (
135
+ <li
136
+ className={clsx(
137
+ ThemeClassNames.docs.docSidebarItemCategory,
138
+ ThemeClassNames.docs.docSidebarItemCategoryLevel(level),
139
+ 'menu__list-item',
140
+ {
141
+ 'menu__list-item--collapsed': collapsed,
142
+ },
143
+ className,
144
+ )}>
145
+ <div
146
+ className={clsx('menu__list-item-collapsible', {
147
+ 'menu__list-item-collapsible--active': isCurrentPage,
148
+ })}>
149
+ <Link
150
+ className={clsx('menu__link', {
151
+ 'menu__link--sublist': collapsible,
152
+ 'menu__link--sublist-caret': !href,
153
+ 'menu__link--active': isActive,
154
+ })}
155
+ onClick={
156
+ collapsible
157
+ ? (e) => {
158
+ onItemClick?.(item);
159
+
160
+ if (href) {
161
+ updateCollapsed(false);
162
+ } else {
163
+ e.preventDefault();
164
+ updateCollapsed();
165
+ }
166
+ }
167
+ : () => {
168
+ onItemClick?.(item);
169
+ }
170
+ }
171
+ aria-current={isCurrentPage ? 'page' : undefined}
172
+ aria-expanded={collapsible ? !collapsed : undefined}
173
+ href={collapsible ? hrefWithSSRFallback ?? '#' : hrefWithSSRFallback}
174
+ {...props}>
175
+ {label}
176
+ </Link>
177
+ {href && collapsible && (
178
+ <CollapseButton
179
+ categoryLabel={label}
180
+ onClick={(e) => {
181
+ e.preventDefault();
182
+ updateCollapsed();
183
+ }}
184
+ />
185
+ )}
186
+ </div>
187
+
188
+ <Collapsible lazy as="ul" className="menu__list" collapsed={collapsed}>
189
+ <DocSidebarItems
190
+ items={items}
191
+ tabIndex={collapsed ? -1 : 0}
192
+ onItemClick={onItemClick}
193
+ activePath={activePath}
194
+ level={level + 1}
195
+ />
196
+ </Collapsible>
197
+ </li>
198
+ );
199
+ }
@@ -0,0 +1,27 @@
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 from 'react';
8
+ import clsx from 'clsx';
9
+ import {ThemeClassNames} from '@docusaurus/theme-common';
10
+ import styles from './Html.module.css';
11
+ export default function DocSidebarItemHtml({item, level, index}) {
12
+ const {value, defaultStyle, className} = item;
13
+ return (
14
+ <li
15
+ className={clsx(
16
+ ThemeClassNames.docs.docSidebarItemLink,
17
+ ThemeClassNames.docs.docSidebarItemLinkLevel(level),
18
+ defaultStyle && `${styles.menuHtmlItem} menu__list-item`,
19
+ className,
20
+ )}
21
+ key={index} // eslint-disable-next-line react/no-danger
22
+ dangerouslySetInnerHTML={{
23
+ __html: value,
24
+ }}
25
+ />
26
+ );
27
+ }
@@ -11,7 +11,3 @@
11
11
  var(--ifm-menu-link-padding-horizontal);
12
12
  }
13
13
  }
14
-
15
- .menuExternalLink {
16
- align-items: center;
17
- }
@@ -0,0 +1,53 @@
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 from 'react';
8
+ import clsx from 'clsx';
9
+ import {isActiveSidebarItem, ThemeClassNames} from '@docusaurus/theme-common';
10
+ import Link from '@docusaurus/Link';
11
+ import isInternalUrl from '@docusaurus/isInternalUrl';
12
+ import IconExternalLink from '@theme/IconExternalLink';
13
+ import styles from './Link.module.css';
14
+ export default function DocSidebarItemLink({
15
+ item,
16
+ onItemClick,
17
+ activePath,
18
+ level,
19
+ index,
20
+ ...props
21
+ }) {
22
+ const {href, label, className} = item;
23
+ const isActive = isActiveSidebarItem(item, activePath);
24
+ const isInternalLink = isInternalUrl(href);
25
+ return (
26
+ <li
27
+ className={clsx(
28
+ ThemeClassNames.docs.docSidebarItemLink,
29
+ ThemeClassNames.docs.docSidebarItemLinkLevel(level),
30
+ 'menu__list-item',
31
+ className,
32
+ )}
33
+ key={label}>
34
+ <Link
35
+ className={clsx(
36
+ 'menu__link',
37
+ !isInternalLink && styles.menuExternalLink,
38
+ {
39
+ 'menu__link--active': isActive,
40
+ },
41
+ )}
42
+ aria-current={isActive ? 'page' : undefined}
43
+ to={href}
44
+ {...(isInternalLink && {
45
+ onClick: onItemClick ? () => onItemClick(item) : undefined,
46
+ })}
47
+ {...props}>
48
+ {label}
49
+ {!isInternalLink && <IconExternalLink />}
50
+ </Link>
51
+ </li>
52
+ );
53
+ }
@@ -0,0 +1,10 @@
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
+ .menuExternalLink {
9
+ align-items: center;
10
+ }
@@ -4,26 +4,10 @@
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 React, {useEffect, useMemo} from 'react';
8
- import clsx from 'clsx';
9
- import {
10
- isActiveSidebarItem,
11
- usePrevious,
12
- Collapsible,
13
- useCollapsible,
14
- findFirstCategoryLink,
15
- ThemeClassNames,
16
- useThemeConfig,
17
- useDocSidebarItemsExpandedState,
18
- isSamePath,
19
- } from '@docusaurus/theme-common';
20
- import Link from '@docusaurus/Link';
21
- import isInternalUrl from '@docusaurus/isInternalUrl';
22
- import {translate} from '@docusaurus/Translate';
23
- import IconExternalLink from '@theme/IconExternalLink';
24
- import DocSidebarItems from '@theme/DocSidebarItems';
25
- import styles from './styles.module.css';
26
- import useIsBrowser from '@docusaurus/useIsBrowser';
7
+ import React from 'react';
8
+ import DocSidebarItemCategory from '@theme/DocSidebarItem/Category';
9
+ import DocSidebarItemLink from '@theme/DocSidebarItem/Link';
10
+ import DocSidebarItemHtml from '@theme/DocSidebarItem/Html';
27
11
  export default function DocSidebarItem({item, ...props}) {
28
12
  switch (item.type) {
29
13
  case 'category':
@@ -36,230 +20,4 @@ export default function DocSidebarItem({item, ...props}) {
36
20
  default:
37
21
  return <DocSidebarItemLink item={item} {...props} />;
38
22
  }
39
- } // If we navigate to a category and it becomes active, it should automatically
40
- // expand itself
41
-
42
- function useAutoExpandActiveCategory({isActive, collapsed, setCollapsed}) {
43
- const wasActive = usePrevious(isActive);
44
- useEffect(() => {
45
- const justBecameActive = isActive && !wasActive;
46
-
47
- if (justBecameActive && collapsed) {
48
- setCollapsed(false);
49
- }
50
- }, [isActive, wasActive, collapsed, setCollapsed]);
51
- }
52
- /**
53
- * When a collapsible category has no link, we still link it to its first child
54
- * during SSR as a temporary fallback. This allows to be able to navigate inside
55
- * the category even when JS fails to load, is delayed or simply disabled
56
- * React hydration becomes an optional progressive enhancement
57
- * see https://github.com/facebookincubator/infima/issues/36#issuecomment-772543188
58
- * see https://github.com/facebook/docusaurus/issues/3030
59
- */
60
-
61
- function useCategoryHrefWithSSRFallback(item) {
62
- const isBrowser = useIsBrowser();
63
- return useMemo(() => {
64
- if (item.href) {
65
- return item.href;
66
- } // In these cases, it's not necessary to render a fallback
67
- // We skip the "findFirstCategoryLink" computation
68
-
69
- if (isBrowser || !item.collapsible) {
70
- return undefined;
71
- }
72
-
73
- return findFirstCategoryLink(item);
74
- }, [item, isBrowser]);
75
- }
76
-
77
- function DocSidebarItemCategory({
78
- item,
79
- onItemClick,
80
- activePath,
81
- level,
82
- index,
83
- ...props
84
- }) {
85
- const {items, label, collapsible, className, href} = item;
86
- const hrefWithSSRFallback = useCategoryHrefWithSSRFallback(item);
87
- const isActive = isActiveSidebarItem(item, activePath);
88
- const isCurrentPage = isSamePath(href, activePath);
89
- const {collapsed, setCollapsed} = useCollapsible({
90
- // active categories are always initialized as expanded
91
- // the default (item.collapsed) is only used for non-active categories
92
- initialState: () => {
93
- if (!collapsible) {
94
- return false;
95
- }
96
-
97
- return isActive ? false : item.collapsed;
98
- },
99
- });
100
- useAutoExpandActiveCategory({
101
- isActive,
102
- collapsed,
103
- setCollapsed,
104
- });
105
- const {expandedItem, setExpandedItem} = useDocSidebarItemsExpandedState();
106
-
107
- function updateCollapsed(toCollapsed = !collapsed) {
108
- setExpandedItem(toCollapsed ? null : index);
109
- setCollapsed(toCollapsed);
110
- }
111
-
112
- const {autoCollapseSidebarCategories} = useThemeConfig();
113
- useEffect(() => {
114
- if (
115
- collapsible &&
116
- expandedItem &&
117
- expandedItem !== index &&
118
- autoCollapseSidebarCategories
119
- ) {
120
- setCollapsed(true);
121
- }
122
- }, [
123
- collapsible,
124
- expandedItem,
125
- index,
126
- setCollapsed,
127
- autoCollapseSidebarCategories,
128
- ]);
129
- return (
130
- <li
131
- className={clsx(
132
- ThemeClassNames.docs.docSidebarItemCategory,
133
- ThemeClassNames.docs.docSidebarItemCategoryLevel(level),
134
- 'menu__list-item',
135
- {
136
- 'menu__list-item--collapsed': collapsed,
137
- },
138
- className,
139
- )}>
140
- <div
141
- className={clsx('menu__list-item-collapsible', {
142
- 'menu__list-item-collapsible--active': isCurrentPage,
143
- })}>
144
- <Link
145
- className={clsx('menu__link', {
146
- 'menu__link--sublist': collapsible,
147
- 'menu__link--sublist-caret': !href,
148
- 'menu__link--active': isActive,
149
- })}
150
- onClick={
151
- collapsible
152
- ? (e) => {
153
- onItemClick?.(item);
154
-
155
- if (href) {
156
- updateCollapsed(false);
157
- } else {
158
- e.preventDefault();
159
- updateCollapsed();
160
- }
161
- }
162
- : () => {
163
- onItemClick?.(item);
164
- }
165
- }
166
- aria-current={isCurrentPage ? 'page' : undefined}
167
- aria-expanded={collapsible ? !collapsed : undefined}
168
- href={collapsible ? hrefWithSSRFallback ?? '#' : hrefWithSSRFallback}
169
- {...props}>
170
- {label}
171
- </Link>
172
- {href && collapsible && (
173
- <button
174
- aria-label={translate(
175
- {
176
- id: 'theme.DocSidebarItem.toggleCollapsedCategoryAriaLabel',
177
- message: "Toggle the collapsible sidebar category '{label}'",
178
- description:
179
- 'The ARIA label to toggle the collapsible sidebar category',
180
- },
181
- {
182
- label,
183
- },
184
- )}
185
- type="button"
186
- className="clean-btn menu__caret"
187
- onClick={(e) => {
188
- e.preventDefault();
189
- updateCollapsed();
190
- }}
191
- />
192
- )}
193
- </div>
194
-
195
- <Collapsible lazy as="ul" className="menu__list" collapsed={collapsed}>
196
- <DocSidebarItems
197
- items={items}
198
- tabIndex={collapsed ? -1 : 0}
199
- onItemClick={onItemClick}
200
- activePath={activePath}
201
- level={level + 1}
202
- />
203
- </Collapsible>
204
- </li>
205
- );
206
- }
207
-
208
- function DocSidebarItemHtml({item, level, index}) {
209
- const {value, defaultStyle, className} = item;
210
- return (
211
- <li
212
- className={clsx(
213
- ThemeClassNames.docs.docSidebarItemLink,
214
- ThemeClassNames.docs.docSidebarItemLinkLevel(level),
215
- defaultStyle && `${styles.menuHtmlItem} menu__list-item`,
216
- className,
217
- )}
218
- key={index} // eslint-disable-next-line react/no-danger
219
- dangerouslySetInnerHTML={{
220
- __html: value,
221
- }}
222
- />
223
- );
224
- }
225
-
226
- function DocSidebarItemLink({
227
- item,
228
- onItemClick,
229
- activePath,
230
- level,
231
- index,
232
- ...props
233
- }) {
234
- const {href, label, className} = item;
235
- const isActive = isActiveSidebarItem(item, activePath);
236
- const isInternalLink = isInternalUrl(href);
237
- return (
238
- <li
239
- className={clsx(
240
- ThemeClassNames.docs.docSidebarItemLink,
241
- ThemeClassNames.docs.docSidebarItemLinkLevel(level),
242
- 'menu__list-item',
243
- className,
244
- )}
245
- key={label}>
246
- <Link
247
- className={clsx(
248
- 'menu__link',
249
- !isInternalLink && styles.menuExternalLink,
250
- {
251
- 'menu__link--active': isActive,
252
- },
253
- )}
254
- aria-current={isActive ? 'page' : undefined}
255
- to={href}
256
- {...(isInternalLink && {
257
- onClick: onItemClick ? () => onItemClick(item) : undefined,
258
- })}
259
- {...props}>
260
- {label}
261
- {!isInternalLink && <IconExternalLink />}
262
- </Link>
263
- </li>
264
- );
265
23
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docusaurus/theme-classic",
3
- "version": "0.0.0-4776",
3
+ "version": "0.0.0-4781",
4
4
  "description": "Classic theme for Docusaurus",
5
5
  "main": "lib/index.js",
6
6
  "types": "src/theme-classic.d.ts",
@@ -21,15 +21,15 @@
21
21
  "format:lib-next": "prettier --config ../../.prettierrc --write \"lib-next/**/*.{js,ts,jsx,tsc}\""
22
22
  },
23
23
  "dependencies": {
24
- "@docusaurus/core": "0.0.0-4776",
25
- "@docusaurus/plugin-content-blog": "0.0.0-4776",
26
- "@docusaurus/plugin-content-docs": "0.0.0-4776",
27
- "@docusaurus/plugin-content-pages": "0.0.0-4776",
28
- "@docusaurus/theme-common": "0.0.0-4776",
29
- "@docusaurus/theme-translations": "0.0.0-4776",
30
- "@docusaurus/utils": "0.0.0-4776",
31
- "@docusaurus/utils-common": "0.0.0-4776",
32
- "@docusaurus/utils-validation": "0.0.0-4776",
24
+ "@docusaurus/core": "0.0.0-4781",
25
+ "@docusaurus/plugin-content-blog": "0.0.0-4781",
26
+ "@docusaurus/plugin-content-docs": "0.0.0-4781",
27
+ "@docusaurus/plugin-content-pages": "0.0.0-4781",
28
+ "@docusaurus/theme-common": "0.0.0-4781",
29
+ "@docusaurus/theme-translations": "0.0.0-4781",
30
+ "@docusaurus/utils": "0.0.0-4781",
31
+ "@docusaurus/utils-common": "0.0.0-4781",
32
+ "@docusaurus/utils-validation": "0.0.0-4781",
33
33
  "@mdx-js/react": "^1.6.22",
34
34
  "clsx": "^1.1.1",
35
35
  "copy-text-to-clipboard": "^3.0.1",
@@ -42,8 +42,8 @@
42
42
  "rtlcss": "^3.5.0"
43
43
  },
44
44
  "devDependencies": {
45
- "@docusaurus/module-type-aliases": "0.0.0-4776",
46
- "@docusaurus/types": "0.0.0-4776",
45
+ "@docusaurus/module-type-aliases": "0.0.0-4781",
46
+ "@docusaurus/types": "0.0.0-4781",
47
47
  "@types/mdx-js__react": "^1.5.5",
48
48
  "@types/prismjs": "^1.26.0",
49
49
  "@types/rtlcss": "^3.1.2",
@@ -59,5 +59,5 @@
59
59
  "engines": {
60
60
  "node": ">=14"
61
61
  },
62
- "gitHead": "e90f70f332f44d7c29a5a363552d9cf0136f6b23"
62
+ "gitHead": "0eece99f805f62b68000d0eef1191ac5855570d8"
63
63
  }
@@ -0,0 +1,212 @@
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, {type ComponentProps, useEffect, useMemo} from 'react';
9
+ import clsx from 'clsx';
10
+ import {
11
+ isActiveSidebarItem,
12
+ usePrevious,
13
+ Collapsible,
14
+ useCollapsible,
15
+ findFirstCategoryLink,
16
+ ThemeClassNames,
17
+ useThemeConfig,
18
+ useDocSidebarItemsExpandedState,
19
+ isSamePath,
20
+ } from '@docusaurus/theme-common';
21
+ import Link from '@docusaurus/Link';
22
+ import {translate} from '@docusaurus/Translate';
23
+
24
+ import DocSidebarItems from '@theme/DocSidebarItems';
25
+ import type {Props} from '@theme/DocSidebarItem/Category';
26
+
27
+ import useIsBrowser from '@docusaurus/useIsBrowser';
28
+
29
+ // If we navigate to a category and it becomes active, it should automatically
30
+ // expand itself
31
+ function useAutoExpandActiveCategory({
32
+ isActive,
33
+ collapsed,
34
+ setCollapsed,
35
+ }: {
36
+ isActive: boolean;
37
+ collapsed: boolean;
38
+ setCollapsed: (b: boolean) => void;
39
+ }) {
40
+ const wasActive = usePrevious(isActive);
41
+ useEffect(() => {
42
+ const justBecameActive = isActive && !wasActive;
43
+ if (justBecameActive && collapsed) {
44
+ setCollapsed(false);
45
+ }
46
+ }, [isActive, wasActive, collapsed, setCollapsed]);
47
+ }
48
+
49
+ /**
50
+ * When a collapsible category has no link, we still link it to its first child
51
+ * during SSR as a temporary fallback. This allows to be able to navigate inside
52
+ * the category even when JS fails to load, is delayed or simply disabled
53
+ * React hydration becomes an optional progressive enhancement
54
+ * see https://github.com/facebookincubator/infima/issues/36#issuecomment-772543188
55
+ * see https://github.com/facebook/docusaurus/issues/3030
56
+ */
57
+ function useCategoryHrefWithSSRFallback(
58
+ item: Props['item'],
59
+ ): string | undefined {
60
+ const isBrowser = useIsBrowser();
61
+ return useMemo(() => {
62
+ if (item.href) {
63
+ return item.href;
64
+ }
65
+ // In these cases, it's not necessary to render a fallback
66
+ // We skip the "findFirstCategoryLink" computation
67
+ if (isBrowser || !item.collapsible) {
68
+ return undefined;
69
+ }
70
+ return findFirstCategoryLink(item);
71
+ }, [item, isBrowser]);
72
+ }
73
+
74
+ function CollapseButton({
75
+ categoryLabel,
76
+ onClick,
77
+ }: {
78
+ categoryLabel: string;
79
+ onClick: ComponentProps<'button'>['onClick'];
80
+ }) {
81
+ return (
82
+ <button
83
+ aria-label={translate(
84
+ {
85
+ id: 'theme.DocSidebarItem.toggleCollapsedCategoryAriaLabel',
86
+ message: "Toggle the collapsible sidebar category '{label}'",
87
+ description:
88
+ 'The ARIA label to toggle the collapsible sidebar category',
89
+ },
90
+ {label: categoryLabel},
91
+ )}
92
+ type="button"
93
+ className="clean-btn menu__caret"
94
+ onClick={onClick}
95
+ />
96
+ );
97
+ }
98
+
99
+ export default function DocSidebarItemCategory({
100
+ item,
101
+ onItemClick,
102
+ activePath,
103
+ level,
104
+ index,
105
+ ...props
106
+ }: Props): JSX.Element {
107
+ const {items, label, collapsible, className, href} = item;
108
+ const hrefWithSSRFallback = useCategoryHrefWithSSRFallback(item);
109
+
110
+ const isActive = isActiveSidebarItem(item, activePath);
111
+ const isCurrentPage = isSamePath(href, activePath);
112
+
113
+ const {collapsed, setCollapsed} = useCollapsible({
114
+ // active categories are always initialized as expanded
115
+ // the default (item.collapsed) is only used for non-active categories
116
+ initialState: () => {
117
+ if (!collapsible) {
118
+ return false;
119
+ }
120
+ return isActive ? false : item.collapsed;
121
+ },
122
+ });
123
+
124
+ useAutoExpandActiveCategory({isActive, collapsed, setCollapsed});
125
+ const {expandedItem, setExpandedItem} = useDocSidebarItemsExpandedState();
126
+ function updateCollapsed(toCollapsed: boolean = !collapsed) {
127
+ setExpandedItem(toCollapsed ? null : index);
128
+ setCollapsed(toCollapsed);
129
+ }
130
+ const {autoCollapseSidebarCategories} = useThemeConfig();
131
+ useEffect(() => {
132
+ if (
133
+ collapsible &&
134
+ expandedItem &&
135
+ expandedItem !== index &&
136
+ autoCollapseSidebarCategories
137
+ ) {
138
+ setCollapsed(true);
139
+ }
140
+ }, [
141
+ collapsible,
142
+ expandedItem,
143
+ index,
144
+ setCollapsed,
145
+ autoCollapseSidebarCategories,
146
+ ]);
147
+
148
+ return (
149
+ <li
150
+ className={clsx(
151
+ ThemeClassNames.docs.docSidebarItemCategory,
152
+ ThemeClassNames.docs.docSidebarItemCategoryLevel(level),
153
+ 'menu__list-item',
154
+ {
155
+ 'menu__list-item--collapsed': collapsed,
156
+ },
157
+ className,
158
+ )}>
159
+ <div
160
+ className={clsx('menu__list-item-collapsible', {
161
+ 'menu__list-item-collapsible--active': isCurrentPage,
162
+ })}>
163
+ <Link
164
+ className={clsx('menu__link', {
165
+ 'menu__link--sublist': collapsible,
166
+ 'menu__link--sublist-caret': !href,
167
+ 'menu__link--active': isActive,
168
+ })}
169
+ onClick={
170
+ collapsible
171
+ ? (e) => {
172
+ onItemClick?.(item);
173
+ if (href) {
174
+ updateCollapsed(false);
175
+ } else {
176
+ e.preventDefault();
177
+ updateCollapsed();
178
+ }
179
+ }
180
+ : () => {
181
+ onItemClick?.(item);
182
+ }
183
+ }
184
+ aria-current={isCurrentPage ? 'page' : undefined}
185
+ aria-expanded={collapsible ? !collapsed : undefined}
186
+ href={collapsible ? hrefWithSSRFallback ?? '#' : hrefWithSSRFallback}
187
+ {...props}>
188
+ {label}
189
+ </Link>
190
+ {href && collapsible && (
191
+ <CollapseButton
192
+ categoryLabel={label}
193
+ onClick={(e) => {
194
+ e.preventDefault();
195
+ updateCollapsed();
196
+ }}
197
+ />
198
+ )}
199
+ </div>
200
+
201
+ <Collapsible lazy as="ul" className="menu__list" collapsed={collapsed}>
202
+ <DocSidebarItems
203
+ items={items}
204
+ tabIndex={collapsed ? -1 : 0}
205
+ onItemClick={onItemClick}
206
+ activePath={activePath}
207
+ level={level + 1}
208
+ />
209
+ </Collapsible>
210
+ </li>
211
+ );
212
+ }
@@ -11,7 +11,3 @@
11
11
  var(--ifm-menu-link-padding-horizontal);
12
12
  }
13
13
  }
14
-
15
- .menuExternalLink {
16
- align-items: center;
17
- }
@@ -0,0 +1,34 @@
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 from 'react';
9
+ import clsx from 'clsx';
10
+ import {ThemeClassNames} from '@docusaurus/theme-common';
11
+ import type {Props} from '@theme/DocSidebarItem/Html';
12
+
13
+ import styles from './Html.module.css';
14
+
15
+ export default function DocSidebarItemHtml({
16
+ item,
17
+ level,
18
+ index,
19
+ }: Props): JSX.Element {
20
+ const {value, defaultStyle, className} = item;
21
+ return (
22
+ <li
23
+ className={clsx(
24
+ ThemeClassNames.docs.docSidebarItemLink,
25
+ ThemeClassNames.docs.docSidebarItemLinkLevel(level),
26
+ defaultStyle && `${styles.menuHtmlItem} menu__list-item`,
27
+ className,
28
+ )}
29
+ key={index}
30
+ // eslint-disable-next-line react/no-danger
31
+ dangerouslySetInnerHTML={{__html: value}}
32
+ />
33
+ );
34
+ }
@@ -0,0 +1,10 @@
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
+ .menuExternalLink {
9
+ align-items: center;
10
+ }
@@ -0,0 +1,58 @@
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 from 'react';
9
+ import clsx from 'clsx';
10
+ import {isActiveSidebarItem, ThemeClassNames} from '@docusaurus/theme-common';
11
+ import Link from '@docusaurus/Link';
12
+ import isInternalUrl from '@docusaurus/isInternalUrl';
13
+ import IconExternalLink from '@theme/IconExternalLink';
14
+
15
+ import type {Props} from '@theme/DocSidebarItem/Link';
16
+
17
+ import styles from './Link.module.css';
18
+
19
+ export default function DocSidebarItemLink({
20
+ item,
21
+ onItemClick,
22
+ activePath,
23
+ level,
24
+ index,
25
+ ...props
26
+ }: Props): JSX.Element {
27
+ const {href, label, className} = item;
28
+ const isActive = isActiveSidebarItem(item, activePath);
29
+ const isInternalLink = isInternalUrl(href);
30
+ return (
31
+ <li
32
+ className={clsx(
33
+ ThemeClassNames.docs.docSidebarItemLink,
34
+ ThemeClassNames.docs.docSidebarItemLinkLevel(level),
35
+ 'menu__list-item',
36
+ className,
37
+ )}
38
+ key={label}>
39
+ <Link
40
+ className={clsx(
41
+ 'menu__link',
42
+ !isInternalLink && styles.menuExternalLink,
43
+ {
44
+ 'menu__link--active': isActive,
45
+ },
46
+ )}
47
+ aria-current={isActive ? 'page' : undefined}
48
+ to={href}
49
+ {...(isInternalLink && {
50
+ onClick: onItemClick ? () => onItemClick(item) : undefined,
51
+ })}
52
+ {...props}>
53
+ {label}
54
+ {!isInternalLink && <IconExternalLink />}
55
+ </Link>
56
+ </li>
57
+ );
58
+ }
@@ -5,34 +5,11 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import React, {useEffect, useMemo} from 'react';
9
- import clsx from 'clsx';
10
- import {
11
- isActiveSidebarItem,
12
- usePrevious,
13
- Collapsible,
14
- useCollapsible,
15
- findFirstCategoryLink,
16
- ThemeClassNames,
17
- useThemeConfig,
18
- useDocSidebarItemsExpandedState,
19
- isSamePath,
20
- } from '@docusaurus/theme-common';
21
- import Link from '@docusaurus/Link';
22
- import isInternalUrl from '@docusaurus/isInternalUrl';
23
- import {translate} from '@docusaurus/Translate';
24
- import IconExternalLink from '@theme/IconExternalLink';
25
-
26
- import DocSidebarItems from '@theme/DocSidebarItems';
8
+ import React from 'react';
9
+ import DocSidebarItemCategory from '@theme/DocSidebarItem/Category';
10
+ import DocSidebarItemLink from '@theme/DocSidebarItem/Link';
11
+ import DocSidebarItemHtml from '@theme/DocSidebarItem/Html';
27
12
  import type {Props} from '@theme/DocSidebarItem';
28
- import type {
29
- PropSidebarItemCategory,
30
- PropSidebarItemLink,
31
- } from '@docusaurus/plugin-content-docs';
32
-
33
- import styles from './styles.module.css';
34
- import useIsBrowser from '@docusaurus/useIsBrowser';
35
- import type {SidebarItemHtml} from '@docusaurus/plugin-content-docs/src/sidebars/types';
36
13
 
37
14
  export default function DocSidebarItem({
38
15
  item,
@@ -48,235 +25,3 @@ export default function DocSidebarItem({
48
25
  return <DocSidebarItemLink item={item} {...props} />;
49
26
  }
50
27
  }
51
-
52
- // If we navigate to a category and it becomes active, it should automatically
53
- // expand itself
54
- function useAutoExpandActiveCategory({
55
- isActive,
56
- collapsed,
57
- setCollapsed,
58
- }: {
59
- isActive: boolean;
60
- collapsed: boolean;
61
- setCollapsed: (b: boolean) => void;
62
- }) {
63
- const wasActive = usePrevious(isActive);
64
- useEffect(() => {
65
- const justBecameActive = isActive && !wasActive;
66
- if (justBecameActive && collapsed) {
67
- setCollapsed(false);
68
- }
69
- }, [isActive, wasActive, collapsed, setCollapsed]);
70
- }
71
-
72
- /**
73
- * When a collapsible category has no link, we still link it to its first child
74
- * during SSR as a temporary fallback. This allows to be able to navigate inside
75
- * the category even when JS fails to load, is delayed or simply disabled
76
- * React hydration becomes an optional progressive enhancement
77
- * see https://github.com/facebookincubator/infima/issues/36#issuecomment-772543188
78
- * see https://github.com/facebook/docusaurus/issues/3030
79
- */
80
- function useCategoryHrefWithSSRFallback(
81
- item: PropSidebarItemCategory,
82
- ): string | undefined {
83
- const isBrowser = useIsBrowser();
84
- return useMemo(() => {
85
- if (item.href) {
86
- return item.href;
87
- }
88
- // In these cases, it's not necessary to render a fallback
89
- // We skip the "findFirstCategoryLink" computation
90
- if (isBrowser || !item.collapsible) {
91
- return undefined;
92
- }
93
- return findFirstCategoryLink(item);
94
- }, [item, isBrowser]);
95
- }
96
-
97
- function DocSidebarItemCategory({
98
- item,
99
- onItemClick,
100
- activePath,
101
- level,
102
- index,
103
- ...props
104
- }: Props & {item: PropSidebarItemCategory}) {
105
- const {items, label, collapsible, className, href} = item;
106
- const hrefWithSSRFallback = useCategoryHrefWithSSRFallback(item);
107
-
108
- const isActive = isActiveSidebarItem(item, activePath);
109
- const isCurrentPage = isSamePath(href, activePath);
110
-
111
- const {collapsed, setCollapsed} = useCollapsible({
112
- // active categories are always initialized as expanded
113
- // the default (item.collapsed) is only used for non-active categories
114
- initialState: () => {
115
- if (!collapsible) {
116
- return false;
117
- }
118
- return isActive ? false : item.collapsed;
119
- },
120
- });
121
-
122
- useAutoExpandActiveCategory({isActive, collapsed, setCollapsed});
123
- const {expandedItem, setExpandedItem} = useDocSidebarItemsExpandedState();
124
- function updateCollapsed(toCollapsed: boolean = !collapsed) {
125
- setExpandedItem(toCollapsed ? null : index);
126
- setCollapsed(toCollapsed);
127
- }
128
- const {autoCollapseSidebarCategories} = useThemeConfig();
129
- useEffect(() => {
130
- if (
131
- collapsible &&
132
- expandedItem &&
133
- expandedItem !== index &&
134
- autoCollapseSidebarCategories
135
- ) {
136
- setCollapsed(true);
137
- }
138
- }, [
139
- collapsible,
140
- expandedItem,
141
- index,
142
- setCollapsed,
143
- autoCollapseSidebarCategories,
144
- ]);
145
-
146
- return (
147
- <li
148
- className={clsx(
149
- ThemeClassNames.docs.docSidebarItemCategory,
150
- ThemeClassNames.docs.docSidebarItemCategoryLevel(level),
151
- 'menu__list-item',
152
- {
153
- 'menu__list-item--collapsed': collapsed,
154
- },
155
- className,
156
- )}>
157
- <div
158
- className={clsx('menu__list-item-collapsible', {
159
- 'menu__list-item-collapsible--active': isCurrentPage,
160
- })}>
161
- <Link
162
- className={clsx('menu__link', {
163
- 'menu__link--sublist': collapsible,
164
- 'menu__link--sublist-caret': !href,
165
- 'menu__link--active': isActive,
166
- })}
167
- onClick={
168
- collapsible
169
- ? (e) => {
170
- onItemClick?.(item);
171
- if (href) {
172
- updateCollapsed(false);
173
- } else {
174
- e.preventDefault();
175
- updateCollapsed();
176
- }
177
- }
178
- : () => {
179
- onItemClick?.(item);
180
- }
181
- }
182
- aria-current={isCurrentPage ? 'page' : undefined}
183
- aria-expanded={collapsible ? !collapsed : undefined}
184
- href={collapsible ? hrefWithSSRFallback ?? '#' : hrefWithSSRFallback}
185
- {...props}>
186
- {label}
187
- </Link>
188
- {href && collapsible && (
189
- <button
190
- aria-label={translate(
191
- {
192
- id: 'theme.DocSidebarItem.toggleCollapsedCategoryAriaLabel',
193
- message: "Toggle the collapsible sidebar category '{label}'",
194
- description:
195
- 'The ARIA label to toggle the collapsible sidebar category',
196
- },
197
- {label},
198
- )}
199
- type="button"
200
- className="clean-btn menu__caret"
201
- onClick={(e) => {
202
- e.preventDefault();
203
- updateCollapsed();
204
- }}
205
- />
206
- )}
207
- </div>
208
-
209
- <Collapsible lazy as="ul" className="menu__list" collapsed={collapsed}>
210
- <DocSidebarItems
211
- items={items}
212
- tabIndex={collapsed ? -1 : 0}
213
- onItemClick={onItemClick}
214
- activePath={activePath}
215
- level={level + 1}
216
- />
217
- </Collapsible>
218
- </li>
219
- );
220
- }
221
-
222
- function DocSidebarItemHtml({
223
- item,
224
- level,
225
- index,
226
- }: Props & {item: SidebarItemHtml}) {
227
- const {value, defaultStyle, className} = item;
228
- return (
229
- <li
230
- className={clsx(
231
- ThemeClassNames.docs.docSidebarItemLink,
232
- ThemeClassNames.docs.docSidebarItemLinkLevel(level),
233
- defaultStyle && `${styles.menuHtmlItem} menu__list-item`,
234
- className,
235
- )}
236
- key={index}
237
- // eslint-disable-next-line react/no-danger
238
- dangerouslySetInnerHTML={{__html: value}}
239
- />
240
- );
241
- }
242
-
243
- function DocSidebarItemLink({
244
- item,
245
- onItemClick,
246
- activePath,
247
- level,
248
- index,
249
- ...props
250
- }: Props & {item: PropSidebarItemLink}) {
251
- const {href, label, className} = item;
252
- const isActive = isActiveSidebarItem(item, activePath);
253
- const isInternalLink = isInternalUrl(href);
254
- return (
255
- <li
256
- className={clsx(
257
- ThemeClassNames.docs.docSidebarItemLink,
258
- ThemeClassNames.docs.docSidebarItemLinkLevel(level),
259
- 'menu__list-item',
260
- className,
261
- )}
262
- key={label}>
263
- <Link
264
- className={clsx(
265
- 'menu__link',
266
- !isInternalLink && styles.menuExternalLink,
267
- {
268
- 'menu__link--active': isActive,
269
- },
270
- )}
271
- aria-current={isActive ? 'page' : undefined}
272
- to={href}
273
- {...(isInternalLink && {
274
- onClick: onItemClick ? () => onItemClick(item) : undefined,
275
- })}
276
- {...props}>
277
- {label}
278
- {!isInternalLink && <IconExternalLink />}
279
- </Link>
280
- </li>
281
- );
282
- }
@@ -235,6 +235,40 @@ declare module '@theme/DocSidebarItem' {
235
235
  export default function DocSidebarItem(props: Props): JSX.Element;
236
236
  }
237
237
 
238
+ declare module '@theme/DocSidebarItem/Link' {
239
+ import type {Props as DocSidebarItemProps} from '@theme/DocSidebarItem';
240
+
241
+ import type {PropSidebarItemLink} from '@docusaurus/plugin-content-docs';
242
+
243
+ export interface Props extends DocSidebarItemProps {
244
+ item: PropSidebarItemLink;
245
+ }
246
+
247
+ export default function DocSidebarItemLink(props: Props): JSX.Element;
248
+ }
249
+
250
+ declare module '@theme/DocSidebarItem/Html' {
251
+ import type {Props as DocSidebarItemProps} from '@theme/DocSidebarItem';
252
+ import type {PropSidebarItemHtml} from '@docusaurus/plugin-content-docs';
253
+
254
+ export interface Props extends DocSidebarItemProps {
255
+ item: PropSidebarItemHtml;
256
+ }
257
+
258
+ export default function DocSidebarItemHtml(props: Props): JSX.Element;
259
+ }
260
+
261
+ declare module '@theme/DocSidebarItem/Category' {
262
+ import type {Props as DocSidebarItemProps} from '@theme/DocSidebarItem';
263
+ import type {PropSidebarItemCategory} from '@docusaurus/plugin-content-docs';
264
+
265
+ export interface Props extends DocSidebarItemProps {
266
+ item: PropSidebarItemCategory;
267
+ }
268
+
269
+ export default function DocSidebarItemCategory(props: Props): JSX.Element;
270
+ }
271
+
238
272
  declare module '@theme/DocSidebarItems' {
239
273
  import type {Props as DocSidebarItemProps} from '@theme/DocSidebarItem';
240
274
  import type {PropSidebarItem} from '@docusaurus/plugin-content-docs';