Modal
A Modal is a dialog over the page that the user has to deal with before going on: a form, a confirmation, a piece of information. It is drawn above everything on the page, and while it is open the page does not scroll, the page behind it is out of reach of the keyboard and assistive technology, focus stays inside it and comes back, and Escape closes it. Write it into the markup (<Modal bind:open>) or call a ready-made dialog from code (modal.confirm(…)).
When to use
- Use it when the user has to answer or finish something before going on, without leaving the page: a form, a confirmation of a destructive action, a short piece of information.
- A short notice that needs no answer is a Message. A panel at the edge of the page is a Drawer, and a small bubble next to an element a popover (both are planned). A Modal interrupts the user: use it sparingly.
- For a confirmation, a dialog from code is the shorter way:
if (await modal.confirm({ … })) ….
Examples
<script lang="ts">
import { Button, Modal } from 'svelte-ant-design';
let open = $state(false);
let log = $state('');
// Every opening starts with an empty log, so that the last way to close is the only thing it says.
function show() {
log = '';
open = true;
}
</script>
<!-- bind:open: the close button, Cancel, Escape and the mask close it by themselves. OK does not: it is for you to close the Modal once you have checked or saved, so onok closes it here (the other examples set closeOnOk). -->
<div class="stack">
<Button type="primary" onclick={show}>Open Modal</Button>
<Modal
bind:open
title="Basic Modal"
onok={() => {
log = 'OK pressed';
open = false;
}}
oncancel={() => (log = 'cancelled')}
onafterclose={() => (log = `${log}, then closed`)}
>
<p>Some contents...</p>
<p>Some contents...</p>
</Modal>
<output class="caption">{log}</output>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
confirmLoading: the app closes it
closeOnOk: onok returns a Promise (the first try is rejected)
<script lang="ts">
import { Button, Modal } from 'svelte-ant-design';
// 1. The app closes the Modal: it sets `confirmLoading` while it saves, and `open = false` when it is done.
let manualOpen = $state(false);
let saving = $state(false);
function save() {
saving = true;
setTimeout(() => {
saving = false;
manualOpen = false;
}, 1500);
}
// 2. `closeOnOk`: the Modal waits for the Promise that onok returns, and closes when it is resolved. A reject keeps it open.
let waitOpen = $state(false);
let attempts = $state(0);
const submit = () =>
new Promise<void>((resolve, reject) =>
setTimeout(() => {
attempts += 1;
if (attempts % 2 === 1) reject(new Error('The first try fails'));
else resolve();
}, 1200)
);
</script>
<div class="stack">
<div class="field">
<span class="caption">confirmLoading: the app closes it</span>
<Button type="primary" onclick={() => (manualOpen = true)}>Open (confirmLoading)</Button>
<Modal bind:open={manualOpen} title="Save the file" confirmLoading={saving} onok={save}>
<p>The OK button is loading for 1.5 seconds, then the Modal closes.</p>
</Modal>
</div>
<div class="field">
<span class="caption">closeOnOk: onok returns a Promise (the first try is rejected)</span>
<Button onclick={() => (waitOpen = true)}>Open (closeOnOk)</Button>
<Modal bind:open={waitOpen} title="Send the form" closeOnOk onok={submit}>
<p>Press OK. The first try fails and the Modal stays open; the second succeeds and closes it.</p>
</Modal>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
<script lang="ts">
import { Button, Modal } from 'svelte-ant-design';
let open = $state(false);
</script>
<!-- okText and cancelText: a text, a number or a snippet. okType is the type of the OK button; okButtonProps and cancelButtonProps are the props of Button itself. -->
<div class="stack">
<Button onclick={() => (open = true)}>Delete…</Button>
<Modal
bind:open
title="Delete the project?"
okText="Delete"
cancelText="Keep it"
okType="primary"
okButtonProps={{ danger: true }}
cancelButtonProps={{ type: 'text' }}
onok={() => (open = false)}
>
<p>The OK button is a danger button, and Cancel is a text button.</p>
</Modal>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
</style>
mask: { closable: false }
mask: false
keyboard: false
<script lang="ts">
import { Button, Modal } from 'svelte-ant-design';
let sticky = $state(false);
let bare = $state(false);
let silent = $state(false);
</script>
<div class="stack">
<div class="field">
<span class="caption">mask: {'{ closable: false }'}</span>
<Button onclick={() => (sticky = true)}>The mask does not close it</Button>
<Modal closeOnOk bind:open={sticky} title="Sticky" mask={{ closable: false }}><p>A press outside does nothing. The close button, Cancel, OK and Escape still close it.</p></Modal>
</div>
<div class="field">
<span class="caption">mask: false</span>
<Button onclick={() => (bare = true)}>No mask</Button>
<Modal closeOnOk bind:open={bare} title="No mask" mask={false}><p>No dim layer is drawn, but the page behind is still out of reach, as in Ant Design.</p></Modal>
</div>
<div class="field">
<span class="caption">keyboard: false</span>
<Button onclick={() => (silent = true)}>Escape does not close it</Button>
<Modal closeOnOk bind:open={silent} title="No Escape" keyboard={false}><p>Escape does nothing here; the close button, Cancel, OK and the mask still close it.</p></Modal>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
<script lang="ts">
import { Button, Modal } from 'svelte-ant-design';
let centered = $state(false);
let wide = $state(false);
let high = $state(false);
let zoom = $state(false);
</script>
<div class="stack">
<div class="row">
<Button onclick={() => (centered = true)}>Centered</Button>
<Button onclick={() => (wide = true)}>Width 720</Button>
<Button onclick={() => (high = true)}>Style and zIndex</Button>
<Button onclick={() => (zoom = true)}>zoomFromClick</Button>
</div>
<Modal closeOnOk bind:open={centered} title="Centered" centered><p>In the middle of the window.</p></Modal>
<Modal closeOnOk bind:open={wide} title="Width 720" width={720}><p>A number is px; a string such as <code>'60%'</code> is used as it is. On a narrow window it is never wider than the window.</p></Modal>
<Modal closeOnOk bind:open={high} title="Style and zIndex" style="top: 20px" zIndex={1100}><p>20px from the top, above the default layer.</p></Modal>
<Modal closeOnOk bind:open={zoom} title="From the click" zoomFromClick><p>It zooms in and out from the place of the click, as in Ant Design. By default it zooms around its own center.</p></Modal>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
</style>
<script lang="ts">
import { Button, Modal } from 'svelte-ant-design';
let plain = $state(false);
let rich = $state(false);
let noClose = $state(false);
let own = $state(false);
</script>
{#snippet richTitle()}<span>Title with <em>emphasis</em></span>{/snippet}
{#snippet crossIcon()}<span aria-hidden="true">✕</span>{/snippet}
<div class="stack">
<div class="row">
<Button onclick={() => (plain = true)}>No title</Button>
<Button onclick={() => (rich = true)}>Snippet title</Button>
<Button onclick={() => (noClose = true)}>closable: false</Button>
<Button onclick={() => (own = true)}>closeIcon and a name</Button>
</div>
<Modal closeOnOk bind:open={plain} aria-label="A dialog with no title"><p>No title, no header: give it a name with <code>aria-label</code>.</p></Modal>
<Modal closeOnOk bind:open={rich} title={richTitle}><p>The title is a string, a number or a snippet.</p></Modal>
<Modal closeOnOk bind:open={noClose} title="No close button" closable={false}><p>Only Cancel, OK, Escape and the mask close it.</p></Modal>
<Modal closeOnOk bind:open={own} title="Own icon" closeIcon={crossIcon} closable={{ 'aria-label': 'Close the dialog' }}><p>The cross is a snippet, and the button has its own name.</p></Modal>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
</style>
<script lang="ts">
import { Button, Modal } from 'svelte-ant-design';
let open = $state(false);
let loose = $state(false);
</script>
<div class="stack">
<div class="row">
<Button onclick={() => (open = true)}>Long content</Button>
<Button onclick={() => (loose = true)}>scrollLock: false</Button>
</div>
<Modal closeOnOk bind:open title="Long content">
{#each Array.from({ length: 40 }, (_, index) => index) as index (index)}
<p>Paragraph {index + 1}: the window scrolls, the page behind does not, and it does not jump sideways when its scrollbar goes away.</p>
{/each}
</Modal>
<Modal closeOnOk bind:open={loose} title="The page may scroll" scrollLock={false}>
<p>The page behind keeps scrolling while this is open (the Modal does not hold the scroll).</p>
</Modal>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
</style>
<script lang="ts">
import { Button, Modal } from 'svelte-ant-design';
let first = $state(false);
let second = $state(false);
</script>
<!-- Escape and Tab go to the Modal on top; the first one is out of reach until the second is closed. -->
<div class="stack">
<Button onclick={() => (first = true)}>Open the first</Button>
<Modal closeOnOk bind:open={first} title="First">
<p>The second Modal opens above this one.</p>
<Button onclick={() => (second = true)}>Open the second</Button>
</Modal>
<Modal closeOnOk bind:open={second} title="Second"><p>Escape closes this one first, then the first.</p></Modal>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
</style>
default: the content is kept
destroyOnHidden
forceRender, onafteropenchange and onafterclose
<script lang="ts">
import { Button, Modal } from 'svelte-ant-design';
let kept = $state(false);
let fresh = $state(false);
let present = $state(false);
let log = $state<string[]>([]);
</script>
<div class="stack">
<div class="field">
<span class="caption">default: the content is kept</span>
<Button onclick={() => (kept = true)}>Keeps what you typed</Button>
<Modal closeOnOk bind:open={kept} title="Kept"><input aria-label="Draft" placeholder="Type, close, open again" /></Modal>
</div>
<div class="field">
<span class="caption">destroyOnHidden</span>
<Button onclick={() => (fresh = true)}>Starts empty each time</Button>
<Modal closeOnOk bind:open={fresh} title="Fresh" destroyOnHidden><input aria-label="Draft" placeholder="Type, close, open again" /></Modal>
</div>
<div class="field">
<span class="caption">forceRender, onafteropenchange and onafterclose</span>
<Button onclick={() => (present = true)}>Present from the start</Button>
<Modal
bind:open={present}
title="Present"
closeOnOk
forceRender
onafteropenchange={(opened) => (log = [...log, opened ? 'opened' : 'closed'])}
onafterclose={() => (log = [...log, 'afterclose'])}
>
<p>It was in the page, hidden, before this first opening.</p>
</Modal>
<output class="caption">{log.join(' → ')}</output>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
the Modal goes into this box
<script lang="ts">
import { Button, Modal } from 'svelte-ant-design';
let selected = $state(false);
let inPlace = $state(false);
let box = $state<HTMLElement | null>(null);
let where = $state('');
// `data-demo` is an attribute of the dialog box (the rest of the attributes go there): it lets the example find it.
function report(name: string) {
const dialog = document.querySelector(`[data-demo="${name}"]`);
where = `${name}: the dialog is ${box?.contains(dialog) ? 'inside the box' : 'outside the box'}`;
}
</script>
<!-- container: an element, a function that returns one, a selector, or false to leave it where it is written. The layer still covers the window. -->
<div class="stack">
<div class="row">
<Button onclick={() => (selected = true)}>container="#modal-container"</Button>
<Button onclick={() => (inPlace = true)}>container={'{false}'}</Button>
</div>
<div id="modal-container" class="box" bind:this={box}>the Modal goes into this box</div>
<output class="caption">{where}</output>
<Modal closeOnOk bind:open={selected} title="Into the box" container="#modal-container" data-demo="selector" onafteropenchange={(opened) => opened && report('selector')}>
<p>Its layer is a child of the box in the page.</p>
</Modal>
<Modal closeOnOk bind:open={inPlace} title="In place" container={false} data-demo="false" onafteropenchange={(opened) => opened && report('false')}>
<p>Its layer stays where the component is written.</p>
</Modal>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
.row {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.box {
padding: 12px;
border: 1px dashed var(--ads-color-border);
border-radius: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
<script lang="ts">
import { Button, Modal } from 'svelte-ant-design';
let open = $state(false);
</script>
<!-- class and style go on the dialog box; wrapClassName on the scroll area; classNames and styles address the parts. -->
<div class="stack">
<Button onclick={() => (open = true)}>Styled</Button>
<Modal
bind:open
title="Styled"
closeOnOk
class="fancy-box"
style="width: 480px"
wrapClassName="fancy-wrap"
classNames={{ header: 'fancy-header' }}
styles={{ title: { 'letter-spacing': '0.04em' }, body: { color: 'var(--ads-color-primary)' } }}
>
<p>The parts: root, mask, wrapper, dialog, content, close, header, title, body and footer.</p>
</Modal>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
/* Classes of this example: they are passed to the Modal, which is drawn in the body, so the rules are global. */
:global(.fancy-box) {
outline: 2px dashed var(--ads-color-primary);
outline-offset: 4px;
}
:global(.fancy-wrap) {
background: rgba(22, 119, 255, 0.06);
}
:global(.fancy-header) {
padding-bottom: 8px;
border-bottom: 1px solid var(--ads-color-border);
}
</style>
focusable: trap false
focusable: focusTriggerAfterClose false
focusable: autoFocusButton ok
<script lang="ts">
import { Button, Modal } from 'svelte-ant-design';
let free = $state(false);
let lost = $state(false);
let primary = $state(false);
</script>
<div class="stack">
<div class="field">
<span class="caption">focusable: trap false</span>
<Button onclick={() => (free = true)}>Tab may leave the dialog</Button>
<Modal closeOnOk bind:open={free} title="No trap" focusable={{ trap: false }}><p>Tab is left to the browser. The page behind is still out of reach.</p></Modal>
</div>
<div class="field">
<span class="caption">focusable: focusTriggerAfterClose false</span>
<Button onclick={() => (lost = true)}>Focus does not come back</Button>
<Modal closeOnOk bind:open={lost} title="No return" focusable={{ focusTriggerAfterClose: false }}><p>After it closes, focus stays where it is.</p></Modal>
</div>
<div class="field">
<span class="caption">focusable: autoFocusButton ok</span>
<Button onclick={() => (primary = true)}>Focus on OK</Button>
<Modal bind:open={primary} title="Focus on OK" focusable={{ autoFocusButton: 'ok' }} onok={() => (primary = false)}><p>The OK button has focus when it opens.</p></Modal>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
<script lang="ts">
import type { Snippet } from 'svelte';
import { Button, Modal } from 'svelte-ant-design';
let open = $state(false);
</script>
<!-- modalRender gets the card as a snippet and may wrap it, for example in a draggable box. -->
{#snippet framed(card: Snippet)}
<div class="frame">
<span class="tag">wrapped by modalRender</span>
{@render card()}
</div>
{/snippet}
<div class="stack">
<Button onclick={() => (open = true)}>modalRender</Button>
<Modal closeOnOk bind:open title="Wrapped" modalRender={framed}><p>The card is inside a box of your own.</p></Modal>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.frame {
padding: 8px;
border: 1px dashed var(--ads-color-primary);
border-radius: 12px;
}
.tag {
display: block;
margin-bottom: 4px;
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
<script lang="ts">
import { Button, Modal, Select, type SelectValue } from 'svelte-ant-design';
let open = $state(false);
let city = $state<SelectValue | undefined>('lisbon');
</script>
<!-- The list of a Select opens above the Modal, Escape closes the list first and the Modal on the second press, and the list follows the field when the Modal scrolls. -->
<div class="stack">
<Button onclick={() => (open = true)}>A form with a Select</Button>
<Modal bind:open title="Where to?" onok={() => (open = false)}>
<div class="field">
<span class="caption">City</span>
<Select aria-label="City" options={['lisbon', 'porto', 'faro']} bind:value={city} style="width: 200px" />
</div>
</Modal>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
<script lang="ts">
import { Button, modal } from 'svelte-ant-design';
</script>
<!-- A call opens a dialog from code: no markup, no state. confirm has Cancel and OK; info, success, warning and error have OK only (okCancel changes that). -->
<div class="stack">
<div class="row">
<Button onclick={() => modal.confirm({ title: 'Delete the file?', content: 'This cannot be undone.' })}>confirm</Button>
<Button onclick={() => modal.info({ title: 'Heads up', content: 'The service restarts at midnight.' })}>info</Button>
<Button onclick={() => modal.success({ title: 'Saved', content: 'Your changes are stored.' })}>success</Button>
<Button onclick={() => modal.warning({ title: 'Careful', content: 'The disk is almost full.' })}>warning</Button>
<Button onclick={() => modal.error({ title: 'Failed', content: 'The upload did not finish.', okText: 'Got it' })}>error</Button>
<Button onclick={() => modal.info({ title: 'Update available', content: 'Install it now?', okCancel: true, okText: 'Install', cancelText: 'Later' })}>info + okCancel</Button>
<Button onclick={() => modal.info({ title: 'From the click', content: 'This dialog zooms from the place of the click.', zoomFromClick: true })}>info + zoomFromClick</Button>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
</style>
<script lang="ts">
import { Button, modal } from 'svelte-ant-design';
let answer = $state('');
// The call returns a handle that can be awaited: true for OK, false for Cancel, Escape and destroy.
// The trap: `await modal.confirm(...)` waits until the dialog is closed, and so does returning the handle from an async function.
async function ask() {
const confirmed = await modal.confirm({ title: 'Publish the post?', content: 'It will be visible to everyone.' });
answer = confirmed ? 'You pressed OK' : 'You cancelled';
}
</script>
<div class="stack">
<Button onclick={ask}>Ask</Button>
<output class="caption">{answer}</output>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
<script lang="ts">
import { Button, modal } from 'svelte-ant-design';
let tries = $state(0);
// onok may return a Promise: the OK button waits (it is loading), a resolve closes the dialog, a reject keeps it open.
function save() {
modal.confirm({
title: 'Save the changes?',
content: 'The first try always fails.',
onok: () =>
new Promise<void>((resolve, reject) =>
setTimeout(() => {
tries += 1;
return tries % 2 === 1 ? reject(new Error('failed')) : resolve();
}, 1200)
)
});
}
// oncancel may return a Promise too: the Cancel button waits, and the dialog closes when it is resolved.
function leave() {
modal.confirm({ title: 'Discard the draft?', content: 'Cancel takes a moment.', oncancel: () => new Promise((resolve) => setTimeout(resolve, 1000)) });
}
</script>
<div class="stack">
<div class="row">
<Button onclick={save}>OK waits for a Promise</Button>
<Button onclick={leave}>Cancel waits for a Promise</Button>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
</style>
<script lang="ts">
import { Button, modal } from 'svelte-ant-design';
// The handle changes the dialog in place (an object is merged, a function gets the previous options) and closes it.
function countdown() {
let seconds = 5;
const handle = modal.success({ title: 'Done', content: `This closes in ${seconds} seconds.`, okText: 'Close now' });
const timer = setInterval(() => {
seconds -= 1;
if (seconds <= 0) {
clearInterval(timer);
handle.destroy();
} else {
handle.update({ content: `This closes in ${seconds} seconds.` });
}
}, 1000);
void handle.then(() => clearInterval(timer));
}
function several() {
modal.info({ title: 'One' });
modal.warning({ title: 'Two' });
modal.error({ title: 'Three' });
}
</script>
<div class="stack">
<div class="row">
<Button onclick={countdown}>update and destroy</Button>
<Button onclick={several}>Open three</Button>
<Button onclick={() => modal.destroyAll()}>destroyAll</Button>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
</style>
<script lang="ts">
import { Button, ModalHost, modal } from 'svelte-ant-design';
// With no <ModalHost /> the library makes its own layer. A host draws the dialogs where it stands, so they see the context of that place.
let hosted = $state(false);
</script>
{#snippet where()}<span>This dialog was drawn {hosted ? 'by the host' : 'by the layer of the library'}.</span>{/snippet}
<div class="stack">
<div class="field">
<label class="row"><input type="checkbox" bind:checked={hosted} /> <span class="caption">Put a ModalHost in this example</span></label>
<Button onclick={() => modal.info({ title: 'Where am I?', content: where })}>Open a dialog</Button>
</div>
{#if hosted}<ModalHost />{/if}
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
API
Props of Modal
| Property | Description | Type | Default |
|---|---|---|---|
| open | Whether the Modal is shown. bind:open works: a way to close sets it to false. | boolean | false |
| title | The title. No title, no header. 0 is shown. | string | number | Snippet | null | — |
| children | The body. | Snippet | — |
| footer | null removes the footer; a text is the footer; a snippet gets origin (both buttons), ok and cancel. | string | number | Snippet<[{ origin, ok, cancel }]> | null | Cancel and OK |
| okText | Text of the OK button. When it is left out the default is used; an empty string stays empty. | string | number | Snippet | OK |
| cancelText | Text of the Cancel button. | string | number | Snippet | Cancel |
| okType | The type of the OK button. | ButtonType | 'primary' |
| okButtonProps | Props of the OK button. Its onclick runs before onok. | ComponentProps<typeof Button> | — |
| cancelButtonProps | Props of the Cancel button. Its onclick runs before oncancel. | ComponentProps<typeof Button> | — |
| confirmLoading | The OK button is loading. | boolean | false |
| onok | The OK button was pressed. The Modal closes only with closeOnOk. | (event: MouseEvent) => unknown | — |
| closeOnOk | The Modal closes after onok, waiting for a Promise it returned: a resolve closes it, a reject keeps it open; event.preventDefault() keeps it open. Ant Design has no such option on the plain Modal. | boolean | false |
| oncancel | The close button, Cancel, Escape or the mask asked to close. The Modal then closes unless the handler calls event.preventDefault(). | (event: MouseEvent | KeyboardEvent) => void | — |
| closable | Shows the close button. An object sets its icon, disables it, or names it (default “Close”). | boolean | { closeIcon?, disabled?, 'aria-label'? } | true |
| closeIcon | Replaces the cross. Hide the button with closable: false. | Snippet | a cross |
| mask | enabled: false (or false) draws no mask: the Modal is modal all the same. closable: false stops a press on the mask from closing it. | boolean | { enabled?, closable? } | true |
| keyboard | Escape closes. | boolean | true |
| centered | Centers the dialog in the window. | boolean | false |
| width | A number is px. Never wider than the window. | string | number | 520 |
| zIndex | The z-index of the layer (--ads-modal-z-index), under the popup layers. | number | 1000 |
| container | The element the layer is moved into, a function that returns it, a selector, or false to leave it where it is written. | HTMLElement | (() => HTMLElement) | string | false | document.body |
| destroyOnHidden | The content is removed on every close. | boolean | false |
| forceRender | The Modal is in the page, hidden, before the first opening. | boolean | false |
| focusable | trap: Tab stays inside. focusTriggerAfterClose: focus goes back to what had it. autoFocusButton: 'ok', 'cancel' or null (the dialog); by default the first element Tab reaches. | { trap?, focusTriggerAfterClose?, autoFocusButton? } | { trap: true, focusTriggerAfterClose: true } |
| scrollLock | The page does not scroll while the Modal is open. | boolean | true |
| zoomFromClick | The dialog zooms in and out from the place of the last click, as in Ant Design, instead of around its own center. A keyboard open, or one that comes later than the click, still starts at the center. | boolean | false |
| modalRender | Gets the card as a snippet and may wrap it. | Snippet<[Snippet]> | — |
| class | Extra classes of the dialog box. | string | — |
| style | Inline style of the dialog box. The width is kept. | string | — |
| wrapClassName | Extra classes of the scroll area (the same part as classNames.wrapper). | string | — |
| classNames | Extra classes for the parts of the Modal. | { root?, mask?, wrapper?, dialog?, content?, close?, header?, title?, body?, footer? } | — |
| styles | Inline styles for the parts: an object of CSS properties (kebab-case or custom properties). | { root?, mask?, wrapper?, dialog?, content?, close?, header?, title?, body?, footer? } | — |
| onafteropenchange | The enter or the leave animation has ended. | (open: boolean) => void | — |
| onafterclose | The leave animation has ended and the Modal is gone. | () => void | — |
Options of a static dialog
| Property | Description | Type | Default |
|---|---|---|---|
| title | The title. | string | number | Snippet | — |
| content | The text under the title. | string | number | Snippet | — |
| icon | A snippet or a text replaces the icon of the kind; false removes it. | Snippet | string | false | the icon of the kind |
| okCancel | confirm has Cancel and OK; the others OK only. This changes that. | boolean | true for confirm |
| footer | As in Modal. | string | number | Snippet<[{ origin, ok, cancel }]> | null | the buttons |
| okText | Text of the OK button. | string | number | Snippet | OK |
| cancelText | Text of the Cancel button. | string | number | Snippet | Cancel |
| okType | The type of the OK button. | ButtonType | 'primary' |
| okButtonProps | Props of the OK button. | ComponentProps<typeof Button> | — |
| cancelButtonProps | Props of the Cancel button. | ComponentProps<typeof Button> | — |
| onok | OK was pressed. A Promise keeps the button loading: a resolve closes the dialog, a reject keeps it open. A static dialog always closes after onok (closeOnOk is on). | (event: MouseEvent) => unknown | — |
| oncancel | Cancel, Escape or another way to cancel. A Promise is waited for in the same way; event.preventDefault() keeps the dialog open. | (event: MouseEvent | KeyboardEvent) => unknown | — |
| closable | Shows the close button (there is none by default). | boolean | { closeIcon?, disabled?, 'aria-label'? } | false |
| closeIcon | Replaces the cross. | Snippet | a cross |
| mask | A press on the mask does not close a static dialog unless closable is true. | boolean | { enabled?, closable? } | { closable: false } |
| keyboard | Escape closes (it is a cancel). | boolean | true |
| centered | Centers the dialog. | boolean | false |
| width | A number is px. | string | number | 416 |
| zIndex | The z-index of the layer. | number | 1000 |
| container | Where the layer is moved to. | HTMLElement | (() => HTMLElement) | string | false | document.body |
| focusable | Focus goes to the OK button by default. | { trap?, focusTriggerAfterClose?, autoFocusButton? } | { autoFocusButton: 'ok' } |
| scrollLock | The page does not scroll while the dialog is open. | boolean | true |
| zoomFromClick | The dialog zooms in and out from the place of the last click instead of around its own center. | boolean | false |
| class | Extra classes of the dialog box. | string | — |
| style | Inline style of the dialog box. | string | — |
| wrapClassName | Extra classes of the scroll area. | string | — |
| classNames | Extra classes for the parts, and for the three parts of a static dialog: confirmBody, icon and description. | { …the parts of the Modal, confirmBody?, icon?, description? } | — |
| styles | Inline styles for the same parts. | { …the parts of the Modal, confirmBody?, icon?, description? } | — |
| onafteropenchange | The enter or the leave animation has ended. | (open: boolean) => void | — |
| onafterclose | The dialog is gone. | () => void | — |
Methods, the handle and ModalHost
| Property | Description | Type | Default |
|---|---|---|---|
| modal.confirm(options) | Opens a dialog with Cancel and OK and the warning icon. | ModalHandle | — |
| modal.info(options) · success · warning · error | Open a dialog with OK only and the icon of the kind. | ModalHandle | — |
| modal.destroyAll() | Closes every static dialog (never a Modal written in the markup). | void | — |
| handle.destroy() | Closes the dialog with the leave animation; the handle resolves false. Does nothing once it is closed. | void | — |
| handle.update(patch) | Changes the options in place. An object is merged into them; a function gets the previous options and returns the changes. Does nothing after a close. | void | — |
| handle.then · await handle | Resolves when the dialog starts to close: true for OK, false for everything else. Awaiting a call waits for the close. | PromiseLike<boolean> | — |
| <ModalHost /> | Optional. Draws the static dialogs where it stands, so they see the context of that place. One host is active; with none, the library makes its own layer in the body. | component | — |
Differences from Ant Design: the cancel ways close the Modal unless
oncancel calls event.preventDefault() (React leaves closing to you); closeOnOk is new; maskClosable is mask.closable, destroyOnClose is destroyOnHidden and getContainer is container; modal.* and <ModalHost> replace Modal.useModal and contextHolder; static dialogs are alert dialogs and can be awaited. Not taken yet: mask.blur, width by breakpoint, loading (planned for v1.1), and a draggable Modal.Design tokens
| Token | Description | Default |
|---|---|---|
| --ads-modal-z-index | Layer of the Modal, under the popup layers (the popover is 1030). | 1000 |
| --ads-modal-width | Width of a dialog when width is not set. | 520px |
| --ads-modal-top | Distance of the dialog from the top, when it is not centered. | 100px |
| --ads-modal-mask-bg | Color of the mask. | rgba(0, 0, 0, 0.45) |
| --ads-modal-content-bg | Background of the card. In the dark theme #1f1f1f. | #ffffff |
| --ads-modal-radius | Corner radius of the card. | 8px |
| --ads-modal-shadow | Shadow of the card. | 0 6px 16px 0 rgba(0, 0, 0, 0.08), 0 3px 6px -4px rgba(0, 0, 0, 0.12), 0 9px 28px 8px rgba(0, 0, 0, 0.05) |
| --ads-modal-padding | Padding of the card. | 20px 24px |
| --ads-modal-title-color | Color of the title. In the dark theme 85% white. | rgba(0, 0, 0, 0.88) |
| --ads-modal-title-font-size | Font size of the title. | 16px |
| --ads-modal-title-line-height | Line height of the title. | 24px |
| --ads-modal-title-font-weight | Weight of the title. | 600 |
| --ads-modal-header-gap | Gap between the title and the body (and between the title and the text of a static dialog). | 8px |
| --ads-modal-body-color | Color of the body text. In the dark theme 85% white. | rgba(0, 0, 0, 0.88) |
| --ads-modal-footer-margin-top | Gap between the body and the footer. | 12px |
| --ads-modal-footer-gap | Gap between the buttons of the footer. | 8px |
| --ads-modal-close-size | Size of the close button (a target of at least 24px, WCAG 2.5.8). | 32px |
| --ads-modal-close-inset | Distance of the close button from the corner. | 12px |
| --ads-modal-close-color | Color of the cross (3:1 on the card). | rgba(0, 0, 0, 0.45) |
| --ads-modal-close-color-hover | Color of the cross on hover. | rgba(0, 0, 0, 0.88) |
| --ads-modal-close-bg-hover | Background of the close button on hover. | rgba(0, 0, 0, 0.06) |
| --ads-modal-motion-ease | Easing of the zoom-in. | cubic-bezier(0.08, 0.82, 0.17, 1) |
| --ads-modal-confirm-width | Width of a static dialog. | 416px |
| --ads-modal-confirm-icon-size | Size of the icon of a static dialog. | 22px |
| --ads-modal-confirm-icon-gap | Gap between the icon and the text of a static dialog. | 12px |
| --ads-modal-confirm-icon-info | Color of the info icon. | #1677ff |
| --ads-modal-confirm-icon-success | Color of the success icon. tokens-accessible.css darkens it to #389e0d (3:1 on the white card). | #52c41a |
| --ads-modal-confirm-icon-warning | Color of the warning icon and of confirm. tokens-accessible.css darkens it to #c27c00. | #faad14 |
| --ads-modal-confirm-icon-error | Color of the error icon. | #ff4d4f |
Accessibility
- The dialog is
role="dialog"witharia-modal="true", named by its title (aria-labelledby). With no title give it a name witharia-label: a Modal with neither logs a warning while developing. A static dialog isrole="alertdialog"and is described by its text. - Focus moves to the first element Tab reaches (the close button) and stays inside: Tab on the last element goes to the first, Shift+Tab on the first to the last. When the Modal closes, focus goes back to the button that opened it. A static dialog puts focus on the OK button.
- The page behind the Modal is out of reach of the keyboard, the pointer and assistive technology (
inert;aria-hiddenwhere a browser does not knowinert). The Message layer is kept reachable, so a message opened by the OK button is read. - Escape closes the Modal that is on top. With a Select open inside it, Escape closes the list first. A key that ends an IME composition does not close anything.
keyboard: falseturns Escape off. - The page does not scroll and does not jump sideways when its scrollbar goes away. Not handled: iOS Safari before 16 scrolls a locked page under a finger.
- Contrast: the title and the text are about 15:1 on the card, the cross 3:1, and the icons of static dialogs are held to 3:1 (in the dark theme, and in the light theme with
tokens-accessible.css, which darkens the green and the amber). In forced-colors modes the card and the close button have a border, and the mask stays translucent. The animation is off underprefers-reduced-motion, and the handle of a static dialog is answered without it. - Known limit: a Modal opened from inside a popup (for example from the list of a Select) is outside that popup, so a press in it closes the popup and takes the Modal with it. Open it from a button outside, or give it a
container. A message that is already on the page when a Modal opens is kept reachable, but a message added later to the same layer may not be read by every screen reader. - Not checked: a real screen reader (NVDA, JAWS, VoiceOver, a phone), a real touch device, and a real Safari. Checked: the keys, focus and the page behind in Chromium, Firefox and WebKit, and on the lowest supported versions of the three engines; Orca with Firefox is run on GitHub.