What is @radix-ui/react-navigation-menu?
@radix-ui/react-navigation-menu is a React component library that provides accessible and customizable navigation menus. It is part of the Radix UI suite, which focuses on providing unstyled, accessible components that can be easily styled to fit any design system.
What are @radix-ui/react-navigation-menu's main functionalities?
Basic Navigation Menu
This code sample demonstrates how to create a basic navigation menu with three links: Home, About, and Contact. The NavigationMenu component wraps the entire menu, while NavigationMenuItem and NavigationMenuLink are used for individual menu items and links, respectively.
import { NavigationMenu, NavigationMenuItem, NavigationMenuLink } from '@radix-ui/react-navigation-menu';
function BasicMenu() {
return (
<NavigationMenu>
<NavigationMenuItem>
<NavigationMenuLink href="#home">Home</NavigationMenuLink>
</NavigationMenuItem>
<NavigationMenuItem>
<NavigationMenuLink href="#about">About</NavigationMenuLink>
</NavigationMenuItem>
<NavigationMenuItem>
<NavigationMenuLink href="#contact">Contact</NavigationMenuLink>
</NavigationMenuItem>
</NavigationMenu>
);
}
Submenu
This code sample demonstrates how to create a navigation menu with a submenu. The Services menu item has a submenu with two links: Design and Development. The NavigationMenuSub, NavigationMenuSubTrigger, and NavigationMenuSubContent components are used to create the submenu structure.
import { NavigationMenu, NavigationMenuItem, NavigationMenuLink, NavigationMenuSub, NavigationMenuSubTrigger, NavigationMenuSubContent } from '@radix-ui/react-navigation-menu';
function Submenu() {
return (
<NavigationMenu>
<NavigationMenuItem>
<NavigationMenuLink href="#home">Home</NavigationMenuLink>
</NavigationMenuItem>
<NavigationMenuItem>
<NavigationMenuSub>
<NavigationMenuSubTrigger>Services</NavigationMenuSubTrigger>
<NavigationMenuSubContent>
<NavigationMenuLink href="#design">Design</NavigationMenuLink>
<NavigationMenuLink href="#development">Development</NavigationMenuLink>
</NavigationMenuSubContent>
</NavigationMenuSub>
</NavigationMenuItem>
<NavigationMenuItem>
<NavigationMenuLink href="#contact">Contact</NavigationMenuLink>
</NavigationMenuItem>
</NavigationMenu>
);
}
Custom Styling
This code sample demonstrates how to apply custom styling to the navigation menu. By adding className properties and importing a custom CSS file, you can style the menu to fit your design requirements.
import { NavigationMenu, NavigationMenuItem, NavigationMenuLink } from '@radix-ui/react-navigation-menu';
import './customStyles.css';
function CustomStyledMenu() {
return (
<NavigationMenu className="custom-menu">
<NavigationMenuItem className="custom-item">
<NavigationMenuLink className="custom-link" href="#home">Home</NavigationMenuLink>
</NavigationMenuItem>
<NavigationMenuItem className="custom-item">
<NavigationMenuLink className="custom-link" href="#about">About</NavigationMenuLink>
</NavigationMenuItem>
<NavigationMenuItem className="custom-item">
<NavigationMenuLink className="custom-link" href="#contact">Contact</NavigationMenuLink>
</NavigationMenuItem>
</NavigationMenu>
);
}
Other packages similar to @radix-ui/react-navigation-menu
react-router-dom
react-router-dom is a popular library for routing in React applications. It provides a set of components for declaratively defining routes and navigation. While it focuses more on routing and less on the UI aspect of navigation menus, it can be used in conjunction with other UI libraries to create complex navigation structures.
react-bootstrap
react-bootstrap is a library that provides Bootstrap components as React components. It includes a Nav component that can be used to create navigation menus. Compared to @radix-ui/react-navigation-menu, react-bootstrap comes with predefined styles and is part of a larger UI framework.
material-ui
material-ui is a popular React component library that implements Google's Material Design. It includes a Menu component that can be used to create navigation menus. Material-UI provides a wide range of pre-styled components, making it a good choice if you want to follow Material Design guidelines.