Skip to content

Conversation

@JayadityaGit
Copy link

clideo_editor_b3a727c65cf5481b997119fdab88f23b.mp4

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 onClick prop to support custom click behavior
  • Passed onClick handler down to the underlying Link component

src/components/Layout/Sidebar/SidebarRouteTree.tsx

  • Imported useState and useEffect
  • Extracted a new SidebarItem component to encapsulate item-level logic
  • Added local isCollapsed state to manage manual expand/collapse
  • Implemented click handler to toggle isCollapsed when clicking on a selected item
  • Added useEffect to reset isCollapsed when the item becomes deselected
  • Refactored main component to use SidebarItem for 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

…pse/expand state and add `onClick` prop to `SidebarLink`.
@github-actions
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant