Skip to content

Sheet

Slides a panel in from the edge of the screen for a secondary task that keeps the page in view.

Installation

pnpm dlx shadcn@latest add @silkware/sheet

To copy the component by hand, install its dependencies:

pnpm add react-aria-components cn lucide-react

Then copy the source into components/ui/sheet.tsx. It uses the Button component, so add that first.

components/ui/sheet.tsx
"use client"
import * as React from "react"import { cn } from "cn"import {  Heading,  ModalOverlay as ModalOverlayPrimitive,  Modal as ModalPrimitive,  Dialog as SheetPrimitive,  DialogTrigger as SheetTriggerPrimitive,  type ModalOverlayProps as ModalOverlayPrimitiveProps,  type DialogProps as SheetPrimitiveProps,  type DialogTriggerProps as SheetTriggerPrimitiveProps,} from "@animate-ui/react/primitives/react-aria"
import { Button } from "@/components/ui/button"import { XIcon } from "lucide-react"
function SheetTrigger({ ...props }: SheetTriggerPrimitiveProps) {  return <SheetTriggerPrimitive data-slot="sheet-trigger" {...props} />}
function SheetClose({  className,  variant = "outline",  size = "default",  ...props}: React.ComponentProps<typeof Button>) {  return (    <Button      slot="close"      data-slot="sheet-close"      variant={variant}      size={size}      className={cn(className)}      {...props}    />  )}
function SheetOverlay({  className,  children,  ...props}: Omit<ModalOverlayPrimitiveProps, "className" | "children"> & {  className?: string  children: React.ReactNode}) {  return (    <ModalOverlayPrimitive      data-slot="sheet-overlay"      isDismissable      className={cn(        "fixed inset-0 z-50 bg-black/10 supports-backdrop-filter:backdrop-blur-xs",        className      )}      {...props}    >      {children}    </ModalOverlayPrimitive>  )}
function Sheet({  className,  children,  side = "right",  showCloseButton = true,  ...props}: Omit<ModalOverlayPrimitiveProps, "className" | "children"> &  Pick<React.ComponentProps<typeof ModalPrimitive>, "isDismissable"> & {    className?: string    children: React.ReactNode    side?: "top" | "right" | "bottom" | "left"    showCloseButton?: boolean  }) {  return (    <SheetOverlay {...props}>      <ModalPrimitive        data-slot="sheet-content"        data-side={side}        className={cn(          "fixed z-50 flex flex-col gap-4 bg-popover bg-clip-padding text-sm text-popover-foreground shadow-lg data-[side=bottom]:inset-x-0 data-[side=bottom]:bottom-0 data-[side=bottom]:h-auto data-[side=bottom]:border-t data-[side=left]:inset-y-0 data-[side=left]:left-0 data-[side=left]:h-full data-[side=left]:w-3/4 data-[side=left]:border-r data-[side=right]:inset-y-0 data-[side=right]:right-0 data-[side=right]:h-full data-[side=right]:w-3/4 data-[side=right]:border-l data-[side=top]:inset-x-0 data-[side=top]:top-0 data-[side=top]:h-auto data-[side=top]:border-b data-[side=left]:sm:max-w-sm data-[side=right]:sm:max-w-sm",          className        )}      >        <SheetPrimitive          data-slot="sheet"          className="[display:inherit] h-full max-h-[inherit] [flex-direction:inherit] [gap:inherit] outline-none"        >          {children}          {showCloseButton && (            <SheetClose              variant="ghost"              className="absolute top-4 right-4"              size="icon-sm"            >              <XIcon              />              <span className="sr-only">Close</span>            </SheetClose>          )}        </SheetPrimitive>      </ModalPrimitive>    </SheetOverlay>  )}
function SheetContent({  className,  children,  side = "right",  showCloseButton = true,  ...props}: React.ComponentProps<typeof Sheet> & {  side?: "top" | "right" | "bottom" | "left"  showCloseButton?: boolean}) {  return (    <Sheet      className={className}      side={side}      showCloseButton={showCloseButton}      {...props}    >      {children}    </Sheet>  )}
function SheetHeader({ className, ...props }: React.ComponentProps<"div">) {  return (    <div      data-slot="sheet-header"      className={cn("flex flex-col gap-1.5 p-4", className)}      {...props}    />  )}
function SheetFooter({ className, ...props }: React.ComponentProps<"div">) {  return (    <div      data-slot="sheet-footer"      className={cn("mt-auto flex flex-col gap-2 p-4", className)}      {...props}    />  )}
function SheetTitle({  className,  ...props}: Omit<React.ComponentProps<typeof Heading>, "slot">) {  return (    <Heading      slot="title"      data-slot="sheet-title"      className={cn("font-heading font-medium text-foreground", className)}      {...props}    />  )}
function SheetDescription({  className,  ...props}: Omit<React.ComponentProps<"div">, "slot">) {  return (    <div      data-slot="sheet-description"      className={cn("text-sm text-muted-foreground", className)}      {...props}    />  )}
export {  type SheetPrimitiveProps,  type SheetTriggerPrimitiveProps,  Sheet,  SheetTrigger,  SheetClose,  SheetContent,  SheetHeader,  SheetFooter,  SheetTitle,  SheetDescription,}

Usage

