Glrk UI

Drawer

A simplified wrapper for Shadcn Drawer with flexible option handling and automatic type conversion

Installation

npx shadcn@latest add @glrk-ui/drawer

If you haven't set up the prerequisites yet, check out Prerequest section.

Copy and paste the following code into shadcn drawer component.

ui/drawer.tsx
type DrawerFooterWrapperProps = {
  cancel?: React.ReactNode
  action?: React.ReactNode
  footerCls?: string
  actionCls?: string
  cancelCls?: string
  onAction?: () => void
  onCancel?: () => void
}

function DrawerFooterWrapper({
  cancel,
  action,
  footerCls,
  actionCls,
  cancelCls,
  onAction = () => { },
  onCancel = () => { },
}: DrawerFooterWrapperProps) {
  return (
    <DrawerFooter className={footerCls}>
      {
        cancel &&
        <DrawerClose asChild>
          <Button
            variant="secondary"
            onClick={onCancel}
            className={cn("border", cancelCls)}
            asChild={typeof cancel !== "string"}
          >
            {cancel}
          </Button>
        </DrawerClose>
      }

      {
        action &&
        <Button
          onClick={onAction}
          className={actionCls}
          asChild={typeof action !== "string"}
        >
          {action}
        </Button>
      }
    </DrawerFooter>
  )
}

type DrawerWrapperProps = {
  title?: React.ReactNode
  trigger?: React.ReactNode
  children?: React.ReactNode
  description?: React.ReactNode
  descriptionCls?: string
  contentCls?: string
  headerCls?: string
  titleCls?: string
} & DrawerFooterWrapperProps

function DrawerWrapper({
  trigger,
  title,
  description,
  children,
  contentCls,
  headerCls,
  titleCls,
  descriptionCls,

  cancel = "Cancel",
  action,
  footerCls,
  actionCls,
  cancelCls,
  onAction,
  onCancel,
  ...props
}: React.ComponentProps<typeof DrawerPrimitive.Root> & DrawerWrapperProps) {
  return (
    <Drawer {...props}>
      {trigger &&
        <DrawerTrigger asChild={typeof trigger !== "string"}>{trigger}</DrawerTrigger>
      }

      <DrawerContent className={contentCls}>
        <DrawerHeader className={headerCls}>
          <DrawerTitle className={titleCls}>{title}</DrawerTitle>
          {description && (
            <DrawerDescription className={descriptionCls}>
              {description}
            </DrawerDescription>
          )}
        </DrawerHeader>

        {children}

        {
          (!!cancel || !!action) &&
          <DrawerFooterWrapper
            cancel={cancel}
            action={action}
            footerCls={footerCls}
            actionCls={actionCls}
            cancelCls={cancelCls}
            onAction={onAction}
            onCancel={onCancel}
          />
        }
      </DrawerContent>
    </Drawer>
  )
}

Add DrawerWrapper and DrawerFooterWrapper at the export block.

export {
  Drawer,
  DrawerPortal,
  DrawerOverlay,
  DrawerTrigger,
  DrawerContent,
  DrawerHeader,
  DrawerFooter,
  DrawerTitle,
  DrawerDescription,
  DrawerAction,
  DrawerClose,
  DrawerWrapper,
  DrawerFooterWrapper,
}

Installation

npm install vaul

drawer.tsx

Copy and paste the following code into your project.

ui/drawer.tsx
"use client"

import * as React from "react"
import { Drawer as DrawerPrimitive } from "vaul"

import { cn } from "@/lib/utils"
import { Button } from "@/components/ui/button"

function Drawer({
  ...props
}: React.ComponentProps<typeof DrawerPrimitive.Root>) {
  return <DrawerPrimitive.Root data-slot="drawer" {...props} />
}

function DrawerTrigger({
  ...props
}: React.ComponentProps<typeof DrawerPrimitive.Trigger>) {
  return <DrawerPrimitive.Trigger data-slot="drawer-trigger" {...props} />
}

function DrawerPortal({
  ...props
}: React.ComponentProps<typeof DrawerPrimitive.Portal>) {
  return <DrawerPrimitive.Portal data-slot="drawer-portal" {...props} />
}

