Skip to content
2k
On This Page

Drawer

A panel that slides in from an edge of the screen, with dragging and snap points.

import { createSignal } from "solid-js"

import { Button } from "~/components/ui/button"
import {
  Drawer,
  DrawerTrigger,
  DrawerContent,
  DrawerHandle,
  DrawerHeader,
  DrawerTitle,
  DrawerDescription,
  DrawerFooter,
  DrawerClose
} from "~/components/ui/drawer"
export default function DrawerDemo() {
  const [goal, setGoal] = createSignal(350)
  return (
    <Drawer>
      <DrawerTrigger render={(p) => <Button {...p} variant="outline" />}>
        Open drawer
      </DrawerTrigger>
      <DrawerContent>
        <div class="mx-auto w-full max-w-sm">
          <DrawerHandle />
          <DrawerHeader>
            <DrawerTitle>Move goal</DrawerTitle>
            <DrawerDescription>Set your daily activity goal.</DrawerDescription>
          </DrawerHeader>
          <div class="flex items-center justify-center gap-6 p-4">
            <Button
              aria-label="Decrease goal"
              variant="outline"
              disabled={goal() <= 200}
              onClick={() => setGoal(goal() - 10)}
            >
              โˆ’
            </Button>
            <output class="text-4xl font-semibold">{goal()}</output>
            <Button
              aria-label="Increase goal"
              variant="outline"
              disabled={goal() >= 400}
              onClick={() => setGoal(goal() + 10)}
            >
              +
            </Button>
          </div>
          <DrawerFooter>
            <DrawerClose render={(p) => <Button {...p} />}>
              Save goal
            </DrawerClose>
            <DrawerClose render={(p) => <Button {...p} variant="outline" />}>
              Cancel
            </DrawerClose>
          </DrawerFooter>
        </div>
      </DrawerContent>
    </Drawer>
  )
}

About

Drawer is built on the UI SDK Drawer. It shares Dialogโ€™s focus and dismissal behavior and adds dragging, snap points, and a handle you can use with the keyboard.

Installation

pnpm dlx solidui-cli@latest add drawer

Install the following dependencies:

pnpm add @ui-sdk/solid @ui-sdk/core

Copy and paste the following code into your project.

components/ui/drawer.tsx
import type { ComponentProps } from "@solidjs/web"
import {
  Drawer as SdkDrawer,
  DrawerTrigger as SdkDrawerTrigger,
  DrawerClose as SdkDrawerClose,
  DrawerContent as SdkDrawerContent,
  DrawerBackdrop as SdkDrawerBackdrop,
  DrawerHandle as SdkDrawerHandle,
  DrawerTitle as SdkDrawerTitle,
  DrawerDescription as SdkDrawerDescription
} from "@ui-sdk/solid/drawer"

import { cn } from "~/lib/utils"

export const Drawer = SdkDrawer
export const DrawerTrigger = SdkDrawerTrigger
export const DrawerClose = SdkDrawerClose
export function DrawerOverlay(
  props: Omit<ComponentProps<typeof SdkDrawerBackdrop>, "class"> & {
    class?: string
  }
) {
  return (
    <SdkDrawerBackdrop
      {...props}
      class={cn("bg-black/10 supports-backdrop-filter:backdrop-blur-xs", props.class)}
    />
  )
}
export function DrawerContent(
  props: Omit<ComponentProps<typeof SdkDrawerContent>, "class"> & {
    class?: string
  }
) {
  // The SDK positions and moves the sheet. Only its contents and appearance are styled here.
  return (
    <>
      <DrawerOverlay />
      <SdkDrawerContent
        {...props}
        class={cn(
          "flex h-auto flex-col bg-popover text-sm text-popover-foreground data-[side=bottom]:rounded-t-xl data-[side=bottom]:border-t data-[side=left]:rounded-r-xl data-[side=left]:border-r data-[side=right]:rounded-l-xl data-[side=right]:border-l data-[side=top]:rounded-b-xl data-[side=top]:border-b flex max-h-[90svh] flex-col data-[side=left]:max-w-sm data-[side=right]:max-w-sm",
          props.class
        )}
      />
    </>
  )
}
export function DrawerHandle(
  props: Omit<ComponentProps<typeof SdkDrawerHandle>, "class"> & {
    class?: string
  }
) {
  return (
    <SdkDrawerHandle
      aria-label="Resize drawer"
      {...props}
      class={cn(
        "mx-auto my-3 h-1 w-24 shrink-0 rounded-full bg-muted",
        props.class
      )}
    />
  )
}
export function DrawerTitle(
  props: Omit<ComponentProps<typeof SdkDrawerTitle>, "class"> & {
    class?: string
  }
) {
  return (
    <SdkDrawerTitle {...props} class={cn("text-base font-medium text-foreground", props.class)} />
  )
}
export function DrawerDescription(
  props: Omit<ComponentProps<typeof SdkDrawerDescription>, "class"> & {
    class?: string
  }
) {
  return (
    <SdkDrawerDescription
      {...props}
      class={cn("text-sm text-muted-foreground", props.class)}
    />
  )
}
export function DrawerHeader(props: ComponentProps<"div">) {
  return (
    <div
      {...props}
      data-slot="drawer-header"
      class={cn("gap-0.5 p-4 md:gap-0.5 md:text-left flex flex-col", props.class)}
    />
  )
}
export function DrawerFooter(props: ComponentProps<"div">) {
  return (
    <div
      {...props}
      data-slot="drawer-footer"
      class={cn("gap-2 p-4 mt-auto flex flex-col", props.class)}
    />
  )
}

