Skip to content

Drawer

Slides a panel from any edge that the user can drag away, built for touch and for short tasks.

Base UI

React Aria has no drawer primitive, so this component is built on Base UI, as the official shadcn version for the React Aria base is. Its parts and props follow the Base UI API: Drawer takes open and onOpenChange, and triggers take render, not React Aria's isOpen and DialogTrigger.

Installation

pnpm dlx shadcn@latest add @silkware/drawer

To copy the component by hand, install its dependencies:

pnpm add @base-ui/react cn

Then copy the source into components/ui/drawer.tsx:

components/ui/drawer.tsx
"use client"
import * as React from "react"import { Drawer as DrawerPrimitive } from "@animate-ui/react/primitives/base-ui/drawer"import { cn } from "cn"
type DrawerContextProps = {  hasSnapPoints: boolean  modal: DrawerPrimitive.Root.Props["modal"]  showSwipeHandle: boolean  swipeDirection: NonNullable<DrawerPrimitive.Root.Props["swipeDirection"]>}
const DrawerContext = React.createContext<DrawerContextProps | null>(null)
function useDrawer() {  const context = React.useContext(DrawerContext)
  if (!context) {    throw new Error("useDrawer must be used within a Drawer.")  }
  return context}
function Drawer({  modal = true,  showSwipeHandle = false,  snapPoints,  swipeDirection = "down",  ...props}: DrawerPrimitive.Root.Props & {  showSwipeHandle?: boolean}) {  const hasSnapPoints = snapPoints != null && snapPoints.length > 0  const contextValue = React.useMemo(    () => ({ hasSnapPoints, modal, showSwipeHandle, swipeDirection }),    [hasSnapPoints, modal, showSwipeHandle, swipeDirection]  )
  return (    <DrawerContext.Provider value={contextValue}>      <DrawerPrimitive.Root        data-slot="drawer"        modal={modal}        snapPoints={snapPoints}        swipeDirection={swipeDirection}        {...props}      />    </DrawerContext.Provider>  )}
