---
title: Navigation Menu for Astro
description: "Astro navigation groups with links and optional popup panels."
seo:
    title: Navigation Menu for Astro | Shadcn & Tailwind CSS
related:
    - Header blocks: /blocks/header
    - /components/dropdown-menu
sidebar:
    label: "Navigation Menu"
---

```astro
---
import {
  NavigationMenu,
  NavigationMenuContent,
  NavigationMenuItem,
  NavigationMenuLink,
  NavigationMenuList,
  NavigationMenuTrigger,
  navigationMenuTriggerStyle,
} from "@/components/ui/navigation-menu"

const components = [
  {
    title: "Alert",
    href: "/components/alert",
    description:
      "Displays important messages with title and description content.",
  },
  {
    title: "Sheet",
    href: "/components/sheet",
    description: "A side panel component built on top of dialog primitives.",
  },
  {
    title: "Native Select",
    href: "/components/native-select",
    description:
      "A styled native select element for choosing one option from a list.",
  },
  {
    title: "Tabs",
    href: "/components/tabs",
    description:
      "A set of layered sections of content displayed one at a time.",
  },
]
---

<div class="@container/navigation-example w-full flex justify-center">
  <NavigationMenu>
    <NavigationMenuList class="flex-wrap gap-1">
      <NavigationMenuItem value="getting-started">
        <NavigationMenuTrigger>Getting started</NavigationMenuTrigger>
        <NavigationMenuContent class="w-[min(24rem,calc(100vw-4rem))] max-w-(--available-width)">
          <ul class="w-full p-1">
            <li>
              <NavigationMenuLink
                href="/docs"
                class="w-full flex-col items-start"
              >
                <span class="leading-none font-medium">Introduction</span>
                <span class="text-muted-foreground line-clamp-2 text-sm">
                  Astro controls with shared theme tokens and local source.
                </span>
              </NavigationMenuLink>
            </li>
            <li>
              <NavigationMenuLink
                href="/docs/installation"
                class="w-full flex-col items-start"
              >
                <span class="leading-none font-medium">Installation</span>
                <span class="text-muted-foreground line-clamp-2 text-sm">
                  Read the project brief and choose your first task.
                </span>
              </NavigationMenuLink>
            </li>
            <li>
              <NavigationMenuLink
                href="/docs/theming#text-and-headings"
                class="w-full flex-col items-start"
              >
                <span class="leading-none font-medium">Typeset</span>
                <span class="text-muted-foreground line-clamp-2 text-sm">
                  Styles for headings, paragraphs, lists, and more.
                </span>
              </NavigationMenuLink>
            </li>
          </ul>
        </NavigationMenuContent>
      </NavigationMenuItem>
      <NavigationMenuItem
        value="components"
        class="hidden @min-sm/navigation-example:block"
      >
        <NavigationMenuTrigger>Components</NavigationMenuTrigger>
        <NavigationMenuContent class="@container/navigation-content w-[min(37.5rem,calc(100vw-4rem))] max-w-(--available-width)">
          <ul class="grid w-full gap-2 p-1 @min-lg/navigation-content:grid-cols-2">
            {components.map((component) => (
              <li>
                <NavigationMenuLink
                  href={component.href}
                  class="w-full flex-col items-start"
                >
                  <span class="leading-none font-medium">
                    {component.title}
                  </span>
                  <span class="text-muted-foreground line-clamp-2 text-sm">
                    {component.description}
                  </span>
                </NavigationMenuLink>
              </li>
            ))}
          </ul>
        </NavigationMenuContent>
      </NavigationMenuItem>
      <NavigationMenuItem value="docs">
        <NavigationMenuLink href="/docs" class={navigationMenuTriggerStyle()}>
          Docs
        </NavigationMenuLink>
      </NavigationMenuItem>
    </NavigationMenuList>
  </NavigationMenu>
</div>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/navigation-menu
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

Install the component packages:

```sh
bun add @data-slot/navigation-menu
```

#### src/components/ui/navigation-menu/index.ts

```ts
export { default as NavigationMenu } from './navigation-menu.astro'
export { default as NavigationMenuContent } from './navigation-menu-content.astro'
export { default as NavigationMenuIndicator } from './navigation-menu-indicator.astro'
export { default as NavigationMenuItem } from './navigation-menu-item.astro'
export { default as NavigationMenuLink } from './navigation-menu-link.astro'
export { default as NavigationMenuList } from './navigation-menu-list.astro'
export { default as NavigationMenuPositioner } from './navigation-menu-positioner.astro'
export { default as NavigationMenuTrigger } from './navigation-menu-trigger.astro'
export { default as navigationMenuTriggerStyle } from './navigation-menu-trigger-style'
export type {
    NavigationMenuProps,
    NavigationMenuContentProps,
    NavigationMenuItemProps,
} from './types.ts'
```

#### src/components/ui/navigation-menu/navigation-menu-content.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"
import type { NavigationMenuContentProps } from "./types.ts"

type Props = HTMLAttributes<"div"> & NavigationMenuContentProps

const {
  class: className = "",
  hidden = true,
  align,
  alignOffset,
  side,
  sideOffset,
  ...props
} = Astro.props
---

<div
  {...props}
  data-slot="navigation-menu-content"
  hidden={hidden}
  data-align={align}
  data-align-offset={alignOffset}
  data-side={side}
  data-side-offset={sideOffset}
  class={cn("group-data-[viewport=false]/navigation-menu:bg-popover group-data-[viewport=false]/navigation-menu:text-popover-foreground group-data-[viewport=false]/navigation-menu:ring-foreground/10 data-[motion=from-end]:slide-in-from-right-52 data-[motion=from-start]:slide-in-from-left-52 data-[motion=to-end]:slide-out-to-right-52 data-[motion=to-start]:slide-out-to-left-52 data-[motion^=from-]:animate-in data-[motion^=from-]:fade-in data-[motion^=to-]:animate-out data-[motion^=to-]:fade-out group-data-[viewport=false]/navigation-menu:data-open:animate-in group-data-[viewport=false]/navigation-menu:data-open:fade-in-0 group-data-[viewport=false]/navigation-menu:data-open:zoom-in-95 group-data-[viewport=false]/navigation-menu:data-closed:animate-out group-data-[viewport=false]/navigation-menu:data-closed:fade-out-0 group-data-[viewport=false]/navigation-menu:data-closed:zoom-out-95 h-full w-auto p-2 pr-2.5 transition-[opacity,transform,translate] duration-[0.35s] ease-[cubic-bezier(0.22,1,0.36,1)] group-data-[viewport=false]/navigation-menu:rounded-md group-data-[viewport=false]/navigation-menu:shadow group-data-[viewport=false]/navigation-menu:ring-1 group-data-[viewport=false]/navigation-menu:duration-300 data-ending-style:opacity-0 data-starting-style:opacity-0 data-ending-style:data-[activation-direction=left]:translate-x-[50%] data-starting-style:data-[activation-direction=left]:translate-x-[-50%] data-ending-style:data-[activation-direction=right]:translate-x-[-50%] data-starting-style:data-[activation-direction=right]:translate-x-[50%] **:data-[slot=navigation-menu-link]:focus:ring-0 **:data-[slot=navigation-menu-link]:focus:outline-none", className)}
>
  <slot />
</div>
```