Update the import paths to match your project setup.

Usage

import {
  Drawer,
  DrawerClose,
  DrawerContent,
  DrawerDescription,
  DrawerFooter,
  DrawerHeader,
  DrawerTitle,
  DrawerTrigger
} from "~/components/ui/drawer"
<Drawer>
  <DrawerTrigger>Open</DrawerTrigger>
  <DrawerContent>
    <DrawerHeader>
      <DrawerTitle>Activity goal</DrawerTitle>
      <DrawerDescription>Review your daily goal.</DrawerDescription>
    </DrawerHeader>
    <DrawerFooter>
      <DrawerClose>Done</DrawerClose>
    </DrawerFooter>
  </DrawerContent>
</Drawer>

Use side, snapPoints, and defaultSnapPoint from the SDK. Content renders in place using the browser top layer. The SDK owns positioning and movement.

See the UI SDK drawer API for native options and keyboard behavior.

Composition

Use the following composition to build a Drawer:

Drawer
โ”œโ”€โ”€ DrawerTrigger
โ””โ”€โ”€ DrawerContent
    โ”œโ”€โ”€ DrawerHeader
    โ”‚   โ”œโ”€โ”€ DrawerTitle
    โ”‚   โ””โ”€โ”€ DrawerDescription
    โ””โ”€โ”€ DrawerFooter

Scrollable Content

Keep actions visible while the content scrolls.

import { For } from "solid-js"

import { Button } from "~/components/ui/button"
import {
  Drawer,
  DrawerClose,
  DrawerContent,
  DrawerDescription,
  DrawerFooter,
  DrawerHeader,
  DrawerTitle,
  DrawerTrigger
} from "~/components/ui/drawer"

export default function DrawerScrollableContent() {
  return (
    <Drawer side="right">
      <DrawerTrigger
        render={(partProps) => (
          <Button {...partProps} variant="outline">
            Scrollable Content
          </Button>
        )}
      />
      <DrawerContent>
        <DrawerHeader>
          <DrawerTitle>Move Goal</DrawerTitle>
          <DrawerDescription>Set your daily activity goal.</DrawerDescription>
        </DrawerHeader>
        <div class="no-scrollbar overflow-y-auto px-4">
          <For each={Array.from({ length: 10 }, (_, index) => index + 1)}>
            {() => (
              <p class="mb-4 leading-normal style-lyra:mb-2 style-lyra:leading-relaxed">
                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do
                eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
                enim ad minim veniam, quis nostrud exercitation ullamco laboris
                nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
                in reprehenderit in voluptate velit esse cillum dolore eu fugiat
                nulla pariatur. Excepteur sint occaecat cupidatat non proident,
                sunt in culpa qui officia deserunt mollit anim id est laborum.
              </p>
            )}
          </For>
        </div>
        <DrawerFooter>
          <Button>Submit</Button>
          <DrawerClose
            render={(partProps) => (
              <Button {...partProps} variant="outline">
                Cancel
              </Button>
            )}
          />
        </DrawerFooter>
      </DrawerContent>
    </Drawer>
  )
}

Sides

Set side on Drawer to open it from the top, right, bottom, or left.

import { For } from "solid-js"

import { Button } from "~/components/ui/button"
import {
  Drawer,
  DrawerTrigger,
  DrawerContent,
  DrawerHeader,
  DrawerTitle,
  DrawerDescription,
  DrawerFooter,
  DrawerClose
} from "~/components/ui/drawer"

const paragraphs = Array.from({ length: 10 }, (_, index) => index)