function DrawerTrigger({ ...props }: DrawerPrimitive.Trigger.Props) {  return <DrawerPrimitive.Trigger data-slot="drawer-trigger" {...props} />}
function DrawerPortal({ ...props }: DrawerPrimitive.Portal.Props) {  return <DrawerPrimitive.Portal data-slot="drawer-portal" {...props} />}
function DrawerClose({ ...props }: DrawerPrimitive.Close.Props) {  return <DrawerPrimitive.Close data-slot="drawer-close" {...props} />}
function DrawerOverlay({  className,  ...props}: DrawerPrimitive.Backdrop.Props) {  return (    <DrawerPrimitive.Backdrop      data-slot="drawer-overlay"      className={cn(        "fixed inset-0 z-50 min-h-dvh bg-black/10 transition-opacity select-none data-ending-style:pointer-events-none data-snap-points:[--drawer-overlay-min-opacity:0.5] supports-backdrop-filter:backdrop-blur-xs supports-[-webkit-touch-callout:none]:absolute",        className      )}      {...props}    />  )}
function DrawerSwipeHandle({  className,  ...props}: React.ComponentProps<"div">) {  return (    <div      data-slot="drawer-swipe-handle"      aria-hidden="true"      className={cn(        "relative z-10 flex shrink-0 cursor-grab transition-opacity duration-200 group-data-nested-drawer-open/drawer-popup:opacity-0 group-data-nested-drawer-swiping/drawer-popup:opacity-100 group-data-[swipe-axis=x]/drawer-popup:h-full group-data-[swipe-axis=x]/drawer-popup:w-3 group-data-[swipe-axis=x]/drawer-popup:items-center group-data-[swipe-axis=y]/drawer-popup:h-3 group-data-[swipe-axis=y]/drawer-popup:w-full group-data-[swipe-axis=y]/drawer-popup:justify-center group-data-[swipe-direction=down]/drawer-popup:items-end group-data-[swipe-direction=left]/drawer-popup:order-last group-data-[swipe-direction=left]/drawer-popup:justify-start group-data-[swipe-direction=right]/drawer-popup:justify-end group-data-[swipe-direction=up]/drawer-popup:order-last group-data-[swipe-direction=up]/drawer-popup:items-start after:block after:shrink-0 after:rounded-full after:bg-muted group-data-[swipe-axis=x]/drawer-popup:after:h-[100px] group-data-[swipe-axis=x]/drawer-popup:after:w-1.5 group-data-[swipe-axis=y]/drawer-popup:after:h-1.5 group-data-[swipe-axis=y]/drawer-popup:after:w-[100px] active:cursor-grabbing",        className      )}      {...props}    />  )}
function DrawerContent({  className,  children,  ...props}: DrawerPrimitive.Popup.Props) {  const { hasSnapPoints, modal, showSwipeHandle, swipeDirection } = useDrawer()  const swipeAxis =    swipeDirection === "down" || swipeDirection === "up" ? "y" : "x"
  return (    <DrawerPortal data-slot="drawer-portal">      {modal === true && (        <DrawerOverlay data-snap-points={hasSnapPoints ? "" : undefined} />      )}      <DrawerPrimitive.Viewport        data-slot="drawer-viewport"        data-modal={modal}        className="pointer-events-none fixed inset-0 z-50 select-none data-[modal=true]:pointer-events-auto"      >        <DrawerPrimitive.Popup          data-slot="drawer-popup"          data-swipe-axis={swipeAxis}          data-snap-points={hasSnapPoints ? "" : undefined}          className={cn(            "group/drawer-popup pointer-events-auto fixed z-50 m-(--drawer-inset,0px) flex h-(--drawer-content-height) max-h-(--drawer-content-max-height,none) min-h-0 w-(--drawer-content-width,auto) flex-col bg-popover text-sm text-popover-foreground will-change-transform outline-none select-none [interpolate-size:allow-keywords] data-[swipe-direction=down]:rounded-t-xl data-[swipe-direction=down]:border-t data-[swipe-direction=left]:rounded-r-xl data-[swipe-direction=left]:border-r data-[swipe-direction=right]:rounded-l-xl data-[swipe-direction=right]:border-l data-[swipe-direction=up]:rounded-b-xl data-[swipe-direction=up]:border-b",            "data-nested-drawer-open:overflow-hidden data-nested-drawer-open:brightness-95",            "after:pointer-events-none after:absolute after:bg-(--drawer-bleed-background,var(--color-popover)) data-[swipe-axis=x]:after:inset-y-0 data-[swipe-axis=x]:after:w-(--bleed) data-[swipe-axis=y]:after:inset-x-0 data-[swipe-axis=y]:after:h-(--bleed) data-[swipe-direction=down]:after:top-full data-[swipe-direction=left]:after:right-full data-[swipe-direction=right]:after:left-full data-[swipe-direction=up]:after:bottom-full",            "[--drawer-content-height:var(--drawer-height,auto)] data-[swipe-axis=x]:[--drawer-content-width:75%] data-[swipe-axis=y]:[--drawer-content-max-height:calc(100dvh-6rem)] data-[swipe-axis=y]:data-snap-points:[--drawer-content-height:100dvh] data-[swipe-axis=x]:sm:[--drawer-content-width:24rem]",            "[--bleed:3rem] [--peek:1rem] [--stack-height:var(--drawer-frontmost-height,var(--drawer-height,0px))] [--stack-peek-offset:max(0px,calc((var(--nested-drawers)-var(--stack-progress))*var(--peek)))] [--stack-progress:clamp(0,var(--drawer-swipe-progress),1)] [--stack-scale-base:max(0,calc(1-(var(--nested-drawers)*var(--stack-step))))] [--stack-scale:clamp(0,calc(var(--stack-scale-base)+(var(--stack-step)*var(--stack-progress))),1)] [--stack-shrink:calc(1-var(--stack-scale))] [--stack-step:0.05]",            "data-ending-style:opacity-[0.9999]",            "data-[swipe-axis=y]:inset-x-0 data-[swipe-axis=y]:data-nested-drawer-open:h-(--stack-height)",            "data-[swipe-axis=x]:inset-y-0 data-[swipe-axis=x]:flex-row",            "data-[swipe-direction=down]:bottom-0 data-[swipe-direction=down]:origin-bottom data-[swipe-direction=down]:[--closed-transform:translate3d(0,calc(100%+var(--drawer-inset,0px)+2px),0)] data-[swipe-direction=down]:[--translate-y:calc(var(--drawer-snap-point-offset,0px)+var(--drawer-swipe-movement-y)-var(--stack-peek-offset)-(var(--stack-shrink)*var(--stack-height)))]",            "data-[swipe-direction=up]:top-0 data-[swipe-direction=up]:origin-top data-[swipe-direction=up]:[--closed-transform:translate3d(0,calc(-100%-var(--drawer-inset,0px)-2px),0)] data-[swipe-direction=up]:[--translate-y:calc(var(--drawer-snap-point-offset,0px)+var(--drawer-swipe-movement-y)+var(--stack-peek-offset)+(var(--stack-shrink)*var(--stack-height)))]",            "data-[swipe-direction=left]:left-0 data-[swipe-direction=left]:origin-left data-[swipe-direction=left]:[--closed-transform:translate3d(calc(-100%-var(--drawer-inset,0px)-2px),0,0)] data-[swipe-direction=left]:[--translate-x:calc(var(--drawer-swipe-movement-x)+var(--stack-peek-offset)+(var(--stack-shrink)*100%))]",            "data-[swipe-direction=right]:right-0 data-[swipe-direction=right]:origin-right data-[swipe-direction=right]:[--closed-transform:translate3d(calc(100%+var(--drawer-inset,0px)+2px),0,0)] data-[swipe-direction=right]:[--translate-x:calc(var(--drawer-swipe-movement-x)-var(--stack-peek-offset)-(var(--stack-shrink)*100%))]",            className          )}          {...props}        >          {showSwipeHandle && <DrawerSwipeHandle />}          <DrawerPrimitive.Content            data-slot="drawer-content"            className={cn(              "flex min-h-0 flex-1 flex-col overflow-hidden overscroll-contain rounded-[inherit] transition-opacity duration-300 ease-[cubic-bezier(0.45,1.005,0,1.005)] select-text group-data-nested-drawer-open/drawer-popup:opacity-0 group-data-nested-drawer-swiping/drawer-popup:opacity-100 group-data-swiping/drawer-popup:select-none"            )}          >            {children}          </DrawerPrimitive.Content>        </DrawerPrimitive.Popup>      </DrawerPrimitive.Viewport>    </DrawerPortal>  )}
function DrawerHeader({ className, ...props }: React.ComponentProps<"div">) {  return (    <div      data-slot="drawer-header"      className={cn(        "flex shrink-0 flex-col gap-0.5 p-4 pb-0 group-data-[swipe-axis=y]/drawer-popup:text-center md:gap-1.5 md:text-left",        className      )}      {...props}    />  )}
function DrawerFooter({ className, ...props }: React.ComponentProps<"div">) {  return (    <div      data-slot="drawer-footer"      className={cn("mt-auto flex shrink-0 flex-col gap-2 p-4 pt-0", className)}      {...props}    />  )}
function DrawerTitle({ className, ...props }: DrawerPrimitive.Title.Props) {  return (    <DrawerPrimitive.Title      data-slot="drawer-title"      className={cn("font-heading font-medium text-foreground", className)}      {...props}    />  )}
function DrawerDescription({  className,  ...props}: DrawerPrimitive.Description.Props) {  return (    <DrawerPrimitive.Description      data-slot="drawer-description"      className={cn("text-sm text-balance text-muted-foreground", className)}      {...props}    />  )}
export {  Drawer,  DrawerPortal,  DrawerOverlay,  DrawerSwipeHandle,  DrawerTrigger,  DrawerClose,  DrawerContent,  DrawerHeader,  DrawerFooter,  DrawerTitle,  DrawerDescription,}

