---
title: "Navigation Menu"
description: "Grouped navigation with shared animated panels, descriptive links and keyboard controls."
---

# Navigation Menu

Grouped navigation with shared animated panels, descriptive links and keyboard controls.

## Installation

Install the shared package once in your Exact app. This version is a beta.

```sh
bun add --exact @rightui/ui@0.4.0-beta.4
```

Or choose another package manager:

```sh
npm install --save-exact @rightui/ui@0.4.0-beta.4
pnpm add --save-exact @rightui/ui@0.4.0-beta.4
yarn add --exact @rightui/ui@0.4.0-beta.4
```

Exact compilation uses Bun 1.4.2. Register Geist and Newsreader, declare the font marker and add the root provider using [Installation](/docs/installation).

### Manual source setup

Install an editable copy of the complete package:

```sh
mkdir -p vendor/rightui vendor/rightui-ui
npm pack @rightui/ui@0.4.0-beta.4 --pack-destination vendor/rightui
tar -xzf ./vendor/rightui/rightui-ui-0.4.0-beta.4.tgz -C vendor/rightui-ui --strip-components=1
bun add file:./vendor/rightui-ui
```

Keep package.json, the Contract files, ui.tamber, assets and licenses together.

### Import

```contract
use NavigationMenuEntry, NavigationMenu, NavigationMenuSection, NavigationMenuSections from "@rightui/ui"
```

## Usage

Use NavigationMenu for one trigger, or NavigationMenuSections with NavigationMenuSection for grouped links. Choose list or grid layout, use empty entries for direct links, and pass the reduced-motion preference.

## Example

```contract
use uiFontFamily, NavigationMenuEntry, NavigationMenu, NavigationMenuSection, NavigationMenuSections from "@rightui/ui"

component Demo
  props
    width: number
    reduced: bool
  inject
    uiFont: string
  derive narrow = width < 600
  view
    column gap="ui.space-16" min-width=0 max-width="100%"
      view min-height="16rem"
        SectionsDemo(reduced=reduced)
      NavigationMenu(id="controls-navigation", reduced=reduced, label="Documentation", entries=[NavigationMenuEntry(id="intro", title="Introduction", description="Accessible components you can compose.", href="/docs/how-exact-works"), NavigationMenuEntry(id="forms", title="Forms", description="Fields, groups and verification code.", href="/docs/components/field")])

component SectionsDemo
  props
    reduced: bool
  view
    column gap="ui.space-16" min-width="0px" max-width="100%"
      NavigationMenuSections(id="site-navigation", label="Product navigation", reduced=reduced, sections=[NavigationMenuSection(id="start", label="Get started", href="", layout="list", entries=[NavigationMenuEntry(id="overview", title="Overview", description="A short introduction to the component library.", href="/docs/how-exact-works"), NavigationMenuEntry(id="install", title="Installation", description="Add the package to your app.", href="/docs/installation"), NavigationMenuEntry(id="type", title="Typography", description="Set a readable visual hierarchy.", href="/docs/components/typography")]), NavigationMenuSection(id="components", label="Components", href="", layout="grid", entries=[NavigationMenuEntry(id="buttons", title="Buttons", description="Actions and interactive states.", href="/docs/components/button"), NavigationMenuEntry(id="forms", title="Forms", description="Inputs and validation.", href="/docs/components/input"), NavigationMenuEntry(id="feedback", title="Feedback", description="Status and notifications.", href="/docs/components/toast"), NavigationMenuEntry(id="overlays", title="Overlays", description="Dialogs and floating surfaces.", href="/docs/components/dialog")]), NavigationMenuSection(id="resources", label="Resources", href="", layout="list", entries=[NavigationMenuEntry(id="guide", title="Guide", description="Patterns for common page flows.", href="/docs/theming"), NavigationMenuEntry(id="examples", title="Examples", description="Working compositions to explore.", href="/blocks")]), NavigationMenuSection(id="docs", label="Docs", href="/docs/how-exact-works", layout="list", entries=[])])

```

## Keyboard

Arrow keys move between triggers and links. Enter or Arrow Down opens a section. Escape closes the panel and returns focus to its trigger.

## API Reference

