navbar

onChange called 0 time(s)

import { lucide, navbar } from '@markless/ui';export default function NavbarBasic() @{	<navbar.root class="navbar" aria-label="Primary" value="products">		<navbar.item class="item" value="home">			<navbar.itemlink class="top" href="#home" current>Home</navbar.itemlink>		</navbar.item>		<navbar.item class="item" value="products">			<navbar.itemtrigger class="top">				Products				<lucide.chevrondown class="caret" aria-hidden="true" />			</navbar.itemtrigger>			<navbar.itemcontent class="panel">				<navbar.itemlink class="link" href="#keyboards">					Keyboards					<span class="link-note">Quiet, clicky, split</span>				</navbar.itemlink>				<navbar.itemlink class="link" href="#mice">					Mice					<span class="link-note">Wired and wireless</span>				</navbar.itemlink>			</navbar.itemcontent>		</navbar.item>		<navbar.item class="item" value="docs">			<navbar.itemtrigger class="top">				Docs				<lucide.chevrondown class="caret" aria-hidden="true" />			</navbar.itemtrigger>			<navbar.itemcontent class="panel">				<navbar.itemlink class="link" href="#start">					Getting started					<span class="link-note">Set up in five minutes</span>				</navbar.itemlink>				<navbar.itemlink class="link" href="#api">					API					<span class="link-note">Every part and prop</span>				</navbar.itemlink>			</navbar.itemcontent>		</navbar.item>	</navbar.root>;}

navbar is your site's main navigation. Each entry is a link, or a button that shows a dropdown of links. Use it for the bar across the top of a site.

It is not a menu bar. It writes no menu, menubar or menuitem role, and the tests check that. A screen reader hears plain links and buttons, which is what site navigation is.

Anatomy

navbar anatomy
Anatomy perspectiveFlat
Layers
OverviewProjectsBrand / Website / Archive
navbar.root

The <nav> landmark that holds the site's primary navigation.

  • navbar.root is the <nav> landmark. It holds which dropdown is showing.
  • navbar.item is one top-level entry. Its value names it.
  • navbar.itemtrigger is a <button> that shows or hides the item's dropdown. It writes aria-expanded and aria-controls.
  • navbar.itemcontent is the dropdown. It carries hidden while it is closed.
  • navbar.itemlink is an <a>. It works at the top level and inside a dropdown.
  • navbar.trigger is a <button> that opens no dropdown, such as a theme switch or a search button. The arrow keys stop on it like any other control in the bar. What it does is up to your onClick.

The root, the item, the trigger and the dropdown write ui-open while a dropdown shows. They write ui-closed while it does not. navbar.item writes ui-active when you set active on it. navbar.itemlink writes ui-active and aria-current="page" when you set current on it. When one dropdown replaces another, the dropdown coming in writes ui-from="end" if its trigger is after the old one in the bar, or ui-from="start" if it is before. The one going out writes ui-to with the opposite side. Use them to slide the panels the way the pointer moved. Opening from closed, or closing, writes neither.

Set current on the one link for the page a person is on. Set active on the item for the section that page sits in. Only current reaches a screen reader.

Keyboard

Key What it does
Enter or Space Opens or closes the dropdown of the focused trigger.
Left arrow or Right arrow Moves between top-level controls, and wraps at the ends. It skips dropdown links.
Home or End Jumps to the first or last top-level control.
Down arrow on a trigger Opens the dropdown and moves focus to its first link.
Arrows inside a dropdown Move between that dropdown's links and buttons, and wrap at the ends.
Home or End in a dropdown Jumps to the dropdown's first or last link.
Escape Closes the dropdown. If focus was inside it, focus goes back to the trigger.
Tab Moves on as normal. When focus leaves the landmark, the open dropdown closes.

A press outside the open dropdown closes it too.

Hover

By default a dropdown opens when the pointer rests on its item. The first one waits for delay. Once one shows, moving along the bar to the next item opens it at once (switchDelay, 0 by default). A pointer heading down across a trigger on its way into the open panel is held back, so the panel never swaps under it.

A dropdown stays open while the pointer is on its trigger, on its panel, or on the way between the two. As soon as the pointer is anywhere else, even elsewhere in the navbar, the dropdown closes.

Name the landmark

navbar.root writes no aria-label. A page can have a main nav and a footer nav. The same made-up name on both gives a screen reader two landmarks it cannot tell apart. Write your own label, as in the example above.

navbar.itemcontent gets no placement CSS from the family. Position the dropdown with your own stylesheet.

Props on navbar.root

Prop Type What it does
value string The value of the item whose dropdown shows. Omit it, or pass '', and every dropdown closes.
hover boolean Opens a dropdown when the pointer rests on its item. Omit it and hover is on.
delay number How long the pointer rests before the first dropdown opens, in ms. Omit it and it is 200.
switchDelay number How long the pointer rests before the next dropdown replaces an open one, in ms. The default is 0.
clickGrace number How long a click on the trigger is ignored after a hover opened it, in ms. The default is 300.
onChange (value) => void Gets the value now showing, or '' when everything closed.

The root passes anything a <nav> accepts to its element. navbar.item takes value, which is required, and active. navbar.itemlink takes current.