Skip to content
Thrum

Navigation menu

The main navigation of a site: a bar of links, some of which open a panel of more links. All panels share one fly-out that moves and resizes between them, so scanning the bar feels like one surface. For commands that act on the page, use a menubar or a dropdown menu.

Install

npx shadcn@latest add https://thrumui.dev/r/navigation-menu.json

Adds components/ui/navigation-menu.tsx and installs @base-ui/react@1.9.0. Nothing else in your project changes.

import { NavigationMenu } from "@/components/ui/navigation-menu";

Automatically installs icon chevron-down from the icon set.

Props

PropTypeDefaultDescription
NavigationMenu: valuestring | null–The value of the open item, when the parent holds it. null closes the fly-out. Use it with onValueChange.
NavigationMenu: defaultValuestring | nullDefault nullThe item that is open at first, when the menu holds its own state.
NavigationMenu: onValueChange(value: string | null, details) => void–Called when an item opens or the fly-out closes. details.reason says why.
NavigationMenu: delaynumberDefault 150Milliseconds the pointer must rest on a trigger before its panel opens.
NavigationMenu: closeDelaynumberDefault 100Milliseconds after the pointer leaves before the fly-out closes.
NavigationMenu: orientation"horizontal" | "vertical"Default "horizontal"Which arrow keys move along the bar. Set vertical for a bar you lay out as a column yourself.
NavigationMenu: classNamestring–Extra classes for the nav element. It fills its container and measures it: under 480 pixels the bar stacks into a column.
NavigationMenuListul props–The bar. A row from 480 pixels of container width, a column below that.
NavigationMenuItem: valuestring–The name of the item in value and onValueChange. Made up for you when left out.
NavigationMenuTriggerbutton props–The button of an item that has a panel, 32 pixels tall, with a chevron that turns while the panel is open.
NavigationMenuTrigger: disabledbooleanDefault falseDimmed, and the panel cannot be opened.
NavigationMenuContent: classNamestring–Give the panel its width here, as in w-72. It is never wider than the screen less 32 pixels.
NavigationMenuLink: hrefstringRequiredWhere the link goes. To use a router's link, pass it as render.
NavigationMenuLink: activebooleanDefault falseMarks the page the visitor is on: sets aria-current="page" and draws a bar under the link on the bar, at the start of its line when the bar is stacked, or beside it in a panel.
NavigationMenuLink: closeOnClickbooleanDefault falseClose the fly-out when the link is pressed. Set it for links that change the page without a full load.
NavigationMenuViewport: side"top" | "bottom" | "left" | "right" | "inline-start" | "inline-end"Default "bottom"The side of the trigger the fly-out opens on. It flips when there is no room.
NavigationMenuViewport: align"start" | "center" | "end"Default "start"How the fly-out lines up with the open trigger.
NavigationMenuViewport: sideOffset, alignOffsetnumberDefault 8, 0The gap to the trigger, and a shift along its edge, in pixels.
NavigationMenuViewport: classNamestring–Extra classes for the fly-out.
NavigationMenuIndicatordiv props–An arrow on the fly-out's edge that points at the open trigger. Pass it as a child of NavigationMenuViewport.

Motion and sound

Motion

  • The fly-out fades in and grows from 96% to full size over 200 ms, and leaves the same way.
  • Moving to another trigger slides the fly-out along the bar and changes its width and height over 200 ms. The old panel fades out towards the side it is left on while the new one fades in from the other.
  • A trigger's chevron turns half a circle over 150 ms, and bar items change fill over 150 ms on hover.
  • Reduced motion: the fly-out and its panels appear, move and resize at once.

Sound

Silent. This component makes no sound.