#### src/components/ui/navigation-menu/navigation-menu-indicator.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="navigation-menu-indicator"
  data-state="hidden"
  class={cn("data-[state=hidden]:animate-out data-[state=hidden]:fade-out data-[state=visible]:animate-in data-[state=visible]:fade-in absolute top-full left-(--indicator-left) z-1 flex h-1.5 w-(--indicator-width) items-end justify-center overflow-hidden data-[state=hidden]:invisible", className)}
  {...props}
>
  <div
    data-slot="navigation-menu-indicator-arrow"
    class="bg-border relative top-[60%] h-2 w-2 rotate-45 rounded-tl-sm shadow-md"
  ></div>
</div>
```

#### src/components/ui/navigation-menu/navigation-menu-item.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"
import type { NavigationMenuItemProps } from "./types.ts"

type Props = HTMLAttributes<"li"> & NavigationMenuItemProps

const {
  class: className,
  value,
  align,
  alignOffset,
  side,
  sideOffset,
  ...props
} = Astro.props
---

<li
  data-slot="navigation-menu-item"
  data-value={value}
  data-align={align}
  data-align-offset={alignOffset}
  data-side={side}
  data-side-offset={sideOffset}
  class={cn("relative list-none", className)}
  {...props}
>
  <slot />
</li>
```