import { Button } from "@/components/ui/button"import {  Sheet,  SheetClose,  SheetDescription,  SheetFooter,  SheetHeader,  SheetTitle,  SheetTrigger,} from "@/components/ui/sheet"
<SheetTrigger>  <Button variant="outline">Edit site</Button>  <Sheet>    <SheetHeader>      <SheetTitle>Edit site</SheetTitle>      <SheetDescription>Change how the site appears in reports.</SheetDescription>    </SheetHeader>    <SheetFooter>      <SheetClose>Cancel</SheetClose>    </SheetFooter>  </Sheet></SheetTrigger>

The sheet has no padding of its own. SheetHeader and SheetFooter add theirs, so give your own body content px-4. The footer sticks to the bottom of the sheet, and a region with flex-1 overflow-y-auto between the header and the footer scrolls on its own.

Examples

Side

side sets the edge: top, right, bottom or left. The default is right. Side sheets are 75% of the screen wide on a phone and at most 24rem wide from the sm breakpoint. Top and bottom sheets span the full width and take the height of their content.

Scrollable content

Make the region between the header and the footer a flex item with flex-1 overflow-y-auto. The header and the footer stay in place.

Without the close button

showCloseButton={false} removes the cross. A click outside the sheet and Escape still close it, so say so in the description or give the footer a way out.

Controlled

Pass isOpen and onOpenChange to SheetTrigger to read or set the open state.

The sheet is closed.

Accessibility

KeyAction
TabMoves focus to the next control inside the sheet. When the sheet opens, focus is on the sheet itself, so the first press reaches the first control. From the last control it returns to the first.
ShiftTabMoves focus to the previous control inside the sheet.
EscCloses the sheet and returns focus to the trigger.
SpacePresses the focused button.
EnterPresses the focused button.

React Aria provides:

  • The dialog role, labelled by the SheetTitle through the title slot. A sheet is a modal dialog placed at an edge.
  • Focus that moves into the sheet, stays inside while it is open and returns to the trigger on close.
  • Content outside the sheet hidden from assistive technology, and closing on Escape or on a click outside.

You provide:

  • A SheetTitle in every sheet, and a SheetDescription when the title does not say enough.
  • Labels on every field and an aria-label on a trigger that holds only an icon.
  • A visible way to close, unless you are sure that Escape and the outside click are enough.

Guidelines

When to use

  • For a secondary task that belongs to the page behind it, such as editing a site, adding a note or setting filters.
  • For content the user may want to compare with the page, such as the lines of an order.
  • For a task longer than a dialog holds, with a footer that keeps its actions in reach.

When not to use

  • For a short question or a few fields. Use a Dialog.
  • To confirm a destructive action. Use an Alert Dialog.
  • For a touch-first panel that people swipe away, such as quick actions on a phone. Use a Drawer.
  • For controls that sit next to a button. Use a Popover.
  • For navigation that stays on screen. Use a sidebar.

Content

  • Write the title as the task or the object: "Edit site", "Order lines".
  • Keep the footer to one main action and a cancel. Put the main action first, because the footer stacks them and the first one is the highest.
  • Put the primary content in the order the user works through it, and keep the sheet to one task.

Choosing the side

Use right by default, since people read the page from the left and the sheet covers the least of it. Use left for navigation and bottom for short, phone-first content. Use top rarely.

Task or confirmation

Do

Use a sheet for a secondary task with fields, where the page behind stays useful.

Don’t

Don’t use a sheet to ask whether the user is sure. A confirmation needs the centre of the screen and the alert role.

API reference

SheetTrigger

Wraps the trigger and the sheet through the React Aria DialogTrigger. It accepts every DialogTrigger prop.

PropTypeDefaultDescription
childrenReactNodeNoneA pressable trigger, then the Sheet.
isOpen?booleanNoneWhether the sheet is open. Makes the sheet controlled.
defaultOpen?booleanNoneWhether the sheet is open on first render, when uncontrolled.
onOpenChange?(isOpen: boolean) => voidNoneCalled when the sheet opens or closes.

Sheet and SheetContent

Renders the overlay, the modal and the dialog through the React Aria ModalOverlay, Modal and Dialog. It accepts every ModalOverlay prop and the props below. SheetContent is an alias with the same props.

PropTypeDefaultDescription
side?"top" | "right" | "bottom" | "left""right"The edge the sheet slides in from.
showCloseButton?booleantrueShows the close button in the top right corner.
isDismissable?booleanNoneCloses the sheet on an interaction outside it. The overlay of this component already sets it, so a click outside closes the sheet.
isKeyboardDismissDisabled?booleanfalseStops Escape from closing the sheet.
className?stringNoneClasses merged onto the sheet. Scope a change to one edge with a data-side variant, for example data-[side=bottom]:max-h-1/2.
childrenReactNodeNoneThe sheet content.

SheetHeader

A div with padding that stacks the title and the description. It accepts every div prop.

SheetTitle

Renders a heading through the React Aria Heading with slot="title". It accepts every Heading prop except slot.

SheetDescription

A div with muted text. It accepts every div prop except slot.

SheetFooter

A div with padding, pushed to the bottom of the sheet, that stacks its actions. It accepts every div prop.

SheetClose

Renders a Button with slot="close", so pressing it closes the sheet. It accepts every Button prop.

PropTypeDefaultDescription
variant?"default" | "secondary" | "outline" | "ghost" | "destructive" | "link""outline"The visual weight of the button.
size?"xs" | "sm" | "default" | "lg" | "icon-xs" | "icon-sm" | "icon" | "icon-lg""default"The height and padding.