Usage

import { Button } from "@/components/ui/button"import {  Drawer,  DrawerClose,  DrawerContent,  DrawerDescription,  DrawerFooter,  DrawerHeader,  DrawerTitle,  DrawerTrigger,} from "@/components/ui/drawer"
<Drawer>  <DrawerTrigger render={<Button variant="outline" />}>    Open batch summary  </DrawerTrigger>  <DrawerContent>    <DrawerHeader>      <DrawerTitle>Batch B-0312</DrawerTitle>      <DrawerDescription>Quality summary for the batch.</DrawerDescription>    </DrawerHeader>    <DrawerFooter>      <DrawerClose render={<Button variant="outline" />}>Close</DrawerClose>    </DrawerFooter>  </DrawerContent></Drawer>

Triggers and close buttons take the element they render through render, here a Button. Put the label as the children of the trigger, not of the Button. The drawer opens from the bottom, and swipeDirection is the direction the user drags to close it: down for a bottom drawer.

Examples

Direction

swipeDirection sets the edge and the closing gesture: down opens from the bottom, up from the top, left from the left and right from the right. Side drawers are 75% of the width on a phone and 24rem from the sm breakpoint.

Swipe handle

showSwipeHandle draws a handle on the edge that faces the page. It tells touch users that the drawer can be dragged. It is hidden from assistive technology, so keep a close button.

Snap points

snapPoints lets a bottom drawer rest at set heights. A number between 0 and 1 is a fraction of the viewport, a larger number is pixels, and a string accepts px and rem. Control the active point with snapPoint and onSnapPointChange.

Scrollable content

Make the scrolling region a flex item with flex-1 overflow-y-auto. Do not use h-full: it does not resolve in a drawer that sizes itself to its content.

Non modal

modal={false} leaves the page behind interactive. Add disablePointerDismissal so that a press on the page does not close the drawer. modal="trap-focus" keeps focus inside the drawer but leaves scrolling and pointer use outside it free.

Nested

A Drawer inside a DrawerFooter or any other part of a drawer opens on top of it. The parent stays mounted and recedes behind the new one. Use it for one level of detail, such as the supplier of an order.

Accessibility

KeyAction
TabMoves focus to the next control inside the drawer while it is modal.
ShiftTabMoves focus to the previous control inside the drawer.
EscCloses the drawer and returns focus to the trigger.
SpacePresses the focused button, including the trigger and the close button.
EnterPresses the focused button, including the trigger and the close button.

The Base UI drawer page has no accessibility section. What follows was checked by running this component: the popup has the dialog role and is labelled by the title, Escape closes it, and Tab moves between the controls inside it.

Base UI provides:

  • A popup labelled by DrawerTitle and described by DrawerDescription.
  • Focus that moves into the drawer when it opens and returns to the trigger when it closes.
  • Closing on Escape, on a press outside the drawer, and by swiping in swipeDirection. disablePointerDismissal turns off the press.
  • Optional modal modes: fully modal, focus trapped, or non modal.

You provide:

  • A DrawerTitle in every drawer, and a DrawerDescription when the title is not enough.
  • A close button inside the drawer. A swipe is not available to keyboard and screen reader users.
  • An accessible name on a trigger that holds only an icon.

Guidelines

When to use

  • For a short task on a phone, where a panel from the bottom is within reach of the thumb: reporting a problem on a line, picking an option.
  • For a detail view that the user dismisses by dragging.
  • For a panel that peeks and expands, with snap points.

