apps/design-system/content/docs/components/navigation-menu.mdx
npx shadcn-ui@latest add navigation-menu
<Step>Install the following dependencies:</Step>
npm install @radix-ui/react-navigation-menu
<Step>Copy and paste the following code into your project.</Step>
<ComponentSource name="navigation-menu" /><Step>Update the import paths to match your project setup.</Step>
</Steps> </TabsContent> </Tabs>import {
NavigationMenu,
NavigationMenuContent,
NavigationMenuIndicator,
NavigationMenuItem,
NavigationMenuLink,
NavigationMenuList,
NavigationMenuTrigger,
NavigationMenuViewport,
} from '@/components/ui/navigation-menu'
<NavigationMenu>
<NavigationMenuList>
<NavigationMenuItem>
<NavigationMenuTrigger>Item One</NavigationMenuTrigger>
<NavigationMenuContent>
<NavigationMenuLink>Link</NavigationMenuLink>
</NavigationMenuContent>
</NavigationMenuItem>
</NavigationMenuList>
</NavigationMenu>
When using the Next.js <Link /> component, you can use navigationMenuTriggerStyle() to apply the correct styles to the trigger.
import { navigationMenuTriggerStyle } from '@/components/ui/navigation-menu'
<NavigationMenuItem>
<Link href="/docs" legacyBehavior passHref>
<NavigationMenuLink className={navigationMenuTriggerStyle()}>Documentation</NavigationMenuLink>
</Link>
</NavigationMenuItem>
See also the Radix UI documentation for handling client side routing.
We can wrap a the <NavigationMenuList /> in a <ScrollArea /> component to make the menu scroll.
However, then we must also make sure the <NavigationMenuContent /> is positioned correctly and rendered outside the scroll area.
We set renderViewport={false} on the <NavigationMenu /> component to prevent the viewport from being rendered.
And then add our own Viewport with <NavigationMenuViewport />.
Then all our content panels will render themselves outside the scroll area and inside the viewport.
You can also add props to <NavigationMenuViewport /> to style it as you like, including the containerProps prop to add classes to the container div element inside the viewport.
<NavigationMenu renderViewport={false}>
<ScrollArea>
<NavigationMenuList>
<NavigationMenuItem>
<NavigationMenuTrigger>Item One</NavigationMenuTrigger>
<NavigationMenuContent>
<NavigationMenuLink>Link</NavigationMenuLink>
</NavigationMenuContent>
</NavigationMenuItem>
</NavigationMenuList>
<ScrollBar />
</ScrollArea>
<NavigationMenuViewport containerProps={{ className: 'w-full' }} />
</NavigationMenu>