| Property | Type |
| --- | --- |
| NavigationMenu.id | string |
| NavigationMenu.label | string |
| NavigationMenu.entries | NavigationMenuEntry[] |
| NavigationMenu.reduced | bool |
| NavigationMenuSections.id | string |
| NavigationMenuSections.label | string |
| NavigationMenuSections.sections | NavigationMenuSection[] |
| NavigationMenuSections.reduced | bool |

## Source

File: packages/ui/navigation-menu.contract

```contract
use FontWeights from "./typography-context.contract"
use uiFontFamily from "./typography-context.contract"
use uiPopupIn, uiPopupOut from "./popup-motion.contract"
use Control from "./styles.contract"
shape NavigationMenuEntry
  id: string
  title: string
  description: string
  href: string
component NavigationMenu
  props
    id: string
    label: string
    entries: NavigationMenuEntry[]
    reduced: bool
  inject
    uiWeights: FontWeights
    uiFont: string
  state expanded = false
  state openEpoch = 0
  action show
    if not expanded
      openEpoch = openEpoch + 1
    expanded = true
    showPopover(id + "-content", source=id)
  action toggle
    if expanded
      expanded = false
    else
      show()
  action changed(epoch: number, e: ToggleEvent)
    if epoch == openEpoch
      expanded = e.newState == "open"

  action keys(k: string)
    if k == "Enter" or k == " "
      preventDefault()
      toggle()
    else if k == "Escape"
      preventDefault()
      expanded = false
      focus(id)
    else if k == "ArrowDown"
      preventDefault()
      show()
      match first(entries)
        case some(entry)
          focus(id + "-" + entry.id)
        case none
          focus(id)
  action linkKeys(index: number, k: string)
    stopPropagation()
    if k == "ArrowDown" or k == "ArrowUp" or k == "ArrowLeft" or k == "ArrowRight" or k == "Home" or k == "End"
      preventDefault()
      let next = (k == "Home" ? 0 : k == "End" ? entries.length - 1 : (index + (k == "ArrowDown" or k == "ArrowRight" ? 1 : entries.length - 1)) % max(1, entries.length))
      match entries.at(next)
        case some(entry)
          focus(id + "-" + entry.id)
        case none
          focus(id)
    else if k == "Escape"
      keys(k)
  view
    column role="navigation" aria-label=label align-items="flex-start"
      button id=id testId=id class=Control font-weight=uiWeights.medium press=toggle key=keys aria-expanded=expanded aria-controls=(id + "-content")
        display="flex" align-items="center" gap="ui.space-6" padding="ui.space-8 ui.space-16" border="0" background-color=(expanded ? "ui.muted" : "ui.surface") color="ui.foreground"
        text label font-family=uiFontFamily(uiFont) data-rightui-font="body"
        svg width="ui.space-12" height="ui.space-12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="ui.stroke-standard" aria-hidden=true
          path d="m6 9 6 6 6-6"
      if expanded
        for epoch in [openEpoch] key=epoch
          column id=(id + "-content") testId=(id + "-content") popover="auto" toggle=changed(epoch) key=keys
            position-area="bottom span-right" position-try-fallbacks="flip-inline, flip-block, flip-block flip-inline" margin="ui.space-4 0px" width="25rem" max-width="calc(100vw - ui.space-32)" padding="ui.space-4" gap="ui.space-4" box-sizing="border-box"
            border-width="ui.border-width" border-style="solid" border-color="ui.border" border-radius="ui.radius-menu" background-color="ui.surface" color="ui.foreground" box-shadow="ui.shadow-menu"
            animation=(reduced ? "none" : "uiPopupIn ui.duration-fast ui.easing both")
            -exact-exit-animation=(reduced ? "none" : "uiPopupOut ui.duration-fast ui.easing both")
            for entry, i in entries key=entry.id
              link id=(id + "-" + entry.id) testId=(id + "-" + entry.id) href=entry.href key=linkKeys(i)
                display="flex" flex-direction="column" gap="ui.space-4" padding="ui.space-12" border-radius="ui.radius-item" color="ui.foreground" text-decoration="none"
                :hover
                  background-color="ui.muted"
                :focus-visible
                  background-color="ui.muted"
                  box-shadow="ui.shadow-focus"
                text entry.title font-size="ui.text-sm" font-weight=uiWeights.medium font-family=uiFontFamily(uiFont) data-rightui-font="body"
                text entry.description font-size="ui.text-sm" line-height="ui.leading-snug" color="ui.muted-foreground" font-family=uiFontFamily(uiFont) data-rightui-font="body"

shape NavigationMenuSection
  id: string
  label: string
  href: string
  entries: NavigationMenuEntry[]
  layout: string

shape NavigationMenuViewport
  width: number

component NavigationMenuSections
  props
    id: string
    label: string
    sections: NavigationMenuSection[]
    reduced: bool
  inject
    uiWeights: FontWeights
    uiFont: string
  resource viewport = exactViewport() as shape NavigationMenuViewport
  state triggerLeft = 0
  state triggerRight = 0
  derive availableWidth = max(viewport.width - triggerLeft, triggerRight)
  state anchorX = 0
  state anchorWidth = 0
  state anchorHeight = 0
  state active = -1
  state openEpoch = 0
  state hoverOpened = false
  state switching = false
  action measureSection(index: number)
    match sections.at(index)
      case some(section)
        let trigger = frame(id + "-" + section.id)
        let container = frame(id)
        if not trigger.unavailable and not trigger.provisional and not container.unavailable and not container.provisional
          anchorX = trigger.x - container.x
          anchorWidth = trigger.width
          anchorHeight = container.height
          triggerLeft = trigger.x
          triggerRight = trigger.x + trigger.width
      case none
        triggerLeft = 0
        triggerRight = 0
  action resized(width: number, height: number)
    measureSection(active)
  action open(index: number)
    match sections.at(index)
      case some(section)
        if section.entries.length > 0
          measureSection(index)
          switching = active >= 0
          if active < 0
            openEpoch = openEpoch + 1
            showPopover(id + "-content", source=id + "-anchor")
          active = index
      case none
        active = -1
  action dismissMenu
    active = -1
    hoverOpened = false
  action toggleSection(index: number)
    if active == index
      if hoverOpened
        hoverOpened = false
      else
        dismissMenu()
    else
      hoverOpened = false
      open(index)
  action triggerHover(index: number, inside: bool)
    if inside and active != index
      open(index)
      hoverOpened = true
  action changed(epoch: number, e: ToggleEvent)
    if epoch == openEpoch and e.newState == "closed"
      active = -1
  action triggerKeys(index: number, k: string)
    if k == "Enter" or k == " " or k == "ArrowDown"
      preventDefault()
      open(index)
      match sections.at(index)
        case some(section)
          match first(section.entries)
            case some(entry)
              focus(id + "-" + section.id + "-" + entry.id)
            case none
              focus(id + "-" + section.id)
        case none
          focus(id)
    else if k == "ArrowRight" or k == "ArrowLeft" or k == "Home" or k == "End"
      preventDefault()
      let next = (k == "Home" ? 0 : k == "End" ? sections.length - 1 : (index + (k == "ArrowRight" ? 1 : sections.length - 1)) % max(1, sections.length))
      match sections.at(next)
        case some(section)
          focus(id + "-" + section.id)
          if active >= 0
            open(next)
        case none
          focus(id)
    else if k == "Escape"
      preventDefault()
      dismissMenu()
      match sections.at(index)
        case some(section)
          focus(id + "-" + section.id)
        case none
          focus(id)
  action linkKeys(sectionIndex: number, index: number, k: string)
    stopPropagation()
    if k == "Escape"
      triggerKeys(sectionIndex, k)
    else if k == "ArrowDown" or k == "ArrowUp" or k == "Home" or k == "End"
      preventDefault()
      match sections.at(sectionIndex)
        case some(section)
          let next = (k == "Home" ? 0 : k == "End" ? section.entries.length - 1 : (index + (k == "ArrowDown" ? 1 : section.entries.length - 1)) % max(1, section.entries.length))
          match section.entries.at(next)
            case some(entry)
              focus(id + "-" + section.id + "-" + entry.id)
            case none
              focus(id + "-" + section.id)
        case none
          focus(id)
    else if k == "ArrowRight" or k == "ArrowLeft"
      triggerKeys(sectionIndex, k)
  view
    row id=id testId=id resize=resized role="navigation" aria-label=label align-items="center" flex-wrap="wrap" gap="ui.space-2" position="relative" max-width="100%"
      // A persistent anchor moves during layout, so the shared panel can slide without reopening.
      view id=(id + "-anchor") aria-hidden=true pointer-events="none" position="absolute" left=anchorX top="0px" width=anchorWidth height=anchorHeight
      for section, i in sections key=section.id
        if section.entries.length > 0
          button id=(id + "-" + section.id) testId=(id + "-" + section.id) class=Control press=toggleSection(i) hover=triggerHover(i) key=triggerKeys(i) aria-expanded=(active == i) aria-controls=(id + "-content") aria-haspopup="true" popovertarget=(active >= 0 ? id + "-content" : "") popovertargetaction="show"
            display="flex" align-items="center" gap="ui.space-6" padding="ui.space-8 ui.space-12" border="0" border-radius="ui.radius-item" background-color=(active == i ? "ui.muted" : "transparent") color="ui.foreground" font-size="ui.text-sm" font-weight=uiWeights.medium transition=(reduced ? "none" : "background-color ui.duration-fast ui.easing")
            :hover
              background-color="ui.muted"
            :focus-visible
              box-shadow="ui.shadow-focus"
            text section.label font-family=uiFontFamily(uiFont) data-rightui-font="body"
            svg width="ui.space-12" height="ui.space-12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="ui.stroke-standard" rotate=(active == i ? "180deg" : "0deg") transition=(reduced ? "none" : "rotate ui.duration-normal ui.easing") aria-hidden=true
              path d="m6 9 6 6 6-6"
        else
          link id=(id + "-" + section.id) testId=(id + "-" + section.id) href=section.href class=Control
            display="flex" align-items="center" padding="ui.space-8 ui.space-12" border-radius="ui.radius-item" color="ui.foreground" text-decoration="none" font-size="ui.text-sm" font-weight=uiWeights.medium
            :hover
              background-color="ui.muted"
            :focus-visible
              box-shadow="ui.shadow-focus"
            text section.label font-family=uiFontFamily(uiFont) data-rightui-font="body"
      if active >= 0
        for epoch in [openEpoch] key=epoch
          column id=(id + "-content") testId=(id + "-content") role="region" aria-label=label popover="auto" toggle=changed(epoch) key=triggerKeys(active)
            position-area="bottom span-right" position-try-fallbacks="flip-inline, flip-block, flip-block flip-inline" margin="ui.space-8 0px" width="36rem" max-width=max(1, min(viewport.width - 32, availableWidth)) padding="0px" border="0" border-radius="0px" box-sizing="border-box" background-color="transparent" color="ui.foreground" box-shadow="none" overflow="visible"
            animation=(reduced or switching ? "none" : "uiPopupIn ui.duration-fast ui.easing both")
            -exact-layout-transition=(reduced ? "none" : "ui.duration-normal ui.easing")
            // Keep the resizing surface inside the top layer, including its transition paint.
            column id=(id + "-surface") width="100%" min-width="0px" padding="ui.space-8" box-sizing="border-box" border-width="ui.border-width" border-style="solid" border-color="ui.border" border-radius="ui.radius-menu" background-color="ui.surface" box-shadow="ui.shadow-menu" overflow="hidden" -exact-layout-transition=(reduced ? "none" : "ui.duration-normal ui.easing")
              for section, sectionIndex in sections key=section.id
                if active == sectionIndex
                  row flex-wrap="wrap" gap="ui.space-4" min-width="0px" width="100%" -exact-layout-transition=(reduced ? "none" : "ui.duration-normal ui.easing")
                    for entry, entryIndex in section.entries key=entry.id
                      link id=(id + "-" + section.id + "-" + entry.id) testId=(id + "-" + section.id + "-" + entry.id) href=entry.href key=linkKeys(sectionIndex, entryIndex)
                        display="flex" flex-direction="column" flex=(section.layout == "grid" ? "1 1 15rem" : "1 1 100%") box-sizing="border-box" gap="ui.space-4" padding="ui.space-12" border-radius="ui.radius-item" color="ui.foreground" text-decoration="none" min-width="0px"
                        :hover
                          background-color="ui.muted"
                        :focus-visible
                          background-color="ui.muted"
                          box-shadow="ui.shadow-focus"
                        text entry.title font-size="ui.text-sm" font-weight=uiWeights.medium font-family=uiFontFamily(uiFont) data-rightui-font="body"
                        text entry.description font-size="ui.text-sm" line-height="ui.leading-snug" color="ui.muted-foreground" font-family=uiFontFamily(uiFont) data-rightui-font="body"

```