Accessibility

  • The menu is a nav landmark: name it with an aria-label such as Main, so it can be told from other navigation on the page.
  • It is a list of links and buttons, not an ARIA menu: screen readers read it like any navigation, and every link can be reached with Tab.
  • A trigger is a button with aria-expanded. The current page's link has aria-current="page" and a bar in the text colour, so it does not rest on a tint.
  • A panel opens when the pointer rests on its trigger for 150 ms, and also on a press, so it works without hover. A strip over the gap keeps it open while the pointer travels to it.
  • Under 480 pixels of its own width the bar stacks into a column of full-width rows. The fly-out is never wider than the screen and stays 8 pixels inside its edges.
  • The fly-out is a floating surface. Under a glass material it is translucent, and its tint keeps the text at 4.5 to 1 over any page.
KeysAction
TabMove through the triggers and links of the bar in order. With a panel open, Tab from its trigger moves into the panel, and from the panel's last link on to the next item of the bar.
← / →Move to the previous or next item of the bar. In a stacked or vertical bar ↑ and ↓ do the same.
Home / EndMove to the first or last item of the bar.
Enter / SpaceOn a trigger: open its panel, or close it when it is open. Enter on a link follows it.
↓On a trigger of a horizontal bar: open its panel and move into it.
EscClose the fly-out. Focus returns to the trigger.

Source

components/ui/navigation-menu.tsx
"use client";

import { createContext, useContext } from "react";
import { NavigationMenu as NavigationMenuPrimitive } from "@base-ui/react/navigation-menu";

import { cn } from "@/lib/utils";
import { ChevronDownIcon } from "@/components/icons/chevron-down";

/** True inside a panel, where a link is a row of the panel and not an item of the bar. */
const PanelContext = createContext(false);

/** An item of the bar: a trigger or a top-level link. A full-width row while the bar is stacked. */
const BAR_ITEM =
  "relative inline-flex h-8 w-full cursor-pointer items-center justify-between gap-1 rounded-md px-2.5 text-sm font-medium whitespace-nowrap outline-none select-none @min-[480px]:w-auto " +
  "transition-[background-color,color] duration-150 ease-out motion-reduce:transition-none " +
  "hover:bg-accent hover:text-accent-foreground focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring " +
  "data-disabled:pointer-events-none data-disabled:opacity-50 data-popup-open:bg-accent data-popup-open:text-accent-foreground";

export type NavigationMenuProps = NavigationMenuPrimitive.Root.Props<string>;

/**
 * While a panel is open, the base puts invisible focus guards around its trigger and inside the fly-out, so Tab can travel
 * between the bar and the fly-out at the end of the page. They are focusable and marked aria-hidden, which accessibility checks
 * report as a focusable element hidden from assistive technology. This lifts the aria-hidden; the guards stay empty and invisible.
 */
function unhideFocusGuards(element: HTMLElement | null) {
  if (!element) return;
  const lift = () => element.querySelectorAll("[data-base-ui-focus-guard][aria-hidden]").forEach((guard) => guard.removeAttribute("aria-hidden"));
  lift();
  const observer = new MutationObserver(lift);
  observer.observe(element, { childList: true, subtree: true });
  return () => observer.disconnect();
}

/**
 * Site navigation: a bar of links, some of which open a panel of more links. All panels share one fly-out, which moves and
 * resizes from one to the next. It is a `<nav>`: name it with an `aria-label`. Below 480 pixels of its own width the bar stacks into a column.
 */
export function NavigationMenu({ delay = 150, closeDelay = 100, className, ...props }: NavigationMenuProps) {
  return <NavigationMenuPrimitive.Root ref={unhideFocusGuards} data-slot="navigation-menu" delay={delay} closeDelay={closeDelay} className={cn("@container relative w-full", className)} {...props} />;
}

/** The bar: a list of items. A row from 480 pixels of container width, a column below that. */
export function NavigationMenuList({ className, ...props }: NavigationMenuPrimitive.List.Props) {
  return <NavigationMenuPrimitive.List data-slot="navigation-menu-list" className={cn("flex list-none flex-col gap-1 @min-[480px]:flex-row @min-[480px]:items-center", className)} {...props} />;
}

/** One item of the bar. It holds a link, or a trigger with its content. */
export function NavigationMenuItem(props: NavigationMenuPrimitive.Item.Props) {
  return <NavigationMenuPrimitive.Item data-slot="navigation-menu-item" {...props} />;
}

