Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Navigation Menu for Svelte UI SDK

A header navigation bar for Svelte on UI SDK: top-level links in a row, some opening a panel of related pages, for docs and marketing sites.

On this page9 sections

A navigation menu is the row of links across a site's header. Some items go straight to a page, and others open a panel of related pages under their trigger: product areas, a grid of docs sections, a list of resources. It renders a nav element, so screen reader users can jump straight to it.

For the many destinations of an app, use a Sidebar. Actions such as Export or Delete belong in a Dropdown Menu.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Navigation Menu anatomy: list, trigger, indicator, content, link

Usage

<script lang="ts">
import {
  NavigationMenu, NavigationMenuContent, NavigationMenuItem,
  NavigationMenuLink, NavigationMenuList, NavigationMenuTrigger,
  navigationMenuTriggerStyle,
} from "@/components/ui/navigation-menu";
</script>

Each NavigationMenuItem holds either a trigger with its content or a single link. Give a top-level link the classes from navigationMenuTriggerStyle() so it lines up with the triggers beside it.

<NavigationMenu>
  <NavigationMenuList>
    <NavigationMenuItem>
      <NavigationMenuTrigger>Products</NavigationMenuTrigger>
      <NavigationMenuContent>
        <ul class="grid w-64 gap-1">
          <li>
            <NavigationMenuLink href="/analytics">Analytics</NavigationMenuLink>
          </li>
          <li>
            <NavigationMenuLink href="/billing">Billing</NavigationMenuLink>
          </li>
        </ul>
      </NavigationMenuContent>
    </NavigationMenuItem>
    <NavigationMenuItem>
      <NavigationMenuLink class={navigationMenuTriggerStyle()} href="/pricing">
        Pricing
      </NavigationMenuLink>
    </NavigationMenuItem>
  </NavigationMenuList>
</NavigationMenu>

Accessibility

NavigationMenu renders a nav element, and each trigger is a button that reports aria-expanded. Its panel opens on hover after a short delay, on click, or with Enter or Space, and ArrowDown opens it and moves focus to the first link. Tab from an open trigger goes into its panel and on to the next item after the last link, and the arrow keys, Home and End move along the bar and between the links of a panel. Escape closes a panel and returns focus to its trigger, and choosing a link closes it too. Add an aria-label to NavigationMenu when the page has a second nav, such as one in the footer.

Enter
On a trigger, opens or closes its panel and leaves focus on the trigger. On a link, follows it and closes the panel.
Space
On a trigger, opens or closes its panel.
Arrow Down
On a trigger, opens its panel and moves focus to its first link. Inside an open panel, moves focus to the next link.
Arrow Up
Inside an open panel, moves focus to the previous link.
Arrow Right
Moves focus to the next trigger or link in the bar, stopping at the last one. Inside an open panel, moves to the next link.
Arrow Left
Moves focus to the previous trigger or link in the bar, stopping at the first one. Inside an open panel, moves to the previous link.
Home
Moves focus to the first item in the bar, or the first link in the open panel.
End
Moves focus to the last item in the bar, or the last link in the open panel.
Tab
Moves focus to the next trigger or link. From a trigger whose panel is open, moves into the panel, and from its last link on to the next item in the bar.
Shift+Tab
Moves focus back, and from the first link of an open panel to its trigger.
Escape
Closes the open panel and returns focus to its trigger.

Demos

RTL

The default header in Arabic, inside a Direction provider and a wrapper, both set to right to left. UI SDK reads the direction from the elements around the bar, so the bar runs right to left and ArrowLeft moves to the next item. The panels open in a portal but keep the direction of the place the menu is written, so they read right to left too.

Preview width

FAQ

Why does the Navigation Menu panel stay open after I click a link?

It shouldn't: a NavigationMenuLink closes the open panel when it's clicked, unless a modifier key such as ⌘ or Ctrl is held to open it in a new tab. A panel that stays open usually holds a plain anchor or a router link that isn't rendered through NavigationMenuLink, so pass it through render. Set closeOnClick to false on a link that should leave the panel open.

