@docusaurus/theme-classic 0.0.0-4777 → 0.0.0-4782

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,145 +5,30 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import React, {type ReactNode, useState, useCallback} from 'react';
8
+ import React from 'react';
9
9
  import renderRoutes from '@docusaurus/renderRoutes';
10
- import type {PropVersionMetadata} from '@docusaurus/plugin-content-docs';
11
- import Layout from '@theme/Layout';
12
- import DocSidebar from '@theme/DocSidebar';
10
+ import type {PropSidebar} from '@docusaurus/plugin-content-docs';
13
11
  import NotFound from '@theme/NotFound';
14
- import type {DocumentRoute} from '@theme/DocItem';
15
12
  import type {Props} from '@theme/DocPage';
16
- import IconArrow from '@theme/IconArrow';
17
- import BackToTopButton from '@theme/BackToTopButton';
13
+ import DocPageLayout from '@theme/DocPage/Layout';
18
14
  import {matchPath} from '@docusaurus/router';
19
- import {translate} from '@docusaurus/Translate';
20
15
 
21
16
  import clsx from 'clsx';
22
- import styles from './styles.module.css';
23
17
 
24
18
  import {
25
19
  HtmlClassNameProvider,
26
20
  ThemeClassNames,
27
21
  docVersionSearchTag,
28
22
  DocsSidebarProvider,
29
- useDocsSidebar,
30
23
  DocsVersionProvider,
31
24
  } from '@docusaurus/theme-common';
32
25
  import SearchMetadata from '@theme/SearchMetadata';
33
26
 
