React dropdown components built with shadcn/ui and Tailwind CSS. Covers the five most common dropdown patterns — basic action menu, user account menu, nested submenus, command palette, and right-click context menu.
Key Classes Reference
| Component | Purpose |
|---|---|
DropdownMenu | Root wrapper — manages open/close state |
DropdownMenuTrigger | Element that opens the menu on click |
DropdownMenuContent | The floating menu panel |
DropdownMenuItem | Individual clickable row |
DropdownMenuSub | Nested submenu wrapper |
ContextMenuTrigger | Wraps the right-click target area |
Command + CommandInput | Searchable command palette |
1. Basic Dropdown Menu
Standard dropdown with icons, separator and keyboard shortcut hints.
tsx
import {
DropdownMenu, DropdownMenuContent, DropdownMenuItem,
DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger,
DropdownMenuShortcut,
} from "@/components/ui/dropdown-menu"
import { Button } from "@/components/ui/button"
import { Settings, User, LogOut, HelpCircle, ChevronDown } from "lucide-react"
export default function BasicDropdown() {
return (
<div className="p-6 flex justify-center">
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline" className="gap-2">
Options <ChevronDown className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent className="w-52">
<DropdownMenuLabel>My Account</DropdownMenuLabel>
<DropdownMenuSeparator />
<DropdownMenuItem className="gap-2 cursor-pointer">
<User className="h-4 w-4" /> Profile
<DropdownMenuShortcut>⇧⌘P</DropdownMenuShortcut>
</DropdownMenuItem>
<DropdownMenuItem className="gap-2 cursor-pointer">
<Settings className="h-4 w-4" /> Settings
<DropdownMenuShortcut>⌘S</DropdownMenuShortcut>
</DropdownMenuItem>
<DropdownMenuItem className="gap-2 cursor-pointer">
<HelpCircle className="h-4 w-4" /> Help
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem className="gap-2 cursor-pointer text-red-500 focus:text-red-500">
<LogOut className="h-4 w-4" /> Sign Out
<DropdownMenuShortcut>⌘Q</DropdownMenuShortcut>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
)
}4. Command Palette Dropdown
Searchable command palette triggered by a button using shadcn/ui Command.
tsx
import { useState } from "react"
import { Button } from "@/components/ui/button"
import {
Command, CommandEmpty, CommandGroup, CommandInput,
CommandItem, CommandList, CommandSeparator,
} from "@/components/ui/command"
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"
import { Search, LayoutDashboard, Users, Settings, FileText, BarChart } from "lucide-react"
const commands = {
Pages: [
{ icon: LayoutDashboard, label: "Dashboard", href: "/" },
{ icon: Users, label: "Team", href: "/team" },
{ icon: BarChart, label: "Analytics", href: "/analytics" },
],
Settings: [
{ icon: Settings, label: "Preferences", href: "/settings" },
{ icon: FileText, label: "API Keys", href: "/api-keys" },
],
}
export default function CommandDropdown() {
const [open, setOpen] = useState(false)
return (
<div className="p-6 flex justify-center">
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button variant="outline" className="gap-2 w-48 justify-start text-muted-foreground">
<Search className="h-4 w-4" />Search...
<kbd className="ml-auto text-xs bg-gray-100 px-1.5 py-0.5 rounded">⌘K</kbd>
</Button>
</PopoverTrigger>
<PopoverContent className="p-0 w-64" align="start">
<Command>
<CommandInput placeholder="Type a command..." />
<CommandList>
<CommandEmpty>No results found.</CommandEmpty>
{Object.entries(commands).map(([group, items]) => (
<CommandGroup key={group} heading={group}>
{items.map((item) => (
<CommandItem
key={item.label}
onSelect={() => { window.location.href = item.href; setOpen(false) }}
className="gap-2 cursor-pointer"
>
<item.icon className="h-4 w-4 text-muted-foreground" />
{item.label}
</CommandItem>
))}
</CommandGroup>
))}
</CommandList>
</Command>
</PopoverContent>
</Popover>
</div>
)
}Frequently Asked Questions
Import DropdownMenu, DropdownMenuTrigger, DropdownMenuContent, and DropdownMenuItem from shadcn/ui. Wrap your trigger button in DropdownMenuTrigger and your menu items in DropdownMenuContent. shadcn/ui handles positioning, keyboard navigation and close-on-click automatically.
Use DropdownMenuSub, DropdownMenuSubTrigger, and DropdownMenuSubContent from shadcn/ui. Nest a DropdownMenuSub block inside your DropdownMenuContent. The submenu opens on hover automatically.
Need a Full React + Next.js Dashboard Template?
Get a complete React + Next.js dashboard with 50+ components — built by the same team behind BootstrapPlanet.
Browse Templates →Use code FIRST30 for 30% off.