Does the UI SDK Navigation Menu open on hover, and can I change the delay?

It does. A panel opens once the pointer has rested on its trigger for 50 milliseconds and closes 50 milliseconds after it leaves. Set delay and closeDelay on NavigationMenu to change either, and raise them when panels flash open as people cross the header on the way to something else.

How do I control which Navigation Menu panel is open in Svelte?

Pass value and onValueChange to NavigationMenu, and give each NavigationMenuItem a value to match. null closes every panel. onValueChange receives the new value first and the event details second, whose reason says whether a hover, a click or a key changed it.

How do I highlight the current page in the Navigation Menu?

Set active on the NavigationMenuLink whose address matches the current route. UI SDK then adds aria-current="page", which screen readers announce, and this file adds data-active, which a data-active: class can style. When the page sits inside a panel, mark that panel's trigger with a class of your own so the bar shows the section.

How do I set the width of a Navigation Menu panel?

A panel is as wide as what you put in it, so set the width on the list inside NavigationMenuContent. The demos use w-96 for the Getting started list and a grid that grows from 400 to 600 pixels with the screen.

Every panel opens in one shared popup that resizes and slides as you move between triggers, lined up with the active trigger's start edge. Set align on NavigationMenu to center or end to line it up differently.

How do I make the Navigation Menu work on mobile?

The bar has no small-screen layout of its own: its items stay in one row, and a wide panel is squeezed to fit the screen. Below a breakpoint, hide the menu with a class such as hidden md:flex and put the same links in a Sheet behind a menu button, where each one gets a full-width tap target.

The demos already hide the Components item below md for this reason.

When should I use a Navigation Menu instead of a Dropdown Menu or a Menubar?

Use a Navigation Menu to move between the pages of a site, where every item is a link or opens a panel of links. A Dropdown Menu holds actions on the current page, such as Export or Delete, with menu roles and keys built for commands.

A Menubar is an app's row of File, Edit and View menus. When an app has dozens of destinations, a Sidebar keeps them in view instead.

API Reference

<nav> data-slot="navigation-menu"

PropType
value?T | null

The open item's value. Pass it to control the menu. null closes every panel. Two-way with bind:value.

Type
T | null
defaultValue?T | null

The item open at first, when uncontrolled.

Type
T | null
Default
null
onValueChange?(value: T | null, details: ChangeDetails<NavigationMenuValueChangeReason>) → void

Runs when an item wants to open or the open one to close. details.reason says why, and details.cancel() stops it.

Type
(value: T | null, details: ChangeDetails<NavigationMenuValueChangeReason>) → void
Parameters

value – T | null

details – ChangeDetails<NavigationMenuValueChangeReason>

Returns
void
align?Align

How the open panel lines up with its trigger.

Default
"start"
closeDelay?number

Milliseconds before the open panel closes once the pointer leaves its trigger and the panel.

Default
50
closeOnSelect?boolean

Whether pressing a link closes the menu, like after a client-side route change. A link's own closeOnSelect wins. A press with a modifier key, which opens a new tab, never closes it.

Default
true
container?DocumentFragment | Element | null

Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.

Type
DocumentFragment | Element | null
controller?{ /** The open item's value, or null when every panel is closed. */ getValue: () → string | null; /** Opens an item's panel, or closes the open one with `null`. Returns false when canceled. */ setValue: (value: string | null, options?: NavigationMenuChangeOptions) → boolean; /** Whether a panel is open. */ isOpen: () → boolean; /** Closes the open panel. Returns false when canceled or already closed. */ close: (options?: NavigationMenuChangeOptions) → boolean; /** Moves focus to an item's trigger: the open one, or the first one, without a value. */ focus: (value?: string) → void; }

Opens and closes panels from anywhere. Create one with createNavigationMenuController().

Type
{ /** The open item's value, or null when every panel is closed. */ getValue: () → string | null; /** Opens an item's panel, or closes the open one with `null`. Returns false when canceled. */ setValue: (value: string | null, options?: NavigationMenuChangeOptions) → boolean; /** Whether a panel is open. */ isOpen: () → boolean; /** Closes the open panel. Returns false when canceled or already closed. */ close: (options?: NavigationMenuChangeOptions) → boolean; /** Moves focus to an item's trigger: the open one, or the first one, without a value. */ focus: (value?: string) → void; }
debug?boolean

