Navigation Menu Primitive
Navigation Menu coordinates a single active top-level item, shared viewport content, hover timing, keyboard movement, and link close behavior for site navigation. Astro and React share one semantic component API. React coordinates reactive state through controlled and default props plus callbacks. Astro renders initial state and coordinates later changes through DOM events and Runtime methods. Raw HTML uses Runtime attributes, DOM events, and imperative 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.
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> );}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> <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>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 50
- Description
- Overrides root hover-open timing for this trigger.
- Kind
- option
- Targets
- root, trigger
- Full type
- number
closeDelay number 50
- 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
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, popup
- Full type
- "top" | "right" | "bottom" | "left"
align "start" | "center" | "end" "start"
- Description
- Sets popup alignment relative to the active trigger.
- Kind
- option
- Targets
- positioner, popup
- 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
- -
Props
side "top" | "right" | "bottom" | "left" "bottom"
- Description
- Sets the preferred side for Navigation Menu content.
- Kind
- option
- Targets
- positioner, popup
- Full type
- "top" | "right" | "bottom" | "left"
align "start" | "center" | "end" "start"
- Description
- Sets how Navigation Menu content aligns to its trigger.
- Kind
- option
- Targets
- positioner, popup
- Full type
- "start" | "center" | "end"
Data Attributes
Runtime hooks
State
Metadata
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.0.0 3 releases
v1.0.0
- Promoted this Primitive's vendoring version to the stable 1.0.0 baseline. Its existing API and Runtime behavior carry forward from the previous release.
v0.1.1
- Marked generated React Primitive files as client modules so vendored installs preserve client boundaries in React server frameworks.
v0.1.0
- Introduced navigation-menu state, keyboard interaction, indicator positioning, and shared viewport behavior.