# Navigation Menu

A site's top navigation, with links and panels of links.

A site's top navigation: a row of links and of triggers that open a panel of links. A trigger is the real Button (ghost, sm) with the menu's caret; a plain link has the same look as an anchor. The panel is the shared popup surface: it grows out of the trigger that opened it when the pointer opens it, and is instant from the keyboard; its links are option rows with an optional description, under the one gliding highlight. Panels open on hover after a short delay or on a click, and one panel box serves every trigger: moving the pointer to another trigger while a panel is open is one motion, the box following to the new trigger and the new panel's size while the content changes in the direction of travel. Behaviour by Ark UI's navigation-menu (Zag).

Kind: component · Category: Navigation · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `navigation-menu` machine, through Ark UI

## Add

```bash
npx fndn add navigation-menu
```

It writes the folder `navigation-menu/` into the project's ui folder (`index.ts`, `navigation-menu.tsx`, `NavigationMenu.vue`, `NavigationMenu.svelte`, `navigation-menu.recipe.ts`), and what it is built from: [Button](/components/button.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { NavigationMenu } from "@/components/ui/navigation-menu";
```

```vue
import { NavigationMenu } from "@/components/ui/navigation-menu";
```

```svelte
import { NavigationMenu } from "$lib/components/ui/navigation-menu";
```

## Parts

In the DOM the root carries `data-slot="navigation-menu"` and each part `data-slot="navigation-menu-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `nav` | `navigation-menu` |  | Ark's root, a nav landmark named by the label prop. As wide as its content (inline-flex column), never wider than its container. |
| `list` | `div` | `navigation-menu-list` |  | The row of items, 2px apart. A crowded row wraps rather than cutting a name. |
| `item` | `div` | `navigation-menu-item` | repeats | One entry of the row: a trigger with its panel content, or a plain link. |
| `trigger` | `button` | `navigation-menu-trigger` | repeats | The real Button (ghost, sm: 28px, pill) marked data-navigation-menu="trigger", with a trailing caret; aria-expanded and aria-controls from the engine. While its panel is open it keeps the ghost button's darker fill (hover-2). |
| `caret` | `svg` | `navigation-menu-caret` | repeats | The trigger's caret-down (12px, bold). It turns 180° while the panel is open, over duration.move with easing.out-strong; instant from the keyboard (kbd-instant). |
| `bar-link` | `a` | `navigation-menu-bar-link` | repeats | A link straight in the row: the ghost sm button's look on an anchor (the button recipe). The current page (aria-current=page) keeps the darker fill (hover-2). |
| `viewport-positioner` | `div` | `navigation-menu-viewport-positioner` |  | Ark's viewport positioner, in a portal like every popup: placed on the root's box (its left, its bottom, its width) while a panel is open, following scroll and resizes; it carries the opening trigger's offset and width (--trigger-x, --trigger-width) for the panel's transform-origin. At the popup layer (z.popup). |
| `viewport` | `div` | `navigation-menu-viewport` |  | The one panel box, sized by the engine to the open content (--viewport-width / --viewport-height) and placed by it (--viewport-x): its start edge on the trigger's start, kept 10px inside the window. The shared popup surface: from the pointer it grows from the trigger's centre at the top edge, at duration.popup; it closes at duration.fast; from the keyboard both are instant. Moving the pointer to another trigger while open, its start edge and size ease to the new panel's (duration.move, easing.out-strong); from the keyboard, under reduced motion and on a first open the box takes them at once. |
| `content` | `div` | `navigation-menu-content` | repeats | One trigger's panel, rendered into the viewport while open: 4px padding, as wide as its longest link (at least 220px, at most 480px or the window less 16px). It holds the gliding highlight and the links. Switching panels from the pointer, the leaving content fades toward the far side and the arriving one fades in from the side of travel (data-motion from the engine, mirrored right-to-left). |
| `highlight` | `span` | `navigation-menu-highlight` |  | The one gliding highlight of the panel (hover colour), following the pointer and the focus between links. |
| `link` | `a` | `navigation-menu-link` | repeats | A link of a panel: the option row (30px minimum, 40px on a coarse pointer; padding 5px 8px; radius chip), its title above its description. Press: hover-2. The current page (aria-current=page) has a medium title. |
| `link-title` | `span` | `navigation-menu-link-title` | repeats | The link's name: text.dense (12.5px) ink, shown whole (it wraps). Author text keeps its own direction. |
| `link-description` | `span` | `navigation-menu-link-description` | optional, repeats | One or two lines under the title: text.xs ink-2; it wraps, never cut. |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `items` | `{ value: string; label: string; href: string? (a plain link); current: boolean? (a plain link: the current page); links: { label: string, href: string, description?: string, current?: boolean }[]? (a trigger and its panel) }[]` |  | The entries of the row in order: a plain link (href) or a trigger with a panel of links (links). |
| `currentHref` | `string` |  | The current page's href. The link with that href (or with current: true) is aria-current="page"; a trigger whose panel holds it is aria-current="true" (the current section) and keeps the ghost button's darker fill (hover-2), as a current plain link does. |
| `value` | `string` |  | Bindable (React: `defaultValue` and `onValueChange`; Vue: `v-model:value`; Svelte: `bind:value`). The open panel's item value (empty: none). Controlled: value with onValueChange. Uncontrolled: defaultValue. |
| `label` | `string` | `"Main"` | The nav landmark's accessible name. |
| `openDelay` | `number` | `200` | Milliseconds a mouse rests on a trigger before its panel opens. |
| `closeDelay` | `number` | `300` | Milliseconds after the mouse leaves before the panel closes. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `valueChange` | (value: string) a panel opened, switched or closed ("") |
| `linkSelect` | (href: string) a link was followed |