Logs every state change with its reason to the console, in development builds.

delay?number

Milliseconds the pointer rests on a trigger before its panel opens. Moving to another trigger while one is open switches at once.

Default
50
onOpenChangeComplete?(open: boolean) → void

Runs once the menu has finished opening or closing, after any animation. Switching items doesn't count.

Parameters

open – boolean

Returns
void
openOnHover?boolean

Whether resting the pointer on a trigger opens its panel. Pressing a trigger always does, so false makes a click-only menu.

Default
true
orientation?NavigationMenuOrientation

Which arrow keys move between the top-level items, and the side panels open on: below a horizontal menu, beside a vertical one.

Default
"horizontal"
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-nested?string

Present on a menu inside another menu's content.

data-orientation"horizontal" | "vertical"

Which arrow keys move between the items, and where panels open.

Type
"horizontal" | "vertical"

<ul> data-slot="navigation-menu-list"

PropType
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-has-icons?string

Present while any link inside holds an icon part, so items without one can line up.

data-orientation"horizontal" | "vertical"

Which arrow keys move between the items, and where panels open.

Type
"horizontal" | "vertical"

<li> data-slot="navigation-menu-item"

PropType
value?string

Identifies the item, for value and onValueChange. Generated when left out.

render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-closed?string

Present while the item's panel is closed, including while it animates out.

data-open?string

Present while the item's panel is open.

<button> data-slot="navigation-menu-trigger"

PropType
disabled?boolean

Stops the trigger from opening its panel. A native button can't be focused or pressed.

Default
false
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-closed?string

Present while the item's panel is closed, including while it animates out.

data-disabled?string

Present when the trigger is disabled.

data-open?string

Present while the item's panel is open.

<div> data-slot="navigation-menu-content"

PropType
align?Align

How the popup lines up with the anchor along that side. start and end follow the reading direction. How it lines up with the trigger along that side.

Default
"start"
alignOffset?number

Shift along the anchor, in pixels. Shift along the trigger, in pixels.

Default
0
anchor?string | Anchor | null

Positions the popup against this instead of its trigger: an element, a CSS selector, a point like { x, y }, or anything with getBoundingClientRect(). It's read on every check, so it can move. A controller's open({ anchor }) wins for that open.

Type
string | Anchor | null
arrowPadding?number

Space to keep between the arrow and the popup's corners, in pixels.

Default
6
collisionPadding?number

Space to keep from the viewport edges, in pixels.

Default
8
container?DocumentFragment | Element | null

Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.

Type
DocumentFragment | Element | null
hiddenUntilFound?boolean

Whether closed content stays findable with hidden="until-found", so find in page opens its item. Implies keepMounted.

Default
false
keepMounted?boolean

Whether the content stays in the page, hidden, while its item is closed, so crawlers and the server's HTML have it. Closed content stays in its item; it moves into the viewport when it opens.

Default
false
side?Side

Which side of the anchor the popup opens on. Flips to the other side when there isn't room. Which side of the trigger it opens on: below a horizontal menu, at the inline end of a vertical one.

Default
"bottom"
sideOffset?number

Gap between the anchor and the popup, in pixels. Gap between the trigger and it, in pixels.

Default
4
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-align"start" | "center" | "end"

How it lines up with the trigger.

Type
"start" | "center" | "end"
data-closed?string

Present while the item's panel is closed, including while it animates out.

data-ending-style?string

Present while it animates out. Style the state you animate to.

data-motion"from-start" | "from-end" | "to-start" | "to-end"

Which way the content moves while another item takes over. Order is logical, so in right-to-left layouts the end is on the left.

Type
"from-start" | "from-end" | "to-start" | "to-end"
data-open?string

Present while the item's panel is open.

data-orientation"horizontal" | "vertical"

Which arrow keys move between the items, and where panels open.

