suiss/ui · Component
Navigation Menu
A site navigation bar whose items open popover or full-width panels of links.
Navigation Menu is an open-source component from suiss/ui for React. The @esuiss/ui CLI copies its source into your project, so you own the code.
Navigation Menu wraps the navigation menu primitive of the chosen base. NavigationMenu is the root; inside it, NavigationMenuList holds NavigationMenuItem entries. An item contains either a NavigationMenuTrigger with NavigationMenuContent, or a single NavigationMenuLink. Inside content, use NavigationMenuLabel for column headings and NavigationMenuLink for destinations.
The root renders the popup for you. With layout="popover" it adds a NavigationMenuPositioner, a floating card that resizes and slides between items. With layout="panel" it adds a NavigationMenuPanel, a full-width sheet under the bar with a blurred backdrop, like a store's global navigation. Use Navigation Menu for site navigation links; use Menubar or Dropdown Menu for commands.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
- Install the following dependencies and add the
cnhelper:
- Copy and paste the following code into your project.
- Update the import paths to match your project setup.
Usage
Composition
Use the following composition to build a NavigationMenu:
Examples
By default
Give each item with content a value, then set defaultValue on the root to open the Products item on mount.
Links only
Pass navigationMenuTriggerStyle() as a link's className to style it like a trigger. Set active on the link for the current page.
Panel
Set layout="panel" and pass a ref to a sticky header as anchor so the panel spans its width. The columns use NavigationMenuLabel headings, and size="lg" links for primary destinations.
Accessibility
- Triggers are buttons with
aria-expandedreflecting whether their content is open. - Enter opens a focused trigger, and Tab moves focus into the open content.
- Escape closes the open content and returns focus to its trigger.
- Hovering another trigger switches the open content.
- A link with
activeset receivesaria-current="page". - The trigger's caret icon is
aria-hidden.
API Reference
NavigationMenu
The root that manages open state and renders the popover or panel popup.
| Prop | Type | Default |
|---|---|---|
layout | "popover" | "panel" | "popover" |
align | "start" | "center" | "end" | "start" |
| Prop | Type | Default |
|---|---|---|
anchor | Element | React.RefObject<Element | null> | VirtualElement | (() => Element | VirtualElement | null) | null | - |
| Prop | Type | Default |
|---|---|---|
anchor | React.RefObject<HTMLElement | null> | - |
Also accepts the props of the primitive parts it wraps.
layout="popover" shows a floating card, and layout="panel" a full-width sheet with a backdrop. align sets how the card lines up with the bar in the popover layout. anchor is the element the popup is positioned against; in the panel layout, pass a ref to the full-width bar. Give each item with content a value so the menu can be opened by default or controlled with value, defaultValue and onValueChange.
NavigationMenuList
The horizontal list of top-level items.
Also accepts the props of the primitive part it wraps.
NavigationMenuItem
One top-level entry, holding a trigger with content or a single link.
Also accepts the props of the primitive part it wraps.
NavigationMenuTrigger
The button that opens its item's content, with a caret in the popover layout.
Also accepts the props of the primitive part it wraps.
NavigationMenuContent
The content shown in the popup when its item is open.
Also accepts the props of the primitive part it wraps.
NavigationMenuPositioner
The floating popover card used by the popover layout. NavigationMenu renders it for you.
| Prop | Type | Default |
|---|---|---|
side | "top" | "right" | "bottom" | "left" | "inline-start" | "inline-end" | "bottom" |
sideOffset | number | 8 |
align | "start" | "center" | "end" | "start" |
alignOffset | number | 0 |
| Prop | Type | Default |
|---|---|---|
align | "start" | "center" | "end" | "start" |
anchor | React.RefObject<HTMLElement | null> | - |
Also accepts the props of the primitive part it wraps.
side is the side of the bar the card opens on, and sideOffset the gap in pixels between them. alignOffset shifts the card in pixels along the alignment axis.
anchor is the element the card is positioned against.
NavigationMenuPanel
The full-width sheet and backdrop used by the panel layout. NavigationMenu renders it for you.
| Prop | Type | Default |
|---|---|---|
anchor | React.RefObject<HTMLElement | null> | - |
Also accepts the props of the primitive part it wraps.
NavigationMenuLink
A navigation link, either in the bar or inside content.
| Prop | Type | Default |
|---|---|---|
size | "default" | "lg" | "default" |
Also accepts the props of the primitive part it wraps.
size="lg" uses large, semibold text for primary destinations. Set active to mark the current page. To style a top-level link like a trigger, pass navigationMenuTriggerStyle() as its className. Triggers and links restyle themselves in the panel layout, and panel list items animate in with a stagger that respects reduced motion.
NavigationMenuLabel
A small secondary heading above a group of links.
Also accepts the props of React.ComponentProps<"p">.
NavigationMenuIndicator
A small arrow that fades in while its menu is open.
Also accepts the props of the primitive part it wraps.
Each part sets a data-slot, such as navigation-menu-trigger or navigation-menu-panel, for styling. The root sets data-layout, and links set data-size and data-active.