Skip to content

Fix nav bar CollapseButton behavior #1320

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

Merged
merged 2 commits into from
Jul 23, 2025
Merged
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
38 changes: 2 additions & 36 deletions src/theme/DocSidebarItem/Category/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,12 @@ function useCategoryHrefWithSSRFallback(item) {
}

function CollapseButton({ categoryLabel, href, collapsed, updateCollapsed }) {
// Modified CollapseButton so that it has the same behavior as the title
const handleClick = (e) => {
e.preventDefault()
e.stopPropagation()

if (!collapsed) {
// When expanded, simply collapse
updateCollapsed(true)
} else {
// When collapsed, expand AND navigate to the page
updateCollapsed(false)
if (href) {
const linkElement = e.target
.closest('.menu__list-item-collapsible')
.querySelector('.menu__link')
if (linkElement) {
linkElement.click()
}
}
}
// Only toggle the collapse state, don't navigate
updateCollapsed(!collapsed)
}

return (
Expand Down Expand Up @@ -142,25 +128,6 @@ export default function DocSidebarItemCategory({
}
}, [collapsible, expandedItem, index, setCollapsed, autoCollapseCategories])

// Modified click handler for the title link
const handleTitleClick = (e) => {
onItemClick?.(item)

if (collapsible) {
if (isActive && !collapsed) {
// When expanded and selected, clicking title should collapse and navigate
updateCollapsed(true)
} else if (href) {
// For items with href, expand and let navigation happen
updateCollapsed(false)
} else {
// For items without href, just toggle
e.preventDefault()
updateCollapsed()
}
}
}

return (
<li
className={clsx(
Expand All @@ -184,7 +151,6 @@ export default function DocSidebarItemCategory({
'menu__link--sublist-caret': !href && collapsible,
'menu__link--active': isActive,
})}
onClick={handleTitleClick}
aria-current={isCurrentPage ? 'page' : undefined}
aria-expanded={collapsible ? !collapsed : undefined}
href={collapsible ? hrefWithSSRFallback ?? '#' : hrefWithSSRFallback}
Expand Down