/** The button of an item that has a panel. Pointing at it for 150 ms opens the panel; so do a press, Enter, Space and the down arrow. */
export function NavigationMenuTrigger({ className, children, ...props }: NavigationMenuPrimitive.Trigger.Props) {
  return (
    <NavigationMenuPrimitive.Trigger data-slot="navigation-menu-trigger" className={cn(BAR_ITEM, className)} {...props}>
      {children}
      <NavigationMenuPrimitive.Icon className="flex-none transition-[rotate] duration-150 ease-out data-popup-open:rotate-180 motion-reduce:transition-none">
        <ChevronDownIcon size={16} />
      </NavigationMenuPrimitive.Icon>
    </NavigationMenuPrimitive.Trigger>
  );
}

/**
 * The panel of an item: a list of links, or a featured block beside one. It is drawn inside the shared fly-out while its item is open.
 * Give it a width with `className`; it never grows wider than the screen.
 */
export function NavigationMenuContent({ className, ...props }: NavigationMenuPrimitive.Content.Props) {
  return (
    <PanelContext.Provider value={true}>
      <NavigationMenuPrimitive.Content
        data-slot="navigation-menu-content"
        className={cn(
          // 8 pixels of padding keep every link clear of the fly-out's rounded edge.
          "h-full w-max max-w-[calc(100vw-2rem)] p-2",
          // Coming from a neighbour the panel slides in from that side; the old one leaves the other way.
          "transition-[opacity,translate] duration-200 ease-out data-ending-style:opacity-0 data-starting-style:opacity-0 motion-reduce:transition-none",
          "data-starting-style:data-[activation-direction=left]:-translate-x-6 data-starting-style:data-[activation-direction=right]:translate-x-6",
          "data-ending-style:data-[activation-direction=left]:translate-x-6 data-ending-style:data-[activation-direction=right]:-translate-x-6",
          className,
        )}
        {...props}
      />
    </PanelContext.Provider>
  );
}

/**
 * A link. On the bar it looks like a trigger; inside a panel it is a row that can hold a title and a line of description.
 * `active` marks the page the visitor is on: it sets `aria-current="page"` and draws a bar beside or under the link.
 */
export function NavigationMenuLink({ className, ...props }: NavigationMenuPrimitive.Link.Props) {
  const inPanel = useContext(PanelContext);
  return (
    <NavigationMenuPrimitive.Link
      data-slot="navigation-menu-link"
      className={cn(
        inPanel
          ? "flex flex-col gap-0.5 rounded-sm border-s-2 border-transparent px-2 py-1.5 text-sm outline-none transition-[background-color,color] duration-150 ease-out motion-reduce:transition-none " +
              "hover:bg-accent hover:text-accent-foreground focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-ring data-active:border-foreground data-active:font-medium"
          : // The current page: a bar under the text on a row, a bar at the start of the line when the bar is stacked.
            BAR_ITEM +
              " data-active:after:absolute data-active:after:start-0 data-active:after:inset-y-1.5 data-active:after:w-0.5 data-active:after:bg-foreground" +
              " @min-[480px]:data-active:after:start-2.5 @min-[480px]:data-active:after:end-2.5 @min-[480px]:data-active:after:top-auto @min-[480px]:data-active:after:bottom-0.5 @min-[480px]:data-active:after:h-0.5 @min-[480px]:data-active:after:w-auto",
        className,
      )}
      {...props}
    />
  );
}

export type NavigationMenuViewportProps = NavigationMenuPrimitive.Popup.Props & Pick<NavigationMenuPrimitive.Positioner.Props, "side" | "align" | "sideOffset" | "alignOffset">;

/**
 * The fly-out that shows the open item's panel. Put one at the end of the `NavigationMenu`. It follows the open trigger and
 * changes size between panels over 200 ms. It is a floating surface, so it turns to glass when the page's material asks for it.
 * A `NavigationMenuIndicator` passed as its child draws an arrow that points at the open trigger.
 */
