Skip to content

Dialog

An accessible modal window for focused content or user actions with customizable open/close behavior.

---
import { Button } from '@bejamas/ui/components/button';
import { Checkbox } from '@bejamas/ui/components/checkbox';
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from '@bejamas/ui/components/dialog';
import { Label } from '@bejamas/ui/components/label';
---
<Dialog>
<DialogTrigger asChild>
<Button>Manage Cookies</Button>
</DialogTrigger>
<DialogContent class="sm:max-w-lg">
<DialogHeader>
<DialogTitle>Manage Cookies</DialogTitle>
<DialogDescription>
Using websites and apps involves storing and retrieving information from your device, including cookies and other identifiers.
</DialogDescription>
</DialogHeader>
<div class="mt-3">
<div class="flex items-center gap-3">
<Checkbox id="cookies-necessary" name="cookies-necessary" disabled checked />
<Label for="cookies-necessary">Strictly Necessary Cookies (always active)</Label>
</div>
<p class="text-sm text-muted-foreground ml-8 mt-1">These cookies are essential for the site to function and cannot be toggled off. They assist with security, user authentication, customer support, etc.</p>
</div>
<div class="mt-3">
<div class="flex items-center gap-3">
<Checkbox id="cookies-analytics" name="cookies-analytics" />
<Label for="cookies-analytics">Analytics Cookies</Label>
</div>
<p class="text-sm text-muted-foreground ml-8 mt-1">These cookies help us understand how visitors interact with our site. They allow us to measure traffic and improve site performance.</p>
</div>
<div class="mt-3">
<div class="flex items-center gap-3">
<Checkbox id="cookies-marketing-performance" name="cookies-marketing-performance" />
<Label for="cookies-marketing-performance">Marketing Performance Cookies</Label>
</div>
<p class="text-sm text-muted-foreground ml-8 mt-1">These cookies help us measure the effectiveness of our marketing campaigns.</p>
</div>
<DialogFooter>
<Button data-slot="dialog-close" class="mt-4" variant="outline">Close</Button>
<Button data-dialog-close class="mt-4">Save changes</Button>
</DialogFooter>
</DialogContent>
</Dialog>
Terminal window
bunx bejamas add dialog
---
import { Dialog, DialogTitle, DialogDescription, DialogContent, DialogFooter, DialogClose } from '@bejamas/ui/components/dialog';
import { Button } from '@bejamas/ui/components/button';
---
<Dialog id="myDialog">
<DialogTrigger>Open Dialog</DialogTrigger>
<DialogContent>
<DialogTitle>Dialog Title</DialogTitle>
<DialogDescription>Dialog description.</DialogDescription>
Content here
<DialogFooter>
<DialogClose>Close</DialogClose>
</DialogFooter>
</DialogContent>
</Dialog>
PropTypeDefault
defaultOpenbooleanfalse
classstring""
closeOnClickOutsidebooleantrue
closeOnEscapebooleantrue
lockScrollbooleantrue
---
import { Button } from '@bejamas/ui/components/button';
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@bejamas/ui/components/dialog';
---
<Dialog>
<Button data-slot="dialog-trigger">Open Nested Dialog</Button>
<DialogContent class="sm:max-w-lg">
<DialogHeader>
<DialogTitle>Nested Dialog</DialogTitle>
<DialogDescription>
Using websites and apps involves storing and retrieving information from your device, including cookies and other identifiers.
</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button data-slot="dialog-close" class="mt-4" variant="outline">Close</Button>
<Dialog>
<Button data-slot="dialog-trigger" class="mt-4">Open Nested Dialog</Button>
<DialogContent class="sm:max-w-md border-red-800">
<DialogHeader>
<DialogTitle>Nested Dialog</DialogTitle>
<DialogDescription>
Using websites and apps involves storing and retrieving information from your device, including cookies and other identifiers.
</DialogDescription>
</DialogHeader>
</DialogContent>
</Dialog>
</DialogFooter>
</DialogContent>
</Dialog>

The dialog emits custom events that you can listen to:

EventDetailDescription
dialog:change{ open: boolean }Fired when the open state changes
<Dialog id="my-dialog">
<DialogTrigger>Open Dialog</DialogTrigger>
<DialogContent>
<DialogTitle>Title</DialogTitle>
<DialogDescription>Description</DialogDescription>
</DialogContent>
</Dialog>
const dialog = document.getElementById('my-dialog');
dialog.addEventListener('dialog:change', (e) => {
console.log('Is open:', e.detail.open);
});

You can control the dialog programmatically by dispatching a dialog:set event:

const dialog = document.getElementById('my-dialog');
// Open the dialog
dialog.dispatchEvent(new CustomEvent('dialog:set', {
detail: { open: true }
}));
// Close the dialog
dialog.dispatchEvent(new CustomEvent('dialog:set', {
detail: { open: false }
}));

The dialog sets these data attributes that you can use for styling or querying state:

AttributeElementDescription
data-statedialogCurrent state (open or closed)