When not to use

  • For a form with many fields or one that needs validation. Use a Dialog on desktop or a Sheet for a longer task.
  • To confirm an action that cannot be undone. Use an Alert Dialog.
  • For a few controls next to a button. Use a Popover.
  • As the only way to reach an action on a desktop page. Dragging is a touch gesture, so make sure the close button and the keyboard path work.

Content

  • Title the drawer after the task or the object, in sentence case: "Batch B-0312", "Report a problem".
  • Keep the body short enough to read without scrolling on a phone, or give it a scroll region.
  • Put the main action first in the footer, where the footer stacks its buttons, and a close button last.

Choosing the edge

Use the bottom edge on a phone, and a side edge on a wider screen when the drawer carries detail. Match the edge to the gesture: a bottom drawer closes with a downward drag.

Size of the task

Do

Use a drawer for a short choice that the user makes with one tap.

Don’t

Don’t put a long form in a drawer. The keyboard and the swipe fight for the same space.

API reference

Drawer

The Base UI Drawer.Root. It owns the open state and provides the swipe direction, the modal mode and the snap points to the parts. It accepts every Base UI Drawer root prop.

PropTypeDefaultDescription
open?booleanNoneWhether the drawer is open. Makes the drawer controlled.
defaultOpen?booleanfalseWhether the drawer is open on first render, when uncontrolled.
onOpenChange?(open: boolean, eventDetails) => voidNoneCalled when the drawer opens or closes.
swipeDirection?"up" | "right" | "down" | "left""down"The edge the drawer comes from and the direction that dismisses it.
modal?boolean | "trap-focus"trueWhether the page behind is blocked. trap-focus keeps focus inside and leaves the page interactive.
disablePointerDismissal?booleanfalseStops a press outside the drawer from closing it.
snapPoints?(number | string)[]NoneHeights the drawer can rest at. They apply to a vertical drawer.
snapPoint?number | string | nullNoneThe active snap point, when controlled.
onSnapPointChange?(snapPoint, eventDetails) => voidNoneCalled when the active snap point changes.
showSwipeHandle?booleanfalseShows a handle that shows the drawer can be dragged.

DrawerTrigger

The Base UI Drawer.Trigger. It renders a button, or the element you pass to render. It accepts every Base UI Drawer trigger prop.

PropTypeDefaultDescription
render?ReactElement | functionNoneThe element to render instead of the default button, for example a Button.

DrawerContent

Renders the portal, the overlay, the viewport, the popup and the content wrapper in one part. The overlay is drawn only when the drawer is modal. It accepts every Base UI Drawer popup prop.

PropTypeDefaultDescription
className?stringNoneClasses merged onto the popup. Set a height with h-* and max-h-*, or a width with w-* and max-w-*, scoped by data-[swipe-axis=y] or data-[swipe-axis=x].
children?ReactNodeNoneThe drawer content.

DrawerHeader

A div that stacks the title and the description. Its text is centered for a vertical drawer. It accepts every div prop.

DrawerTitle

The Base UI Drawer.Title, which labels the drawer. It accepts every Base UI Drawer title prop.

DrawerDescription

The Base UI Drawer.Description. It accepts every Base UI Drawer description prop.

DrawerFooter

A div pushed to the bottom of the drawer that stacks its actions. It accepts every div prop.

DrawerClose

The Base UI Drawer.Close. It renders a button, or the element you pass to render, and closes the drawer.

PropTypeDefaultDescription
render?ReactElement | functionNoneThe element to render instead of the default button, for example a Button.

DrawerPortal, DrawerOverlay and DrawerSwipeHandle

DrawerContent composes these for you. DrawerPortal is the Base UI portal, DrawerOverlay the dimmed backdrop and DrawerSwipeHandle the handle. Use them only to build a drawer by hand.