下拉菜单
向用户显示菜单 - 例如一组操作或功能 - 由按钮触发。
安装
¥Installation
pnpm dlx shadcn@latest add dropdown-menu
用法
¥Usage
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
<DropdownMenu>
<DropdownMenuTrigger>Open</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuLabel>My Account</DropdownMenuLabel>
<DropdownMenuSeparator />
<DropdownMenuItem>Profile</DropdownMenuItem>
<DropdownMenuItem>Billing</DropdownMenuItem>
<DropdownMenuItem>Team</DropdownMenuItem>
<DropdownMenuItem>Subscription</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
示例
¥Examples
复选框
¥Checkboxes
单选按钮组
¥Radio Group
更新日志
¥Changelog
2024-10-16 图标类
¥2024-10-16 Classes for icons
将 gap-2 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0
添加到 DropdownMenuItem
以自动设置下拉菜单项内的图标样式。
¥Added gap-2 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0
to the DropdownMenuItem
to automatically style icon inside the dropdown menu item.
将以下类添加到你的 dropdown-menu.tsx
文件中的 DropdownMenuItem
。
¥Add the following classes to the DropdownMenuItem
in your dropdown-menu.tsx
file.
const DropdownMenuItem = React.forwardRef<
React.ElementRef<typeof DropdownMenuPrimitive.Item>,
React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.Item> & {
inset?: boolean
}
>(({ className, inset, ...props }, ref) => (
<DropdownMenuPrimitive.Item
ref={ref}
className={cn(
"relative ... gap-2 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
inset && "pl-8",
className
)}
{...props}
/>
))
2024-10-25 <DropdownMenuSubTrigger />
的类
¥2024-10-25 Classes for <DropdownMenuSubTrigger />
将 gap-2 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0
添加到 <DropdownMenuSubTrigger />
以自动设置内部图标样式。
¥Added gap-2 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0
to the <DropdownMenuSubTrigger />
to automatically style icon inside.
将以下类添加到你的 dropdown-menu.tsx
文件中的 cva
调用。
¥Add the following classes to the cva
call in your dropdown-menu.tsx
file.
<DropdownMenuPrimitive.SubTrigger
ref={ref}
className={cn(
"flex cursor-default gap-2 select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none focus:bg-accent data-[state=open]:bg-accent [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
inset && "pl-8",
className
)}
{...props}
>
{/* ... */}
</DropdownMenuPrimitive.SubTrigger>