#### src/components/ui/navigation-menu/navigation-menu-link.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"a"> & {
  active?: boolean
}

const { class: className, active, ...props } = Astro.props
---

<a
  data-slot="navigation-menu-link"
  data-active={active ? "true" : undefined}
  aria-current={active ? "page" : undefined}
  class={cn("hover:bg-muted focus:bg-muted focus-visible:ring-ring/50 data-[active=true]:bg-muted/50 data-[active=true]:hover:bg-muted data-[active=true]:focus:bg-muted flex items-center gap-1.5 rounded-md p-2 text-sm transition-all outline-none focus-visible:ring-3 focus-visible:outline-1 in-data-[slot=navigation-menu-content]:rounded-sm [&_svg:not([class*='size-'])]:size-4", className)}
  {...props}
>
  <slot />
</a>
```

#### src/components/ui/navigation-menu/navigation-menu-list.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"ul">

const { class: className, ...props } = Astro.props
---

<ul
  data-slot="navigation-menu-list"
  class={cn("relative flex flex-1 list-none items-center justify-center gap-0 group", className)}
  {...props}
>
  <slot />
</ul>
```

#### src/components/ui/navigation-menu/navigation-menu-positioner.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"
import type { NavigationMenuContentProps } from "./types.ts"

type Props = HTMLAttributes<"div"> & NavigationMenuContentProps

const {
  class: className,
  side = "bottom",
  sideOffset = 8,
  align = "start",
  alignOffset = 0,
  ...props
} = Astro.props
---

<div data-slot="navigation-menu-portal">
  <div
    {...props}
    data-slot="navigation-menu-positioner"
    data-side={side}
    data-side-offset={sideOffset}
    data-align={align}
    data-align-offset={alignOffset}
    class={cn(
      "isolate z-50 h-(--positioner-height) w-(--positioner-width) max-w-(--available-width) transition-[top,left,right,bottom] duration-[0.35s] ease-[cubic-bezier(0.22,1,0.36,1)] data-instant:transition-none data-[side=bottom]:before:-top-2.5 data-[side=bottom]:before:right-0 data-[side=bottom]:before:left-0",
      className,
    )}
  >
    <div
      data-slot="navigation-menu-popup"
      hidden
      class="data-[ending-style]:ease-[ease] bg-popover text-popover-foreground ring-foreground/10 relative h-(--popup-height) w-(--popup-width) origin-(--transform-origin) rounded-lg shadow ring-1 transition-[opacity,transform,width,height,scale,translate] duration-[0.35s] ease-[cubic-bezier(0.22,1,0.36,1)] outline-none data-ending-style:scale-90 data-ending-style:opacity-0 data-ending-style:duration-150 data-starting-style:scale-90 data-starting-style:opacity-0"
    >
      <div
        data-slot="navigation-menu-viewport"
        hidden
        class="relative size-full overflow-hidden"
      ></div>
    </div>
  </div>
</div>
```

#### src/components/ui/navigation-menu/navigation-menu-trigger-style.ts

```ts
import { cva } from 'class-variance-authority'
import { cn } from 'cn'

