Skip to content

Commit 6021faf

Browse files
authored
fix(theme): render sidebar category index with unlisted children as a simple doc/link item (#11389)
Co-authored-by: slorber <749374+slorber@users.noreply.github.com>
1 parent bfdcc73 commit 6021faf

File tree

5 files changed

+70
-1
lines changed

5 files changed

+70
-1
lines changed

packages/docusaurus-theme-classic/src/theme/DocSidebarItem/Category/index.tsx

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,19 @@ import {
2424
isActiveSidebarItem,
2525
findFirstSidebarItemLink,
2626
useDocSidebarItemsExpandedState,
27+
useVisibleSidebarItems,
2728
} from '@docusaurus/plugin-content-docs/client';
2829
import Link from '@docusaurus/Link';
2930
import {translate} from '@docusaurus/Translate';
3031
import useIsBrowser from '@docusaurus/useIsBrowser';
3132
import DocSidebarItems from '@theme/DocSidebarItems';
33+
import DocSidebarItemLink from '@theme/DocSidebarItem/Link';
3234
import type {Props} from '@theme/DocSidebarItem/Category';
3335

36+
import type {
37+
PropSidebarItemCategory,
38+
PropSidebarItemLink,
39+
} from '@docusaurus/plugin-content-docs';
3440
import styles from './styles.module.css';
3541

3642
// If we navigate to a category and it becomes active, it should automatically
@@ -136,7 +142,48 @@ function CategoryLinkLabel({label}: {label: string}) {
136142
);
137143
}
138144

139-
export default function DocSidebarItemCategory({
145+
export default function DocSidebarItemCategory(props: Props): ReactNode {
146+
const visibleChildren = useVisibleSidebarItems(
147+
props.item.items,
148+
props.activePath,
149+
);
150+
if (visibleChildren.length === 0) {
151+
return <DocSidebarItemCategoryEmpty {...props} />;
152+
} else {
153+
return <DocSidebarItemCategoryCollapsible {...props} />;
154+
}
155+
}
156+
157+
function isCategoryWithHref(
158+
category: PropSidebarItemCategory,
159+
): category is PropSidebarItemCategory & {href: string} {
160+
return typeof category.href === 'string';
161+
}
162+
163+
// If a category doesn't have any visible children, we render it as a link
164+
function DocSidebarItemCategoryEmpty({item, ...props}: Props): ReactNode {
165+
// If the category has no link, we don't render anything
166+
// It's not super useful to render a category you can't open nor click
167+
if (!isCategoryWithHref(item)) {
168+
return null;
169+
}
170+
// We remove props that don't make sense for a link and forward the rest
171+
const {
172+
type,
173+
collapsed,
174+
collapsible,
175+
items,
176+
linkUnlisted,
177+
...forwardableProps
178+
} = item;
179+
const linkItem: PropSidebarItemLink = {
180+
type: 'link',
181+
...forwardableProps,
182+
};
183+
return <DocSidebarItemLink item={linkItem} {...props} />;
184+
}
185+
186+
function DocSidebarItemCategoryCollapsible({
140187
item,
141188
onItemClick,
142189
activePath,

project-words.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Fienny
8585
flac
8686
Flightcontrol
8787
Flightcontrol's
88+
forwardable
8889
FOUC
8990
Français
9091
froms
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Unlisted Except Index
2+
3+
This index page is listed, but the other pages are unlisted.
4+
5+
import DocCardList from '@theme/DocCardList';
6+
7+
<DocCardList />
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
unlisted: true
3+
---
4+
5+
# Unlisted 1
6+
7+
This page is unlisted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
unlisted: true
3+
---
4+
5+
# Unlisted 2
6+
7+
This page is unlisted.

0 commit comments

Comments
 (0)