Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Drawer for UI SDK

A panel for React on UI SDK that slides in from any edge and follows a swipe to close, with snap points for a sheet that peeks before it expands.

On this page9 sections

A drawer slides a panel in from the edge of the screen and follows the finger, so dragging it back closes it. It suits phones, where a bottom drawer sits under the thumb: a filter list, a delivery slot picker, a short form.

On a wide screen with nothing to swipe, a Sheet does the same job from the side, and a Dialog fits a short task in the middle.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Drawer anatomy: grabber, title, description, content, close

Usage

import { useState } from "react";
import { useIsMobile } from "@/hooks/use-is-mobile";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import {
  Drawer, DrawerClose, DrawerContent, DrawerDescription, DrawerFooter,
  DrawerHeader, DrawerTitle, DrawerTrigger,
} from "@/components/ui/drawer";
import {
  Field, FieldContent, FieldDescription, FieldLabel, FieldTitle,
} from "@/components/ui/field";
import {
  RadioGroup, RadioGroupItem,
} from "@/components/ui/radio-group";
import { ScrollArea } from "@/components/ui/scroll-area";
import { Toaster, toast } from "@/components/ui/toast";
const deliveryTimes = [
  {
    badge: "Fastest",
    description: "25–35 min · Driver assigned now",
    id: "delivery-asap",
    label: "Standard delivery",
    value: "asap",
  },
  {
    description: "Prep starts at 4:45 PM",
    id: "delivery-5-00",
    label: "5:00 PM – 5:15 PM",
    value: "5-00",
  },
  {
    description: "Good if you're heading home",
    id: "delivery-5-30",
    label: "5:30 PM – 5:45 PM",
    value: "5-30",
  },
  {
    description: "Most popular · High demand",
    id: "delivery-6-00",
    label: "6:00 PM – 6:15 PM",
    value: "6-00",
  },
  {
    description: "Last slot before kitchen closes",
    id: "delivery-6-30",
    label: "6:30 PM – 6:45 PM",
    value: "6-30",
  },
];

const DrawerDemo = () => {
  const [open, setOpen] = useState(false);
  const [deliveryTime, setDeliveryTime] = useState("asap");
  const isMobile = useIsMobile();

  const handleConfirm = () => {
    const selected = deliveryTimes.find((time) => time.value === deliveryTime);

    if (!selected) {
      return;
    }

    setOpen(false);
    toast.add({
      description: selected.label,
      title: "Delivery time confirmed",
    });
  };

  return (
    <>
      <Toaster />
      <Drawer
        onOpenChange={setOpen}
        open={open}
        showSwipeHandle={isMobile}
        swipeDirection={isMobile ? "down" : "right"}
      >
        <DrawerTrigger render={<Button variant="secondary" />}>
          Open Drawer
        </DrawerTrigger>
        <DrawerContent>
          <DrawerHeader>
            <DrawerTitle>Pick a delivery time</DrawerTitle>
            <DrawerDescription>
              We&apos;ll prepare your order as soon as possible.
            </DrawerDescription>
          </DrawerHeader>
          <ScrollArea className="min-h-0 flex-1 p-4">
            <RadioGroup
              className="gap-2"
              onValueChange={setDeliveryTime}
              value={deliveryTime}
            >
              {deliveryTimes.map((time) => (
                <FieldLabel htmlFor={time.id} key={time.value}>
                  <Field orientation="horizontal">
                    <FieldContent>
                      <FieldTitle className="flex items-center gap-2">
                        {time.label}
                        {time.badge ? (
                          <Badge variant="secondary">{time.badge}</Badge>
                        ) : null}
                      </FieldTitle>
                      <FieldDescription>{time.description}</FieldDescription>
                    </FieldContent>
                    <RadioGroupItem id={time.id} value={time.value} />
                  </Field>
                </FieldLabel>
              ))}
            </RadioGroup>
          </ScrollArea>
          <DrawerFooter>
            <Button className="h-[34px]" onClick={handleConfirm} type="button">
              Confirm Delivery Time
            </Button>
            <DrawerClose render={<Button variant="outline" />}>
              Cancel
            </DrawerClose>
          </DrawerFooter>
        </DrawerContent>
      </Drawer>
    </>
  );
};

Accessibility

