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 { display: flex; gap: 0.4em; align-items: center; box-sizing: border-box; width: min(26rem, 100%); margin-block-end: 8.5rem; padding: 0.4em; border: 1px solid var(--ink); border-radius: 6px; font-family: 'Shantell Sans', system-ui, sans-serif; font-size: var(--step--1); line-height: 1.5; color: var(--ink);}.item { position: relative;}.top { display: inline-flex; gap: 0.35em; align-items: center; padding: 0.4em 0.75em; border: 0; border-radius: 4px; background: transparent; color: var(--ink); font: inherit; font-family: 'Shantell Sans', system-ui, sans-serif; text-decoration: none; cursor: pointer; transition: background 140ms ease;}.top:hover { background: color-mix(in srgb, var(--purple) 16%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.top[ui-open] { background: color-mix(in srgb, var(--purple) 26%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.top[ui-active] { text-decoration: underline; text-decoration-color: var(--pink); text-decoration-thickness: 2px; text-underline-offset: 0.25em;}.caret { width: 1em; height: 1em; opacity: 0.65; transition: transform 140ms ease;}.top[ui-open] .caret { transform: rotate(180deg);}.panel { position: absolute; inset-block-start: calc(100% + 0.6em); inset-inline-start: 0; z-index: 3; display: grid; gap: 0.2em; box-sizing: border-box; width: 13rem; padding: 0.4em; border: 1px solid var(--ink); border-radius: 6px; background: var(--paper); box-shadow: 3px 3px 0 var(--muted-ink);}.panel[hidden] { display: none;}.link { display: grid; padding: 0.45em 0.65em; border-radius: 4px; color: var(--ink); font-family: 'Shantell Sans', system-ui, sans-serif; text-decoration: none;}.link:hover { background: color-mix(in srgb, var(--purple) 16%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.link-note { color: var(--muted-ink); font-family: 'Shantell Sans', system-ui, sans-serif; font-size: var(--step--2);}.top:focus-visible,.link:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px;}@media (prefers-reduced-motion: reduce) { .top, .caret { transition: none; }}
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
The <nav> landmark that holds the site's primary navigation.
navbar.rootis the<nav>landmark. It holds which dropdown is showing.navbar.itemis one top-level entry. Itsvaluenames it.navbar.itemtriggeris a<button>that shows or hides the item's dropdown. It writesaria-expandedandaria-controls.navbar.itemcontentis the dropdown. It carrieshiddenwhile it is closed.navbar.itemlinkis an<a>. It works at the top level and inside a dropdown.navbar.triggeris 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 youronClick.
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.