import { Button } from "@/components/ui/button"
import {
DrawerActionTrigger,
DrawerBackdrop,
DrawerBody,
DrawerCloseTrigger,
DrawerContent,
DrawerFooter,
DrawerHeader,
DrawerRoot,
DrawerTitle,
DrawerTrigger,
} from "@/components/ui/drawer"
const Demo = () => {
return (
<DrawerRoot>
<DrawerBackdrop />
<DrawerTrigger asChild>
<Button variant="outline" size="sm">
Open Drawer
</Button>
</DrawerTrigger>
<DrawerContent>
<DrawerHeader>
<DrawerTitle>Drawer Title</DrawerTitle>
</DrawerHeader>
<DrawerBody>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
</DrawerBody>
<DrawerFooter>
<DrawerActionTrigger asChild>
<Button variant="outline">Cancel</Button>
</DrawerActionTrigger>
<Button>Save</Button>
</DrawerFooter>
<DrawerCloseTrigger />
</DrawerContent>
</DrawerRoot>
)
}
Setup
If you don't already have the snippet, run the following command to add the
drawer
snippet
npx @chakra-ui/cli snippet add drawer
The snippet includes a closed component composition for the Drawer
component.
Usage
import {
DrawerBackdrop,
DrawerBody,
DrawerCloseTrigger,
DrawerContent,
DrawerFooter,
DrawerHeader,
DrawerRoot,
DrawerTitle,
DrawerTrigger,
} from "@/components/ui/drawer"
<DrawerRoot>
<DrawerBackdrop />
<DrawerTrigger />
<DrawerContent>
<DrawerCloseTrigger />
<DrawerHeader>
<DrawerTitle />
</DrawerHeader>
<DrawerBody />
<DrawerFooter />
</DrawerContent>
</DrawerRoot>
Examples
Controlled
Use the open
and onOpenChange
props to control the drawer component.
"use client"
import { Button } from "@/components/ui/button"
import {
DrawerActionTrigger,
DrawerBackdrop,
DrawerBody,
DrawerCloseTrigger,
DrawerContent,
DrawerFooter,
DrawerHeader,
DrawerRoot,
DrawerTitle,
DrawerTrigger,
} from "@/components/ui/drawer"
import { useState } from "react"
const Demo = () => {
const [open, setOpen] = useState(false)
return (
<DrawerRoot open={open} onOpenChange={(e) => setOpen(e.open)}>
<DrawerBackdrop />
<DrawerTrigger asChild>
<Button variant="outline" size="sm">
Open Drawer
</Button>
</DrawerTrigger>
<DrawerContent>
<DrawerHeader>
<DrawerTitle>Drawer Title</DrawerTitle>
</DrawerHeader>
<DrawerBody>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
</DrawerBody>
<DrawerFooter>
<DrawerActionTrigger asChild>
<Button variant="outline">Cancel</Button>
</DrawerActionTrigger>
<Button>Save</Button>
</DrawerFooter>
<DrawerCloseTrigger />
</DrawerContent>
</DrawerRoot>
)
}
Sizes
Use the size
prop to change the size of the drawer component.
import { For, HStack, Kbd } from "@chakra-ui/react"
import { Button } from "@/components/ui/button"
import {
DrawerActionTrigger,
DrawerBackdrop,
DrawerBody,
DrawerCloseTrigger,
DrawerContent,
DrawerFooter,
DrawerHeader,
DrawerRoot,
DrawerTitle,
DrawerTrigger,
} from "@/components/ui/drawer"
const Demo = () => {
return (
<HStack wrap="wrap">
<For each={["xs", "sm", "md", "lg", "xl", "full"]}>
{(size) => (
<DrawerRoot key={size} size={size}>
<DrawerBackdrop />
<DrawerTrigger asChild>
<Button variant="outline" size="sm">
Open ({size})
</Button>
</DrawerTrigger>
<DrawerContent>
<DrawerHeader>
<DrawerTitle>Drawer Title</DrawerTitle>
</DrawerHeader>
<DrawerBody>
Press the <Kbd>esc</Kbd> key to close the drawer.
</DrawerBody>
<DrawerFooter>
<DrawerActionTrigger asChild>
<Button variant="outline">Cancel</Button>
</DrawerActionTrigger>
<Button>Save</Button>
</DrawerFooter>
<DrawerCloseTrigger />
</DrawerContent>
</DrawerRoot>
)}
</For>
</HStack>
)
}
Offset
Pass the offset
prop to the DrawerContent
to change the offset of the drawer
component.
import { Button } from "@/components/ui/button"
import {
DrawerActionTrigger,
DrawerBackdrop,
DrawerBody,
DrawerCloseTrigger,
DrawerContent,
DrawerFooter,
DrawerHeader,
DrawerRoot,
DrawerTitle,
DrawerTrigger,
} from "@/components/ui/drawer"
const Demo = () => {
return (
<DrawerRoot>
<DrawerBackdrop />
<DrawerTrigger asChild>
<Button variant="outline" size="sm">
Open Drawer
</Button>
</DrawerTrigger>
<DrawerContent offset="4" rounded="md">
<DrawerHeader>
<DrawerTitle>Drawer Title</DrawerTitle>
</DrawerHeader>
<DrawerBody>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
</DrawerBody>
<DrawerFooter>
<DrawerActionTrigger asChild>
<Button variant="outline">Cancel</Button>
</DrawerActionTrigger>
<Button>Save</Button>
</DrawerFooter>
<DrawerCloseTrigger />
</DrawerContent>
</DrawerRoot>
)
}
Placement
Use the placement
prop to change the placement of the drawer component.
import { For, HStack } from "@chakra-ui/react"
import { Button } from "@/components/ui/button"
import {
DrawerActionTrigger,
DrawerBackdrop,
DrawerBody,
DrawerCloseTrigger,
DrawerContent,
DrawerFooter,
DrawerHeader,
DrawerRoot,
DrawerTitle,
DrawerTrigger,
} from "@/components/ui/drawer"
const Demo = () => {
return (
<HStack wrap="wrap">
<For each={["bottom", "top", "start", "end"]}>
{(placement) => (
<DrawerRoot key={placement} placement={placement}>
<DrawerBackdrop />
<DrawerTrigger asChild>
<Button variant="outline" size="sm">
Open ({placement})
</Button>
</DrawerTrigger>
<DrawerContent
roundedTop={placement === "bottom" ? "l3" : undefined}
roundedBottom={placement === "top" ? "l3" : undefined}
>
<DrawerHeader>
<DrawerTitle>Drawer Title</DrawerTitle>
</DrawerHeader>
<DrawerBody>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
</DrawerBody>
<DrawerFooter>
<DrawerActionTrigger asChild>
<Button variant="outline">Cancel</Button>
</DrawerActionTrigger>
<Button>Save</Button>
</DrawerFooter>
<DrawerCloseTrigger />
</DrawerContent>
</DrawerRoot>
)}
</For>
</HStack>
)
}
Initial Focus
Use the initialFocusEl
prop to set the initial focus of the drawer component.
"use client"
import { Input, Stack } from "@chakra-ui/react"
import { Button } from "@/components/ui/button"
import {
DrawerActionTrigger,
DrawerBackdrop,
DrawerBody,
DrawerCloseTrigger,
DrawerContent,
DrawerFooter,
DrawerHeader,
DrawerRoot,
DrawerTitle,
DrawerTrigger,
} from "@/components/ui/drawer"
import { useRef } from "react"
const Demo = () => {
const ref = useRef<HTMLInputElement>(null)
return (
<DrawerRoot initialFocusEl={() => ref.current}>
<DrawerBackdrop />
<DrawerTrigger asChild>
<Button variant="outline" size="sm">
Open Drawer
</Button>
</DrawerTrigger>
<DrawerContent>
<DrawerHeader>
<DrawerTitle>Drawer Title</DrawerTitle>
</DrawerHeader>
<DrawerBody>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
<Stack mt="5">
<Input defaultValue="Naruto" placeholder="First name" />
<Input ref={ref} placeholder="Email" />
</Stack>
</DrawerBody>
<DrawerFooter>
<DrawerActionTrigger asChild>
<Button variant="outline">Cancel</Button>
</DrawerActionTrigger>
<Button>Save</Button>
</DrawerFooter>
<DrawerCloseTrigger />
</DrawerContent>
</DrawerRoot>
)
}
Props
Root
Prop | Default | Type |
---|---|---|
colorPalette | 'gray' | 'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink' | 'accent' 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 |
contained | 'true' | 'false' The contained 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. |