Dialog
A modal dialog for a focused decision. The title asks the question; the body states the consequence.
Example
Lukas loses access to 8 projects immediately. Their files stay in the workspace.
<focsd-button commandfor="dialog-demo" command="show-modal">Remove member</focsd-button><focsd-dialog id="dialog-demo" headline="Remove Lukas Brandt?"> <p>Lukas loses access to 8 projects immediately. Their files stay in the workspace.</p> <focsd-button slot="actions" data-dialog-close value="keep">Keep member</focsd-button> <focsd-button slot="actions" variant="critical" data-dialog-close value="remove" >Remove Lukas</focsd-button ></focsd-dialog>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/components/focsd-dialog.js';</script>
<focsd-dialog headline="Remove Lukas Brandt?">…</focsd-dialog>import { Dialog } from '@focsd-dev/react';
export function Example() { return <Dialog onCancel={(e) => console.log(e.target)} headline="Remove Lukas Brandt?">…</Dialog>;}<script setup>import '@focsd-dev/components/focsd-dialog.js';</script>
<template> <focsd-dialog @cancel="onChange" headline="Remove Lukas Brandt?">…</focsd-dialog></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-dialog.js';
// template<focsd-dialog (cancel)="onChange($event)" headline="Remove Lukas Brandt?">…</focsd-dialog><script> import '@focsd-dev/components/focsd-dialog.js';</script>
<focsd-dialog oncancel={(e) => console.log(e.target)} headline="Remove Lukas Brandt?">…</focsd-dialog>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
headline | headline | string | — |
open | open | boolean | false |
closedby | closedBy | 'any' | 'closerequest' | 'none' | closerequest |
no-close | noClose | boolean | false |
Methods
| Method | Description |
|---|---|
show(opener) | Open as a modal dialog. Focus returns to opener (default: the element focused now) on close,
which matters in Safari where clicking a button does not focus it. |
showModal(opener) | Same as show(): Focsd dialogs are always modal. |
requestClose(returnValue) | Ask to close: fires a cancelable cancel, then closes with returnValue unless it was canceled. |
close(returnValue) | Close, optionally setting returnValue. |
Events
| Event | Description |
|---|---|
cancel | Fired before a close request (Escape, backdrop, requestClose); cancel it to keep the dialog open. |
close | Fired after the dialog closes; read returnValue for how it closed. |
Slots
| Slot | Description |
|---|---|
(default) | Default content. |
actions | The dialog's buttons. |
CSS parts
Style the inside of the component with ::part(), for example focsd-dialog::part(actions).
| Part | Description |
|---|---|
actions | The actions element. |
body | The body element. |
close | The close (×) button, last in the tab order; it asks to close like Escape (hidden with `no-close`). |
dialog | The dialog element. |
headline | The headline element. |
Accessibility
requestClose()and Escape fire a cancelablecancel;showModal()is an alias ofshow().closedby="any"also closes on a backdrop click;closedby="none"needs an explicit close action.- Built on native
<dialog>.showModal(): top layer, inert background, Escape to close. - A close (×) button named "Close" sits in the top corner and asks to close as Escape does (a cancelable
cancel). It comes last in the tab order, so opening the dialog focuses its content first. Addno-closefor a dialog that must be answered with one of its actions. - Open it from a button with no script:
<focsd-button commandfor="dialog-id" command="show-modal">. Focus returns to that button on close. From your own code, callshow(opener). - Any slotted element with
data-dialog-closecloses it; itsvaluebecomesreturnValue. Putdata-autofocuson the element that should receive focus first.
Keyboard
| Key | Action |
|---|---|
| Tab | Moves focus within the dialog; the page behind is inert |
| Escape | Closes (unless closedby="none") |
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.