Navigation Menu Primitive
Use Navigation Menu for top-level links and panels, such as the sections of a product site. Each trigger opens its panel in a shared space.
Anatomy
Use the Astro primitive adapter to render NavigationMenu anatomy with the Runtime wiring included.
---import { NavigationMenu } from "@starwind-ui/astro/navigation-menu";---
<NavigationMenu.Root> <NavigationMenu.List> <NavigationMenu.Item value="products"> <NavigationMenu.Trigger> <NavigationMenu.Icon>v</NavigationMenu.Icon> </NavigationMenu.Trigger> <NavigationMenu.Content> <NavigationMenu.Link href="/docs">Docs</NavigationMenu.Link> </NavigationMenu.Content> </NavigationMenu.Item> </NavigationMenu.List> <NavigationMenu.Portal> <NavigationMenu.Positioner> <NavigationMenu.Popup> <NavigationMenu.Viewport /> <NavigationMenu.Arrow /> </NavigationMenu.Popup> </NavigationMenu.Positioner> </NavigationMenu.Portal></NavigationMenu.Root>Use the React primitive adapter when NavigationMenu state participates in React rendering.
import { NavigationMenu } from "@starwind-ui/react/navigation-menu";
export function Example() { return ( <NavigationMenu.Root> <NavigationMenu.List> <NavigationMenu.Item value="products"> <NavigationMenu.Trigger> <NavigationMenu.Icon>v</NavigationMenu.Icon> </NavigationMenu.Trigger> <NavigationMenu.Content> <NavigationMenu.Link href="/docs">Docs</NavigationMenu.Link> </NavigationMenu.Content> </NavigationMenu.Item> </NavigationMenu.List> <NavigationMenu.Portal> <NavigationMenu.Positioner> <NavigationMenu.Popup> <NavigationMenu.Viewport /> <NavigationMenu.Arrow /> </NavigationMenu.Popup> </NavigationMenu.Positioner> </NavigationMenu.Portal> </NavigationMenu.Root> );}Use the Vue components to build NavigationMenu.
<script setup lang="ts">import NavigationMenu from "@starwind-ui/vue/navigation-menu";</script>
<template> <NavigationMenu.Root> <NavigationMenu.List> <NavigationMenu.Item value="products"> <NavigationMenu.Trigger> <NavigationMenu.Icon>v</NavigationMenu.Icon> </NavigationMenu.Trigger> <NavigationMenu.Content> <NavigationMenu.Link href="/docs">Docs</NavigationMenu.Link> </NavigationMenu.Content> </NavigationMenu.Item> </NavigationMenu.List> <NavigationMenu.Portal> <NavigationMenu.Positioner> <NavigationMenu.Popup> <NavigationMenu.Viewport /> <NavigationMenu.Arrow /> </NavigationMenu.Popup> </NavigationMenu.Positioner> </NavigationMenu.Portal> </NavigationMenu.Root></template>Use the Svelte components to build NavigationMenu.
<script lang="ts"> import { NavigationMenu } from "@starwind-ui/svelte/navigation-menu";</script>
<NavigationMenu.Root> <NavigationMenu.List> <NavigationMenu.Item value="products"> <NavigationMenu.Trigger> <NavigationMenu.Icon>v</NavigationMenu.Icon> </NavigationMenu.Trigger> <NavigationMenu.Content> <NavigationMenu.Link href="/docs">Docs</NavigationMenu.Link> </NavigationMenu.Content> </NavigationMenu.Item> </NavigationMenu.List> <NavigationMenu.Portal> <NavigationMenu.Positioner> <NavigationMenu.Popup> <NavigationMenu.Viewport /> <NavigationMenu.Arrow /> </NavigationMenu.Popup> </NavigationMenu.Positioner> </NavigationMenu.Portal></NavigationMenu.Root>Render the NavigationMenu data-sw-* contract yourself, then initialize createNavigationMenu.
<nav data-sw-nav-menu> <ul data-sw-nav-menu-list> <li data-sw-nav-menu-item data-value="products"> <button data-sw-nav-menu-trigger type="button" aria-haspopup="menu"> <span data-sw-nav-menu-icon aria-hidden="true">v</span> </button> <div data-sw-nav-menu-content hidden> <a data-sw-nav-menu-link href="/docs">Docs</a> </div> </li> </ul> <div data-sw-nav-menu-portal data-sw-portal-placement="runtime"> <div data-sw-nav-menu-positioner> <div data-sw-nav-menu-popup hidden> <div data-sw-nav-menu-viewport hidden></div> <div data-sw-nav-menu-arrow aria-hidden="true"></div> </div> </div> </div></nav>
<script type="module"> import { createNavigationMenu } from "@starwind-ui/runtime/navigation-menu";
const root = document.querySelector("[data-sw-nav-menu]"); if (root) { createNavigationMenu(root); }</script>Astro, React, Vue, and Svelte components use the same underlying interactions. Each framework has its own way to read and update state. See the examples and your installed packageโs types for the available props and events. Plain HTML uses DOM events and Runtime methods.
Usage Guidelines
- Use Navigation Menu for top-level site or product navigation. Use it when a row or column of triggers reveals rich panels that share one viewport.
- Use Menu or Popover for isolated actions. Navigation Menu is value-driven and viewport-driven, so it is heavier than a single contextual popup.
Shared Viewport
Content panels are authored inside items, then moved into the shared viewport while active so the popup can animate size and direction consistently.
Floating Behavior
| Fact | Value |
|---|---|
| Anchor part | trigger |
| Positioner part | positioner |
| Popup part | popup |
| Portal part | portal |
| Option props | side, align, sideOffset, alignOffset, avoidCollisions, collisionPadding |
API Reference
Root
The navigation landmark that owns the active value, orientation, hover timing, dismissal options, and Runtime instance.
- Default element
- nav
- Discovery hook
- data-sw-nav-menu
- Role
- -
Props
value string | null -
- Description
- Controls the active item value when a framework adapter owns state.
- Kind
- control
- Targets
- root
- Full type
- string | null
- React
Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.
valuedefaultValueonValueChange
- Astro
Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.
valuedefaultValuestarwind:value-changesetValue
- Runtime / HTML
Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates.
data-default-valuestarwind:value-changesetValue
defaultValue string | null null
- Description
- Sets the initial uncontrolled active item value.
- Kind
- control
- Targets
- root
- Full type
- string | null
- React
Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.
valuedefaultValueonValueChange
- Astro
Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.
valuedefaultValuestarwind:value-changesetValue
- Runtime / HTML
Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates.
data-default-valuestarwind:value-changesetValue
openDelay number 50
- Description
- Sets the default hover-open delay in milliseconds.
- Kind
- option
- Targets
- root, trigger
- Full type
- number
closeDelay number 50
- Description
- Sets the default hover-close delay in milliseconds.
- Kind
- option
- Targets
- root, trigger
- Full type
- number
closeOnEscape boolean true
- Description
- Closes Navigation Menu when Escape is pressed.
- Kind
- option
- Targets
- -
- Full type
- boolean
closeOnOutsideInteract boolean true
- Description
- Closes Navigation Menu when the user interacts outside it.
- Kind
- option
- Targets
- -
- Full type
- boolean
onValueChange (value: string | null, details: NavigationMenuValueChangeDetails) => void -
- Description
- Runs when the Navigation Menu value changes.
- Kind
- callback
- Targets
- -
- Full type
- (value: string | null, details: NavigationMenuValueChangeDetails) => void
- React
Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.
valuedefaultValueonValueChange
- Astro
Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.
valuedefaultValuestarwind:value-changesetValue
- Runtime / HTML
Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates.
data-default-valuestarwind:value-changesetValue
orientation "horizontal" | "vertical" "horizontal"
- Description
- Sets horizontal or vertical trigger keyboard movement.
- Kind
- option
- Targets
- root
- Full type
- "horizontal" | "vertical"
Events
valueChange onValueChange value: string | null
- Description
- Fires when the value changes for Navigation Menu.
- DOM event
- starwind:value-change
- Details type
- NavigationMenuValueChangeDetails
- Timing
- before-state-commit
- Cancelable
- Yes
- Cancellation sequence
- Check internal eligibility and intent.
- Create one details object for the proposal.
- Call the Runtime callback with the details object when the controller exposes one.
- Dispatch the cancelable DOM event with the same details object, including when the callback canceled it.
- Read details.isCanceled, including cancellation caused by preventDefault().
- Apply the accepted state.
- Notify Runtime subscribers and other accepted-only observers.
State
value string | null value / defaultValue
- Description
- Tracks the current Navigation Menu value.
- Initial attribute
- data-default-value
- Runtime getter
- getValue
- Runtime setter
- setValue
- React
Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.
valuedefaultValueonValueChange
- Astro
Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.
valuedefaultValuestarwind:value-changesetValue
- Runtime / HTML
Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates.
data-default-valuestarwind:value-changesetValue
Data Attributes
Runtime hooks
State
Metadata
List
The list of selectable Navigation Menu items.
- Default element
- ul
- Discovery hook
- data-sw-nav-menu-list
- Role
- -
Data Attributes
Runtime hooks
Item
An interactive item inside the Navigation Menu collection.
- Default element
- li
- Discovery hook
- data-sw-nav-menu-item
- Role
- -
Props
value string -
- Description
- Controls the current Navigation Menu value.
- Kind
- option
- Targets
- item
- Full type
- string
- React
Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.
valuedefaultValueonValueChange
- Astro
Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.
valuedefaultValuestarwind:value-changesetValue
- Runtime / HTML
Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates.
data-default-valuestarwind:value-changesetValue
Data Attributes
Runtime hooks
State
Metadata
Trigger
The button or asChild control that opens, closes, and keyboard-targets a content panel.
- Default element
- button
- Discovery hook
- data-sw-nav-menu-trigger
- Role
- -
Props
openDelay number -
- Description
- Overrides root hover-open timing for this trigger.
- Kind
- option
- Targets
- root, trigger
- Full type
- number
closeDelay number -
- Description
- Overrides root hover-close timing for this trigger.
- Kind
- option
- Targets
- root, trigger
- Full type
- number
asChild boolean -
- Description
- Merges trigger behavior into the slotted child control.
- Kind
- rendering
- Targets
- trigger
- Full type
- boolean
disabled boolean false
- Description
- Prevents trigger interaction and removes it from roving trigger movement.
- Kind
- option
- Targets
- trigger
- Full type
- boolean
Data Attributes
Runtime hooks
State
Metadata
Icon
Decorative icon rendered by Navigation Menu.
- Default element
- span
- Discovery hook
- data-sw-nav-menu-icon
- Role
- -
Data Attributes
Runtime hooks
State
Content
The authored item panel. The Runtime moves the active content into the shared viewport.
- Default element
- div
- Discovery hook
- data-sw-nav-menu-content
- Role
- -
Data Attributes
Runtime hooks
State
Link
A navigation link inside content. Links close the active menu by default and can mark current-page state.
- Default element
- a
- Discovery hook
- data-sw-nav-menu-link
- Role
- -
Props
href string -
- Description
- Sets the href attribute on the Link part.
- Kind
- attribute
- Targets
- link
- Full type
- string
active boolean false
- Description
- Marks the link as active and sets aria-current.
- Kind
- option
- Targets
- link
- Full type
- boolean
closeOnClick boolean true
- Description
- Controls whether clicking the link closes the menu.
- Kind
- option
- Targets
- link
- Full type
- boolean
Data Attributes
Runtime hooks
Metadata
Portal
Moves Navigation Menu overlay content to the document body when needed.
- Default element
- div
- Discovery hook
- data-sw-nav-menu-portal
- Role
- -
Data Attributes
Runtime hooks
Metadata
Positioner
The floating positioning part for the shared popup.
- Default element
- div
- Discovery hook
- data-sw-nav-menu-positioner
- Role
- -
Props
side "top" | "right" | "bottom" | "left" "bottom"
- Description
- Sets the preferred side for the popup.
- Kind
- option
- Targets
- positioner
- Full type
- "top" | "right" | "bottom" | "left"
align "start" | "center" | "end" "start"
- Description
- Sets popup alignment relative to the active trigger.
- Kind
- option
- Targets
- positioner
- Full type
- "start" | "center" | "end"
sideOffset number 4
- Description
- Sets the distance between the popup and trigger.
- Kind
- option
- Targets
- positioner
- Full type
- number
alignOffset number 0
- Description
- Adjusts the cross-axis alignment offset for Navigation Menu content.
- Kind
- option
- Targets
- positioner
- Full type
- number
avoidCollisions boolean true
- Description
- Allows the popup to shift or flip to remain visible.
- Kind
- option
- Targets
- positioner
- Full type
- boolean
collisionPadding number 8
- Description
- Configures the collision padding option for the Positioner part.
- Kind
- option
- Targets
- positioner
- Full type
- number
Data Attributes
Runtime hooks
State
Metadata
Popup
The floating content container for Navigation Menu.
- Default element
- div
- Discovery hook
- data-sw-nav-menu-popup
- Role
- -
Data Attributes
Runtime hooks
State
Viewport
The shared visible container that receives active item content and carries measured size state.
- Default element
- div
- Discovery hook
- data-sw-nav-menu-viewport
- Role
- -
Data Attributes
Runtime hooks
State
Arrow
The arrow element that visually points to the trigger.
- Default element
- div
- Discovery hook
- data-sw-nav-menu-arrow
- Role
- -
Data Attributes
Runtime hooks
State
Runtime API
- Factory
createNavigationMenu- Import
@starwind-ui/runtime/navigation-menu- Root hook
- root
data-sw-nav-menu - Option props
- closeDelay, closeOnEscape, closeOnOutsideInteract, defaultValue, onValueChange, openDelay, value
Option Lifecycles
| Option | Lifecycle |
|---|---|
| closeDelay | constructor-only |
| closeOnEscape | constructor-only |
| closeOnOutsideInteract | constructor-only |
| defaultValue | constructor-only |
| onValueChange | constructor-only |
| openDelay | constructor-only |
| value | setter-backed |
Runtime Setters
Related Styled Components
Changelog
View version history v1.3.0 7 releases
v1.3.0
- Navigation Menu trigger keys follow visible horizontal order. Menus that move into a portal keep the direction of their trigger.
- Navigation Menu now returns focus without scrolling the page. Before, in Safari, closing its content after scrolling could make the page jump back to the trigger.
- A click during the Navigation Menu close animation now goes to the page below the popup. Before, the closing popup caught the click, so a quick click on a control under it did nothing.
- When you close a Navigation Menu with Escape, focus now goes back to the trigger as the close starts. The closing menu takes no keyboard input until it hides.
- Navigation Menu now stays on screen when its trigger is at the edge of the screen, for example on a phone. Before, the popup kept its alignment, so part of it went off screen. Now the popup moves along its trigger until it is inside the viewport padding. It stays next to the trigger and keeps its side.
- The positioner now sets `--sw-floating-arrow-x` for a top or bottom side and `--sw-floating-arrow-y` for a left or right side when the Navigation Menu arrow has a width and a height. These values point the arrow at the center of the trigger when the arrow can reach it. Otherwise the arrow points at the nearest part of the trigger. The arrow stays inside the popup, at least 6px from each end. Each value is the distance from the left or top edge of the popup to the same edge of the arrow, in RTL too. To use it, position a custom arrow absolutely inside the popup, set `left: var(--sw-floating-arrow-x)` or `top: var(--sw-floating-arrow-y)`, and remove any centering transform on that axis.
- In Svelte, the `children` snippet of `NavigationMenu.Root` no longer receives arguments. If you read the value from a snippet argument, use `bind:value` on the root instead.
- In Vue, this Primitive now uses the same `asChild` child handling as the other Vue Primitives. Its public API is unchanged. An invalid `asChild` child now gives one shorter error message.
- In React and Vue, a Navigation Menu with a `defaultValue` no longer opens again after the user closes it and a prop on its Root changes.
- In Svelte, the server HTML of `NavigationMenu.Root` now prints `data-default-value`, as in the other frameworks.
- In Vue, this component now needs Vue 3.5.21 or newer.
- Internal code cleanup, such as unused code and import order. This cleanup does not change behavior.
v1.2.0
- Fixed a Navigation Menu panel that flashed and closed when you moved the mouse to a trigger and clicked it. A click that starts within 500 ms after hover opened the panel now keeps it open. A second click, a later click, the keyboard, and touch still close it.
- In Astro, an attribute that you set on a Navigation Menu part now wins over the part's own default for the same attribute, such as `role` or `aria-label`.
- In Svelte, a defined `value` that you pass when NavigationMenu mounts now stays under your control, as in React and Vue. To change it, use `bind:` or update your state in the change callback. A form reset keeps it. If you pass `undefined` at mount, NavigationMenu keeps its own state. The first render now follows the initial value.
- In Svelte, `NavigationMenu.Popup` no longer takes `side` or `align`. Set them on `NavigationMenu.Positioner`.
- In Vue, the template ref of the Navigation Menu root no longer has the undocumented methods `open`, `close`, `updatePosition`, `getValue`, and `setValue`. It exposes only `element`. Use `v-model` on the root instead.
- Navigation Menu no longer moves focus back to its trigger when you move to another control while it closes.
- In Vue, Navigation Menu now moves keyboard focus into its content when you use `v-model`.
- In Svelte, the Navigation Menu trigger now accepts a link in `child`. Before, it warned in development, type checks rejected the link, and `ref` did not run. Use the new `TriggerChildPayload` type for your snippet.
v1.1.1
- Fixed links after menu content moves into the shared viewport. They follow your close-on-select settings and affect the correct menu when menus are nested.
- Reduced background work when a React page uses multiple menus, tooltips, or other portals.
- Fixed nested Svelte overlays inside a dialog appearing behind their parent or failing to receive clicks.
- Reduced background work when a Vue page uses multiple menus, tooltips, or other portals.
- Fixed nested overlays appearing behind their parent in Vue and Svelte. Overlays can now render under the document body; if custom CSS or DOM queries require them to stay inside a parent, set a portal container or add `data-floating-root` to that parent.
- Fixed React handling of menu content added to or removed from the shared viewport.
- Fixed React 18 retaining removed popup elements when the parent component stays mounted.
- Reduced repeated JavaScript when Astro Navigation Menu is used with other Starwind components.
- Reduced JavaScript used by Svelte Navigation Menu. Element props, event handlers, and refs keep the same behavior.
v1.1.0
- Added `container` and `disabled` to the Portal part. `container` sets the element that holds the popup, and `disabled` keeps the popup in place. Portals now place nested popups in the same way every time, and they show their placement in data attributes.
v1.0.0
- This Primitive is now stable at version `1.0.0`. Its API and behavior stay the same as in the previous release.
v0.1.1
- The React Primitive files that the CLI copies into your project are now marked with `"use client"`, so they stay client components in React server frameworks.
v0.1.0
- First release. Navigation Menu tracks the open item and supports the keyboard. It moves its indicator and shows the panels in one shared area.