feat: Introduce `SidebarItem` component to manage sidebar entry colla…
https://github.com/user-attachments/assets/fced4544-f701-43d3-b011-3b4e74620106
Overview
This PR enhances the React documentation website's sidebar navigation by allowing users to toggle dropdowns when clicking an already-selected item. This improves overall navigation usability and aligns the behavior with common UI expectations.
Problem Statement
Previously, clicking on an active/selected sidebar item that had a dropdown produced no action. Users naturally expect that clicking the same selected item again should collapse the dropdown—similar to accordion-style navigation patterns.
This mismatch resulted in:
- Reduced control over expanded sections
- Unnecessary vertical scrolling
- A navigation flow that felt less intuitive
Solution
A toggle mechanism has been implemented to allow collapsing an expanded dropdown by clicking the currently selected item a second time.
Technical Changes
Modified Files
src/components/Layout/Sidebar/SidebarLink.tsx
- Added optional
onClickprop to support custom click behavior - Passed
onClickhandler down to the underlyingLinkcomponent
src/components/Layout/Sidebar/SidebarRouteTree.tsx
- Imported
useStateanduseEffect - Extracted a new
SidebarItemcomponent to encapsulate item-level logic - Added local
isCollapsedstate to manage manual expand/collapse - Implemented click handler to toggle
isCollapsedwhen clicking on a selected item - Added
useEffectto resetisCollapsedwhen the item becomes deselected - Refactored main component to use
SidebarItemfor cleaner structure
Key Logic
// Determine if item should be expanded
const shouldBeExpanded = isForceExpanded || isBreadcrumb || selected;
const isExpanded = shouldBeExpanded && !isCollapsed;
// Toggle on click when selected
const handleClick = (event: React.MouseEvent<HTMLAnchorElement>) => {
if (selected) {
event.preventDefault();
setIsCollapsed((prev) => !prev);
}
};
// Reset collapsed state when navigating away
useEffect(() => {
if (!selected) {
setIsCollapsed(false);
}
}, [selected]);
Behavior
Before
- Clicking an active sidebar item with children: No action
- Navigation to another page: Dropdown expanded/collapsed strictly by breadcrumb logic
After
- 1st click on item: Navigates & expands dropdown (if applicable)
- 2nd click on same selected item: Collapses dropdown
- 3rd click: Expands again
- Navigating away: Resets collapse state for correct default behavior
Benefits
- Improved UX: Matches intuitive accordion-like patterns
- Enhanced Control: Users can collapse sections they're done with
- Backwards Compatible: Navigation behavior remains unchanged
- No Regressions: Collapsed state resets on navigation to avoid stale UI
Testing
-
✅
npm run tsc(TypeScript compilation) -
✅
npm run lint(ESLint validation) -
All existing warnings were unrelated to this change #8149
Size changes
📦 Next.js Bundle Analysis for react-dev
This analysis was generated by the Next.js Bundle Analysis action. 🤖
Five Pages Changed Size
The following pages changed size from the code in this PR compared to its base branch:
| Page | Size (compressed) | First Load |
|---|---|---|
/404 |
128.41 KB (🟡 +523 B) |
238.95 KB |
/500 |
128.42 KB (🟡 +523 B) |
238.96 KB |
/[[...markdownPath]] |
130.86 KB (🟡 +523 B) |
241.4 KB |
/errors |
128.66 KB (🟡 +523 B) |
239.2 KB |
/errors/[errorCode] |
128.64 KB (🟡 +523 B) |
239.18 KB |
Details
Only the gzipped size is provided here based on an expert tip.
First Load is the size of the global bundle plus the bundle for the individual page. If a user were to show up to your website and land on a given page, the first load size represents the amount of javascript that user would need to download. If next/link is used, subsequent page loads would only need to download that page's bundle (the number in the "Size" column), since the global bundle has already been downloaded.
Any third party scripts you have added directly to your app using the <script> tag are not accounted for in this analysis
Next to the size is how much the size has increased or decreased compared with the base branch of this PR. If this percentage has increased by 10% or more, there will be a red status indicator applied, indicating that special attention should be given to this.