export default function DrawerSides() {
  return (
    <div class="flex flex-wrap gap-2">
      <For each={["top", "right", "bottom", "left"] as const}>
        {(side) => (
          <Drawer side={side}>
            <DrawerTrigger
              render={(p) => (
                <Button {...p} variant="outline" class="capitalize" />
              )}
            >
              {side}
            </DrawerTrigger>
            <DrawerContent class="data-[side=bottom]:max-h-[50vh] data-[side=top]:max-h-[50vh]">
              <DrawerHeader>
                <DrawerTitle>Move Goal</DrawerTitle>
                <DrawerDescription>
                  Set your daily activity goal.
                </DrawerDescription>
              </DrawerHeader>
              <div class="no-scrollbar overflow-y-auto px-4">
                <For each={paragraphs}>
                  {() => (
                    <p class="mb-4 leading-normal style-lyra:mb-2 style-lyra:leading-relaxed">
                      Lorem ipsum dolor sit amet, consectetur adipiscing elit.
                      Sed do eiusmod tempor incididunt ut labore et dolore magna
                      aliqua. Ut enim ad minim veniam, quis nostrud exercitation
                      ullamco laboris nisi ut aliquip ex ea commodo consequat.
                      Duis aute irure dolor in reprehenderit in voluptate velit
                      esse cillum dolore eu fugiat nulla pariatur. Excepteur
                      sint occaecat cupidatat non proident, sunt in culpa qui
                      officia deserunt mollit anim id est laborum.
                    </p>
                  )}
                </For>
              </div>
              <DrawerFooter>
                <DrawerClose render={(p) => <Button {...p} />}>
                  Submit
                </DrawerClose>
                <DrawerClose
                  render={(p) => <Button {...p} variant="outline" />}
                >
                  Cancel
                </DrawerClose>
              </DrawerFooter>
            </DrawerContent>
          </Drawer>
        )}
      </For>
    </div>
  )
}

Responsive Dialog

You can combine the Dialog and Drawer components to create a responsive dialog. This renders a Dialog component on desktop and a Drawer on mobile.

import { createSignal, Show } from "solid-js"

import { useIsMobile } from "~/hooks/use-mobile"
import { Button } from "~/components/ui/button"
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
  DialogTrigger
} from "~/components/ui/dialog"
import {
  Drawer,
  DrawerClose,
  DrawerContent,
  DrawerDescription,
  DrawerFooter,
  DrawerHeader,
  DrawerTitle,
  DrawerTrigger
} from "~/components/ui/drawer"
import { Input } from "~/components/ui/input"
import { Label } from "~/components/ui/label"

const description =
  "Make changes to your profile here. Click save when you're done."

function ProfileForm(props: { class?: string; onSave: () => void }) {
  return (
    <form
      class={["grid items-start gap-6", props.class]}
      onSubmit={(event) => {
        event.preventDefault()
        props.onSave()
      }}
    >
      <div class="grid gap-3">
        <Label for="drawer-profile-email">Email</Label>
        <Input
          type="email"
          id="drawer-profile-email"
          defaultValue="alex@example.com"
        />
      </div>
      <div class="grid gap-3">
        <Label for="drawer-profile-username">Username</Label>
        <Input id="drawer-profile-username" defaultValue="@alex" />
      </div>
      <Button type="submit">Save changes</Button>
    </form>
  )
}

export default function DrawerDialogDemo() {
  const mobile = useIsMobile()
  const [open, setOpen] = createSignal(false)
  const close = () => setOpen(false)
  return (
    <Show
      when={mobile()}
      fallback={
        <Dialog open={open()} onOpenChange={setOpen}>
          <DialogTrigger
            render={(part) => <Button {...part} variant="outline" />}
          >
            Edit Profile
          </DialogTrigger>
          <DialogContent class="sm:max-w-[425px]">
            <DialogHeader>
              <DialogTitle>Edit profile</DialogTitle>
              <DialogDescription>{description}</DialogDescription>
            </DialogHeader>
            <ProfileForm onSave={close} />
          </DialogContent>
        </Dialog>
      }
    >
      <Drawer open={open()} onOpenChange={setOpen}>
        <DrawerTrigger
          render={(part) => <Button {...part} variant="outline" />}
        >
          Edit Profile
        </DrawerTrigger>
        <DrawerContent>
          <DrawerHeader class="text-start">
            <DrawerTitle>Edit profile</DrawerTitle>
            <DrawerDescription>{description}</DrawerDescription>
          </DrawerHeader>
          <ProfileForm class="px-4" onSave={close} />
          <DrawerFooter class="pt-2">
            <DrawerClose
              render={(part) => <Button {...part} variant="outline" />}
            >
              Cancel
            </DrawerClose>
          </DrawerFooter>
        </DrawerContent>
      </Drawer>
    </Show>
  )
}

API Reference

See the UI SDK Drawer API for the full API reference.