-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
feat(v2): Sidebar category linking to doc #3898
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,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); | ||
|
|
@@ -46,6 +50,7 @@ function DocSidebarItemCategory({ | |
| onItemClick, | ||
| collapsible, | ||
| activePath, | ||
| link, | ||
| ...props | ||
| }) { | ||
| const {items, label} = item; | ||
|
|
@@ -61,6 +66,7 @@ function DocSidebarItemCategory({ | |
| } | ||
| return isActive ? false : item.collapsed; | ||
| }); | ||
| const [initialLink, setInitialLink] = useState(''); | ||
|
|
||
| const menuListRef = useRef<HTMLUListElement>(null); | ||
| const [menuListHeight, setMenuListHeight] = useState<string | undefined>( | ||
|
|
@@ -78,17 +84,32 @@ function DocSidebarItemCategory({ | |
| if (justBecameActive && collapsed) { | ||
| setCollapsed(false); | ||
| } | ||
| if (link && !collapsed) { | ||
| for (const i in items) { | ||
| if (items.hasOwnProperty(i)) { | ||
| const childItem = items[i]; | ||
| if (childItem.type === 'link') { | ||
| if (link.id.includes(childItem.label.toLowerCase())) { | ||
| setInitialLink(childItem.href); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }, [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); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here you wait 100ms after setting the state, while before we waited before setting the state |
||
| }, | ||
| [menuListHeight], | ||
| ); | ||
|
|
@@ -103,17 +124,27 @@ 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, | ||
| })} | ||
| {...(isInternalUrl(initialLink) | ||
| ? { | ||
| isNavLink: true, | ||
| exact: 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} | ||
|
|
@@ -129,6 +160,7 @@ function DocSidebarItemCategory({ | |
| <DocSidebarItem | ||
| tabIndex={collapsed ? '-1' : '0'} | ||
| key={childItem.label} | ||
| link={link} | ||
| item={childItem} | ||
| onItemClick={onItemClick} | ||
| collapsible={collapsible} | ||
|
|
@@ -268,6 +300,7 @@ function DocSidebar({ | |
| <DocSidebarItem | ||
| key={item.label} | ||
| item={item} | ||
| link={item.link} | ||
| onItemClick={(e) => { | ||
| e.target.blur(); | ||
| setShowResponsiveSidebar(false); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,11 +73,11 @@ declare module '@theme/DocPaginator' { | |
| } | ||
|
|
||
| declare module '@theme/DocSidebar' { | ||
| import type {PropSidebarItem} from '@docusaurus/plugin-content-docs-types'; | ||
| // import type {PropSidebarItem} from '@docusaurus/plugin-content-docs-types'; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the issue I am having with deployment, since I'm adding a new field to sidebar.js its falling the test is there somewhere where I can add my new
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| export type Props = { | ||
| readonly path: string; | ||
| readonly sidebar: readonly PropSidebarItem[]; | ||
| readonly sidebar: any; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should still be a list of sidebar items |
||
| readonly sidebarCollapsible?: boolean; | ||
| readonly onCollapse: () => void; | ||
| readonly isHidden: boolean; | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove