modal-dialog
A modal dialog on the native <dialog>: the page behind is inert, focus moves into the dialog and back to where it was, and Escape, the close button or a click on the backdrop closes it. open is two-way.
This deletes the project and its 14 files. You can't undo it.
Try it: events appear here (openchange).
Source: modal-dialog.zb, and its registry item.
Install
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/modal-dialog.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/modal-dialog.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/modal-dialog.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/modal-dialog.json
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/modal-dialog.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/modal-dialog.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/modal-dialog.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/modal-dialog.json
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/modal-dialog.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/modal-dialog.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/modal-dialog.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/modal-dialog.json
<link rel="stylesheet" href="https://zubra-js.github.io/zubra-ui/themes/default.css">
<script type="module" src="https://zubra-js.github.io/zubra-ui/cdn/modal-dialog.js"></script>The compiled install writes src/components/zubra/modal-dialog.js and the theme, and adds @zubra/runtime to your dependencies. The one-file install writes the same path as a plain custom element that needs nothing installed (or download it and load it with a <script type="module">); it's smaller on a page with one to three components, the shared runtime with more.
Usage
<button type="button" id="open-modal-dialog">Delete project…</button>
<modal-dialog label="Delete project">
<p>This deletes the project and its 14 files. You can't undo it.</p>
<action-button slot="actions" variant="ghost" data-close>Cancel</action-button>
<action-button slot="actions" variant="destructive" data-close>Delete project</action-button>
</modal-dialog>
<script type="module">
import './components/zubra/modal-dialog.js';
import './components/zubra/action-button.js';
const element = document.querySelector('modal-dialog');
element.open = false;
element.addEventListener('openchange', (event) => console.log('openchange', event.detail));
document.querySelector('#open-modal-dialog').addEventListener('click', () => { element.open = true; });
</script>import './components/zubra/modal-dialog.js';
import './components/zubra/action-button.js';
import { useState } from 'react';
export function ModalDialogExample() {
const [open, setOpen] = useState(false);
return (
<>
<button type="button" onClick={() => setOpen(true)}>Delete project…</button>
<modal-dialog
label="Delete project"
open={open}
onopenchange={(event) => setOpen(event.detail)}
>
<p>This deletes the project and its 14 files. You can't undo it.</p>
<action-button slot="actions" variant="ghost" data-close>Cancel</action-button>
<action-button slot="actions" variant="destructive" data-close>Delete project</action-button>
</modal-dialog>
</>
);
}<script setup>
import { ref } from 'vue';
import './components/zubra/modal-dialog.js';
import './components/zubra/action-button.js';
const open = ref(false);
</script>
<template>
<button type="button" @click="open = true">Delete project…</button>
<modal-dialog
label="Delete project"
:open.prop="open"
@openchange="open = $event.detail"
>
<p>This deletes the project and its 14 files. You can't undo it.</p>
<action-button slot="actions" variant="ghost" data-close>Cancel</action-button>
<action-button slot="actions" variant="destructive" data-close>Delete project</action-button>
</modal-dialog>
</template>
<!-- vite.config.js: vue({ template: { compilerOptions: { isCustomElement: (tag) => tag.includes('-') } } }) --><script>
import './components/zubra/modal-dialog.js';
import './components/zubra/action-button.js';
let open = $state(false);
</script>
<button type="button" onclick={() => (open = true)}>Delete project…</button>
<modal-dialog
label="Delete project"
{open}
onopenchange={(event) => (open = event.detail)}
>
<p>This deletes the project and its 14 files. You can't undo it.</p>
<action-button slot="actions" variant="ghost" data-close>Cancel</action-button>
<action-button slot="actions" variant="destructive" data-close>Delete project</action-button>
</modal-dialog>import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import './components/zubra/modal-dialog.js';
import './components/zubra/action-button.js';
@Component({
selector: 'app-modal-dialog-example',
schemas: [CUSTOM_ELEMENTS_SCHEMA],
template: `
<button type="button" (click)="open = true">Delete project…</button>
<modal-dialog
label="Delete project"
[open]="open"
(openchange)="open = $any($event).detail"
>
<p>This deletes the project and its 14 files. You can't undo it.</p>
<action-button slot="actions" variant="ghost" data-close>Cancel</action-button>
<action-button slot="actions" variant="destructive" data-close>Delete project</action-button>
</modal-dialog>
`,
})
export class ModalDialogExample {
open = false;
}API
| Property | Type | Default |
|---|---|---|
open | boolean | false |
label | string | "Dialog" |
| Event | When |
|---|---|
openchange | open changed; detail is the new value. |
| Slot | For |
|---|---|
(default) | The dialog's content. |
actions | Buttons at the bottom, shown only when given. One with data-close closes the dialog. |
heading | Its title, which labels the dialog (defaults to label). |
Size
1.4 KB gzipped for the component, plus the runtime, which every Zubra component on a page shares (up to 22.1 KB, all of it). On its own, with the parts of the runtime it uses, it's 18.2 KB.