The panel has role="dialog", takes its name from DrawerTitle and its description from DrawerDescription. While it is modal, focus stays inside it and the page behind is hidden from screen readers, and Escape closes it and returns focus to the trigger. Focus starts on the first focusable element inside, or on the panel itself when it was opened by touch, so a phone's keyboard doesn't pop up. A swipe has no keyboard equivalent, so give every drawer a visible Close or Cancel button.

Enter
When focus is on the trigger, opens the drawer. On the swipe handle of a drawer with more than one snap point, moves it to the next larger one, and from the largest back to the smallest.
Space
When focus is on the trigger, opens the drawer. On the swipe handle of a drawer with more than one snap point, moves it to the next larger one, and from the largest back to the smallest.
Arrow Up
On the swipe handle, moves a bottom drawer to its next larger snap point and a top drawer to its next smaller one, without wrapping around.
Arrow Down
On the swipe handle, moves a bottom drawer to its next smaller snap point and a top drawer to its next larger one, without wrapping around.
Arrow Left
On the swipe handle, moves a right drawer to its next larger snap point and a left drawer to its next smaller one, without wrapping around.
Arrow Right
On the swipe handle, moves a left drawer to its next larger snap point and a right drawer to its next smaller one, without wrapping around.
Home
On the swipe handle, moves the drawer to its smallest snap point.
End
On the swipe handle, moves the drawer to its largest snap point.
Tab
Moves focus to the next focusable element. While the drawer is modal, focus stays inside it.
Shift+Tab
Moves focus to the previous focusable element. While the drawer is modal, focus stays inside it.
Escape
Closes the drawer, the top one when drawers are nested, and moves focus back to its trigger.

Demos

Sides

swipeDirection set to left opens this drawer from the left edge, and a swipe to the left closes it. The other values are right, up and down, the default. Use a side for navigation or a settings tray that sits beside the page, and keep the bottom for phones, where it is under the thumb.

Preview width

Swipe handle

A bottom drawer with the bar drawn along its top edge, set with showSwipeHandle on Drawer, which is off unless you turn it on. The whole panel follows a drag either way, and the bar is the hint that it can be pulled down. Show it on bottom drawers, and leave it off a side panel, where the Close button does the job.

Preview width

Nested

Four drawers deep: each footer has a button that opens the next drawer from the same edge, the right on a wide screen and the bottom on a phone. The drawers behind stay mounted, step back and come forward again as the front one closes. Use it for a short drill-in, such as a category and then an item.

Preview width

Non-modal

A drawer on the right with modal set to false, so the page behind stays usable, and disablePointerDismissal added, so a click on the page doesn't close it. Use it for a reference or help panel beside a form people are filling in. Keep a drawer modal when people have to finish the choice inside it.

Preview width

Snap points

snapPoints set to 31rem and 1 gives this bottom drawer two heights: it opens at the first and drags up to nearly the full screen at the second. A number up to 1 is a share of the screen's height, and a larger one is pixels. The handle switches heights on a press or the arrow keys. Use it for a map, a playlist or comments that preview first.

Preview width

Dialog

One edit profile form behind one open state: from 768 pixels wide it opens in a centered Dialog, and below that in a bottom drawer, switched by useIsMobile. The form is one component used in both, and saving it closes whichever is open. Pick this when the same short task runs on desktop and on phones, and only the container should change.

Preview width

FAQ

When should I use a Drawer instead of a Sheet?

Use a Drawer when people will drag the panel with a finger: it follows the swipe, closes once dragged far enough back toward its edge, and can stop at snap points. That is the bottom panel phone users expect for filters, options or a short form.

A Sheet also slides in from an edge, but it has no gesture: a button, Escape or a click outside closes it. It suits a side panel on a desktop layout, such as an inspector next to a table or a cart.

How do I open a Drawer from the left or the right in UI SDK?

Set swipeDirection on Drawer to the direction that closes it: left for a drawer on the left edge, right for the right, up for the top, and down, the default, for the bottom. The panel opens from that same edge, and a swipe toward it closes the panel.

The Sides demo shows it in use.

How do I open and close a Drawer from code in React?

Hold the open state yourself: pass open to Drawer and update it in onOpenChange, which receives the new boolean first.

Set it to false from your own code to close the drawer after a save. The Dialog demo holds one open state this way and hands it to a dialog on wide screens and a drawer on phones.

How do I add snap points to a Drawer?

