Drawer
A panel that slides in from an edge of the screen, such as a detail panel.
By default, Drawer is a non-modal dialog: the rest of the page stays usable while it is open. With modal, it follows the Dialog (Modal) WAI-ARIA design pattern like Dialog.
import { component$, useStyles$ } from '@builder.io/qwik';
import { Drawer } from '@leve-ui/drawer';
import { LuX } from '@qwikest/icons/lucide';
import styles from './default.css?inline';
export default component$(() => {
useStyles$(styles);
// Non-modal by default: the page stays usable while the panel is open
return (
<Drawer.Root class="drawer-default">
<Drawer.Trigger class="drawer-default-trigger">Order details</Drawer.Trigger>
<button type="button" class="drawer-default-trigger">
Another page action
</button>
<Drawer.Content class="drawer-default-content">
<Drawer.Title class="drawer-default-title">Order #1042</Drawer.Title>
<Drawer.Description class="drawer-default-description">
Shipped on November 12 to Osaka.
</Drawer.Description>
<dl class="drawer-default-list">
<dt>Items</dt>
<dd>3</dd>
<dt>Total</dt>
<dd>Â¥12,800</dd>
</dl>
<Drawer.Close class="drawer-default-close" aria-label="Close">
<LuX class="drawer-default-icon" aria-hidden="true" />
</Drawer.Close>
</Drawer.Content>
</Drawer.Root>
);
});.drawer-default {
display: flex;
gap: 8px;
font-family: inherit;
}
.drawer-default-trigger {
padding: 8px 14px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
font: inherit;
font-size: 14px;
font-weight: 500;
color: light-dark(#0f172a, #ffffff);
cursor: pointer;
}
.drawer-default-trigger:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
/* The Content is a native <dialog>; data-side places it on an edge of the screen */
.drawer-default-content {
box-sizing: border-box;
margin: 0;
padding: 24px;
border: none;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
font-family: inherit;
box-shadow: 0 16px 40px rgb(15 23 42 / 0.25);
}
.drawer-default-content[data-side="right"] {
inset: 0 0 0 auto;
width: min(320px, 100vw);
height: 100dvh;
max-height: none;
border-left: 1px solid light-dark(#cbd5e1, #ffffff3d);
}
/* Slide in when it opens */
.drawer-default-content[data-side="right"]:popover-open {
transition: translate 200ms ease;
@starting-style {
translate: 100% 0;
}
}
.drawer-default-title {
margin: 0;
font-size: 18px;
font-weight: 600;
}
.drawer-default-description {
margin: 6px 0 20px;
font-size: 14px;
color: light-dark(#475569, #c9ddf0);
}
.drawer-default-list {
display: grid;
grid-template-columns: auto 1fr;
gap: 8px 16px;
margin: 0;
font-size: 14px;
}
.drawer-default-list dt {
color: light-dark(#475569, #c9ddf0);
}
.drawer-default-list dd {
margin: 0;
}
.drawer-default-close {
position: absolute;
top: 12px;
right: 12px;
display: inline-flex;
padding: 4px;
border: none;
border-radius: 6px;
background: none;
color: light-dark(#475569, #c9ddf0);
cursor: pointer;
}
.drawer-default-close:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
.drawer-default-icon {
width: 16px;
height: 16px;
}
.drawer-default-trigger:focus-visible,
.drawer-default-close:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}When to use#
| Use | When |
|---|---|
| Drawer | A panel that opens next to the content while the page stays usable, such as the details of a row in a table. |
Drawer with modal | A panel that blocks the page, such as the navigation menu on small screens. |
| Dialog | A task or decision that must be finished before going back to the page. |
A plain <aside> | A panel that is always visible and never opens or closes. It is part of the layout, not a dialog. |
Installation#
Install the individual package, or use @leve-ui/core to get every component at once:
pnpm add @leve-ui/drawernpm install @leve-ui/draweryarn add @leve-ui/drawerbun add @leve-ui/drawerpnpm add @leve-ui/corenpm install @leve-ui/coreyarn add @leve-ui/corebun add @leve-ui/coreAnatomy#
import { Drawer } from '@leve-ui/drawer'; // or '@leve-ui/core'
<Drawer.Root>
<Drawer.Trigger />
<Drawer.Content>
<Drawer.Title />
<Drawer.Description />
<Drawer.Close />
</Drawer.Content>
</Drawer.Root>
| Part | Element | Description |
|---|---|---|
Drawer.Root | <div> | Wraps the parts and holds the open state, modal and side. |
Drawer.Trigger | <button> | The button that opens and closes the drawer. Use as to render another element or component. |
Drawer.Content | <dialog> | The panel. |
Drawer.Title | <h2> | The title that names the drawer. |
Drawer.Description | <p> | The description of the drawer. |
Drawer.Close | <button> | A button that closes the drawer. Use as to render another element or component. |
A component passed to as on Drawer.Trigger or Drawer.Close must spread the props it receives onto a single element, including id, aria-*, data-*, and on*$ event handlers.
Drawer.Content is a native <dialog>, rendered where you place it and shown above the page in the top layer. A non-modal drawer is a popover="manual" element shown with showPopover(); a modal drawer is opened with showModal(). side only sets data-side: place the panel and its animation with CSS, as in the demos. A modal drawer has a backdrop, styled with ::backdrop like Dialog.
Examples#
Modal#
modal blocks the page: focus stays in the drawer, the page does not scroll, and clicking the backdrop closes it. side="left" sets data-side="left".
import { component$, type PropsOf, Slot, useStyles$ } from '@builder.io/qwik';
import { Drawer } from '@leve-ui/drawer';
import { LuMenu, LuX } from '@qwikest/icons/lucide';
import styles from './modal.css?inline';
// Custom button component passed via `as` must spread received props onto one element
const IconButton = component$<PropsOf<'button'>>(({ type = 'button', ...props }) => (
<button type={type} {...props}>
<Slot />
</button>
));
export default component$(() => {
useStyles$(styles);
// modal blocks the page like a Dialog, for example for navigation on small screens
return (
<Drawer.Root modal side="left" class="drawer-modal">
<Drawer.Trigger as={IconButton} class="drawer-modal-trigger" aria-label="Open navigation">
<LuMenu class="drawer-modal-icon" aria-hidden="true" />
</Drawer.Trigger>
<Drawer.Content class="drawer-modal-content">
<Drawer.Title class="drawer-modal-title">Navigation</Drawer.Title>
<Drawer.Description class="drawer-modal-description">
Go to a section of the site.
</Drawer.Description>
<nav class="drawer-modal-nav">
<a href="#home" class="drawer-modal-link">
Home
</a>
<a href="#projects" class="drawer-modal-link">
Projects
</a>
<a href="#settings" class="drawer-modal-link">
Settings
</a>
</nav>
<Drawer.Close as={IconButton} class="drawer-modal-close" aria-label="Close">
<LuX class="drawer-modal-icon" aria-hidden="true" />
</Drawer.Close>
</Drawer.Content>
</Drawer.Root>
);
});.drawer-modal-trigger {
display: inline-flex;
padding: 8px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
cursor: pointer;
}
.drawer-modal-trigger:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
/* The Content is a native <dialog>; data-side places it on an edge of the screen */
.drawer-modal-content {
box-sizing: border-box;
margin: 0;
padding: 24px;
border: none;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
font-family: inherit;
box-shadow: 0 16px 40px rgb(15 23 42 / 0.25);
}
.drawer-modal-content[data-side="left"] {
inset: 0 auto 0 0;
width: min(280px, 100vw);
height: 100dvh;
max-height: none;
}
/* A modal drawer has a backdrop, the ::backdrop of the <dialog> */
.drawer-modal-content::backdrop {
background: light-dark(rgb(15 23 42 / 0.5), rgb(0 0 0 / 0.6));
}
/* Slide in when it opens */
.drawer-modal-content[data-side="left"][open] {
transition: translate 200ms ease;
@starting-style {
translate: -100% 0;
}
}
.drawer-modal-title {
margin: 0;
font-size: 18px;
font-weight: 600;
}
.drawer-modal-description {
margin: 6px 0 20px;
font-size: 14px;
color: light-dark(#475569, #c9ddf0);
}
.drawer-modal-nav {
display: flex;
flex-direction: column;
gap: 2px;
}
.drawer-modal-link {
padding: 8px 10px;
border-radius: 6px;
font-size: 14px;
color: inherit;
text-decoration: none;
}
.drawer-modal-link:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
.drawer-modal-close {
position: absolute;
top: 12px;
right: 12px;
display: inline-flex;
padding: 4px;
border: none;
border-radius: 6px;
background: none;
color: light-dark(#475569, #c9ddf0);
cursor: pointer;
}
.drawer-modal-close:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
.drawer-modal-icon {
width: 16px;
height: 16px;
}
.drawer-modal-trigger:focus-visible,
.drawer-modal-link:focus-visible,
.drawer-modal-close:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}API Reference#
Root#
| Prop | Default | Type |
|---|---|---|
| ids | – | { trigger?: string; content?: string; title?: string; description?: string } Overrides the element IDs of the parts. |
| defaultOpen | false | boolean Whether the drawer is open initially (uncontrolled). The drawer opens when the page resumes, since it is shown from the client. |
| bind:open | – | Signal<boolean> A signal with the open state (controlled). |
| onOpenChange$ | – | QRL<(open: boolean) => void> Called when the drawer opens or closes by a user interaction. |
| modal | false | boolean Whether the drawer is modal: it traps focus, blocks the page and closes on outside click, like a Dialog. By default the page stays usable while the drawer is open. |
| side | 'right' | 'top' | 'right' | 'bottom' | 'left' The edge of the screen the drawer is attached to. Only sets |
Also accepts all <div> attributes.
| Data attribute | Value |
|---|---|
data-leve-drawer | Always present |
data-state | "open" | "closed" |
data-side | "top" | "right" | "bottom" | "left" |
The open state is uncontrolled with defaultOpen, or controlled with bind:open. onOpenChange$ is called when the user opens or closes the drawer. The SSR HTML always renders the drawer closed: a drawer that is open initially opens when the page resumes.
Trigger#
| Prop | Default | Type |
|---|---|---|
| as | 'button' | keyof HTMLElementTagNameMap | Component<any> The element or component to render instead of |
Also accepts all <button> attributes.
| Data attribute | Value |
|---|---|
data-leve-drawer-trigger | Always present |
data-state | "open" | "closed" |
The trigger toggles the drawer: a non-modal drawer leaves it usable while open.
Content#
| Prop | Default | Type |
|---|---|---|
| closeOnEscape | true | boolean Whether Escape closes the drawer. |
| closeOnOutsideClick | true | boolean Whether a pointer down on the backdrop closes a modal drawer. A non-modal drawer never closes on outside interaction. |
Also accepts all <dialog> attributes.
| Data attribute | Value |
|---|---|
data-leve-drawer-content | Always present |
data-state | "open" | "closed" |
data-side | "top" | "right" | "bottom" | "left" |
A non-modal drawer matches :popover-open while it is shown, and a modal drawer has the open attribute. Use them with @starting-style to animate the drawer when it appears.
Title#
No props of its own. Accepts all <h2> attributes.
| Data attribute | Value |
|---|---|
data-leve-drawer-title | Always present |
Description#
No props of its own. Accepts all <p> attributes.
| Data attribute | Value |
|---|---|
data-leve-drawer-description | Always present |
Close#
| Prop | Default | Type |
|---|---|---|
| as | 'button' | keyof HTMLElementTagNameMap | Component<any> The element or component to render instead of |
Also accepts all <button> attributes.
| Data attribute | Value |
|---|---|
data-leve-drawer-close | Always present |
data-state | "open" | "closed" |
Accessibility#
There is no WAI-ARIA APG pattern for non-modal dialogs. A non-modal drawer is a role="dialog" without aria-modal, where focus can move freely between the drawer and the page (ADR 0028). With modal, Drawer complies with the Dialog (Modal) WAI-ARIA design pattern.
Drawer.Contentrendersaria-labelledbypointing toDrawer.Title,aria-describedbypointing toDrawer.Description, andaria-modal="true"only whenmodal.Drawer.Triggerrendersaria-haspopup="dialog",aria-expandedandaria-controlspointing to the Content.- When the drawer opens, focus moves to its first focusable element. When it closes, focus returns to the element that was focused before it opened, or to the Trigger. If a non-modal drawer closes while focus is elsewhere on the page, focus does not move.
- A non-modal drawer does not trap focus or stop the page from scrolling, and does not close when the page is clicked. A modal drawer does all three.
- If
Drawer.Closeonly contains an icon, give it anaria-label.
Keyboard Interactions#
| Key | Description |
|---|---|
Tab | Non-modal: moves focus through the drawer and on to the page. Modal: cycles through the focusable elements in the drawer. |
Shift + Tab | Moves focus backwards, in the same way. |
Escape | Closes the drawer, unless closeOnEscape is false. |