Skip to content

Dialog

A modal window over the page, for a task the user should finish or cancel before going on. Same markup and props in every style: switch the style in the header to see it change.

Basic

A short form. In HTML it's the browser's <dialog>: commandfor opens it, form method=dialog closes it on submit.

Edit profile

Change your public details. Save when you're done.

dialog-basic.html
<button type="button" class="vin-btn vin-btn--outline" commandfor="dlg-profile" command="show-modal">Edit profile</button>
<dialog class="vin-dialog" id="dlg-profile" aria-labelledby="dlg-profile-title" aria-describedby="dlg-profile-desc" closedby="any">
  <div class="vin-dialog__header">
    <h2 class="vin-dialog__title" id="dlg-profile-title">Edit profile</h2>
    <p class="vin-dialog__description" id="dlg-profile-desc">Change your public details. Save when you're done.</p>
  </div>
  <form method="dialog">
    <div class="vin-dialog__body">
      <div class="vin-field">
        <label class="vin-label" for="dlg-name">Name</label>
        <input id="dlg-name" class="vin-input" value="Ada Lovelace" autocomplete="name">
      </div>
      <div class="vin-field">
        <label class="vin-label" for="dlg-user">Username</label>
        <input id="dlg-user" class="vin-input" value="@ada" autocomplete="username">
      </div>
    </div>
    <div class="vin-dialog__footer">
      <button type="button" class="vin-btn vin-btn--outline" commandfor="dlg-profile" command="close">Cancel</button>
      <button type="submit" class="vin-btn" value="save">Save changes</button>
    </div>
  </form>
  <button type="button" class="vin-btn vin-btn--ghost vin-btn--icon vin-dialog__close" commandfor="dlg-profile" command="close" aria-label="Close"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg></button>
</dialog>

Message

A note with one button.

Your site is live

Anyone with the link can see it now. It can take a minute to show up everywhere.

dialog-message.html
<button type="button" class="vin-btn" commandfor="dlg-done" command="show-modal">Publish</button>
<dialog class="vin-dialog" id="dlg-done" aria-labelledby="dlg-done-title" aria-describedby="dlg-done-desc" closedby="any">
  <div class="vin-dialog__header">
    <h2 class="vin-dialog__title" id="dlg-done-title">Your site is live</h2>
    <p class="vin-dialog__description" id="dlg-done-desc">Anyone with the link can see it now. It can take a minute to show up everywhere.</p>
  </div>
  <div class="vin-dialog__footer">
    <button type="button" class="vin-btn" commandfor="dlg-done" command="close">Got it</button>
  </div>
  <button type="button" class="vin-btn vin-btn--ghost vin-btn--icon vin-dialog__close" commandfor="dlg-done" command="close" aria-label="Close"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg></button>
</dialog>

Long content

The dialog scrolls inside; the page behind it stays put.

Terms of use

VinUI is free and MIT licensed. You can use it in personal and commercial work, change it, and ship it in your own products.

Keep the licence notice in copies of the source. You don't need to credit VinUI in your app, though a link is always welcome.

The components come as they are, without a warranty. Test them in your own product before you ship.

Fonts and icons used in the examples have their own licences. Check them before you use them.

When the content is longer than the screen, the dialog scrolls inside and the page behind it stays put.

dialog-scroll.html
<button type="button" class="vin-btn vin-btn--outline" commandfor="dlg-terms" command="show-modal">Read the terms</button>
<dialog class="vin-dialog" id="dlg-terms" aria-labelledby="dlg-terms-title" closedby="any">
  <div class="vin-dialog__header">
    <h2 class="vin-dialog__title" id="dlg-terms-title">Terms of use</h2>
  </div>
  <div class="vin-dialog__body">
    <p>VinUI is free and MIT licensed. You can use it in personal and commercial work, change it, and ship it in your own products.</p>
    <p>Keep the licence notice in copies of the source. You don't need to credit VinUI in your app, though a link is always welcome.</p>
    <p>The components come as they are, without a warranty. Test them in your own product before you ship.</p>
    <p>Fonts and icons used in the examples have their own licences. Check them before you use them.</p>
    <p>When the content is longer than the screen, the dialog scrolls inside and the page behind it stays put.</p>
  </div>
  <div class="vin-dialog__footer">
    <button type="button" class="vin-btn vin-btn--outline" commandfor="dlg-terms" command="close">Decline</button>
    <button type="button" class="vin-btn" commandfor="dlg-terms" command="close">Accept</button>
  </div>
  <button type="button" class="vin-btn vin-btn--ghost vin-btn--icon vin-dialog__close" commandfor="dlg-terms" command="close" aria-label="Close"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg></button>
</dialog>

Installation

React, Vue and Svelte: add the component to your project with the CLI. It reads your framework from vin.json and also copies any component this one needs. Then import it:

Terminal
npx @vinish/ui add dialog
React
import { Dialog, DialogClose } from "@/components/vin/dialog";
Vue
import Dialog, { DialogClose } from "@/components/vin/dialog.vue";
Svelte
import Dialog from "$lib/components/vin/dialog.svelte";

The import paths assume the default folders: @/components/vin (React and Vue, with the usual @ → src alias) and $lib/components/vin (SvelteKit). Use your own if you chose another folder in init. How the Vue and Svelte versions pass values and content is in Frameworks.

Plain HTML: the classes come with the style's stylesheet. It needs no JavaScript.

API

From spec/dialog.json, which every framework version is tested against.

PropValuesDefaultIn HTML
trigger
The button that opens it.
ReactElement—button[commandfor="id"][command="show-modal"]
title / descriptionReactNode—.vin-dialog__title, .vin-dialog__description (wire them up with aria-labelledby and aria-describedby)
footer
Actions at the end. Wrap a button in DialogClose to make it close the dialog.
ReactNode—.vin-dialog__footer; button[commandfor][command="close"] closes
open / defaultOpen
Use onOpenChange to hear about changes.
boolean——
onSubmit
Wraps the body and footer in a form; a submit button sends it and closes the dialog.
(data: FormData) => void—form[method=dialog]
dismissible
Close on a click outside.
booleantrueclosedby="any" on the dialog
showClosebooleantruebutton.vin-dialog__close

Parts

React, Vue, SvelteHTML classes
<Dialog><DialogClose>.vin-dialog.vin-dialog__header.vin-dialog__title.vin-dialog__description.vin-dialog__body.vin-dialog__footer.vin-dialog__close

Accessibility

  • It's a native modal <dialog>: the page behind is inert, so Tab only reaches the dialog (and the browser's own toolbar). Escape closes it.
  • The title names it (aria-labelledby) and the description describes it (aria-describedby).
  • Focus moves to the first field when it opens, and back to the trigger when it closes. The close button comes last, so it isn't the first stop.
  • A click outside closes it (closedby="any"). Leave that off for forms where losing input would hurt.