Drawer

Display overlay panel at any side of the screen.

Import

import { Drawer } from '@tidbcloud/uikit'

Usage

import { useDisclosure } from '@mantine/hooks'
import { Drawer, Button } from '@tidbcloud/uikit'
 
function Demo() {
  const [opened, { open, close }] = useDisclosure(false)
 
  return (
    <>
      <Drawer opened={opened} onClose={close} title="Authentication">
        {/* Drawer content */}
      </Drawer>
 
      <Button onClick={open}>Open Drawer</Button>
    </>
  )
}

Position

Drawer can be positioned on left (default), top, right, and bottom:

import { useDisclosure } from '@mantine/hooks'
import { Drawer, Button, Group } from '@tidbcloud/uikit'
 
function Demo() {
  const [rightOpened, { open: openRight, close: closeRight }] = useDisclosure(false)
  const [topOpened, { open: openTop, close: closeTop }] = useDisclosure(false)
 
  return (
    <>
      <Drawer position="right" opened={rightOpened} onClose={closeRight} title="Right Drawer">
        Drawer content
      </Drawer>
      <Drawer position="top" opened={topOpened} onClose={closeTop} title="Top Drawer">
        Drawer content
      </Drawer>
 
      <Group>
        <Button onClick={openRight}>Right</Button>
        <Button onClick={openTop}>Top</Button>
      </Group>
    </>
  )
}

Offset

Set offset to change drawer offset from the edge of the viewport:

<Drawer offset={8} radius="md" />

Size

Set size to control drawer width (for left/right position) or height (for top/bottom position):

<Drawer size="xs" />  // 320px
<Drawer size="sm" />  // 380px
<Drawer size="md" />  // 440px (default)
<Drawer size="lg" />  // 620px
<Drawer size="xl" />  // 780px
<Drawer size="100%" /> // Full width/height
<Drawer size={500} />  // Custom pixels

Without overlay

Use withOverlay={false} to render drawer without overlay:

<Drawer withOverlay={false} />

Without close button

Use withCloseButton={false} to hide close button:

<Drawer withCloseButton={false} />

Change transition

Use transitionProps to customize the transition:

<Drawer transitionProps={{ transition: 'rotate-left', duration: 150 }} />

Scrollable content

import { useDisclosure } from '@mantine/hooks'
import { Drawer, Button } from '@tidbcloud/uikit'
 
function Demo() {
  const [opened, { open, close }] = useDisclosure(false)
 
  const content = Array(100)
    .fill(0)
    .map((_, index) => <p key={index}>Drawer with scroll</p>)
 
  return (
    <>
      <Drawer opened={opened} onClose={close} title="Header is sticky">
        {content}
      </Drawer>
 
      <Button onClick={open}>Open Drawer</Button>
    </>
  )
}

Compound components

Use compound components for better control over drawer parts:

import { useDisclosure } from '@mantine/hooks'
import { Drawer, Button } from '@tidbcloud/uikit'
 
function Demo() {
  const [opened, { open, close }] = useDisclosure(false)
 
  return (
    <>
      <Drawer.Root opened={opened} onClose={close}>
        <Drawer.Overlay />
        <Drawer.Content>
          <Drawer.Header>
            <Drawer.Title>Drawer title</Drawer.Title>
            <Drawer.CloseButton />
          </Drawer.Header>
          <Drawer.Body>Drawer content</Drawer.Body>
        </Drawer.Content>
      </Drawer.Root>
 
      <Button onClick={open}>Open drawer</Button>
    </>
  )
}

Props

PropTypeDefaultDescription
openedboolean-Controls drawer opened state
onClose() => void-Called when drawer is closed
titleReactNode-Drawer title
position’top’ | ‘right’ | ‘bottom’ | ‘left''left’Drawer position
sizeMantineSize | number’md’Drawer width/height
offsetnumber0Offset from viewport edge
radiusMantineRadius0Border radius
withCloseButtonbooleantrueShow close button
withOverlaybooleantrueShow overlay
closeOnClickOutsidebooleantrueClose on outside click
closeOnEscapebooleantrueClose on escape key
trapFocusbooleantrueTrap focus inside drawer
transitionPropsTransitionProps-Transition configuration