Pass snapPoints to Drawer, as the Snap points demo does with "31rem" and 1. A number up to 1 is a share of the screen's height, a larger number is pixels, and a string such as "31rem" or "240px" is a length. The drawer opens at the first point unless defaultSnapPoint names another, and snapPoint with onSnapPointChange lets you follow or set it yourself.

How do I show a Dialog on desktop and a Drawer on mobile?

Render one or the other from a media query and give both the same open state, as the Dialog demo does with useIsMobile, which is true below 768 pixels. Put the form in a component of its own and use it in both, so the fields and their handlers stay in one place.

The query is false on the server, so the page first renders the dialog, then switches to the drawer once it hydrates on a phone. Keep the trigger's label the same in both branches, so nothing visible changes when it does.

How do I stop a Drawer from closing on an outside click or a swipe?

Add disablePointerDismissal to Drawer to ignore clicks outside. To refuse Escape and a swipe too, set dismissible to false: a swiped drawer resists and springs back, and onDismissAttempt tells you what was tried. To refuse only one, call details.cancel() in onOpenChange when details.reason is "escape-key" or "swipe".

Give people a button that closes it, or they are stuck.

Can I open a Drawer from inside another Drawer?

Yes. Put a second Drawer, with its own trigger and the same swipeDirection, inside the first one's content, and it opens on top from the same edge. The first stays mounted, steps back while the second is open and comes forward when it closes, as in the Nested demo.

Two levels are usually enough. Deeper than that, a page of its own serves people better.

How do I show or hide the drag handle on a Drawer?

The handle is off by default. Add showSwipeHandle to Drawer to draw it along the drawer's inner edge, the top of a bottom drawer, as in the Swipe handle demo, and the whole panel follows a drag either way. With one snap point or none it is only a visual hint, hidden from screen readers, and with more it is a button named Resize that moves the drawer to its next snap point, as in the Snap points demo.

API Reference

Drawer

PropType
open?boolean

Whether the dialog is open. Pass it to control the dialog.

defaultOpen?boolean

Whether the dialog starts open, when uncontrolled. Focus moves into it once it's on the page.

Default
false
onOpenChange?(open: boolean, details: DialogOpenChangeDetails<DrawerOpenChangeReason>) → void

Runs when the drawer wants to open or close. details.reason says why, "swipe" included, details.cancel() stops it, and details.returnValue carries what closed it.

Type
(open: boolean, details: DialogOpenChangeDetails<DrawerOpenChangeReason>) → void
Parameters

open – boolean

details – DialogOpenChangeDetails<DrawerOpenChangeReason>

Returns
void
snapPoint?DrawerSnapPoint | null

The snap point it rests on. Pass it to control it.

Type
DrawerSnapPoint | null
defaultSnapPoint?DrawerSnapPoint

The snap point it opens at, when uncontrolled.

onSnapPointChange?(snapPoint: SnapPoint, details: ChangeDetails<DrawerSnapPointChangeReason>) → void

Runs when the drawer moves to another snap point. details.reason says why: a swipe, the handle, the keyboard, a resize or the controller.

Type
(snapPoint: SnapPoint, details: ChangeDetails<DrawerSnapPointChangeReason>) → void
Parameters

snapPoint – SnapPoint

details – ChangeDetails<DrawerSnapPointChangeReason>

Returns
void
closeOnOutsidePress?boolean

Whether pressing outside the drawer, like on its backdrop, closes it.

Default
true, or false while the drawer isn't modal
container?DocumentFragment | Element | null

Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.

Type
DocumentFragment | Element | null
controller?DrawerController<any, any>

Opens, closes and moves the drawer from anywhere, and connects triggers and outlets outside it. Create one with createDrawerController().

Type
DrawerController<any, any>
debug?boolean

Logs every state change with its reason to the console, in development builds.

disablePointerDismissal?boolean

The other bases' name for closeOnOutsidePress={false}.

dismissible?boolean

Whether Escape, an outside press and Android's back gesture can close the dialog. Close parts, triggers and the controller always can. Pass false while a form has unsaved changes, and ask in onDismissAttempt.

Default
true
driver?SheetDriverName

How it moves. "pointer" follows touch, mouse and pen with a spring fitted to iOS. "scroll-snap" uses the browser's own scrolling for bottom drawers. "auto" picks scroll snap for a bottom drawer on a touch screen, unless modalAbove is set or expandOnScroll is false, and the pointer driver otherwise.

