@docusaurus/theme-classic 0.0.0-4778 → 0.0.0-4783

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.
@@ -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';