章节
组件
"use client"
import * as React from "react"抽屉是从屏幕边缘滑入的面板,还可以拖动关闭。它基于 Emil Kowalski 的 Vaul 构建,并按新粗野主义的配方设计:粗边框、硬阴影、加粗字体。
适合以下场景:
- 移动端操作表——分享菜单、排序选项、快速确认。触屏上拖动关闭,比找一个小小的关闭按钮更顺手。
- 筛选与设置面板——电商筛选或偏好设置,比弹出层需要更多空间,又不必单独开一页。
- 响应式对话框——与
Dialog搭配,桌面端用模态框、移动端用抽屉(见下方示例)。
概述#
Drawer 构建于 emilkowalski 打造的 Vaul 之上。
安装#
pnpm dlx shadcn@latest add https://neobrutalism.com/r/base/drawer.jsonnpx shadcn@latest add https://neobrutalism.com/r/base/drawer.jsonyarn dlx shadcn@latest add https://neobrutalism.com/r/base/drawer.jsonbunx --bun shadcn@latest add https://neobrutalism.com/r/base/drawer.json
安装以下依赖:
pnpm add vaulnpm install vaulyarn add vaulbun add vaul
将以下代码复制并粘贴到您的项目中。
"use client"
import * as React from "react"
import { Drawer as DrawerPrimitive } from "vaul"
import { cn } from "@/lib/utils"
function Drawer({
...props
}: React.ComponentProps<typeof DrawerPrimitive.Root>) {
return <DrawerPrimitive.Root data-slot="drawer" {...props} />
}
function DrawerTrigger({
...props
}: React.ComponentProps<typeof DrawerPrimitive.Trigger>) {
return <DrawerPrimitive.Trigger data-slot="drawer-trigger" {...props} />
}
function DrawerPortal({
...props
}: React.ComponentProps<typeof DrawerPrimitive.Portal>) {
return <DrawerPrimitive.Portal data-slot="drawer-portal" {...props} />
}
function DrawerClose({
...props
}: React.ComponentProps<typeof DrawerPrimitive.Close>) {
return <DrawerPrimitive.Close data-slot="drawer-close" {...props} />
}
function DrawerOverlay({
className,
...props
}: React.ComponentProps<typeof DrawerPrimitive.Overlay>) {
return (
<DrawerPrimitive.Overlay
data-slot="drawer-overlay"
className={cn(
"fixed inset-0 z-50 bg-foreground/20 supports-backdrop-filter:backdrop-blur-xs data-open:animate-in data-open:fade-in-0 data-closed:animate-out data-closed:fade-out-0",
className
)}
{...props}
/>
)
}
function DrawerContent({
className,
children,
...props
}: React.ComponentProps<typeof DrawerPrimitive.Content>) {
return (
<DrawerPortal data-slot="drawer-portal">
<DrawerOverlay />
<DrawerPrimitive.Content
data-slot="drawer-content"
className={cn(
"group/drawer-content fixed z-50 flex h-auto flex-col bg-popover text-sm text-popover-foreground shadow-md data-[vaul-drawer-direction=bottom]:inset-x-0 data-[vaul-drawer-direction=bottom]:bottom-0 data-[vaul-drawer-direction=bottom]:mt-24 data-[vaul-drawer-direction=bottom]:max-h-[80vh] data-[vaul-drawer-direction=bottom]:rounded-t-xl data-[vaul-drawer-direction=bottom]:border-t-2 data-[vaul-drawer-direction=left]:inset-y-0 data-[vaul-drawer-direction=left]:left-0 data-[vaul-drawer-direction=left]:w-3/4 data-[vaul-drawer-direction=left]:rounded-r-xl data-[vaul-drawer-direction=left]:border-r-2 data-[vaul-drawer-direction=right]:inset-y-0 data-[vaul-drawer-direction=right]:right-0 data-[vaul-drawer-direction=right]:w-3/4 data-[vaul-drawer-direction=right]:rounded-l-xl data-[vaul-drawer-direction=right]:border-l-2 data-[vaul-drawer-direction=top]:inset-x-0 data-[vaul-drawer-direction=top]:top-0 data-[vaul-drawer-direction=top]:mb-24 data-[vaul-drawer-direction=top]:max-h-[80vh] data-[vaul-drawer-direction=top]:rounded-b-xl data-[vaul-drawer-direction=top]:border-b-2 data-[vaul-drawer-direction=left]:sm:max-w-sm data-[vaul-drawer-direction=right]:sm:max-w-sm",
className
)}
{...props}
>
<div className="mx-auto mt-4 hidden h-1 w-[100px] shrink-0 rounded-full bg-muted group-data-[vaul-drawer-direction=bottom]/drawer-content:block" />
{children}
</DrawerPrimitive.Content>
</DrawerPortal>
)
}
function DrawerHeader({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="drawer-header"
className={cn(
"flex flex-col gap-0.5 p-4 group-data-[vaul-drawer-direction=bottom]/drawer-content:text-center group-data-[vaul-drawer-direction=top]/drawer-content:text-center md:gap-0.5 md:text-left",
className
)}
{...props}
/>
)
}
function DrawerFooter({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="drawer-footer"
className={cn("mt-auto flex flex-col gap-2 p-4", className)}
{...props}
/>
)
}
function DrawerTitle({
className,
...props
}: React.ComponentProps<typeof DrawerPrimitive.Title>) {
return (
<DrawerPrimitive.Title
data-slot="drawer-title"
className={cn(
"font-head text-base font-medium text-foreground",
className
)}
{...props}
/>
)
}
function DrawerDescription({
className,
...props
}: React.ComponentProps<typeof DrawerPrimitive.Description>) {
return (
<DrawerPrimitive.Description
data-slot="drawer-description"
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
)
}
export {
Drawer,
DrawerPortal,
DrawerOverlay,
DrawerTrigger,
DrawerClose,
DrawerContent,
DrawerHeader,
DrawerFooter,
DrawerTitle,
DrawerDescription,
}
更新导入路径以匹配您的项目结构。
用法#
import {
Drawer,
DrawerClose,
DrawerContent,
DrawerDescription,
DrawerFooter,
DrawerHeader,
DrawerTitle,
DrawerTrigger,
} from "@/components/ui/drawer"<Drawer>
<DrawerTrigger>Open</DrawerTrigger>
<DrawerContent>
<DrawerHeader>
<DrawerTitle>Are you absolutely sure?</DrawerTitle>
<DrawerDescription>This action cannot be undone.</DrawerDescription>
</DrawerHeader>
<DrawerFooter>
<Button>Submit</Button>
<DrawerClose>
<Button variant="outline">Cancel</Button>
</DrawerClose>
</DrawerFooter>
</DrawerContent>
</Drawer>组合#
使用以下组合来构建 Drawer:
Drawer
├── DrawerTrigger
└── DrawerContent
├── DrawerHeader
│ ├── DrawerTitle
│ └── DrawerDescription
└── DrawerFooter示例#
可滚动内容#
内容滚动时保持操作按钮可见。
import { Button } from "@/components/ui/button"
import {
Drawer,方位#
使用 direction 属性来设置抽屉展开的方位。可选值为 top、right、bottom 和 left。
import { Button } from "@/components/ui/button"
import {
Drawer,响应式对话框#
您可以将 Dialog 和 Drawer 组件组合起来创建响应式对话框。它在桌面端渲染 Dialog 组件,在移动端渲染 Drawer。
"use client"
import * as React from "react"RTL#
若要在 Neobrutalism 中启用 RTL 支持,请参阅 RTL 配置指南。
"use client"
import * as React from "react"无障碍#
抽屉遵循 WAI-ARIA Dialog (Modal) 模式:Vaul 构建在 Radix Dialog 基元之上,因此面板渲染为带 role="dialog" 与 aria-modal 的元素,打开时锁定焦点,并由 DrawerTitle 标注、由 DrawerDescription 描述。拖动关闭仅适用于指针操作——键盘用户通过 Escape 或 DrawerClose 按钮关闭,所以务必提供其一。
键盘交互:
| 按键 | 操作 |
|---|---|
Space / Enter | 从聚焦的触发器打开抽屉 |
Tab / Shift + Tab | 在抽屉内容中循环焦点(焦点被锁定) |
Escape | 关闭抽屉,并把焦点交还给触发器 |
API 参考#
完整的 API 参考请参阅 Vaul 文档。