shadcn.io is not affiliated with official shadcn/ui
Page Tree Navbar Block for React
An app bar whose Browse button opens a page tree where spaces expand and collapse, the current page is marked, and the button shows that page's name.
Installation
Overview
A page-tree navbar for React with Base UI, the app bar of an internal knowledge base called Almanac Team. The wordmark leads. A Browse button shows the page you're reading and opens a popover with the page tree: spaces expand and collapse, the current page is marked, and the button's label is that page's name. Home, Recent and Starred follow, and a New page Shine Button closes the row. Choosing a page rewrites the button, says so in a polite line and closes the tree. On a phone the name stays in the bar and the same tree fills a sheet from the top, with the links and New page under it. The popover and the sheet open over the page, so the block stays one bar high (56 pixels) and draws no bottom border: the next block's top rule is its bottom edge. It's built from Tree View, Popover, Sheet, Guides, Shine Button and Button, with your theme's tokens for every colour. For wikis and knowledge bases whose bar should open the page tree instead of a flat menu.
FAQ
How do I change the spaces and the pages?
Edit SPACES at the top of the file. Each space is an id, a name and a children list of pages, and each page is an id and a name. The tree, the "spaces, pages" line and the width of the Browse button are all built from that list, so adding an object is enough. WRITING is the page the demo opens on; it has to be one of the pages. The names are made up. In an app, replace choose with your router's page change.
What does choosing a page do?
It marks that page in the tree (aria-current="page", plus weight, a muted tint and a stroke on the row's start edge) and writes its name on the Browse button. A polite live region says the page's name and then "is the current page." The panel that was open closes. On the bar, focus returns to Browse; in the sheet, it returns to the menu button. Choosing the page that's already current leaves the panel open. Choosing a space expands or collapses it; a space is not a page, so it never becomes current. Nothing is stored or sent. New page is a link to # and does not add a row: wire it to your own flow.
How does it work with a keyboard and a screen reader?
The links sit in a nav named Main. The wordmark is a link home. The Browse button is named "Browse, current page Writing a page" and says whether the panel is open. Enter or Space opens it and moves focus to the current page in the tree. Up and Down move between the rows that are open. The arrow along the writing direction opens a space, and the arrow against it closes one. Home and End jump to the first and last open row. Enter or Space on a page chooses it. Typing a letter jumps to the next open row whose name starts with it. Escape closes the panel and returns focus to the button. The tree is the same on every base.
The panel does not trap focus, so Tab can leave it for the rest of the page. Escape still closes it.
How does it work on a phone?
Below the md breakpoint the bar shows the wordmark, the current page's name (it truncates) and a menu button, and nothing wraps at 320 pixels. The button says whether the menu is open and which panel it controls. It opens a sheet from the top whose header lines up with the bar, then Home, Recent and Starred, then the same tree filling the rest of the sheet (it scrolls when the list is taller than the screen), then New page. Escape, the close button, a link or a chosen page closes it, and focus goes back to the menu button.
How are the links and New page wired?
Home, Recent and Starred are the same three entries in the bar and in the sheet, and none of them is the page the tree marks. In the sheet, choosing one closes the sheet. Every link points at #.
New page renders the link it's given with render, with nativeButton={false}. The one in the sheet closes the sheet on click.
How does it sit on the page, and what moves?
The navbar is the page's first block: its marks and rails line up with every block under it, its top rule falls on the window's top edge, and it draws no bottom border, because the next block's top rule is its bottom edge. The panel opens over the page, so the page below doesn't move. From the lg breakpoint the Browse button is as wide as the longest page name, so choosing one doesn't push the links. Between md and lg the name truncates. To keep the bar in view while the page scrolls, add sticky top-0 z-40 bg-background/80 backdrop-blur to the header. Nothing moves on load. The panel fades in, a space opens with a short height change, the Browse chevron turns, the sheet slides down, and a band of light sweeps across New page once on hover and keyboard focus. With reduced motion the chevron turns at once, the spaces and the panel appear without the height change or the fade, the sheet appears without sliding, and the light fades in where it stands. The bar mirrors. The panel opens under the button's start edge. The wordmark is artwork and keeps its direction.
Wrap the block in your direction provider (the catalog's DirectionProvider) so the popover and the sheet follow the page.
Related
- Pro
Workspace Switcher Navbar
Accounts and scopesAn app bar whose switcher button opens a searchable list of workspaces: recent ones first with number keys, a plan badge on every row, a check on the current one and a Create workspace button in the foot.
- Pro
Sidebar Toggle Navbar
Mobile and drawersA navbar with a sidebar toggle for a project tracker: a button, the [ key and a tooltip fold a mini sidebar under the bar between icons only and icons with labels, and on phones the same button opens it as a sheet.
- Pro
A navbar for a support-desk product with a search field whose popup lists help articles and categories in two labelled groups, marks the matching letters, and offers an Ask support link when nothing matches.
- Pro
Command Palette Navbar
SearchA navbar for a notes app with a field-looking button and a shortcut that open a command palette: recent notes first, then pages, actions and settings in labelled groups, with the keys that work shown beside each row.
- Pro
Search Overlay Navbar
SearchA navbar for a legal-research database whose field-looking search button opens a panel from the top with a large input, recent searches you can remove, popular topic tags and live results with the matching letters marked.
- Pro
Category Rail Detail Panel Features
Tabs and accordionsA features section for a knowledge base as a working tree: a rail of three categories and six features beside a detail panel with the selected feature's sentence, three ticks and a small drawing.