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 10 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;
ben-qnimble marked this conversation as resolved.
Show resolved Hide resolved
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']);
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 @@ -40,7 +40,6 @@ function DocPageContent({
const {pluginId, permalinkToSidebar, docsSidebars, version} = versionMetadata;
const sidebarName = permalinkToSidebar[currentDocRoute.path];
const sidebar = docsSidebars[sidebarName];

const [hiddenSidebarContainer, setHiddenSidebarContainer] = useState(false);
const [hiddenSidebar, setHiddenSidebar] = useState(false);
const toggleSidebar = useCallback(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function DocSidebarItemCategory({
onItemClick,
collapsible,
activePath,
link,
...props
}) {
const {items, label} = item;
Expand All @@ -64,6 +65,7 @@ function DocSidebarItemCategory({
}
return isActive ? false : item.collapsed;
});
const [initialLink, setInitialLink] = useState('');
ben-qnimble marked this conversation as resolved.
Show resolved Hide resolved

const menuListRef = useRef<HTMLUListElement>(null);
const [menuListHeight, setMenuListHeight] = useState<string | undefined>(
Expand All @@ -81,16 +83,29 @@ function DocSidebarItemCategory({
if (justBecameActive && collapsed) {
setCollapsed(false);
}
if (item.link) {
if (Object.hasOwnProperty.call(item.link, 'type')) {
switch (item.link.type) {
case 'doc':
setInitialLink(item.link.id);
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is not correct, the id is not the URL path, as docs can use slugs they have ids that can be totally different from their final URL.

This requires resolving the id to the doc URL path

(+ shouldn't need setState)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a function for getting the URL path from a document id?

break;
case 'link':
setInitialLink(item.link.href);
break;
default:
setInitialLink('');
break;
}
}
}
}, [isActive, wasActive, collapsed]);

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

if (!menuListHeight) {
handleMenuListHeight();
}

setTimeout(() => setCollapsed((state) => !state), 100);
},
[menuListHeight],
Expand All @@ -106,17 +121,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',
ben-qnimble marked this conversation as resolved.
Show resolved Hide resolved
rel: 'noreferrer noopener',
})}
onClick={collapsible ? handleItemClick : undefined}
href={collapsible ? '#!' : undefined}
{...props}>
{label}
</a>
</Link>
<ul
className="menu__list"
ref={menuListRef}
Expand All @@ -132,6 +156,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 +293,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: 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