const navigationMenuTriggerClasses = cva(
    "hover:bg-muted focus:bg-muted focus-visible:ring-ring/50 data-popup-open:bg-muted/50 data-popup-open:hover:bg-muted data-open:bg-muted/50 data-open:hover:bg-muted data-open:focus:bg-muted inline-flex h-9 w-max items-center justify-center rounded-md px-4 py-2 text-sm font-medium transition-all outline-none focus-visible:ring-3 focus-visible:outline-1 disabled:pointer-events-none disabled:opacity-50 group/navigation-menu-trigger",
)

const navigationMenuTriggerStyle = (
    ...args: Parameters<typeof navigationMenuTriggerClasses>
) => cn(navigationMenuTriggerClasses(...args))

export default navigationMenuTriggerStyle
```

#### src/components/ui/navigation-menu/navigation-menu-trigger.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import ChevronDownIcon from "@lucide/astro/icons/chevron-down"

import { cn } from "cn"

import navigationMenuTriggerStyle from "./navigation-menu-trigger-style"

type Props = HTMLAttributes<"button">

const { class: className, type = "button", ...props } = Astro.props
---

<button
  data-slot="navigation-menu-trigger"
  type={type}
  class={cn(navigationMenuTriggerStyle(), "group", className)}
  {...props}
>
  <slot />{" "}
  <ChevronDownIcon
    data-slot="navigation-menu-trigger-icon"
    class="relative top-px ml-1 size-3 transition duration-300 group-data-open/navigation-menu-trigger:rotate-180 group-data-popup-open/navigation-menu-trigger:rotate-180"
    aria-hidden="true"
  />
</button>
```

#### src/components/ui/navigation-menu/navigation-menu.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

import NavigationMenuPositioner from "./navigation-menu-positioner.astro"
import type { NavigationMenuProps } from "./types.ts"

type Props = HTMLAttributes<"nav"> & NavigationMenuProps

const {
  class: className = "",
  delayOpen = 0,
  delayClose = 0,
  openOnFocus,
  side = "bottom",
  align = "start",
  sideOffset = 0,
  alignOffset = 0,
  positionMethod = "absolute",
  safeTriangle,
  debugSafeTriangle,
  mountStrategy = "eager",
  ...props
} = Astro.props
---

<nav
  {...props}
  data-slot="navigation-menu"
  data-delay-open={delayOpen}
  data-delay-close={delayClose}
  data-open-on-focus={openOnFocus}
  data-side={side}
  data-align={align}
  data-side-offset={sideOffset}
  data-align-offset={alignOffset}
  data-position-method={positionMethod}
  data-safe-triangle={safeTriangle}
  data-debug-safe-triangle={debugSafeTriangle}
  data-mount-strategy={mountStrategy}
  class={cn("relative flex max-w-max flex-1 items-center justify-center group/navigation-menu", className)}
>
  <slot />
  <NavigationMenuPositioner align={align} />
</nav>

<script>
  import { create } from "@data-slot/navigation-menu"
  import type { NavigationMenuController } from "@data-slot/navigation-menu"
  // Destroy bindings before a view transition so the next page-load binds once.
  const controllers: NavigationMenuController[] = []
  const initialize = () => controllers.push(...create())
  initialize()
  document.addEventListener("astro:page-load", initialize)
  document.addEventListener("astro:before-swap", () => {
    for (const controller of controllers) controller.destroy()
    controllers.length = 0
  })
</script>
```

#### src/components/ui/navigation-menu/types.ts

```ts
import type { NavigationMenuOptions } from '@data-slot/navigation-menu'

/**
 * Navigation menu configuration rendered as data attributes.
 */
export type NavigationMenuProps = Pick<
    NavigationMenuOptions,
    | 'delayOpen'
    | 'delayClose'
    | 'openOnFocus'
    | 'side'
    | 'align'
    | 'sideOffset'
    | 'alignOffset'
    | 'positionMethod'
    | 'safeTriangle'
    | 'debugSafeTriangle'
    | 'mountStrategy'
> & {
    /**
     * How to mount the navigation menu panels.
     *
     * Use eager for primary navigation to keep closed-panel links in the DOM
     * after initialization. Both modes include all panel links in the server HTML.
     *
     * - eager: Keep closed panels connected and hidden.
     * - lazy: Remove closed panels from the DOM until they are activated.
     *
     * @default 'eager'
     */
    mountStrategy?: 'eager' | 'lazy'
}

