Dialog
Modal dialogs with focus trap and backdrop handling.
Props
Features
Section titled “Features”- 🪟 Uses native HTML
<dialog>element - 🔒 Built-in scroll lock when open
- 🎯 Focus trap and focus management
- 🎨 TODO: Animation support
<script lang="ts"> import { Dialog } from "melt/builders";
const dialog = new Dialog();</script>
<button {...dialog.trigger}> Open Dialog</button>
<dialog {...dialog.content}> Dialog content</dialog>API Reference
Section titled “API Reference”Constructor Props
The props that are passed when calling
new Dialog()new Dialog()export type DialogProps = { /** * If the Dialog is open. * * When passing a getter, it will be used as source of truth, * meaning that the value only changes when the getter returns a new value. * * Otherwise, if passing a static value, it'll serve as the default value. * * @default false */ open?: MaybeGetter<boolean | undefined>;
/** * Called when the value is supposed to change. */ onOpenChange?: (value: boolean) => void;
/** * If the dialog should close when clicking escape. * * @default true */ closeOnEscape?: MaybeGetter<boolean | undefined>;
/** * If the dialog should close when clicking outside. * Alternatively, accepts a function that receives the clicked element, * and returns if the dialog should close. * * @default true */ closeOnOutsideClick?: MaybeGetter<boolean | undefined>;
/** * If the dialog should lock the document scroll when open. * * @default true */ scrollLock?: MaybeGetter<boolean | undefined>;};export type DialogProps = { /** * If the Dialog is open. * * When passing a getter, it will be used as source of truth, * meaning that the value only changes when the getter returns a new value. * * Otherwise, if passing a static value, it'll serve as the default value. * * @default false */ open?: MaybeGetter<boolean | undefined>;
/** * Called when the value is supposed to change. */ onOpenChange?: (value: boolean) => void;
/** * If the dialog should close when clicking escape. * * @default true */ closeOnEscape?: MaybeGetter<boolean | undefined>;
/** * If the dialog should close when clicking outside. * Alternatively, accepts a function that receives the clicked element, * and returns if the dialog should close. * * @default true */ closeOnOutsideClick?: MaybeGetter<boolean | undefined>;
/** * If the dialog should lock the document scroll when open. * * @default true */ scrollLock?: MaybeGetter<boolean | undefined>;};Properties
The properties returned from
new Dialog()new Dialog()-
closeOnEscape
booleanboolean -
closeOnOutsideClick
booleanboolean -
scrollLock
booleanboolean -
refs
{get: (key: "trigger" | "content") => HTMLElement | undefinedattach: (key: "trigger" | "content") => Attachment<HTMLElement>key: any}{get: (key: "trigger" | "content") => HTMLElement | undefinedattach: (key: "trigger" | "content") => Attachment<HTMLElement>key: any} -
open
booleanboolean -
sharedProps
{ "data-open": "" | undefined }{ "data-open": "" | undefined } -
trigger
{readonly "data-open": "" | undefinedreadonly "data-melt-dialog-trigger": ""readonly onclick: () => voidreadonly type: "button"}{readonly "data-open": "" | undefinedreadonly "data-melt-dialog-trigger": ""readonly onclick: () => voidreadonly type: "button"}The trigger element. -
content
{readonly "data-open": "" | undefinedreadonly "data-melt-dialog-content": ""readonly onclose: () => void}{readonly "data-open": "" | undefinedreadonly "data-melt-dialog-content": ""readonly onclose: () => void}The element for the dialog itself.