export function NavigationMenuViewport({ side = "bottom", align = "start", sideOffset = 8, alignOffset, className, children, ...props }: NavigationMenuViewportProps) {
  return (
    <NavigationMenuPrimitive.Portal>
      <NavigationMenuPrimitive.Positioner
        side={side}
        align={align}
        sideOffset={sideOffset}
        alignOffset={alignOffset}
        collisionPadding={8}
        className={cn(
          "z-50 h-(--positioner-height) w-(--positioner-width) max-w-(--available-width) transition-[top,left,right,bottom] duration-200 ease-out data-instant:transition-none motion-reduce:transition-none",
          // An unseen strip over the gap, so the pointer can travel from the trigger to the panel without leaving both.
          "before:absolute data-[side=bottom]:before:inset-x-0 data-[side=bottom]:before:-top-2 data-[side=bottom]:before:h-2 data-[side=top]:before:inset-x-0 data-[side=top]:before:-bottom-2 data-[side=top]:before:h-2",
          "data-[side=left]:before:inset-y-0 data-[side=left]:before:-right-2 data-[side=left]:before:w-2 data-[side=right]:before:inset-y-0 data-[side=right]:before:-left-2 data-[side=right]:before:w-2",
        )}
      >
        <NavigationMenuPrimitive.Popup
          ref={unhideFocusGuards}
          data-slot="navigation-menu-viewport"
          data-surface="floating"
          className={cn(
            "relative h-(--popup-height) w-(--popup-width) origin-(--transform-origin) rounded-lg border bg-popover text-popover-foreground shadow-md outline-none",
            "transition-[opacity,scale,width,height] duration-200 ease-out data-ending-style:scale-[0.96] data-ending-style:opacity-0 data-starting-style:scale-[0.96] data-starting-style:opacity-0 motion-reduce:transition-none",
            className,
          )}
          {...props}
        >
          {children}
          <NavigationMenuPrimitive.Viewport className="relative size-full overflow-hidden rounded-lg" />
        </NavigationMenuPrimitive.Popup>
      </NavigationMenuPrimitive.Positioner>
    </NavigationMenuPrimitive.Portal>
  );
}

/** An arrow on the fly-out's edge that points at the open trigger and slides along as the fly-out moves. Pass it as a child of `NavigationMenuViewport`. */
export function NavigationMenuIndicator({ className, ...props }: NavigationMenuPrimitive.Arrow.Props) {
  return (
    <NavigationMenuPrimitive.Arrow
      data-slot="navigation-menu-indicator"
      className={cn(
        // A box that shows only the half of a turned square that lies outside the fly-out.
        "block h-1.5 w-3 overflow-clip transition-[left,right,top,bottom] duration-200 ease-out motion-reduce:transition-none",
        "before:absolute before:bottom-0 before:left-1/2 before:size-2 before:-translate-x-1/2 before:translate-y-1/2 before:rotate-45 before:border before:bg-popover",
        "data-[side=bottom]:-top-1.5 data-[side=left]:-right-2.5 data-[side=left]:rotate-90 data-[side=right]:-left-2.5 data-[side=right]:-rotate-90 data-[side=top]:-bottom-1.5 data-[side=top]:rotate-180",
        className,
      )}
      {...props}
    />
  );
}

Files and dependencies

Adds
components/ui/navigation-menu.tsx
Packages
@base-ui/react@1.9.0
Brings along
Nothing
Icons
chevron-down

Changelog

  1. 1.0.0 · 2026-12-03First release.

Loading index…

Keyboard shortcuts

/Ctrl KSearch components, docs and actions
?Show this list
AAppearance studio
SSound on or off
MFull or reduced motion
TTheme: auto, light, dark
GSurface: solid or frosted
BBackdrop photograph (under glass)
CCorner radius: square to round
RReplay the demo, on a component page
DDraw new data, on a component page
←→HomeEndMove the cursor in a focused chart
EscClose a dialog, or leave a chart cursor