/** Placement fields shared by a navigation menu panel. */
export type NavigationMenuContentProps = Pick<
    NavigationMenuOptions,
    'side' | 'align' | 'sideOffset' | 'alignOffset'
>

/** One navigation item and its optional placement overrides. */
export type NavigationMenuItemProps = NavigationMenuContentProps & {
    /** Value that identifies this menu item. */
    value: string
}
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


## Usage

```astro
---
import {
  NavigationMenu,
  NavigationMenuContent,
  NavigationMenuItem,
  NavigationMenuLink,
  NavigationMenuList,
  NavigationMenuTrigger,
} from "@/components/ui/navigation-menu"
---
```

```astro
<NavigationMenu>
  <NavigationMenuList>
    <NavigationMenuItem value="item-one">
      <NavigationMenuTrigger>Item One</NavigationMenuTrigger>
      <NavigationMenuContent>
        <NavigationMenuLink href="/docs">Link</NavigationMenuLink>
      </NavigationMenuContent>
    </NavigationMenuItem>
  </NavigationMenuList>
</NavigationMenu>
```

## Composition

Arrange the parts as shown for a `NavigationMenu`:

```text
NavigationMenu
├── NavigationMenuList
│   ├── NavigationMenuItem
│   │   ├── NavigationMenuTrigger
│   │   └── NavigationMenuContent
│   │       ├── NavigationMenuLink
│   │       └── NavigationMenuLink
│   └── NavigationMenuItem
│       └── NavigationMenuLink
└── NavigationMenuIndicator
```

## Notes

- Keep inactive panels hidden in the initial HTML. The controller sets their visibility when it starts.
- Put `NavigationMenuIndicator` in `NavigationMenuList`. Its position depends on measurements from that list.

- Use `NavigationMenuTrigger` for items that open richer panels and
  `NavigationMenuLink` for direct navigation targets.
- `NavigationMenuLink` accepts `active` when the current route should be
  visually highlighted.
- Keep the content panel layout inside `NavigationMenuContent` simple and
  list-like so keyboard navigation stays predictable.

## API Reference

### NavigationMenuProps

<AutoTypeTable
    path="../../packages/ui/src/components/ui/navigation-menu/types.ts"
    name="NavigationMenuProps"
/>

### NavigationMenuContentProps

<AutoTypeTable
    path="../../packages/ui/src/components/ui/navigation-menu/types.ts"
    name="NavigationMenuContentProps"
/>

### NavigationMenuItemProps

<AutoTypeTable
    path="../../packages/ui/src/components/ui/navigation-menu/types.ts"
    name="NavigationMenuItemProps"
/>

See the [data-slot docs](https://github.com/bejamas/data-slot/blob/main/packages/navigation-menu/README.md) for more information on interactivity.

### Events

Dispatch these events on the navigation menu root. The root emits the outbound events.

#### Outbound events

| Event                    | Detail                      |
| ------------------------ | --------------------------- |
| `navigation-menu:change` | `{ value: string \| null }` |

#### Inbound events

| Event                 | Detail                      |
| --------------------- | --------------------------- |
| `navigation-menu:set` | `{ value: string \| null }` |

Read the [installed source](/r/navigation-menu.json) for the complete prop types.

## When to use Navigation Menu

Use Navigation Menu for the main sections of a site. Keep direct destinations as links and use triggers only when they open a group. Organize popup content so the first useful destination is easy to find.

## Keyboard and behavior

The data-slot controller handles opening, timing, and pointer movement between parts. Test keyboard focus through links and triggers, including Escape from an open panel. Provide a separate narrow-screen composition when the desktop layout no longer fits.

## Theme and layout

Delay, side, and alignment props tune the existing behavior. The safe-triangle option helps pointer travel but does not replace a clear layout. Check panels at viewport edges after changing widths.
