Skip to content
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ declare module '@docusaurus/plugin-content-docs-types' {
export type PropSidebarItemCategory = PropsSidebarItemBase & {
type: 'category';
label: string;
link?: object;
items: PropSidebarItem[];
collapsed?: boolean;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-content-docs/src/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function assertItem<K extends string>(
function assertIsCategory(
item: unknown,
): asserts item is SidebarItemCategoryJSON {
assertItem(item, ['items', 'label', 'collapsed', 'customProps']);
assertItem(item, ['items', 'label', 'collapsed', 'link', 'customProps']);
if (typeof item.label !== 'string') {
throw new Error(
`Error loading ${JSON.stringify(item)}. "label" must be a string.`,
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-content-docs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export type SidebarItemCategory = SidebarItemBase & {
label: string;
items: SidebarItem[];
collapsed: boolean;
link?: object;
};

export type SidebarItem =
Expand Down
54 changes: 45 additions & 9 deletions packages/docusaurus-theme-classic/src/theme/DocSidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ function usePrevious(value) {
return ref.current;
}

function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

const isActiveSidebarItem = (item, activePath) => {
if (item.type === 'link') {
return isSamePath(item.href, activePath);
Expand All @@ -49,6 +53,7 @@ function DocSidebarItemCategory({
onItemClick,
collapsible,
activePath,
link,
...props
}) {
const {items, label} = item;
Expand All @@ -64,6 +69,7 @@ function DocSidebarItemCategory({
}
return isActive ? false : item.collapsed;
});
const [initialLink, setInitialLink] = useState('');

const menuListRef = useRef<HTMLUListElement>(null);
const [menuListHeight, setMenuListHeight] = useState<string | undefined>(
Expand All @@ -81,17 +87,36 @@ function DocSidebarItemCategory({
if (justBecameActive && collapsed) {
setCollapsed(false);
}

if (link) {
if (Object.hasOwnProperty.call(link, 'type')) {
switch (link.type) {
case 'doc':
setInitialLink(link.id);
break;
case 'link':
setInitialLink(link.href);
break;
default:
setInitialLink('');
break;
}
}
}
}, [isActive, wasActive, collapsed]);

const handleItemClick = useCallback(
(e) => {
e.preventDefault();

async (e) => {
if (!collapsed) {
e.preventDefault();
}
if (!menuListHeight) {
handleMenuListHeight();
}

setTimeout(() => setCollapsed((state) => !state), 100);
setCollapsed((state) => {
return !state;
});
await sleep(100);
},
[menuListHeight],
);
Expand All @@ -106,17 +131,26 @@ function DocSidebarItemCategory({
'menu__list-item--collapsed': collapsed,
})}
key={label}>
<a
<Link
to={initialLink}
className={clsx('menu__link', {
'menu__link--sublist': collapsible,
'menu__link--active': collapsible && isActive,
[styles.menuLinkText]: !collapsible,
[styles.menuLinkText]: !collapsible && initialLink === '',
})}
{...(isInternalUrl(initialLink)
? {
isNavLink: true,
onClick: onItemClick,
}
: {
target: '_blank',
rel: 'noreferrer noopener',
})}
onClick={collapsible ? handleItemClick : undefined}
href={collapsible ? '#!' : undefined}
{...props}>
{label}
</a>
</Link>
<ul
className="menu__list"
ref={menuListRef}
Expand All @@ -132,6 +166,7 @@ function DocSidebarItemCategory({
<DocSidebarItem
tabIndex={collapsed ? '-1' : '0'}
key={childItem.label}
link={link}
item={childItem}
onItemClick={onItemClick}
collapsible={collapsible}
Expand Down Expand Up @@ -268,6 +303,7 @@ function DocSidebar({
<DocSidebarItem
key={item.label}
item={item}
link={item.type === 'category' ? item.link : ''}
onItemClick={(e) => {
e.target.blur();
setShowResponsiveSidebar(false);
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-theme-classic/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ declare module '@theme/DocSidebar' {

export type Props = {
readonly path: string;
readonly sidebar: readonly PropSidebarItem[];
readonly sidebar: PropSidebarItem[];
readonly sidebarCollapsible?: boolean;
readonly onCollapse: () => void;
readonly isHidden: boolean;
Expand Down
1 change: 1 addition & 0 deletions website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ const LocaleConfigs = isI18nStaging
],
themeConfig: {
hideableSidebar: true,
sidebarCollapsible: true,
colorMode: {
defaultMode: 'light',
disableSwitch: false,
Expand Down
2 changes: 2 additions & 0 deletions website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ module.exports = {
{
type: 'category',
label: 'Getting Started',
link: {type: 'link', href: 'https://google.com'},
collapsed: false,
items: ['installation', 'configuration', 'typescript-support'],
},
{
type: 'category',
label: 'Guides',
link: {type: 'doc', id: 'creating-pages'},
items: [
'guides/creating-pages',
{
Expand Down