Navigation Menu
A component for displaying navigation menus.
Follow the installation and quick start below to use Navigation Menu in your project, or read the complete setup guide.
On this page
Installation
1. Initialize once
Run from an existing React project with Node.js 18 or later. Skip this step if the project already has u-lib.json. Initialization copies the shared stylesheet and adds its import to your application entry.
npx @scriptwithahmad/u-lib@0.2.2 init2. Add Navigation Menu and example components
This command includes the UI components used by this page's usage examples. The CLI copies editable source into your components directory and installs each component's dependencies. Existing source files are preserved.
npx @scriptwithahmad/u-lib@0.2.2 add navigation-menuChoose the tab for your package manager. Yarn commands require modern Yarn; Yarn Classic users can use npx. Use --dry-run to preview changes. See the installation guide for custom paths, JavaScript output and package-import alternatives.
Quick start
Copy this example into src/Example.tsx (or Example.tsx at the project root when you do not use src). Imports assume the default CLI paths. From a nested file such as src/app/page.tsx, adjust ./components to ../components. Keep the client directive in Next.js when using state or event handlers.
"use client";
import React from 'react';
import {
NavigationMenu,
NavigationMenuList,
NavigationMenuItem,
NavigationMenuTrigger,
NavigationMenuContent,
NavigationMenuLink,
} from './components/ui/navigation-menu';
const ExampleNavigationMenu = () => {
return (
<div className="flex min-h-[220px] w-full justify-center pt-4"><NavigationMenu className="self-start" aria-label="Main navigation">
<NavigationMenuList>
<NavigationMenuItem>
<NavigationMenuTrigger>Home</NavigationMenuTrigger>
<NavigationMenuContent><div className="grid w-[min(240px,75vw)] gap-1 p-2">
<NavigationMenuLink className="block rounded-lg px-3 py-2 text-sm leading-5 outline-none hover:bg-accent focus-visible:bg-accent focus-visible:ring-2 focus-visible:ring-ring" href="/">Home Page</NavigationMenuLink>
<NavigationMenuLink className="block rounded-lg px-3 py-2 text-sm leading-5 outline-none hover:bg-accent focus-visible:bg-accent focus-visible:ring-2 focus-visible:ring-ring" href="/docs">About</NavigationMenuLink>
</div></NavigationMenuContent>
</NavigationMenuItem>
<NavigationMenuItem>
<NavigationMenuTrigger>Services</NavigationMenuTrigger>
<NavigationMenuContent><div className="grid w-[min(240px,75vw)] gap-1 p-2">
<NavigationMenuLink className="block rounded-lg px-3 py-2 text-sm leading-5 outline-none hover:bg-accent focus-visible:bg-accent focus-visible:ring-2 focus-visible:ring-ring" href="/web-design">Web Design</NavigationMenuLink>
<NavigationMenuLink className="block rounded-lg px-3 py-2 text-sm leading-5 outline-none hover:bg-accent focus-visible:bg-accent focus-visible:ring-2 focus-visible:ring-ring" href="/seo">SEO Services</NavigationMenuLink>
</div></NavigationMenuContent>
</NavigationMenuItem>
<NavigationMenuItem>
<NavigationMenuTrigger>Contact</NavigationMenuTrigger>
<NavigationMenuContent><div className="grid w-[min(240px,75vw)] gap-1 p-2">
<NavigationMenuLink className="block rounded-lg px-3 py-2 text-sm leading-5 outline-none hover:bg-accent focus-visible:bg-accent focus-visible:ring-2 focus-visible:ring-ring" href="/contact">Contact Us</NavigationMenuLink>
</div></NavigationMenuContent>
</NavigationMenuItem>
</NavigationMenuList>
</NavigationMenu></div>
);
};
export default ExampleNavigationMenu;Keep the shared stylesheet imported once. Replace sample image URLs and data with your own. Examples may use layout utilities beyond the precompiled component styles; compile added Tailwind classes in your app or replace them with your own CSS. Customize local source or className, and preserve accessible names and keyboard behavior.
Usage examples
Try each example, then switch to Code to copy its complete implementation. These catalog snippets use the website's @/components/ui alias; replace it with your local generated path as shown in Quick start.
Basic Usage
Use the `NavigationMenu` component to create a simple navigation menu with collapsible sections for different categories.
Loading example…
Custom Styling
You can customize the appearance of the navigation menu using the `className` prop to pass custom styles.
Loading example…
Accessibility
Improve accessibility by using `aria-label` and `aria-expanded` for better screen reader support and state management.
Loading example…
Props & configuration
Everything you need to fine-tune this component.
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | undefined | Additional CSS classes to apply to the root element of the NavigationMenu or any subcomponent. |
children | React.ReactNode | undefined | The child elements to render inside the NavigationMenu component, including NavigationMenuList, NavigationMenuItem, etc. |
aria-label | string | undefined | An optional label for accessibility, describing the menu's purpose for screen readers. |
aria-expanded | boolean | false | Indicates whether the content of the NavigationMenu item is expanded or collapsed. |
