Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When clicking the sidebar caret, open the category index page if available #1071

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 210 additions & 0 deletions src/theme/DocSidebarItem/Category/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
import React, { type ComponentProps, useEffect, useMemo } from 'react';
import clsx from 'clsx';
import {
ThemeClassNames,
useThemeConfig,
usePrevious,
Collapsible,
useCollapsible,
} from '@docusaurus/theme-common';
import {
isActiveSidebarItem,
findFirstCategoryLink,
useDocSidebarItemsExpandedState,
isSamePath,
} from '@docusaurus/theme-common/internal';
import Link from '@docusaurus/Link';
import { translate } from '@docusaurus/Translate';
import useIsBrowser from '@docusaurus/useIsBrowser';
import DocSidebarItems from '@theme/DocSidebarItems';
import type { Props } from '@theme/DocSidebarItem/Category';
import { useHistory } from '@docusaurus/router';

// If we navigate to a category and it becomes active, it should automatically
// expand itself
function useAutoExpandActiveCategory({
isActive,
collapsed,
updateCollapsed,
}: {
isActive: boolean;
collapsed: boolean;
updateCollapsed: (b: boolean) => void;
}) {
const wasActive = usePrevious(isActive);
useEffect(() => {
const justBecameActive = isActive && !wasActive;
if (justBecameActive && collapsed) {
updateCollapsed(false);
}
}, [isActive, wasActive, collapsed, updateCollapsed]);
}

/**
* When a collapsible category has no link, we still link it to its first child
* during SSR as a temporary fallback. This allows to be able to navigate inside
* the category even when JS fails to load, is delayed or simply disabled
* React hydration becomes an optional progressive enhancement
* see https://github.com/facebookincubator/infima/issues/36#issuecomment-772543188
* see https://github.com/facebook/docusaurus/issues/3030
*/
function useCategoryHrefWithSSRFallback(
item: Props['item']
): string | undefined {
const isBrowser = useIsBrowser();
return useMemo(() => {
if (item.href) {
return item.href;
}
// In these cases, it's not necessary to render a fallback
// We skip the "findFirstCategoryLink" computation
if (isBrowser || !item.collapsible) {
return undefined;
}
return findFirstCategoryLink(item);
}, [item, isBrowser]);
}

function CollapseButton({
categoryLabel,
onClick,
}: {
categoryLabel: string;
onClick: ComponentProps<'button'>['onClick'];
}) {
return (
<button
aria-label={translate(
{
id: 'theme.DocSidebarItem.toggleCollapsedCategoryAriaLabel',
message: "Toggle the collapsible sidebar category '{label}'",
description:
'The ARIA label to toggle the collapsible sidebar category',
},
{ label: categoryLabel }
)}
type="button"
className="clean-btn menu__caret"
onClick={onClick}
/>
);
}

export default function DocSidebarItemCategory({
item,
onItemClick,
activePath,
level,
index,
...props
}: Props): JSX.Element {
const { items, label, collapsible, className, href } = item;
const {
docs: {
sidebar: { autoCollapseCategories },
},
} = useThemeConfig();
const hrefWithSSRFallback = useCategoryHrefWithSSRFallback(item);

const isActive = isActiveSidebarItem(item, activePath);
const isCurrentPage = isSamePath(href, activePath);
const history = useHistory();

const { collapsed, setCollapsed } = useCollapsible({
// Active categories are always initialized as expanded. The default
// (`item.collapsed`) is only used for non-active categories.
initialState: () => {
if (!collapsible) {
return false;
}
return isActive ? false : item.collapsed;
},
});

const { expandedItem, setExpandedItem } = useDocSidebarItemsExpandedState();
// Use this instead of `setCollapsed`, because it is also reactive
const updateCollapsed = (toCollapsed: boolean = !collapsed) => {
setExpandedItem(toCollapsed ? null : index);
setCollapsed(toCollapsed);
};
useAutoExpandActiveCategory({ isActive, collapsed, updateCollapsed });
useEffect(() => {
if (
collapsible &&
expandedItem != null &&
expandedItem !== index &&
autoCollapseCategories
) {
setCollapsed(true);
}
}, [collapsible, expandedItem, index, setCollapsed, autoCollapseCategories]);

return (
<li
className={clsx(
ThemeClassNames.docs.docSidebarItemCategory,
ThemeClassNames.docs.docSidebarItemCategoryLevel(level),
'menu__list-item',
{
'menu__list-item--collapsed': collapsed,
},
className
)}
>
<div
className={clsx('menu__list-item-collapsible', {
'menu__list-item-collapsible--active': isCurrentPage,
})}
>
<Link
className={clsx('menu__link', {
'menu__link--sublist': collapsible,
'menu__link--sublist-caret': !href && collapsible,
'menu__link--active': isActive,
})}
onClick={
collapsible
? (e) => {
onItemClick?.(item);
if (href) {
updateCollapsed(false);
} else {
e.preventDefault();
updateCollapsed();
}
}
: () => {
onItemClick?.(item);
}
}
aria-current={isCurrentPage ? 'page' : undefined}
aria-expanded={collapsible ? !collapsed : undefined}
href={collapsible ? hrefWithSSRFallback ?? '#' : hrefWithSSRFallback}
{...props}
>
{label}
</Link>
{href && collapsible && (
<CollapseButton
categoryLabel={label}
onClick={(e) => {
e.preventDefault();
updateCollapsed();
collapsed && history.push(hrefWithSSRFallback);
}}
/>
)}
</div>

<Collapsible lazy as="ul" className="menu__list" collapsed={collapsed}>
<DocSidebarItems
items={items}
tabIndex={collapsed ? -1 : 0}
onItemClick={onItemClick}
activePath={activePath}
level={level + 1}
/>
</Collapsible>
</li>
);
}