Type
"horizontal" | "vertical"
data-side"top" | "right" | "bottom" | "left"

Where the viewport, or a content without one, ended up after flipping away from edges.

Type
"top" | "right" | "bottom" | "left"
data-starting-style?string

Present for the first frame. Style the state you animate from.

CSS variableType
--anchor-heightstring

Without a viewport: height of the anchor.

--anchor-widthstring

Without a viewport: width of the anchor, usually the trigger. Use it to match widths.

--arrow-offsetstring

Without a viewport: where the anchor's center falls along the popup's edge, kept arrowPadding from the corners. Position a ::before arrow with it.

--available-heightstring

Without a viewport: space between the anchor and the viewport edge. Use it for max-height. Not inherited, so a child that needs it copies it on the popup, like [--list-height:var(--available-height)], and reads max-h-(--list-height). The first popup registers both names for the whole page with CSS.registerProperty, so they don't inherit anywhere. A page that needs them to inherit calls it first, with inherits: true. An @property rule can't, since a registration in script wins.

--available-widthstring

Without a viewport: space beside the popup, up to the viewport edge. Not inherited either.

--navigation-menu-available-widthstring
--transform-originstring

Without a viewport: the point on the anchor the popup opens from. Use it to scale from the right place.

<div> data-slot="navigation-menu-viewport"

PropType
align?Align

How the popup lines up with the anchor along that side. start and end follow the reading direction. How it lines up with the trigger along that side.

Default
"start"
alignOffset?number

Shift along the anchor, in pixels. Shift along the trigger, in pixels.

Default
0
anchor?string | Anchor | null

Positions the popup against this instead of its trigger: an element, a CSS selector, a point like { x, y }, or anything with getBoundingClientRect(). It's read on every check, so it can move. A controller's open({ anchor }) wins for that open.

Type
string | Anchor | null
arrowPadding?number

Space to keep between the arrow and the popup's corners, in pixels.

Default
6
collisionPadding?number

Space to keep from the viewport edges, in pixels.

Default
8
container?DocumentFragment | Element | null

Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.

Type
DocumentFragment | Element | null
side?NavigationMenuSide

The side of the trigger it opens on. It flips when there's no room, unless avoidCollisions is off.

sideOffset?number

Gap between the anchor and the popup, in pixels. Gap between the trigger and it, in pixels. UI SDK's default: 4.

Default
8
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-align"start" | "center" | "end"

How it lines up with the trigger.

Type
"start" | "center" | "end"
data-closed?string

Present while every panel is closed, including while it animates out.

data-ending-style?string

Present while it animates out. Style the state you animate to.

data-open?string

Present while a panel is open.

data-orientation"horizontal" | "vertical"

Which arrow keys move between the items, and where panels open.

Type
"horizontal" | "vertical"
data-side"top" | "right" | "bottom" | "left"

Where the viewport, or a content without one, ended up after flipping away from edges.

Type
"top" | "right" | "bottom" | "left"
data-starting-style?string

Present for the first frame. Style the state you animate from.

CSS variableType
--anchor-heightstring

Height of the anchor.

--anchor-widthstring

Width of the anchor, usually the trigger. Use it to match widths.

--arrow-offsetstring

Where the anchor's center falls along the popup's edge, kept arrowPadding from the corners. Position a ::before arrow with it.

--available-heightstring

Space between the anchor and the viewport edge. Use it for max-height. Not inherited, so a child that needs it copies it on the popup, like [--list-height:var(--available-height)], and reads max-h-(--list-height). The first popup registers both names for the whole page with CSS.registerProperty, so they don't inherit anywhere. A page that needs them to inherit calls it first, with inherits: true. An @property rule can't, since a registration in script wins.

--available-widthstring

The width left between the anchor and the viewport's edge.

--transform-originstring

Where the content scales from, on its trigger's side.

--viewport-heightstring

The open content's height plus the viewport's own border.

--viewport-widthstring

The open content's width plus the viewport's own border. Transition width for a viewport that resizes as it switches.

<div> data-slot="navigation-menu-viewport"

PropType
align?Align

