Navigation
Navigation Menu
The links across the top of a site or app, with panels of links that open under them. For layouts with a top bar instead of a sidebar.
The navigation menu is the main navigation of a layout without a sidebar. An item is a link, or a button that opens a panel of links, each with a line saying what is behind it. Put it at the start of the top bar. For an app with many sections, use the sidebar instead; for commands rather than pages, use the menubar.
Preview
Section titled “Preview”Installation
Section titled “Installation”Install the package for a versioned dependency:
npm install @nana-tec/ui-componentsOr copy the source in and own it:
npx @nana-tec/ui add navigation-menu --framework web-componentsnpx @nana-tec/ui add navigation-menu --framework svelteLands in src/lib/components/ui/navigation-menu/ as NanaNavigationMenu.svelte, styled
with Tailwind classes that resolve to the design tokens. Requires Tailwind v4.
The items are data. An item with href is a link; an item with items is a button that
opens those links in a panel. A link in a panel can carry a description. Mark the current
page with active; a button whose panel holds it is marked too.
<script lang="ts"> import { NanaNavigationMenu, type NavigationMenuItem } from '$lib/components/ui/navigation-menu';
const items: NavigationMenuItem[] = [ { label: 'Home', href: '/', active: true }, { label: 'Cover', items: [{ label: 'Motor', href: '/motor', description: 'Cars and motorbikes.' }], }, ];</script>
<NanaNavigationMenu {items} label="Site" />
Import the autoloader once, in your app's entry point. Every <nana-*> tag then works anywhere, and
each component downloads only the first time a page uses it:
import "@nana-tec/ui-components/autoloader";import "@nana-tec/ui-components/theme";The second line gives every component the Nana look and the design tokens behind it. Leave it out to style the components entirely yourself.
Import a component directly when it must be ready at once: on screen at first paint, or when you call its methods straight away. The two mix freely:
import "@nana-tec/ui-components/navigation-menu";
Copied in with npx @nana-tec/ui add navigation-menu? Import your project's own
@/lib/nana-ui/autoloader.js once instead; the CLI keeps it up to date.
<nana-navigation-menu label="Site"></nana-navigation-menu>
<script type="module"> document.querySelector('nana-navigation-menu').items = [ { label: 'Home', href: '/', active: true }, { label: 'Cover', items: [{ label: 'Motor', href: '/motor', description: 'Cars and motorbikes.' }], }, ];</script>Keyboard
Section titled “Keyboard”| Key | Does |
|---|---|
| Tab | Moves through the items, and into an open panel |
| Left and Right arrows | Move along the row |
| Home and End | Go to the first or last item |
| Enter or Space on a button | Opens or closes its panel |
| Down arrow on a button | Opens its panel and moves to the first link |
| Up and Down arrows in a panel | Move between its links; Up from the first goes back to the button |
| Escape | Closes the panel and returns to its button |
A panel also closes on a click outside, or when focus leaves the menu.
Phones
Section titled “Phones”The row does not fold away by itself. On a narrow screen, hide it and put the same items in a drawer opened from a button in the top bar.
Accessibility
Section titled “Accessibility”- The menu is a navigation landmark named by
label, “Main” by default. Name each navigation on a page differently, such as “Site” and “Footer”. - The buttons are disclosure buttons: they say whether their panel is open, and the panel holds ordinary links, so a screen reader reads it as a list of links, not as a menu.
- The current page has
aria-current="page".
Properties
| Property | Attribute | Type | Default | Description |
|---|---|---|---|---|
items | — | NavigationMenuItem[] | [] | The items in the row. An item with items opens a panel of those links. |
label | label | string | 'Main' | Names the navigation landmark for screen readers. |
CSS parts
| Part | Description |
|---|---|
::part(base) | The navigation landmark |
::part(list) | The row of items |
::part(item) | Each item in the row |
::part(link) | A top-level link, with the active state on the current page |
::part(trigger) | A button that opens a panel, with the open and active states |
::part(chevron) | The arrow on a button |
::part(panel) | The panel of links under a button |
::part(panel-link) | A link in a panel, with the active state on the current page |
::part(panel-title) | The name of a link in a panel |
::part(panel-description) | The line under a link's name |
CSS custom properties
| Property | Description |
|---|---|
--nana-navigation-menu-z | Stacking order of an open panel |
NanaNavigationMenu
| Prop | Type | Default |
|---|---|---|
itemsrequired | NavigationMenuItem[] | |
label | string | 'Main' |
Also accepts Omit<HTMLAttributes<HTMLElement>, 'class'>.
items is required. label names the landmark. Other attributes go on the nav.