106k
New

抽屉

React 的抽屉组件。

"use client"

import * as React from "react"

关于

抽屉组件基于 emilkowalskiVaul 构建。

安装

pnpm dlx shadcn@latest add drawer

用法

import {
  Drawer,
  DrawerClose,
  DrawerContent,
  DrawerDescription,
  DrawerFooter,
  DrawerHeader,
  DrawerTitle,
  DrawerTrigger,
} from "@/components/ui/drawer"
<Drawer>
  <DrawerTrigger>打开</DrawerTrigger>
  <DrawerContent>
    <DrawerHeader>
      <DrawerTitle>你确定要这样做吗?</DrawerTitle>
      <DrawerDescription>此操作不可撤销。</DrawerDescription>
    </DrawerHeader>
    <DrawerFooter>
      <Button>提交</Button>
      <DrawerClose>
        <Button variant="outline">取消</Button>
      </DrawerClose>
    </DrawerFooter>
  </DrawerContent>
</Drawer>

示例

可滚动内容

保持操作可见,同时内容可滚动。

import { Button } from "@/components/ui/button"
import {
  Drawer,

侧边

使用 direction 属性设置抽屉显示的侧边。可选项有 toprightbottomleft

import { Button } from "@/components/ui/button"
import {
  Drawer,

响应式对话框

你可以结合 DialogDrawer 组件来创建响应式对话框。在桌面端渲染 Dialog 组件,在移动端渲染 Drawer

"use client"

import * as React from "react"

API 参考

完整 API 参考请参见 Vaul 文档