function DrawerClose({
  ...props
}: React.ComponentProps<typeof DrawerPrimitive.Close>) {
  return <DrawerPrimitive.Close data-slot="drawer-close" {...props} />
}

function DrawerOverlay({
  className,
  ...props
}: React.ComponentProps<typeof DrawerPrimitive.Overlay>) {
  return (
    <DrawerPrimitive.Overlay
      data-slot="drawer-overlay"
      className={cn(
        "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 fixed inset-0 z-50 bg-black/50",
        className
      )}
      {...props}
    />
  )
}

function DrawerContent({
  className,
  children,
  ...props
}: React.ComponentProps<typeof DrawerPrimitive.Content>) {
  return (
    <DrawerPortal data-slot="drawer-portal">
      <DrawerOverlay />
      <DrawerPrimitive.Content
        data-slot="drawer-content"
        className={cn(
          "group/drawer-content bg-background fixed z-50 flex h-auto flex-col",
          "data-[vaul-drawer-direction=top]:inset-x-0 data-[vaul-drawer-direction=top]:top-0 data-[vaul-drawer-direction=top]:mb-24 data-[vaul-drawer-direction=top]:max-h-[80vh] data-[vaul-drawer-direction=top]:rounded-b-lg data-[vaul-drawer-direction=top]:border-b",
          "data-[vaul-drawer-direction=bottom]:inset-x-0 data-[vaul-drawer-direction=bottom]:bottom-0 data-[vaul-drawer-direction=bottom]:mt-24 data-[vaul-drawer-direction=bottom]:max-h-[80vh] data-[vaul-drawer-direction=bottom]:rounded-t-lg data-[vaul-drawer-direction=bottom]:border-t",
          "data-[vaul-drawer-direction=right]:inset-y-0 data-[vaul-drawer-direction=right]:right-0 data-[vaul-drawer-direction=right]:w-3/4 data-[vaul-drawer-direction=right]:border-l data-[vaul-drawer-direction=right]:sm:max-w-sm",
          "data-[vaul-drawer-direction=left]:inset-y-0 data-[vaul-drawer-direction=left]:left-0 data-[vaul-drawer-direction=left]:w-3/4 data-[vaul-drawer-direction=left]:border-r data-[vaul-drawer-direction=left]:sm:max-w-sm",
          className
        )}
        {...props}
      >
        <div className="bg-muted mx-auto mt-4 hidden h-2 w-[100px] shrink-0 rounded-full group-data-[vaul-drawer-direction=bottom]/drawer-content:block" />
        {children}
      </DrawerPrimitive.Content>
    </DrawerPortal>
  )
}

function DrawerHeader({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="drawer-header"
      className={cn(
        "flex flex-col gap-0.5 p-4 group-data-[vaul-drawer-direction=bottom]/drawer-content:text-center group-data-[vaul-drawer-direction=top]/drawer-content: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 flex-col gap-2 p-4", className)}
      {...props}
    />
  )
}

function DrawerTitle({
  className,
  ...props
}: React.ComponentProps<typeof DrawerPrimitive.Title>) {
  return (
    <DrawerPrimitive.Title
      data-slot="drawer-title"
      className={cn("text-foreground font-semibold", className)}
      {...props}
    />
  )
}

function DrawerDescription({
  className,
  ...props
}: React.ComponentProps<typeof DrawerPrimitive.Description>) {
  return (
    <DrawerPrimitive.Description
      data-slot="drawer-description"
      className={cn("text-muted-foreground text-sm", className)}
      {...props}
    />
  )
}

type DrawerFooterWrapperProps = {
  cancel?: React.ReactNode
  action?: React.ReactNode
  footerCls?: string
  actionCls?: string
  cancelCls?: string
  onAction?: () => void
  onCancel?: () => void
}

