Skip to the content

Controls and surfaces

navigation-menu

A row of site links where some open a panel of further links beneath them.

Primitives

When to use it

The main navigation of a site whose sections each have pages worth showing: products, documentation, a catalogue. A plain link is a NavigationMenuLink in an item; a section is a NavigationMenuTrigger with a NavigationMenuContent. Mark the current page with active. For a header with a brand, links and a phone menu use navbar; for commands rather than places use menubar or dropdown-menu.

See it working

The demonstration the browser checks drive, with the keyboard as well as the pointer. More of it is on the Primitives page.

Alert dialog

The project is kept.

Collapsible

The options are closed.

Hover card

The first published program was written by Ada Lovelace.

Menubar

Command: none yet. Ruler: hidden. Zoom: 100.

Navigation menu

Toolbar

Styles: none. Saved 0 times.

One-time code

Typed: nothing yet. Sent: nothing yet.

Password

At least twelve characters.

Words for screen readers

Add it

npx shadcn@latest add @rivier-ui/navigation-menu

The registry is named once in components.json: see Installation.

Example

Import it from @/components/ui/navigation-menu.

<NavigationMenu aria-label="Main">
  <NavigationMenuList>
    <NavigationMenuItem>
      <NavigationMenuTrigger>Guides</NavigationMenuTrigger>
      <NavigationMenuContent>
        <NavigationMenuLink href="/guides/start">Getting started</NavigationMenuLink>
        <NavigationMenuLink href="/guides/theming">Theming</NavigationMenuLink>
      </NavigationMenuContent>
    </NavigationMenuItem>
    <NavigationMenuItem>
      <NavigationMenuLink href="/pricing" active>Pricing</NavigationMenuLink>
    </NavigationMenuItem>
  </NavigationMenuList>
</NavigationMenu>

What it needs

A client component: it uses state, effects or browser behaviour. Packages: lucide-react, radix-ui.

It imports

Source

The whole file. Once copied it is yours: the registry item holds the same text.

src/components/ui/navigation-menu.tsx
"use client";
/**
 * @name navigation-menu
 * @category ui
 * @description A row of site links where some open a panel of further links beneath them.
 * @when The main navigation of a site whose sections each have pages worth showing: products, documentation, a catalogue. A plain link is a NavigationMenuLink in an item; a section is a NavigationMenuTrigger with a NavigationMenuContent. Mark the current page with `active`. For a header with a brand, links and a phone menu use navbar; for commands rather than places use menubar or dropdown-menu.
 * @example
 * <NavigationMenu aria-label="Main">
 *   <NavigationMenuList>
 *     <NavigationMenuItem>
 *       <NavigationMenuTrigger>Guides</NavigationMenuTrigger>
 *       <NavigationMenuContent>
 *         <NavigationMenuLink href="/guides/start">Getting started</NavigationMenuLink>
 *         <NavigationMenuLink href="/guides/theming">Theming</NavigationMenuLink>
 *       </NavigationMenuContent>
 *     </NavigationMenuItem>
 *     <NavigationMenuItem>
 *       <NavigationMenuLink href="/pricing" active>Pricing</NavigationMenuLink>
 *     </NavigationMenuItem>
 *   </NavigationMenuList>
 * </NavigationMenu>
 */
import { NavigationMenu as NavigationMenuPrimitive } from "radix-ui";
import { ChevronDown } from "lucide-react";
import type { ComponentProps } from "react";
import { cn } from "@/lib/cn";

const link =
  "inline-flex min-h-(--control-h) items-center gap-2 rounded-control px-3 text-sm font-medium text-foreground transition-[background-color] duration-instant ease-out select-none hover:bg-accent data-active:bg-highlight-soft motion-reduce:transition-none";

/** The `nav` landmark. The panel of the open section is drawn under the row, at its start, never wider than the screen. */
export function NavigationMenu({ className, children, ...props }: ComponentProps<typeof NavigationMenuPrimitive.Root>) {
  return (
    <NavigationMenuPrimitive.Root data-slot="navigation-menu" className={cn("relative z-10 flex w-fit max-w-full", className)} {...props}>
      {children}
      <div className="absolute start-0 top-full pt-1.5">
        <NavigationMenuPrimitive.Viewport
          data-slot="navigation-menu-viewport"
          className="h-(--radix-navigation-menu-viewport-height) w-(--radix-navigation-menu-viewport-width) origin-top overflow-hidden rounded-surface border bg-popover text-popover-foreground ease-out animation-duration-(--duration-base) fade-in-0 fade-out-0 zoom-in-95 zoom-out-95 data-[state=closed]:animate-out data-[state=open]:animate-in motion-reduce:animate-none!"
        />
      </div>
    </NavigationMenuPrimitive.Root>
  );
}

export function NavigationMenuList({ className, ...props }: ComponentProps<typeof NavigationMenuPrimitive.List>) {
  return <NavigationMenuPrimitive.List data-slot="navigation-menu-list" className={cn("flex flex-wrap items-center gap-1", className)} {...props} />;
}

export function NavigationMenuItem(props: ComponentProps<typeof NavigationMenuPrimitive.Item>) {
  return <NavigationMenuPrimitive.Item data-slot="navigation-menu-item" {...props} />;
}

/** The button of a section. It opens on hover and on click; the chevron turns with a transform. */
export function NavigationMenuTrigger({ className, children, ...props }: ComponentProps<typeof NavigationMenuPrimitive.Trigger>) {
  return (
    <NavigationMenuPrimitive.Trigger data-slot="navigation-menu-trigger" className={cn(link, "group/navigation-menu-trigger data-[state=open]:bg-accent", className)} {...props}>
      {children}
      <ChevronDown
        aria-hidden="true"
        strokeWidth={1.5}
        className="size-4 shrink-0 text-muted-foreground transition-transform duration-base ease-out group-data-[state=open]/navigation-menu-trigger:rotate-180 motion-reduce:transition-none"
      />
    </NavigationMenuPrimitive.Trigger>
  );
}

/** What a section opens: links, in a column by default. Arrange it with `className` (a grid for a wide panel). */
export function NavigationMenuContent({ className, ...props }: ComponentProps<typeof NavigationMenuPrimitive.Content>) {
  return (
    <NavigationMenuPrimitive.Content
      data-slot="navigation-menu-content"
      className={cn("flex w-max max-w-[calc(100vw-2rem)] min-w-48 flex-col gap-1 p-2", className)}
      {...props}
    />
  );
}

/** A link, in the row or in a panel. `active` marks the page being shown and sets `aria-current="page"`. Use asChild for a framework's link component. */
export function NavigationMenuLink({ className, ...props }: ComponentProps<typeof NavigationMenuPrimitive.Link>) {
  return <NavigationMenuPrimitive.Link data-slot="navigation-menu-link" className={cn(link, className)} {...props} />;
}