Navigation Menu

The React Navigation Menu component displays a website or application's main links in a structured menu. It is usually placed at the top and can include dropdown menus with related links.

It helps users move between pages easily by grouping links into clear sections. For example, a navigation menu can include items like Products, Pricing, or Docs, with dropdown links under each.

Built with Base UI and styled using Tailwind CSS. It supports keyboard navigation and accessibility, and it works well across different screen sizes.

Installation

Install the component using the Tailgrids CLI.

npx @tailgrids/cli add navigation-menu

Anatomy

Import the component parts and combine them to create a structured navigation menu.

import {
  NavigationMenu,
  NavigationMenuContent,
  NavigationMenuItem,
  NavigationMenuLink,
  NavigationMenuList,
  NavigationMenuTrigger,
  NavigationMenuIndicator,
  NavigationMenuPositioner
} from "@/registry/core/navigation-menu";

export const NavigationMenuExample = () => (
  <NavigationMenu>
    <NavigationMenuList>
      <NavigationMenuItem>
        <NavigationMenuTrigger>Item One</NavigationMenuTrigger>
        <NavigationMenuContent>
          <NavigationMenuLink href="/link1">Link One</NavigationMenuLink>
        </NavigationMenuContent>
      </NavigationMenuItem>
      <NavigationMenuItem>
        <NavigationMenuLink href="/link2">Direct Link Two</NavigationMenuLink>
      </NavigationMenuItem>
      <NavigationMenuIndicator />
    </NavigationMenuList>
    <NavigationMenuPositioner />
  </NavigationMenu>
);

Usage

Import the navigation menu components and compose them to build your menu structure. You can use NavigationMenuContent for dropdowns or NavigationMenuLink for direct links.

import {
  NavigationMenu,
  NavigationMenuContent,
  NavigationMenuItem,
  NavigationMenuLink,
  NavigationMenuList,
  NavigationMenuTrigger,
  NavigationMenuPositioner
} from "@/registry/core/navigation-menu";

export default function NavigationMenuExample() {
  return (
    <NavigationMenu>
      <NavigationMenuList>
        <NavigationMenuItem>
          <NavigationMenuTrigger>Products</NavigationMenuTrigger>
          <NavigationMenuContent>
            <NavigationMenuLink href="/analytics">Analytics</NavigationMenuLink>
          </NavigationMenuContent>
        </NavigationMenuItem>
        <NavigationMenuItem>
          <NavigationMenuLink href="/pricing">Pricing</NavigationMenuLink>
        </NavigationMenuItem>
      </NavigationMenuList>
      <NavigationMenuPositioner />
    </NavigationMenu>
  );
}

Examples

Simple Navigation Menu

A clean, text-based navigation menu with simple dropdown lists.

Positioning

Use the NavigationMenuPositioner component to position the viewport according to your need.

API Reference

The root component that manages the state of the navigation menu.

PropTypeDefaultDescription
defaultValueanynullThe uncontrolled value of the item that should be initially selected.
valueanynullThe controlled value of the navigation menu item that should be currently open.
onValueChange(value: any) => void-Callback fired when the value changes.
delaynumber50How long to wait before opening the navigation menu (ms).
closeDelaynumber50How long to wait before closing the navigation menu (ms).
orientation"horizontal" | "vertical""horizontal"The orientation of the navigation menu.
classNamestring-Additional CSS classes.

The container for the top-level menu items.

PropTypeDefaultDescription
childrenReact.ReactNode-NavigationMenuItem elements.
classNamestring-Additional CSS classes.

A single item in the navigation menu.

PropTypeDefaultDescription
valueany-A unique value that identifies this navigation menu item.
classNamestring-Additional CSS classes.

The button that opens the associated NavigationMenuContent.

PropTypeDefaultDescription
nativeButtonbooleantrueWhether the component renders a native <button> element.
classNamestring-Additional CSS classes.

An interactive link within the menu.

PropTypeDefaultDescription
hrefstring-The URL to link to.
activebooleanfalseWhether the link is the currently active page.
closeOnClickbooleanfalseWhether to close the navigation menu when the link is clicked.
classNamestring-Additional CSS classes.

The dropdown panel containing sub-links.

PropTypeDefaultDescription
keepMountedbooleanfalseWhether to keep the content mounted in the DOM while the popup is closed.
classNamestring-Additional CSS classes.

Positions the navigation menu against the currently active trigger.

PropTypeDefaultDescription
side"top" | "bottom" | "left" | "right""bottom"Which side of the anchor element to align the popup against.
sideOffsetnumber | function8Distance between the anchor and the popup in pixels.
align"start" | "center" | "end""start"How to align the popup relative to the specified side.
alignOffsetnumber | function0Additional offset along the alignment axis in pixels.
disableAnchorTrackingbooleanfalseWhether to disable tracking the layout shift of the anchor.
arrowPaddingnumber5Minimum distance between the arrow and the edge of the popup.
anchorElement | VirtualElement | React.RefObject<Element | null> | null-Explicit anchor element to position against. Positions to trigger if not provided.
collisionAvoidanceCollisionAvoidance Object-Determines how to handle collisions.
collisionBoundary'clipping-ancestors' | Element | Element[] | Rect'clipping-ancestors'Area that the popup is confined to.
collisionPaddingPadding Object | number5Additional space to maintain from the edge of the collision boundary.
stickybooleanfalseWhether to maintain the popup in the viewport after the anchor element was scrolled out of view.
positionMethod'absolute' | 'fixed''absolute'CSS positioning method to use.
renderReactElement | function-Replaces the rendered component with a custom element.
classNamestring | function-Additional CSS classes.

An icon that indicates that the trigger button opens a menu.

PropTypeDefaultDescription
classNamestring-Additional CSS classes.

Accessibility

  • Semantic structure: Uses roles like menubar, menu, and menuitem so assistive technologies can understand the navigation structure.
  • Keyboard support: Users can move between items using Arrow keys and activate them using Enter or Space.
  • Focus management: Focus moves between triggers and dropdown content, making the menu usable without a mouse.
  • Screen reader support: Expanded states and active items are announced through built-in accessibility behavior.
  • Base UI integration: Accessibility roles and behaviors are handled by the underlying Base UI components.