Default
"auto"
expandOnScroll?boolean

Whether dragging toward a larger snap point on scrollable content moves the drawer first, until it's at its largest.

Default
true
modal?"trap-focus" | boolean

true: the rest of the page is inert and doesn't scroll, focus is trapped, and an outside press only closes the drawer. "trap-focus": traps focus and leaves the page usable. false: a drawer beside the page. See modalAbove to make it modal only above a snap point.

Type
"trap-focus" | boolean
Default
true
modalAbove?SnapPoint

At or below this snap point the page stays usable and undimmed, like Maps: no inert page, no scroll lock, no aria-modal, and presses beside the drawer reach the page. Above it, modal applies. Changes once the drawer comes to rest.

onDismissAttempt?(details: DrawerDismissAttemptDetails) → void

Runs when a dismissal was refused because dismissible is false, including a swipe. The drawer resists and springs back. Ask "Discard changes?" here.

Type
(details: DrawerDismissAttemptDetails) → void
Parameters

details – DrawerDismissAttemptDetails

Returns
void
onOpenChangeComplete?(open: boolean) → void

Runs once the dialog has finished opening or closing, after any animation.

Parameters

open – boolean

Returns
void
onPayloadChange?(payload: unknown, details: ChangeDetails<DrawerPayloadChangeReason>) → void

Runs when the payload the content shows changes: a trigger with another payload opens the drawer, or takes a non-modal drawer over while it's open, which fires no onOpenChange, and so does a controller's open({ payload }) on the open drawer. details.cancel() keeps the previous payload.

Type
(payload: unknown, details: ChangeDetails<DrawerPayloadChangeReason>) → void
Parameters

payload – unknown

details – ChangeDetails<DrawerPayloadChangeReason>

Returns
void
pendingAnnouncement?string

What screen readers hear once a Close part's action has been running for a second, so a slow action doesn't pass in silence. Set it in the page's language. An empty string says nothing.

Default
"Loading"
persistentElements?(string | Element)[] | (() → Element[])

Elements that stay usable while the dialog makes the page inert, like a chat launcher or a password manager's button. Elements, CSS selectors, or a function that returns elements, read when the dialog opens. Presses on them never close it.

Type
(string | Element)[] | (() → Element[])
Parameters

