This week I moved the PubTrivia dashboard navigation out of the header and into a grouped left sidebar. It is the sort of change that touches six files and invites six places to disagree: the desktop sidebar, the phone menu, the label at the top of each page, the spotlight steps of the first-run tour, and the "which item is highlighted" logic, which every navigation has and which quietly goes wrong.
I made them agree by making them the same thing. This is a short tour of that one file and the two rules in it that I would reuse anywhere, with the behaviour printed from the real code. The dashboard itself is behind a login. The free tier needs no card if you want to look at it, and the venue-side pages it manages are public, for example the bookings and venue page feature.
The file is data
nav-items.ts is 103 lines and has no components in it. It exports three groups, a list of account pages, and two functions:
export type NavItem = { href: string label: string icon: LucideIcon /** Only this exact path, not the pages under it. */ exact?: boolean tour?: string } export const NAV_GROUPS: NavGroup[] = [ { items: [{ href: '/dashboard', label: 'Home', icon: Home, exact: true, tour: 'nav-home' }] }, { label: 'Quizzes', tour: 'nav-quizzes', items: [ /* Packs, Shop, History */ ] }, { label: 'Your pub', tour: 'nav-pub', items: [ /* Tables & QR codes, Venue page, What's on & bookings, Teams & leagues, Promo slides */ ] }, ] export const ACCOUNT_ITEMS: NavItem[] = [ /* Plan & billing, Refer a pub, Settings, Help */ ] Thirteen items in three groups, with the account pages pinned to the bottom. The groups are named for how a landlord thinks about their week: running quizzes, their pub, and their account. They are not named for how the code is organised, which is why "Shop" sits beside "Packs" even though they are different routes with different data.
The doc comment states the point of the file: one list drives the desktop sidebar, the phone menu and the top bar's "where am I" label, "so they cannot disagree". A page added to the array appears in all of them.
One component, two shapes
The list is rendered by SidebarNav, and the two layouts are the same component with a different wrapper and a large flag.
// desktop: a fixed column from the lg breakpoint up <aside className="sticky top-0 hidden h-screen w-60 shrink-0 flex-col border-r lg:flex"> <Brand venue={venue} /> <SidebarNav isAdmin={isAdmin} /> </aside> // below lg: the same list in a dialog, bigger and closing itself on a tap <SidebarNav isAdmin={isAdmin} onNavigate={close} large /> large changes row height from 36 to 48 pixels and the text from small to base, and nothing else. onNavigate lets the phone version close the dialog when a link is tapped. On a tablet the dialog slides in from the left at a fixed width, and on a phone it fills the screen. I used a Radix dialog rather than a hand-rolled drawer because focus trapping, Escape and screen-reader announcement come with it, and it carries a visually hidden title ("Menu") and description so it has an accessible name.
Both layouts open with the pub's own name and a link to its public page, so a host who works for two venues always sees which one they are looking at.
Rule one: a prefix match needs a separator
export function isActive(pathname: string, item: NavItem): boolean { const path = ALSO[pathname] ?? pathname if (item.exact) return path === item.href return path === item.href || path.startsWith(item.href + '/') } The slash is the whole rule. startsWith('/dashboard/packs') would light up Packs for a page called /dashboard/packsfoo, and startsWith('/dashboard/host') would match /dashboard/hosting. Adding + '/' and comparing for equality separately means a page and its children match, and a longer word that happens to begin the same way does not.
I printed what currentPage makes of a few paths:
/dashboard Home /dashboard/packs Quizzes / Packs /dashboard/packs/abc/edit Quizzes / Packs /dashboard/packsfoo null /dashboard/events/42 Your pub / What's on & bookings /dashboard/hosting null The second row shows children counting as their parent. The fourth and sixth show a near miss not being matched.
Home is the exception, and that is what exact is for. Every dashboard path starts with /dashboard, so without it Home would be highlighted on every page. I checked that too: Home is not active on /dashboard/packs.
Rule two: say which item a page belongs to when it is not in the menu
Some pages are reached from another page and have no menu entry of their own. Choosing a plan is reached from the billing page, so the highlighted item should be Plan & billing. That is one small lookup rather than a flag on every item:
const ALSO: Record<string, string> = { '/dashboard/plans': '/dashboard/billing', } isActive rewrites the path before matching. /dashboard/plans gives "Account / Plan & billing" in the table above, and there is no second code path for it.
The alternative I considered was an alsoMatches array on the item itself. That would have kept the knowledge next to the item, but it would also have made every item carry a field that is empty for twelve of the thirteen, so I took the one-entry record.
"Where am I" is a function of the same list
export function currentPage(pathname: string) { if (pathname === '/dashboard/host' || pathname.startsWith('/dashboard/host/')) { return { item: { href: '/dashboard/host', label: 'Host a quiz', icon: Home } } } for (const group of NAV_GROUPS) { const item = group.items.find((i) => isActive(pathname, i)) if (item) return { group: group.label, item } } const account = ACCOUNT_ITEMS.find((i) => isActive(pathname, i)) return account ? { group: 'Account', item: account } : null } The top bar shows "Your pub / Promo slides" for the current page by calling this, so it cannot show something the sidebar does not highlight. It returns null for a path in no group, and the caller decides what to show.
Hosting a quiz is deliberately not in the list. It is the main action, so it is a button in the top bar on every page, and the special case above gives it a label when you are on it. I will own up to the duplication here: the same two-part host check, equality or prefix with a slash, also sits in the top bar component as onHostPage. It is the same rule written twice, and if the host route ever changed I would have to change both. It is on my list.
The tour reads the same names
Nine items carry a tour string, and each group has one as well. These become data-tour attributes, and the first-run tour looks elements up by them, spotlighting "Your pub" as one block rather than five rows. The tour's own header comment points back at this file for the names. On a phone the sidebar is not on the screen, so one of the tour steps anchors to the menu button instead and opens the dialog.
That is a lot of consumers for one array: two layouts, a label, a tour, and the active state. It is a fairly boring file, and that is what I wanted from it.
What I would copy
- Define navigation as data, and render it in as many shapes as you need.
- Match a path as equal or prefix plus a slash. Mark Home as exact.
- Handle "reached from, not listed in" with a small alias table.
- Derive the page label from the same list that draws the menu.
- Write down the duplicates you left behind, so the next change finds them.
If you run a quiz and want to see what the other end of this looks like, the pages it manages are the public ones: your venue page, the directory and the pitch for listing.