34
- type DocPageContentProps = {
35
- readonly currentDocRoute: DocumentRoute;
36
- readonly versionMetadata: PropVersionMetadata;
37
- readonly children: ReactNode;
38
- readonly sidebarName: string | undefined;
39
- };
40
-
41
- function DocPageContent({
42
- currentDocRoute,
43
- versionMetadata,
44
- children,
45
- sidebarName,
46
- }: DocPageContentProps): JSX.Element {
47
- const sidebar = useDocsSidebar();
48
- const {pluginId, version} = versionMetadata;
49
- const [hiddenSidebarContainer, setHiddenSidebarContainer] = useState(false);
50
- const [hiddenSidebar, setHiddenSidebar] = useState(false);
51
- const toggleSidebar = useCallback(() => {
52
- if (hiddenSidebar) {
53
- setHiddenSidebar(false);
54
- }
55
-
56
- setHiddenSidebarContainer((value) => !value);
57
- }, [hiddenSidebar]);
58
-
59
- return (
60
- <>
61
- <SearchMetadata
62
- version={version}
63
- tag={docVersionSearchTag(pluginId, version)}
64
- />
65
- <Layout>
66
- <div className={styles.docPage}>
67
- <BackToTopButton />
68
-
69
- {sidebar && (
70
- <aside
71
- className={clsx(
72
- ThemeClassNames.docs.docSidebarContainer,
73
- styles.docSidebarContainer,
74
- hiddenSidebarContainer && styles.docSidebarContainerHidden,
75
- )}
76
- onTransitionEnd={(e) => {
77
- if (
78
- !e.currentTarget.classList.contains(
79
- styles.docSidebarContainer!,
80
- )
81
- ) {
82
- return;
83
- }
84
-
85
- if (hiddenSidebarContainer) {
86
- setHiddenSidebar(true);
87
- }
88
- }}>
89
- <DocSidebar
90
- key={
91
- // Reset sidebar state on sidebar changes
92
- // See https://github.com/facebook/docusaurus/issues/3414
93
- sidebarName
94
- }
95
- sidebar={sidebar}
96
- path={currentDocRoute.path}
97
- onCollapse={toggleSidebar}
98
- isHidden={hiddenSidebar}
99
- />
100
-
101
- {hiddenSidebar && (
102
- <div
103
- className={styles.collapsedDocSidebar}
104
- title={translate({
105
- id: 'theme.docs.sidebar.expandButtonTitle',
106
- message: 'Expand sidebar',
107
- description:
108
- 'The ARIA label and title attribute for expand button of doc sidebar',
109
- })}
110
- aria-label={translate({
111
- id: 'theme.docs.sidebar.expandButtonAriaLabel',
112
- message: 'Expand sidebar',
113
- description:
114
- 'The ARIA label and title attribute for expand button of doc sidebar',
115
- })}
116
- tabIndex={0}
117
- role="button"
118
- onKeyDown={toggleSidebar}
119
- onClick={toggleSidebar}>
120
- <IconArrow className={styles.expandSidebarButtonIcon} />
121
- </div>
122
- )}
123
- </aside>
124
- )}
125
- <main
126
- className={clsx(
127
- styles.docMainContainer,
128
- (hiddenSidebarContainer || !sidebar) &&
129
- styles.docMainContainerEnhanced,
130
- )}>
131
- <div
132
- className={clsx(
133
- 'container padding-top--md padding-bottom--lg',
134
- styles.docItemWrapper,
135
- hiddenSidebarContainer && styles.docItemWrapperEnhanced,
136
- )}>
137
- {children}
138
- </div>
139
- </main>
140
- </div>
141
- </Layout>
142
- </>
143
- );
144
- }
145
-
146
- export default function DocPage(props: Props): JSX.Element {
27
+ function extractDocRouteMetadata(props: Props): null | {
28
+ docElement: JSX.Element;
29
+ sidebarName: string | undefined;
30
+ sidebarItems: PropSidebar | undefined;
31
+ } {
147
32
  const {
148
33
  route: {routes: docRoutes},
149
34
  versionMetadata,
@@ -153,33 +38,55 @@ export default function DocPage(props: Props): JSX.Element {
153
38
  matchPath(location.pathname, docRoute),
154
39
  );
155
40
  if (!currentDocRoute) {
156
- return <NotFound />;
41
+ return null;
157
42
  }
158
43
 
159
44
  // For now, the sidebarName is added as route config: not ideal!
160
45
  const sidebarName = currentDocRoute.sidebar;
161
46
 
162
- const sidebar = sidebarName
47
+ const sidebarItems = sidebarName
163
48
  ? versionMetadata.docsSidebars[sidebarName]
164
- : null;
49
+ : undefined;
50
+
51
+ const docElement = renderRoutes(props.route.routes, {
52
+ versionMetadata,
53
+ });
165
54
 
55
+ return {
56
+ docElement,
57
+ sidebarName,
58
+ sidebarItems,
59
+ };
60
+ }
61
+
62
+ export default function DocPage(props: Props): JSX.Element {
63
+ const {versionMetadata} = props;
64
+ const currentDocRouteMetadata = extractDocRouteMetadata(props);
65
+ if (!currentDocRouteMetadata) {
66
+ return <NotFound />;
67
+ }
68
+ const {docElement, sidebarName, sidebarItems} = currentDocRouteMetadata;
166
69
  return (
167
- <HtmlClassNameProvider
168
- className={clsx(
169
- ThemeClassNames.wrapper.docsPages,
170
- ThemeClassNames.page.docsDocPage,
171
- versionMetadata.className,
172
- )}>
173
- <DocsVersionProvider version={versionMetadata}>
174
- <DocsSidebarProvider sidebar={sidebar ?? null}>
175
- <DocPageContent
176
- currentDocRoute={currentDocRoute}
177
- versionMetadata={versionMetadata}
178
- sidebarName={sidebarName}>
179
- {renderRoutes(docRoutes, {versionMetadata})}
180
- </DocPageContent>
181
- </DocsSidebarProvider>
182
- </DocsVersionProvider>
183
- </HtmlClassNameProvider>
70
+ <>
71
+ <SearchMetadata
72
+ version={versionMetadata.version}
73
+ tag={docVersionSearchTag(
74
+ versionMetadata.pluginId,
75
+ versionMetadata.version,
76
+ )}
77
+ />
78
+ <HtmlClassNameProvider
79
+ className={clsx(
80
+ ThemeClassNames.wrapper.docsPages,
81
+ ThemeClassNames.page.docsDocPage,
82
+ props.versionMetadata.className,
83
+ )}>
84
+ <DocsVersionProvider version={versionMetadata}>
85
+ <DocsSidebarProvider name={sidebarName} items={sidebarItems}>
86
+ <DocPageLayout>{docElement}</DocPageLayout>
87
+ </DocsSidebarProvider>
88
+ </DocsVersionProvider>
89
+ </HtmlClassNameProvider>
90
+ </>
184
91
  );
185
92
  }
@@ -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
+ }