Skip to Content
NarakUI

Drawer

A drawer component for displaying content in a slide-out panel from any side of the screen.

Drawers open from the right by default, for settings and forms. Most product drawers set width between 460 and 560px. The footer's customRenderer gets a closeDrawer callback.

Left

Set placement="left" for lists that relate to the page's own navigation. contentClassName adjusts the content padding.

Wide

Use 800px or more for multi-column forms and detail views. Without width the drawer grows up to 1100px. On small screens it fills the viewport.

Setup

Mount <DrawerProvider /> once near the root of your app (inside the relevant context providers). Nested mounts will render duplicate drawers because all drawers share the same global store.

// app/layout.tsx (or the highest layout that has the contexts your drawers need)
import { DrawerProvider } from '@/components/ui/drawer';

export default function RootLayout({ children }) {
  return (
    <>
      {children}
      <DrawerProvider />
    </>
  );
}

Installation

pnpm dlx shadcn@latest add @narakui/drawer

Add "@narakui": "https://ui.narakcode.dev/r/{name}.json" to components.json, or install directly from https://ui.narakcode.dev/r/drawer.json.

Usage

Open drawers through the store. DrawerProvider renders the drawer parts for you. Opening a drawer with an id that is already open replaces it; opening a new id stacks it on top.

import { useDrawerStore } from '@/components/ui/drawer';

useDrawerStore.getState().openDrawer({
  id: 'my-drawer',
  placement: 'right',
  header: {
    title: 'My Drawer',
  },
  content: <div>Drawer content</div>,
});

useDrawerStore.getState().closeDrawer('my-drawer');

The store also has updateDrawer(id, updates) to change an open drawer and closeAllDrawers().

openDrawer Options

PropTypeDefaultDescription
id (Required)string—Unique identifier for the drawer. Pass it to closeDrawer and updateDrawer.
contentReactNode—Content rendered inside the drawer's padded scroll area
placement'left' | 'right' | 'top' | 'bottom''right'Side of the screen where the drawer appears
widthnumber1100Maximum width of the drawer in pixels
headerobject—Header configuration. See header Options below.
hideHeaderbooleanfalseRemoves the header bar, close button included, for content that renders its own header
footer{ customRenderer?: ({ closeDrawer, id }) => ReactNode }—Footer rendered below the scroll area. Nothing renders without customRenderer.
withOverlaybooleantrueRenders the backdrop and makes the drawer modal
closeOnEscbooleantrueWhether to close drawer when escape key is pressed
closeOnOutsideClickbooleantrueWhether to close drawer when clicking outside
beforeClose() => boolean—Called before closing. Return false to keep the drawer open.
onClose() => void—Called when the drawer closes. May return a promise, which closeDrawer passes back.
onCloseAutoFocus(event: Event) => void—Called when focus moves back after the drawer closes
classNamestring—Additional CSS classes for the drawer panel
contentClassNamestring—Additional CSS classes for the padded content box
scrollAreaClassNamestring—Additional CSS classes for the scroll area around the content

header Options

PropTypeDefaultDescription
titleReactNode—Title shown on the left of the header
showCloseButtonbooleantrueShows the close button on the right of the header
headerEndReactNode—Content placed after the title
isBackButtonbooleanfalseRenders the title as a back button with a left chevron
onBack() => void—Called by the back button. Closes the drawer when omitted.
showNavigationbooleanfalseShows the previous/next pager. Needs currentIndex and totalItems.
currentIndexnumber—0-based index of the item shown in the pager
totalItemsnumber—Number of items in the pager
labelPrefixstring—Text shown before the pager count
onPrevious() => void—Called by the pager's previous button
onNext() => void—Called by the pager's next button
customRenderer({ closeDrawer, id }) => ReactNode—Replaces the whole header, including its container