function DrawerFooterWrapper({
  cancel,
  action,
  footerCls,
  actionCls,
  cancelCls,
  onAction = () => { },
  onCancel = () => { },
}: DrawerFooterWrapperProps) {
  return (
    <DrawerFooter className={footerCls}>
      {
        cancel &&
        <DrawerClose asChild>
          <Button
            variant="secondary"
            onClick={onCancel}
            className={cn("border", cancelCls)}
            asChild={typeof cancel !== "string"}
          >
            {cancel}
          </Button>
        </DrawerClose>
      }

      {
        action &&
        <Button
          onClick={onAction}
          className={actionCls}
          asChild={typeof action !== "string"}
        >
          {action}
        </Button>
      }
    </DrawerFooter>
  )
}

type DrawerWrapperProps = {
  title?: React.ReactNode
  trigger?: React.ReactNode
  children?: React.ReactNode
  description?: React.ReactNode
  descriptionCls?: string
  contentCls?: string
  headerCls?: string
  titleCls?: string
} & DrawerFooterWrapperProps

function DrawerWrapper({
  trigger,
  title,
  description,
  children,
  contentCls,
  headerCls,
  titleCls,
  descriptionCls,

  cancel = "Cancel",
  action,
  footerCls,
  actionCls,
  cancelCls,
  onAction,
  onCancel,
  ...props
}: React.ComponentProps<typeof DrawerPrimitive.Root> & DrawerWrapperProps) {
  return (
    <Drawer {...props}>
      {trigger &&
        <DrawerTrigger asChild={typeof trigger !== "string"}>{trigger}</DrawerTrigger>
      }

      <DrawerContent className={contentCls}>
        <DrawerHeader className={headerCls}>
          <DrawerTitle className={titleCls}>{title}</DrawerTitle>
          {description && (
            <DrawerDescription className={descriptionCls}>
              {description}
            </DrawerDescription>
          )}
        </DrawerHeader>

        {children}

        {
          (!!cancel || !!action) &&
          <DrawerFooterWrapper
            cancel={cancel}
            action={action}
            footerCls={footerCls}
            actionCls={actionCls}
            cancelCls={cancelCls}
            onAction={onAction}
            onCancel={onCancel}
          />
        }
      </DrawerContent>
    </Drawer>
  )
}

export {
  Drawer,
  DrawerPortal,
  DrawerOverlay,
  DrawerTrigger,
  DrawerClose,
  DrawerContent,
  DrawerHeader,
  DrawerFooter,
  DrawerTitle,
  DrawerDescription,
  DrawerWrapper,
  DrawerFooterWrapper,
}

Done

You can now use DrawerWrapper

Usage

Basic

import { DrawerWrapper } from "@/components/ui/drawer";
import { Button } from "@/components/ui/button";

export function Basic() {
  return (
    <DrawerWrapper
      trigger={<Button>Delete</Button>}
      title="Are you absolutely sure?"
      description="This action cannot be undone. This will permanently delete your account and remove your data from our servers."
    />
  )
}

Controlled

import { useState } from "react"
import { DrawerWrapper } from "@/components/ui/drawer";
import { Button } from "@/components/ui/button";

export function Controlled() {
  const [open, setOpen] = useState(false)

  return (
    <DrawerWrapper
      open={open}
      onOpenChange={setOpen}
      trigger={<Button>Controlled</Button>}
      onAction={() => setOpen(false)}
      title="Are you absolutely sure?"
      description="This action cannot be undone. This will permanently delete your account and remove your data from our servers."
    />
  )
}

// Programaticaly open model without trigger
export function Controlled2() {
  const [open, setOpen] = useState(false)
 
  const updateOpen = () => setOpen(p => !p)

  return (
    <>
      <Button onClick={updateOpen}>Controlled 2</Button>

      <DrawerWrapper
        open={open}
        onOpenChange={updateOpen}
        onAction={updateOpen}
        title="Are you absolutely sure?"
        description="This action cannot be undone. This will permanently delete your account and remove your data from our servers."
      />
    </>
  )
}

Custom

export function Custom() {
  return (
    <DrawerWrapper
      trigger={<Button>Delete</Button>}
      title="Some Custom Modal"

      // to remove footer make cancel empty
      cancel=""
    >
      <div>...</div>
    </DrawerWrapper>
  )
}

Reference

DrawerFooterWrapper

Prop

Type

DrawerWrapper

Prop

Type

Properties of DrawerFooterWrapper can be passed directly.