## States

`hover`, `press`, `focus-visible`, `open`, `current`

## Keyboard and accessibility

Role: navigation.

Keyboard:

- Tab moves through the triggers and plain links of the row, and into an open panel after its trigger
- Enter or Space on a trigger opens or closes its panel, at once
- Arrow Left / Right move between the entries of the row (mirrored right-to-left); Home / End to the first / last
- Arrow Down on the trigger of an open panel moves into it; Arrow Up / Down move between its links
- Escape closes the panel and returns focus to its trigger

Notes:

- The root is a nav landmark named by label. Triggers carry aria-expanded and aria-controls; a panel is labelled by its trigger.
- The current page's link carries aria-current=page, in the row or in a panel.
- Hover opens a panel only for a mouse, after openDelay; touch and pen open it with a tap.
- The current page is aria-current="page" on its link; the trigger of the panel that holds it is aria-current="true" (a section, not the page), so a screen reader hears where the reader is before opening it.

## Motion

- panel: the shared popup (opacity + scale from motion.popup-scale at duration.popup, easing.out-strong), growing from the opening trigger's centre at the top edge; closes at duration.fast; instant when opened or closed from the keyboard
- switching between open panels from the pointer, one motion (the box does not fade or grow again): the box's start edge and size ease to the new panel's over duration.move with easing.move; the leaving content fades out toward the far side (motion.enter-shift, a 2px blur) at duration.fast while the arriving one fades in from the side of travel (the same shift and blur) at duration.popup with easing.out-strong, so the two are never both fully visible; transitions, so a fast sweep across the row retargets from where each part is and ends at rest; right-to-left mirrors the direction
- switching from the keyboard is instant; under reduced motion it is a fade only (no shift, the box jumps, no blur); a first open has none of it (only the popup's entrance)
- caret: turns 180° over duration.move, easing.out-strong; instant from the keyboard
- highlight: one layer glides between links over duration.move under the pointer and the focus
- triggers and links: the Button's colour and press feedback; links in a panel press with hover-2
- nothing at first paint

## Tokens

`color.surface`, `color.hover`, `color.hover-2`, `color.ink`, `color.ink-2`, `shadow.raised`, `radius.card`, `radius.chip`, `radius.pill`, `text.xs`, `text.sm`, `duration.fast`, `duration.popup`, `duration.move`, `easing.out-strong`, `motion.enter-shift`, `motion.popup-scale`, `z.popup`

## Dependencies

- [Button](/components/button.md)

## Examples

### Top bar of the workspace

React · `workspace-header.tsx`

```tsx
import { NavigationMenu, type NavigationMenuItem } from "@/components/ui/navigation-menu";

const items: NavigationMenuItem[] = [
  {
    value: "projects",
    label: "Projects",
    links: [
      { label: "web-app", href: "#web-app", description: "Customer dashboard, live on Production" },
      { label: "billing-api", href: "#billing-api", description: "Payments, invoices and webhooks" },
      { label: "mobile", href: "#mobile", description: "iOS and Android app" },
      { label: "docs-site", href: "#docs-site", description: "Public documentation" },
    ],
  },
  {
    value: "team",
    label: "Team",
    links: [
      { label: "Members", href: "#members", description: "7 people in Northwind" },
      { label: "Roles and permissions", href: "#roles" },
      { label: "Audit log", href: "#audit-log" },
    ],
  },
  { value: "billing", label: "Billing", href: "#billing" },
  { value: "docs", label: "Docs", href: "#docs" },
];

export default function WorkspaceHeader() {
  return <NavigationMenu label="Northwind" items={items} currentHref="#billing-api" />;
}
```

Vue · `workspace-header.vue`

```vue
<script setup lang="ts">
import { NavigationMenu, type NavigationMenuItem } from "@/components/ui/navigation-menu";

const items: NavigationMenuItem[] = [
  {
    value: "projects",
    label: "Projects",
    links: [
      { label: "web-app", href: "#web-app", description: "Customer dashboard, live on Production" },
      { label: "billing-api", href: "#billing-api", description: "Payments, invoices and webhooks" },
      { label: "mobile", href: "#mobile", description: "iOS and Android app" },
      { label: "docs-site", href: "#docs-site", description: "Public documentation" },
    ],
  },
  {
    value: "team",
    label: "Team",
    links: [
      { label: "Members", href: "#members", description: "7 people in Northwind" },
      { label: "Roles and permissions", href: "#roles" },
      { label: "Audit log", href: "#audit-log" },
    ],
  },
  { value: "billing", label: "Billing", href: "#billing" },
  { value: "docs", label: "Docs", href: "#docs" },
];
</script>

<template>
  <NavigationMenu label="Northwind" :items="items" current-href="#billing-api" />
</template>
```

Svelte · `workspace-header.svelte`

```svelte
<script lang="ts">
  import { NavigationMenu, type NavigationMenuItem } from "$lib/components/ui/navigation-menu";

  const items: NavigationMenuItem[] = [
    {
      value: "projects",
      label: "Projects",
      links: [
        { label: "web-app", href: "#web-app", description: "Customer dashboard, live on Production" },
        { label: "billing-api", href: "#billing-api", description: "Payments, invoices and webhooks" },
        { label: "mobile", href: "#mobile", description: "iOS and Android app" },
        { label: "docs-site", href: "#docs-site", description: "Public documentation" },
      ],
    },
    {
      value: "team",
      label: "Team",
      links: [
        { label: "Members", href: "#members", description: "7 people in Northwind" },
        { label: "Roles and permissions", href: "#roles" },
        { label: "Audit log", href: "#audit-log" },
      ],
    },
    { value: "billing", label: "Billing", href: "#billing" },
    { value: "docs", label: "Docs", href: "#docs" },
  ];
</script>

<NavigationMenu label="Northwind" {items} currentHref="#billing-api" />
```

### Mark the current page, with a client router

React · `client-router.tsx`

```tsx
import { useState } from "react";
import { NavigationMenu, type NavigationMenuItem } from "@/components/ui/navigation-menu";

/* onLinkSelect hears every followed link: keep the current page in state and mark it */
export default function ClientRouter() {
  const [page, setPage] = useState("#deploys");
  const items: NavigationMenuItem[] = [
    { value: "overview", label: "Overview", href: "#overview", current: page === "#overview" },
    { value: "deploys", label: "Deploys", href: "#deploys", current: page === "#deploys" },
    {
      value: "observability",
      label: "Observability",
      links: [
        { label: "Logs", href: "#logs", current: page === "#logs" },
        { label: "Usage", href: "#usage", current: page === "#usage" },
      ],
    },
  ];
  return <NavigationMenu label="billing-api" items={items} onLinkSelect={setPage} />;
}
```

Vue · `client-router.vue`

```vue
<script setup lang="ts">
import { computed, ref } from "vue";
import { NavigationMenu, type NavigationMenuItem } from "@/components/ui/navigation-menu";

/* link-select hears every followed link: keep the current page in state and mark it */
const page = ref("#deploys");
const items = computed<NavigationMenuItem[]>(() => [
  { value: "overview", label: "Overview", href: "#overview", current: page.value === "#overview" },
  { value: "deploys", label: "Deploys", href: "#deploys", current: page.value === "#deploys" },
  {
    value: "observability",
    label: "Observability",
    links: [
      { label: "Logs", href: "#logs", current: page.value === "#logs" },
      { label: "Usage", href: "#usage", current: page.value === "#usage" },
    ],
  },
]);
</script>

<template>
  <NavigationMenu label="billing-api" :items="items" @link-select="(href) => (page = href)" />
</template>
```

Svelte · `client-router.svelte`

```svelte
<script lang="ts">
  import { NavigationMenu, type NavigationMenuItem } from "$lib/components/ui/navigation-menu";

  /* onlinkselect hears every followed link: keep the current page in state and mark it */
  let page = $state("#deploys");
  const items: NavigationMenuItem[] = $derived([
    { value: "overview", label: "Overview", href: "#overview", current: page === "#overview" },
    { value: "deploys", label: "Deploys", href: "#deploys", current: page === "#deploys" },
    {
      value: "observability",
      label: "Observability",
      links: [
        { label: "Logs", href: "#logs", current: page === "#logs" },
        { label: "Usage", href: "#usage", current: page === "#usage" },
      ],
    },
  ]);
</script>

<NavigationMenu label="billing-api" {items} onlinkselect={(href) => (page = href)} />
```

## Recipe

`navigation-menu.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";
import { button } from "../button/button.recipe";
import { popup } from "../lib/popup";
import { option } from "../lib/option";
import { glideLayer } from "../lib/glide";

/* navigation-menu — a row of links and of triggers that open a panel of links (Ark UI
 * navigation-menu).
 *
 * The triggers are the real Button (ghost, sm) with a trailing caret, marked
 * data-navigation-menu="trigger"; a plain link in the row has the same look on an anchor (the
 * button recipe), and the current page keeps the ghost button's darker fill, as an open trigger
 * does. The row wraps when crowded: a name is never cut.
 *
 * One panel box (the engine's viewport) serves every trigger: the engine sizes it to the open
 * content (--viewport-width / --viewport-height) and places it (--viewport-x, from the root's
 * start; kept 10px inside the window). It is the shared popup surface: from the pointer it grows
 * from the opening trigger's centre at its top edge (--trigger-x / --trigger-width, from the root's
 * start, set on the positioner by navigation-menu.logic.ts: the box is in a portal, like every popup),
 * at duration.popup, and closes at duration.fast; from the keyboard it is instant (kbd-instant,
 * in the popup recipe). The box itself clips; the content inside carries the 4px padding, so the
 * box is exactly the content's size.
 *
 * Switching triggers while open is one motion (the engine marks the two contents involved
 * data-motion: from-start / from-end on the arriving one, to-start / to-end on the leaving one,
 * physical sides, mirrored right-to-left by the engine). While the box holds such a content it
 * follows: its start edge (left) and its size ease to the new panel's over duration.move with
 * easing.out-strong (snappy: it starts fast and reaches the new trigger with
 * the content); on a first open there is no data-motion, so the box takes its place and size at once
 * and only grows as a popup. The leaving content goes toward the far side (motion.enter-shift, a
 * fade and a 2px blur, duration.fast); the arriving one comes from the side of travel (the same
 * shift, fade and blur, duration.popup). Transitions, not keyframes: a sweep across the row
 * retargets each content from where it is, and a content that is neither arriving nor leaving keeps
 * opacity 0 while its display runs out. From the keyboard all of it is instant (kbd-instant); under
 * reduced motion the tokens make it a fade (the shift is 0, the box jumps; the blur is motion-safe).
 *
 * The links of a panel are option rows (title above an optional description) under the one
 * gliding highlight (glideLayer of lib/glide.ts, follow: hover). */

const ghost = button({ variant: "ghost", size: "sm" });
const row = option();

export const navigationMenu = tv({
  slots: {
    base: "relative inline-flex flex-col max-w-full min-w-0",
    list: "flex flex-wrap items-center gap-0.5 min-w-0",
    item: "flex min-w-0",
    /* a trigger whose panel holds the current page keeps the darker fill, as a current link does */
    trigger: "aria-[current=true]:bg-hover-2",
    caret: [
      "kbd-instant motion-transform-move",
      "[[data-navigation-menu=trigger][aria-expanded=true]_&]:rotate-180",
    ],
    barLink: [ghost.base(), "aria-[current=page]:bg-hover-2"],
    /* in a portal: navigation-menu.logic.ts sets left, top and width to the root's box. The engine's
     * and the logic's offsets are page coordinates, physical in both directions, so the boxes they
     * place are placed by `left`, written as the property */
    viewportPositioner: "absolute top-0 [left:0] z-popup",
    viewport: [
      popup(),
      "absolute top-1 [left:var(--viewport-x)] w-(--viewport-width) h-(--viewport-height) p-0 overflow-hidden max-h-none",
      "[--transform-origin:calc(var(--trigger-x,0px)_+_var(--trigger-width,0px)/2_-_var(--viewport-x,0px))_top]",
      /* fndn-check-ok: the one box follows the pointer between panels (its start edge and size ease
       * to the new panel's); it is absolutely placed in a portal and its contents are absolute, so
       * nothing else is laid out again. Only while switching (a content carries data-motion). */
      "has-[[data-motion]]:motion-size-move has-[[data-motion]]:motion-also-[display_var(--fndn-duration-popup)_allow-discrete,left_var(--fndn-duration-move)_var(--fndn-easing-out-strong)]",
    ],
    content: [
      "absolute top-0 [left:0] w-max min-w-[220px] max-w-[min(480px,calc(100vw-16px))] p-1 outline-none",
      "kbd-instant motion-opacity-popup motion-transform-popup motion-filter-popup motion-also-[display_var(--fndn-duration-popup)_allow-discrete]",
      /* arriving from the side of travel (only when switching: a first open has no data-motion) */
      "data-[motion=from-end]:starting:opacity-0 data-[motion=from-end]:starting:translate-x-(--fndn-motion-enter-shift)",
      "data-[motion=from-start]:starting:opacity-0 data-[motion=from-start]:starting:-translate-x-(--fndn-motion-enter-shift)",
      "motion-safe:data-[motion^=from]:starting:[filter:blur(2px)]",
      /* leaving toward the far side, shorter; any other closed content in the open box stays at 0
       * (a fast sweep never brings one back). When the whole menu closes the box fades, not this. */
      "[[data-slot=navigation-menu-viewport][data-state=open]>&]:data-[state=closed]:opacity-0 [[data-slot=navigation-menu-viewport][data-state=open]>&]:data-[state=closed]:duration-fast",
      "data-[motion=to-start]:-translate-x-(--fndn-motion-enter-shift) data-[motion=to-end]:translate-x-(--fndn-motion-enter-shift)",
      "motion-safe:data-[motion^=to]:[filter:blur(2px)]",
    ],
    highlight: [glideLayer, "inset-x-1"],
    /* a link takes real focus (the option row draws no ring: its lists move a highlight instead), so
     * it carries the shared ring; the content's 4px padding leaves it room inside the box */
    link: [
      row.base(),
      "flex-col items-start justify-center gap-0.5 no-underline",
      /* the title stacks above its description here: keep the row's own padding, not the one the
       * option row uses to centre a description beside the label */
      "has-[>[data-slot$=-description]]:py-[5px] pointer-coarse:has-[>[data-slot$=-description]]:py-[5px]",
      "focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-accent focus-visible:outline-offset-2",
    ],
    /* shown whole on its own line: not the 70% the option row keeps for a description beside it */
    linkTitle: [row.text(), "flex-none w-full [&:has(+[data-slot$=-description])]:flex-none [&:has(+[data-slot$=-description])]:max-w-none [[aria-current=page]>&]:font-medium"],
    linkDescription: "w-full min-w-0 text-xs leading-[1.35] text-ink-2 wrap-anywhere [unicode-bidi:plaintext] [text-align:match-parent]",
  },
});

export type NavigationMenuRecipeProps = VariantProps<typeof navigationMenu>;
```