How the popup lines up with the anchor along that side. start and end follow the reading direction. How it lines up with the trigger along that side.

Default
"start"
alignOffset?number

Shift along the anchor, in pixels. Shift along the trigger, in pixels.

Default
0
anchor?string | Anchor | null

Positions the popup against this instead of its trigger: an element, a CSS selector, a point like { x, y }, or anything with getBoundingClientRect(). It's read on every check, so it can move. A controller's open({ anchor }) wins for that open.

Type
string | Anchor | null
arrowPadding?number

Space to keep between the arrow and the popup's corners, in pixels.

Default
6
collisionPadding?number

Space to keep from the viewport edges, in pixels.

Default
8
container?DocumentFragment | Element | null

Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.

Type
DocumentFragment | Element | null
side?NavigationMenuSide
sideOffset?number

Gap between the anchor and the popup, in pixels. Gap between the trigger and it, in pixels.

Default
4
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-align"start" | "center" | "end"

How it lines up with the trigger.

Type
"start" | "center" | "end"
data-closed?string

Present while every panel is closed, including while it animates out.

data-ending-style?string

Present while it animates out. Style the state you animate to.

data-open?string

Present while a panel is open.

data-orientation"horizontal" | "vertical"

Which arrow keys move between the items, and where panels open.

Type
"horizontal" | "vertical"
data-side"top" | "right" | "bottom" | "left"

Where the viewport, or a content without one, ended up after flipping away from edges.

Type
"top" | "right" | "bottom" | "left"
data-starting-style?string

Present for the first frame. Style the state you animate from.

CSS variableType
--anchor-heightstring

Height of the anchor.

--anchor-widthstring

Width of the anchor, usually the trigger. Use it to match widths.

--arrow-offsetstring

Where the anchor's center falls along the popup's edge, kept arrowPadding from the corners. Position a ::before arrow with it.

--available-heightstring

Space between the anchor and the viewport edge. Use it for max-height. Not inherited, so a child that needs it copies it on the popup, like [--list-height:var(--available-height)], and reads max-h-(--list-height). The first popup registers both names for the whole page with CSS.registerProperty, so they don't inherit anywhere. A page that needs them to inherit calls it first, with inherits: true. An @property rule can't, since a registration in script wins.

--available-widthstring

Space beside the popup, up to the viewport edge. Not inherited either.

--transform-originstring

The point on the anchor the popup opens from. Use it to scale from the right place.

--viewport-heightstring

The open content's height plus the viewport's own border.

--viewport-widthstring

The open content's width plus the viewport's own border. Transition width for a viewport that resizes as it switches.

<a> data-slot="navigation-menu-link"

PropType
active?boolean

Marks the link to the current page: aria-current="page".

closeOnClick?boolean

Close the open panel when the link is clicked, as after a client-side * route change. * * @default true.

Default
true
closeOnSelect?boolean

Whether pressing the link closes the menu. Overrides the menu's closeOnSelect.

current?boolean

Whether the link is the current page. Sets aria-current="page" and data-current. UI SDK's default: false.

Default
active
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-active?string

Present when current is true.

data-has-icon?string

Present on a link while it holds an icon part.

<div> data-slot="navigation-menu-indicator"

PropType
followHover?boolean

Whether it follows the top-level trigger or link under the pointer, and goes back to the open or current one when the pointer leaves the list.

render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-closed?string

Present while every panel is closed.

data-open?string

Present while a panel is open, so it sits under a trigger.

data-orientation"horizontal" | "vertical"

Which arrow keys move between the items, and where panels open.

Type
"horizontal" | "vertical"
data-visible?string

Present while there's a trigger or a current page's link to sit under.

CSS variableType
--active-item-bottomstring

Distance from its positioned ancestor's bottom edge to the item.

--active-item-heightstring

Height of the item it sits under.

--active-item-leftstring

Distance from its positioned ancestor's left edge to the open trigger, or the current page's link.

--active-item-rightstring

Distance from its positioned ancestor's right edge to the item.

--active-item-topstring

Distance from its positioned ancestor's top edge to the item.

--active-item-widthstring

Width of the item it sits under.