Skip to content

Commit

Permalink
breadcrumb of unlisted category index shouldn't be clickable
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Nov 3, 2022
1 parent 6840195 commit 3c81af8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
22 changes: 13 additions & 9 deletions packages/docusaurus-plugin-content-docs/src/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,24 @@ Available document ids are:
link: SidebarItemCategoryLink | undefined,
): string | undefined {
switch (link?.type) {
case 'doc': {
const doc = getDocById(link.id);
if (doc.unlisted) {
// TODO, not ideal solution because an unlisted category link
// can't be displayed/highlighted in sidebar when browsed
return undefined;
}
return doc.permalink;
}
case 'doc':
return getDocById(link.id).permalink;
case 'generated-index':
return link.permalink;
default:
return undefined;
}
}

function getCategoryLinkUnlisted(
link: SidebarItemCategoryLink | undefined,
): boolean {
if (link?.type === 'doc') {
return getDocById(link.id).unlisted;
}
return false;
}

function getCategoryLinkCustomProps(
link: SidebarItemCategoryLink | undefined,
) {
Expand All @@ -113,12 +115,14 @@ Available document ids are:
function convertCategory(item: SidebarItemCategory): PropSidebarItemCategory {
const {link, ...rest} = item;
const href = getCategoryLinkHref(link);
const linkUnlisted = getCategoryLinkUnlisted(link);
const customProps = item.customProps ?? getCategoryLinkCustomProps(link);

return {
...rest,
items: item.items.map(normalizeItem),
...(href && {href}),
...(linkUnlisted && {linkUnlisted}),
...(customProps && {customProps}),
};
}
Expand Down
6 changes: 6 additions & 0 deletions packages/docusaurus-plugin-content-docs/src/sidebars/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ export type PropSidebarItemCategory = Expand<
SidebarItemCategoryBase & {
items: PropSidebarItem[];
href?: string;

// Weird name => it would have been more convenient to have link.unlisted
// Note it is the category link that is unlisted, not the category itself
// We want to prevent users from clicking on an unlisted category link
// We can't use "href: undefined" otherwise sidebar item is not highlighted
linkUnlisted?: boolean;
}
>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,17 @@ export default function DocBreadcrumbs(): JSX.Element | null {
{homePageRoute && <HomeBreadcrumbItem />}
{breadcrumbs.map((item, idx) => {
const isLast = idx === breadcrumbs.length - 1;
const href =
item.type === 'category' && item.linkUnlisted
? undefined
: item.href;
return (
<BreadcrumbsItem
key={idx}
active={isLast}
index={idx}
addMicrodata={!!item.href}>
<BreadcrumbsItemLink href={item.href} isLast={isLast}>
addMicrodata={!!href}>
<BreadcrumbsItemLink href={href} isLast={isLast}>
{item.label}
</BreadcrumbsItemLink>
</BreadcrumbsItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function useCategoryHrefWithSSRFallback(
): string | undefined {
const isBrowser = useIsBrowser();
return useMemo(() => {
if (item.href) {
if (item.href && !item.linkUnlisted) {
return item.href;
}
// In these cases, it's not necessary to render a fallback
Expand Down

0 comments on commit 3c81af8

Please sign in to comment.