string | Element)[] | ((

Returns
Element[])
physics?Partial<SheetPhysics>

Overrides the motion: spring, projection, rubber band and dim. Starts from sheetPhysics.ios.

Type
Partial<SheetPhysics>
preventEdgeSwipe?boolean

Whether presses on the drawer near the screen's left and right edges are kept from starting iOS Safari's back and forward swipe. Taps right at those edges then don't click. Read when the content mounts.

Default
false
showSwipeHandle?boolean

Shows the handle bar you can drag the drawer by.

Default
false
snapPoints?DrawerSnapPoint[]

Where it can rest, from the edge it comes from.

Default
[1]
swipeDirection?DrawerSwipeDirection

The direction that swipes it closed, so the edge it comes from.

Default
"down"
themeColor?boolean

Whether <meta name="theme-color"> dims with the backdrop, so Safari's toolbars darken too. Written where motion starts and ends, never per frame.

Default
false

DrawerTrigger

<button> data-slot="drawer-trigger"

PropType
controller?PopupController<any, DialogOpenChangeReason, {}, any, DialogPayloadChangeReason>

Connects a trigger outside the dialog, like one per table row, to the dialog with this controller.

Type
PopupController<any, DialogOpenChangeReason, {}, any, DialogPayloadChangeReason>
disabled?boolean

Stops the trigger from opening the dialog. A native button can't be focused or pressed, unless focusableWhenDisabled is set.

Default
false
focusableWhenDisabled?boolean

Keeps a disabled trigger in the tab order with aria-disabled instead of the native attribute, so it can host a tooltip that says why. Presses still do nothing.

Default
false
payload?unknown

Data for the content to render when this trigger opens the dialog, like the row it belongs to.

shortcut?string

A keyboard shortcut that presses the trigger, like "mod+k". mod is Command on Apple platforms and Control elsewhere. Sets aria-keyshortcuts, and a Shortcut part inside shows it. A modal dialog pauses the page's shortcuts while it's open.

render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
AttributeType
data-closed?string

Present while the drawer is closed, including while it animates out.

data-disabled?string

Present when the trigger is disabled.

data-open?string

Present while the drawer is open. On a trigger, while it's open from that trigger.

DrawerPortal

DrawerClose

<button> data-slot="drawer-close"

PropType
action?(event: Event, details: ActionDetails) → unknown

Runs when the part is pressed, before the dialog closes. A returned Promise keeps the dialog open while it runs: the part gets data-pending, aria-busy and aria-disabled, screen readers hear the dialog's pendingAnnouncement after a second, and nothing but the controller closes the dialog. It closes with reason "action" once the Promise resolves. A rejection keeps it open and puts the reason in the part's error state until the next press, or until the dialog opens again. details.source says how the part was pressed, for changes made after an await.

Type
(event: Event, details: ActionDetails) → unknown
Parameters

event – Event

details – ActionDetails

Returns
unknown
value?string

Reported as returnValue in onOpenChange and by the controller's open(). createConfirm() resolves true for "confirm".

render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
AttributeType
data-disabled?string
data-pending?string

Present on a Close part while its action runs.

DrawerOverlay

<div> data-slot="drawer-backdrop"

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-closed?string

Present while the drawer is closed, including while it animates out.

data-dragging?string

Present while a finger, mouse or pen drags it.

data-ending-style?string

Present while the drawer animates out.

data-interactive?string

Present while it rests at or below modalAbove, so the page behind is usable. The backdrop lets presses through.

data-open?string

Present while the drawer is open. On a trigger, while it's open from that trigger.

data-starting-style?string

Present for the first frame. The drawer slides itself, so leave the content's translate and the backdrop's opacity alone.

DrawerSwipeHandle

<button> data-slot="drawer-swipe-handle"

PropType
className?string

Merged with the part's classes.

AttributeType
data-expanded?string

Present while it rests at its largest snap point.

data-snap-point"content" | "0.2"

The snap point it rests on or is moving to, as written, like 0.5 or content.

Type
"content" | "0.2"

DrawerContent

<div> data-slot="drawer-content"

PropType
container?DocumentFragment | Element | null

Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.

Type
DocumentFragment | Element | null
finalFocus?PopupFocusTarget

Where focus goes when the dialog closes while focus is inside it.

Default
"trigger"
initialFocus?DialogInitialFocus

Where focus goes when the dialog opens. Opening with touch focuses the dialog itself, so no on-screen keyboard appears, unless this is set to something other than "auto".

Default
"auto"
keepMounted?boolean

Whether the dialog stays in the page, hidden, while closed. Its content keeps its state between opens.

Default
false
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-closed?string

Present while the drawer is closed, including while it animates out.

data-dragging?string

Present while a finger, mouse or pen drags it.

data-driver"pointer" | "scroll-snap"

What moves it.

Type
"pointer" | "scroll-snap"
data-ending-style?string

Present while the drawer animates out.

data-expanded?string

Present while it rests at its largest snap point.

data-interactive?string

Present while it rests at or below modalAbove, so the page behind is usable. The backdrop lets presses through.

data-open?string

Present while the drawer is open. On a trigger, while it's open from that trigger.

data-side"top" | "right" | "bottom" | "left"

The edge it comes from, with start and end resolved for the writing direction.

Type
"top" | "right" | "bottom" | "left"
data-snap-point"content" | "0.2"

The snap point it rests on or is moving to, as written, like 0.5 or content.

Type
"content" | "0.2"
data-starting-style?string

Present for the first frame. The drawer slides itself, so leave the content's translate and the backdrop's opacity alone.

CSS variableType
--bleedstring
--drawer-dimstring

How dim the page behind is, from 0 to 1. The backdrop's opacity follows it.

--drawer-expandstring

0 at the second-largest snap point and below, 1 at the largest. With one snap point, the whole travel.

--drawer-progressstring

How far open the sheet is, from 0 closed to 1 at its largest snap point. Never past 1.

--drawer-stackstring

How many sheets opened from inside this one are open above it, rising smoothly as each one opens. Push it back with it.

DrawerHeader

<div> data-slot="drawer-header"

PropType
description?string

The description of the drawer.

title?string

The title of the drawer.

className?string

Merged with the part's classes.

DrawerFooter

<div> data-slot="drawer-footer"

PropType
className?string

Merged with the part's classes.

DrawerTitle

<h2> data-slot="drawer-title"

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

DrawerDescription

<p> data-slot="drawer-description"

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

useDrawer