Back

A sidebar with nested content and a breadcrumb is one of the most common, and clearest, ways to navigate. This is a basic version of both, with the design and the logic behind it.

For state, a hook like useState or a store like Redux or Zustand sounds like the obvious choice. I prefer query states, because they act as a shared context that lives in the URL. When someone reloads the page, or shares its link, a sidebar and breadcrumb that read from the URL can open straight to that page.

The example below keeps the open page's path in a single query parameter, as a unique ID. The sidebar uses it to highlight its node, and the breadcrumb splits it into segments. The state is kept with nuqs.

Use desktop to use component preview

Unlike local component state, a query state persists in the URL, which makes navigation sturdier. The sidebar and breadcrumb survive a refresh or a shared link with no extra work, however the page is loaded.

Breadcrumb

The breadcrumb truncates to stay clean on deeply nested paths. With more than two segments, it folds the middle ones into a menu and keeps Home and the current page in view. Inside the menu, each segment is indented one step further than the last, to show how they nest. The breadcrumb stays short and readable, and the full path is one click away.

Home

Settings

Profiles

Use desktop to use component preview

Opening the page from a link

The most important edge case in a system like this is showing the right active state when a page first loads. To get there, we walk back through the parent segments and open each folder in the tree, so the current page is visible.

The selected node's ID, for example engineering/setup/frontend, is read from the query state and split on the slash. Starting at the root, each parent path is built by joining one more segment: first engineering, then engineering/setup, and finally engineering/setup/frontend. Those paths go into a Set, and every folder in it opens. A deep link loads with its whole path open, and nobody has to expand anything by hand.

In the sidebar component, the open folders come from two places. First, the folders the query state requires:

TypeScript
const requiredExpandedNodes = useMemo(  () => computeExpandedNodes(selectedFileNodeId),  [selectedFileNodeId],);

Then those are merged with the folders the reader opened by hand, while respecting any they closed:

TypeScript
const expandedNodes = useMemo(() => {  const merged = new Set<string>();  // Add all required nodes (from query state),  // except manually collapsed ones  requiredExpandedNodes.forEach((nodeId) => {    if (!manuallyCollapsedNodes.has(nodeId)) {      merged.add(nodeId);    }  });  // Add all manually expanded nodes  manuallyExpandedNodes.forEach((nodeId) => merged.add(nodeId));  return merged;}, [requiredExpandedNodes, manuallyExpandedNodes, manuallyCollapsedNodes]);
Use desktop to use component preview

When a page loads with a node in its query state, every parent opens to reveal it. If the reader then collapses one of those folders, the choice holds, until they open it again or pick another page. Reload this page with a deep link to see the demo open on it.

Articles are only designed for light and dark, so the other color themes are off here.