Skip to Content
文档

抽屉

用于渲染从屏幕边缘滑入的内容。

SourceStorybookRecipeArk

用法

¥Usage

import { Drawer } from "@chakra-ui/react"
<Drawer.Root>
  <Drawer.Backdrop />
  <Drawer.Trigger />
  <Drawer.Positioner>
    <Drawer.Content>
      <Drawer.CloseTrigger />
      <Drawer.Header>
        <Drawer.Title />
      </Drawer.Header>
      <Drawer.Body />
      <Drawer.Footer />
    </Drawer.Content>
  </Drawer.Positioner>
</Drawer.Root>

示例

¥Examples

受控

¥Controlled

使用 openonOpenChange 属性控制抽屉组件。

¥Use the open and onOpenChange props to control the drawer component.

尺寸

¥Sizes

使用 size 属性更改抽屉组件的大小。

¥Use the size prop to change the size of the drawer component.

上下文

¥Context

使用 DrawerContext 组件从抽屉外部访问抽屉状态和方法。

¥Use the DrawerContext component to access the drawer state and methods from outside the drawer.

偏移

¥Offset

Drawer.Positioner 上使用 padding CSS 属性来调整抽屉组件的偏移量。

¥Use the padding CSS property on Drawer.Positioner to adjust the offset of the drawer component.

放置位置

¥Placement

使用 placement 属性更改抽屉组件的位置。

¥Use the placement prop to change the placement of the drawer component.

初始焦点

¥Initial Focus

使用 initialFocusEl 属性设置抽屉组件的初始焦点。

¥Use the initialFocusEl prop to set the initial focus of the drawer component.

自定义容器

¥Custom Container

以下是如何在自定义容器中渲染抽屉组件的示例。

¥Here's an example of how to render the drawer component in a custom container.

考虑将 closeOnInteractOutside 设置为 false,以防止抽屉在与外部交互时关闭。

¥Consider setting closeOnInteractOutside to false to prevent the drawer from closing when interacting outside the drawer.

Render drawer here

标题操作

¥Header Actions

以下是在抽屉组件标题中渲染操作的示例。

¥Here's an example of rendering actions in the header of the drawer component.

带条件变量的抽屉

¥Drawer with conditional variants

以下是如何根据不同断点更改变体的示例。

¥Here is an example of how to change variants based on the different breakpoints.

本示例使用 mdDown 断点更改抽屉在小屏幕上的位置。推荐使用此方法,因为两种条件都会转换为 CSS 媒体查询,从而避免基础样式合并问题。

¥This example uses the mdDown breakpoint to change the drawer's placement on smaller screens. This approach is recommended because both conditions are translated into CSS media queries, which helps avoid base style merging issues.

如果你确实想使用基本条件,则还需要定义相应的大小。例如:<Drawer.Root placement={{ base: "bottom", md: "end" }} size={{ base: "xs", md: "md" }}>

¥If you really want to use the base condition instead, you’ll also need to define corresponding sizes. For example: <Drawer.Root placement={{ base: "bottom", md: "end" }} size={{ base: "xs", md: "md" }}>

Open drawer and resize screen to mobile size

属性

¥Props

根元素

¥Root

PropDefaultType
closeOnEscape true
boolean

Whether to close the dialog when the escape key is pressed

closeOnInteractOutside true
boolean

Whether to close the dialog when the outside is clicked

defaultOpen false
boolean

The initial open state of the dialog when rendered. Use when you don't need to control the open state of the dialog.

lazyMount false
boolean

Whether to enable lazy mounting

modal true
boolean

Whether to prevent pointer interaction outside the element and hide all content below it

preventScroll true
boolean

Whether to prevent scrolling behind the dialog when it's opened

role '\'dialog\''
'dialog' | 'alertdialog'

The dialog's role

skipAnimationOnMount false
boolean

Whether to allow the initial presence animation.

trapFocus true
boolean

Whether to trap focus inside the dialog when it's opened

unmountOnExit false
boolean

Whether to unmount on exit.

colorPalette 'gray'
'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink'

The color palette of the component

size 'xs'
'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'full'

The size of the component

placement 'end'
'start' | 'end' | 'top' | 'bottom'

The placement of the component

as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.
unstyled
boolean

Whether to remove the component's style.

aria-label
string

Human readable label for the dialog, in event the dialog title is not rendered

finalFocusEl
() => MaybeElement

Element to receive focus when the dialog is closed

id
string

The unique identifier of the machine.

ids
Partial<{ trigger: string positioner: string backdrop: string content: string closeTrigger: string title: string description: string }>

The ids of the elements in the dialog. Useful for composition.

immediate
boolean

Whether to synchronize the present change immediately or defer it to the next frame

initialFocusEl
() => MaybeElement

Element to receive focus when the dialog is opened

onEscapeKeyDown
(event: KeyboardEvent) => void

Function called when the escape key is pressed

onExitComplete
VoidFunction

Function called when the animation ends in the closed state

onFocusOutside
(event: FocusOutsideEvent) => void

Function called when the focus is moved outside the component

onInteractOutside
(event: InteractOutsideEvent) => void

Function called when an interaction happens outside the component

onOpenChange
(details: OpenChangeDetails) => void

Function to call when the dialog's open state changes

onPointerDownOutside
(event: PointerDownOutsideEvent) => void

Function called when the pointer is pressed down outside the component

open
boolean

The controlled open state of the dialog

persistentElements
(() => Element | null)[]

Returns the persistent elements that: - should not have pointer-events disabled - should not trigger the dismiss event

present
boolean

Whether the node is present (controlled by the user)

restoreFocus
boolean

Whether to restore focus to the element that had focus before the dialog was opened

contained
'true' | 'false'

The contained of the component

Previous

对话框

Next

悬停卡片