Extends the Dialog component to display content that complements the main content of the screen.
import { html } from '@webjsdev/core';
import { buttonClass } from '#components/ui/button.ts';
import { inputClass } from '#components/ui/input.ts';
import { labelClass } from '#components/ui/label.ts';
import {
sheetHeaderClass, sheetFooterClass, sheetTitleClass, sheetDescriptionClass,
} from '#components/ui/sheet.ts';
export default function SheetDemo() {
return html`
<ui-sheet>
<ui-sheet-trigger>
<button type="button" data-slot="button" data-variant="outline" data-size="default" class=${buttonClass({ variant: 'outline' })}>Open</button>
</ui-sheet-trigger>
<ui-sheet-content side="right">
<div data-slot="sheet-header" class=${sheetHeaderClass()}>
<h2 data-slot="sheet-title" class=${sheetTitleClass()}>Edit profile</h2>
<p data-slot="sheet-description" class=${sheetDescriptionClass()}>
Make changes to your profile here. Click save when you're done.
</p>
</div>
<div class="grid flex-1 auto-rows-min gap-6 px-4">
<div class="grid gap-3">
<label data-slot="label" for="sheet-demo-name" class=${labelClass()}>Name</label>
<input data-slot="input" id="sheet-demo-name" value="Pedro Duarte" class=${inputClass()}>
</div>
<div class="grid gap-3">
<label data-slot="label" for="sheet-demo-username" class=${labelClass()}>Username</label>
<input data-slot="input" id="sheet-demo-username" value="@peduarte" class=${inputClass()}>
</div>
</div>
<div data-slot="sheet-footer" class=${sheetFooterClass()}>
<button type="submit" data-slot="button" data-variant="default" data-size="default" class=${buttonClass()}>Save changes</button>
<ui-sheet-close>
<button type="button" data-slot="button" data-variant="outline" data-size="default" class=${buttonClass({ variant: 'outline' })}>Close</button>
</ui-sheet-close>
</div>
</ui-sheet-content>
</ui-sheet>
`;
}
View Code
Installation
Copy and paste the following code into your project.
/**
* Sheet: a dialog that slides in from an edge of the screen (a side panel, a
* mobile menu, a filter drawer). Tier-2. It IS a dialog, the way shadcn's
* Sheet is the Dialog primitive with a side: the same native `<dialog>`
* opened with `showModal()`, the same focus trap, Escape and backdrop close,
* focus restoration, scroll lock and label wiring, attached to an edge.
*
* shadcn parity:
* Sheet → <ui-sheet open>
* SheetTrigger → <ui-sheet-trigger>
* SheetContent (side: top | right | bottom | left)
* → <ui-sheet-content side="right">
* SheetClose → <ui-sheet-close>
* SheetHeader / SheetFooter
* → <div class=${sheetHeaderClass()}> / <div class=${sheetFooterClass()}>
* SheetTitle / SheetDescription
* → <h2 data-slot="sheet-title" class=${sheetTitleClass()}> /
* <p data-slot="sheet-description" class=${sheetDescriptionClass()}>
*
* `side` names a physical edge, as shadcn's does, so `side="right"` stays on
* the right under `dir="rtl"`. Pick the side from the reading direction when
* a panel should follow it.
*
* A11y (handled by the element, plus one obligation): the native `<dialog>`
* carries `role="dialog"`, and on open it is named by the `sheet-title` node
* (falling back to the first heading, then to a generic name) and described by
* the `sheet-description` node. Focus moves into the sheet, Tab stays inside,
* Escape and a backdrop click close it, and focus returns to the trigger. Give
* every sheet a real title, since a generic "Dialog" name says nothing about
* what opened, and put the trigger's own accessible name on a real `<button>`.
*
* @example
* ```html
* <ui-sheet>
* <ui-sheet-trigger><button class=${buttonClass({ variant: 'outline' })} type="button">Filters</button></ui-sheet-trigger>
* <ui-sheet-content side="right">
* <div class=${sheetHeaderClass()}>
* <h2 data-slot="sheet-title" class=${sheetTitleClass()}>Filters</h2>
* <p data-slot="sheet-description" class=${sheetDescriptionClass()}>Narrow the list.</p>
* </div>
* </ui-sheet-content>
* </ui-sheet>
* ```
*/
import { UiDialog, UiDialogTrigger, UiDialogContent, UiDialogClose } from './dialog.ts';
export type SheetSide = 'top' | 'right' | 'bottom' | 'left';
/** The visible panel. The element applies it; exported for a custom shell. */
export function sheetContentClass(): string {
return 'fixed z-50 flex flex-col gap-4 bg-background shadow-lg transition ease-in-out data-[state=closed]:duration-300 data-[state=open]:duration-500 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=right]:sm:max-w-sm 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=left]:sm:max-w-sm data-[side=top]:inset-x-0 data-[side=top]:top-0 data-[side=top]:h-auto data-[side=top]:border-b data-[side=bottom]:inset-x-0 data-[side=bottom]:bottom-0 data-[side=bottom]:h-auto data-[side=bottom]:border-t data-open:animate-in data-open:fade-in-0 data-[side=bottom]:data-open:slide-in-from-bottom-10 data-[side=left]:data-open:slide-in-from-left-10 data-[side=right]:data-open:slide-in-from-right-10 data-[side=top]:data-open:slide-in-from-top-10 data-closed:animate-out data-closed:fade-out-0 data-[side=bottom]:data-closed:slide-out-to-bottom-10 data-[side=left]:data-closed:slide-out-to-left-10 data-[side=right]:data-closed:slide-out-to-right-10 data-[side=top]:data-closed:slide-out-to-top-10';
}
/** The auto-injected close button in the corner. */
export function sheetCloseButtonClass(): string {
return "absolute top-4 end-4 rounded-xs opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:ring-2 focus:ring-ring focus:ring-offset-2 focus:outline-hidden disabled:pointer-events-none data-[state=open]:bg-secondary [&_svg:not([class*='size-'])]:size-4";
}
/** The overlay behind the panel, which each style dims in its own way. */
export function sheetOverlayClass(): string {
return 'bg-black/50 fixed inset-0 z-50 duration-100 data-open:animate-in data-open:fade-in-0 data-closed:animate-out data-closed:fade-out-0';
}
/** The title and description block at the top. */
export function sheetHeaderClass(): string {
return 'gap-1.5 p-4 flex flex-col';
}
/** The actions block, pushed to the bottom. */
export function sheetFooterClass(): string {
return 'gap-2 p-4 mt-auto flex flex-col';
}
/** The sheet's title. Mark it `data-slot="sheet-title"` so the sheet is named by it. */
export function sheetTitleClass(): string {
return 'font-semibold text-foreground cn-font-heading';
}
/** The sheet's description. Mark it `data-slot="sheet-description"`. */
export function sheetDescriptionClass(): string {
return 'text-sm text-muted-foreground';
}
export class UiSheet extends UiDialog {
static rootTag = 'ui-sheet';
static contentTag = 'ui-sheet-content';
static slotPrefix = 'sheet';
}
UiSheet.register('ui-sheet');
export class UiSheetTrigger extends UiDialogTrigger {
static rootTag = 'ui-sheet';
static slotPrefix = 'sheet';
}
UiSheetTrigger.register('ui-sheet-trigger');
export class UiSheetClose extends UiDialogClose {
static rootTag = 'ui-sheet';
static slotPrefix = 'sheet';
}
UiSheetClose.register('ui-sheet-close');
export class UiSheetContent extends UiDialogContent {
static rootTag = 'ui-sheet';
static slotPrefix = 'sheet';
// `side` is read straight from the attribute rather than declared as a
// reactive prop: the element is built on the dialog's factory base, and the
// side is something an author writes once in markup. Attribute reads work
// during server rendering too, so the first paint already carries data-side.
_side(): SheetSide {
const s = this.getAttribute?.('side');
return s === 'top' || s === 'bottom' || s === 'left' ? s : 'right';
}
_panelClass(): string {
return sheetContentClass();
}
_closeClass(): string {
return sheetCloseButtonClass();
}
_overlayClass(): string {
return sheetOverlayClass();
}
}
UiSheetContent.register('ui-sheet-content');
Update the import paths to match your project setup.
Usage
import {
sheetDescriptionClass,
sheetFooterClass,
sheetHeaderClass,
sheetTitleClass,
} from '#components/ui/sheet.ts';
<ui-sheet>
<ui-sheet-trigger>
<button type="button" class=${buttonClass({ variant: 'outline' })}>Open</button>
</ui-sheet-trigger>
<ui-sheet-content>
<div class=${sheetHeaderClass()}>
<h2 data-slot="sheet-title" class=${sheetTitleClass()}>Are you absolutely sure?</h2>
<p data-slot="sheet-description" class=${sheetDescriptionClass()}>This action cannot be undone.</p>
</div>
</ui-sheet-content>
</ui-sheet>
Composition
Use the following composition to build a sheet:
ui-sheet
├── ui-sheet-trigger
└── ui-sheet-content
├── div (sheetHeaderClass)
│ ├── h2 (sheetTitleClass)
│ └── p (sheetDescriptionClass)
└── div (sheetFooterClass)
└── ui-sheet-close
Side
Use the side attribute on <ui-sheet-content> to set the edge of the screen where the sheet appears. Values are top, right, bottom, or left.
import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import { buttonClass } from '#components/ui/button.ts';
import {
sheetHeaderClass, sheetFooterClass, sheetTitleClass, sheetDescriptionClass,
} from '#components/ui/sheet.ts';
const SHEET_SIDES = ['top', 'right', 'bottom', 'left'] as const;
export default function SheetSide() {
return html`
<div class="flex flex-wrap gap-2">
${SHEET_SIDES.map((side) => html`
<ui-sheet>
<ui-sheet-trigger>
<button type="button" data-slot="button" data-variant="outline" data-size="default" class=${cn(buttonClass({ variant: 'outline' }), 'capitalize')}>
${side}
</button>
</ui-sheet-trigger>
<ui-sheet-content side=${side} class="data-[side=bottom]:max-h-[50vh] data-[side=top]:max-h-[50vh]">
<div data-slot="sheet-header" class=${sheetHeaderClass()}>
<h2 data-slot="sheet-title" class=${sheetTitleClass()}>Edit profile</h2>
<p data-slot="sheet-description" class=${sheetDescriptionClass()}>
Make changes to your profile here. Click save when you're
done.
</p>
</div>
<div class="no-scrollbar overflow-y-auto px-4">
${Array.from({ length: 10 }).map(() => html`
<p class="mb-2 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>
`)}
</div>
<div data-slot="sheet-footer" class=${sheetFooterClass()}>
<button type="submit" data-slot="button" data-variant="default" data-size="default" class=${buttonClass()}>Save changes</button>
<ui-sheet-close>
<button type="button" data-slot="button" data-variant="outline" data-size="default" class=${buttonClass({ variant: 'outline' })}>Cancel</button>
</ui-sheet-close>
</div>
</ui-sheet-content>
</ui-sheet>
`)}
</div>
`;
}
View Code
No Close Button
Use show-close-button="false" on <ui-sheet-content> to hide the close button.
import { html } from '@webjsdev/core';
import { buttonClass } from '#components/ui/button.ts';
import {
sheetHeaderClass, sheetTitleClass, sheetDescriptionClass,
} from '#components/ui/sheet.ts';
export default function SheetNoCloseButton() {
return html`
<ui-sheet>
<ui-sheet-trigger>
<button type="button" data-slot="button" data-variant="outline" data-size="default" class=${buttonClass({ variant: 'outline' })}>Open Sheet</button>
</ui-sheet-trigger>
<ui-sheet-content show-close-button="false">
<div data-slot="sheet-header" class=${sheetHeaderClass()}>
<h2 data-slot="sheet-title" class=${sheetTitleClass()}>No Close Button</h2>
<p data-slot="sheet-description" class=${sheetDescriptionClass()}>
This sheet doesn't have a close button in the top-right corner.
Click outside to close.
</p>
</div>
</ui-sheet-content>
</ui-sheet>
`;
}
View Code
RTL
To enable RTL support, see the RTL configuration guide. side names a physical edge, so side="right" stays on the right under dir="rtl": pick the side from the reading direction when the panel should follow it, as this demo does.
import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import { buttonClass } from '#components/ui/button.ts';
import { fieldGroupClass, fieldClass, fieldLabelClass } from '#components/ui/field.ts';
import { inputClass } from '#components/ui/input.ts';
import {
sheetHeaderClass, sheetFooterClass, sheetTitleClass, sheetDescriptionClass,
} from '#components/ui/sheet.ts';
const translations = {
en: {
dir: 'ltr',
values: {
open: 'Open',
editProfile: 'Edit profile',
description:
"Make changes to your profile here. Click save when you're done.",
name: 'Name',
username: 'Username',
save: 'Save changes',
close: 'Close',
},
},
ar: {
dir: 'rtl',
values: {
open: 'فتح',
editProfile: 'تعديل الملف الشخصي',
description:
'قم بإجراء تغييرات على ملفك الشخصي هنا. انقر حفظ عند الانتهاء.',
name: 'الاسم',
username: 'اسم المستخدم',
save: 'حفظ التغييرات',
close: 'إغلاق',
},
},
he: {
dir: 'rtl',
values: {
open: 'פתח',
editProfile: 'עריכת פרופיל',
description: 'בצע שינויים בפרופיל שלך כאן. לחץ שמור כשתסיים.',
name: 'שם',
username: 'שם משתמש',
save: 'שמור שינויים',
close: 'סגור',
},
},
};
export default function SheetRtl() {
const lang = 'ar';
const { dir, values: t } = translations[lang];
return html`
<ui-sheet>
<ui-sheet-trigger>
<button type="button" data-slot="button" data-variant="outline" data-size="default" class=${buttonClass({ variant: 'outline' })}>${t.open}</button>
</ui-sheet-trigger>
<ui-sheet-content dir=${dir} lang=${lang} side=${dir === 'rtl' ? 'left' : 'right'}>
<div data-slot="sheet-header" class=${sheetHeaderClass()}>
<h2 data-slot="sheet-title" class=${sheetTitleClass()}>${t.editProfile}</h2>
<p data-slot="sheet-description" class=${sheetDescriptionClass()}>${t.description}</p>
</div>
<div data-slot="field-group" class=${cn(fieldGroupClass(), 'px-4')}>
<div role="group" data-slot="field" data-orientation="vertical" class=${fieldClass()}>
<label data-slot="field-label" for="sheet-rtl-name" class=${fieldLabelClass()}>${t.name}</label>
<input data-slot="input" id="sheet-rtl-name" value="Pedro Duarte" class=${inputClass()}>
</div>
<div role="group" data-slot="field" data-orientation="vertical" class=${fieldClass()}>
<label data-slot="field-label" for="sheet-rtl-username" class=${fieldLabelClass()}>${t.username}</label>
<input data-slot="input" id="sheet-rtl-username" value="peduarte" class=${inputClass()}>
</div>
</div>
<div data-slot="sheet-footer" class=${sheetFooterClass()}>
<button type="submit" data-slot="button" data-variant="default" data-size="default" class=${buttonClass()}>${t.save}</button>
<ui-sheet-close>
<button type="button" data-slot="button" data-variant="outline" data-size="default" class=${buttonClass({ variant: 'outline' })}>${t.close}</button>
</ui-sheet-close>
</div>
</ui-sheet-content>
</ui-sheet>
`;
}
View Code
API Reference
A sheet is a dialog attached to an edge of the screen: the same native <dialog> opened with showModal(), with the same focus trap, Escape and backdrop close, focus restoration and scroll lock. See the Dialog documentation.
ui-sheet
ui-sheet-content
ui-sheet-trigger and ui-sheet-close
Wrap a real <button>: a click on it opens or closes the sheet.
Class helpers
sheetHeaderClass(), sheetFooterClass(), sheetTitleClass() and sheetDescriptionClass() style the parts inside the content. Mark the title data-slot="sheet-title" and the description data-slot="sheet-description", so the sheet is named and described by them.