Skip to content
Nana UI

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.

View .md Download

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.

Install the package for a versioned dependency:

npm install @nana-tec/ui-components

Or copy the source in and own it:

npx @nana-tec/ui add navigation-menu --framework web-components
npx @nana-tec/ui add navigation-menu --framework svelte

Lands 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>
KeyDoes
TabMoves through the items, and into an open panel
Left and Right arrowsMove along the row
Home and EndGo to the first or last item
Enter or Space on a buttonOpens or closes its panel
Down arrow on a buttonOpens its panel and moves to the first link
Up and Down arrows in a panelMove between its links; Up from the first goes back to the button
EscapeCloses the panel and returns to its button

A panel also closes on a click outside, or when focus leaves the menu.

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.

  • 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

PropertyAttributeTypeDefaultDescription
items—NavigationMenuItem[][]The items in the row. An item with items opens a panel of those links.
labellabelstring'Main'Names the navigation landmark for screen readers.

CSS parts

PartDescription
::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

PropertyDescription
--nana-navigation-menu-zStacking order of an open panel

NanaNavigationMenu

PropTypeDefault
itemsrequiredNavigationMenuItem[]
labelstring'Main'

Also accepts Omit<HTMLAttributes<HTMLElement>, 'class'>.

items is required. label names the landmark. Other attributes go on the nav.