Zubra UI
Code for
GitHub

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.

Cancel Delete project

Try it: events appear here (openchange).

Source: modal-dialog.zb, and its registry item.

Install

Compiled (any project)
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
One file, no dependencies (4.5 KB gzipped)
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
.zb source (to edit it with Zubra)
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
No build: a script tag, straight from this site
HTML
<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

index.html
<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>
Example.jsx
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>
    </>
  );
}
Example.vue
<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('-') } } }) -->
Example.svelte
<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>
example.component.ts
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

Properties (each is also an HTML attribute of the same name, unless marked)
PropertyTypeDefault
openbooleanfalse
labelstring"Dialog"
Events
EventWhen
openchangeopen changed; detail is the new value.
Slots
SlotFor
(default)The dialog's content.
actionsButtons at the bottom, shown only when given. One with data-close closes the dialog.
headingIts 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.

Theme

Every component reads shadcn/ui's CSS variables, so any shadcn theme works. Pick a look or a colour, or paste a theme's CSS.

Look: colours, fonts, corners and motion

Accent colour