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

Finish go to selected paged from category sidebar #4323

Closed
wants to merge 15 commits into from
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?: PropSidebarItemCategoryLink;
items: PropSidebarItem[];
collapsed?: boolean;
};
Expand All @@ -43,6 +44,20 @@ declare module '@docusaurus/plugin-content-docs-types' {
export type PropSidebars = {
[sidebarId: string]: PropSidebarItem[];
};

export type PropSidebarItemCategoryLinkDoc = {
type: 'doc';
id: string;
};

export type PropSidebarItemCategoryLinkLink = {
type: 'link';
href: string;
};

export type PropSidebarItemCategoryLink =
| PropSidebarItemCategoryLinkLink
| PropSidebarItemCategoryLinkDoc;
}

declare module '@theme/DocItem' {
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']);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to validate that the link attribute is valid in this function + add unit tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might need some help on this one

if (typeof item.label !== 'string') {
throw new Error(
`Error loading ${JSON.stringify(item)}. "label" must be a string.`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import React, {useState, useCallback, useEffect, useRef} from 'react';
import React, {useState, useMemo, useCallback, useEffect, useRef} from 'react';
import clsx from 'clsx';
import {useThemeConfig, isSamePath} from '@docusaurus/theme-common';
import useUserPreferencesContext from '@theme/hooks/useUserPreferencesContext';
Expand Down Expand Up @@ -49,6 +49,7 @@ function DocSidebarItemCategory({
onItemClick,
collapsible,
activePath,
link,
...props
}) {
const {items, label} = item;
Expand All @@ -64,6 +65,24 @@ function DocSidebarItemCategory({
}
return isActive ? false : item.collapsed;
});
const initialLink = useMemo(() => {
if (item.link) {
if (Object.hasOwnProperty.call(item.link, 'type')) {
switch (item.link.type) {
case 'doc':
return item.link.id;
case 'link':
return item.link.href;
default:
return undefined;
}
} else {
return undefined;
}
} else {
return undefined;
}
}, [item.link]);

const menuListRef = useRef<HTMLUListElement>(null);
const [menuListHeight, setMenuListHeight] = useState<string | undefined>(
Expand All @@ -86,11 +105,9 @@ function DocSidebarItemCategory({
const handleItemClick = useCallback(
(e) => {
e.preventDefault();

if (!menuListHeight) {
handleMenuListHeight();
}

setTimeout(() => setCollapsed((state) => !state), 100);
},
[menuListHeight],
Expand All @@ -106,17 +123,21 @@ 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 === undefined,
})}
{...{
isNavLink: true,
onClick: onItemClick,
}}
onClick={collapsible ? handleItemClick : undefined}
href={collapsible ? '#!' : undefined}
{...props}>
{label}
</a>
</Link>
<ul
className="menu__list"
ref={menuListRef}
Expand All @@ -132,6 +153,7 @@ function DocSidebarItemCategory({
<DocSidebarItem
tabIndex={collapsed ? '-1' : '0'}
key={childItem.label}
link={link}
Copy link
Collaborator

@Josh-Cena Josh-Cena Aug 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why bind this to the children elements? Only the category with link attribute should have a link associated right?

item={childItem}
onItemClick={onItemClick}
collapsible={collapsible}
Expand Down Expand Up @@ -268,6 +290,7 @@ function DocSidebar({
<DocSidebarItem
key={item.label}
item={item}
link={item.type === 'category' ? item.link : undefined}
onItemClick={(e) => {
e.target.blur();
setShowResponsiveSidebar(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: 'doc', id: 'installation'},
collapsed: false,
items: ['installation', 'configuration', 'typescript-support'],
},
{
type: 'category',
label: 'Guides',
link: {type: 'doc', id: 'creating-pages'},
items: [
'guides/creating-pages',
{
Expand Down