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.
<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>import { Dialog, DialogClose } from "@/components/vin/dialog";
import { Button } from "@/components/vin/button";
import { Field } from "@/components/vin/input";
export function DialogBasic() {
return (
<Dialog
trigger={<Button variant="outline">Edit profile</Button>}
title="Edit profile"
description="Change your public details. Save when you're done."
onSubmit={(data) => console.log(Object.fromEntries(data))}
footer={
<>
<DialogClose><Button variant="outline">Cancel</Button></DialogClose>
<Button type="submit" value="save">Save changes</Button>
</>
}
>
<Field label="Name" name="name" defaultValue="Ada Lovelace" autoComplete="name" />
<Field label="Username" name="username" defaultValue="@ada" autoComplete="username" />
</Dialog>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import Dialog, { DialogClose } from "@/components/vin/dialog.vue";
import Field from "@/components/vin/input.vue";
import Button from "@/components/vin/button.vue";
const onSave = (data: FormData) => console.log(Object.fromEntries(data));
</script>
<template>
<Dialog title="Edit profile" description="Change your public details. Save when you're done." @submit="onSave">
<template #trigger><Button variant="outline">Edit profile</Button></template>
<Field label="Name" name="name" default-value="Ada Lovelace" autocomplete="name" />
<Field label="Username" name="username" default-value="@ada" autocomplete="username" />
<template #footer>
<DialogClose><Button variant="outline">Cancel</Button></DialogClose>
<Button type="submit" value="save">Save changes</Button>
</template>
</Dialog>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import Dialog from "$lib/components/vin/dialog.svelte";
import Button from "$lib/components/vin/button.svelte";
import Field from "$lib/components/vin/input-field.svelte";
</script>
<Dialog
title="Edit profile"
description="Change your public details. Save when you're done."
onSubmit={(data) => console.log(Object.fromEntries(data))}
>
{#snippet trigger(props)}<Button variant="outline" {...props}>Edit profile</Button>{/snippet}
<Field label="Name" name="name" value="Ada Lovelace" autocomplete="name" />
<Field label="Username" name="username" value="@ada" autocomplete="username" />
{#snippet footer(close)}
<Button variant="outline" onclick={close}>Cancel</Button>
<Button type="submit" value="save">Save changes</Button>
{/snippet}
</Dialog>Message
A note with one button.
<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>import { Dialog, DialogClose } from "@/components/vin/dialog";
import { Button } from "@/components/vin/button";
export function DialogMessage() {
return (
<Dialog
trigger={<Button>Publish</Button>}
title="Your site is live"
description="Anyone with the link can see it now. It can take a minute to show up everywhere."
footer={<DialogClose><Button>Got it</Button></DialogClose>}
/>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import Dialog, { DialogClose } from "@/components/vin/dialog.vue";
import Button from "@/components/vin/button.vue";
</script>
<template>
<Dialog title="Your site is live" description="Anyone with the link can see it now. It can take a minute to show up everywhere.">
<template #trigger><Button>Publish</Button></template>
<template #footer><DialogClose><Button>Got it</Button></DialogClose></template>
</Dialog>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import Dialog from "$lib/components/vin/dialog.svelte";
import Button from "$lib/components/vin/button.svelte";
</script>
<Dialog title="Your site is live" description="Anyone with the link can see it now. It can take a minute to show up everywhere.">
{#snippet trigger(props)}<Button {...props}>Publish</Button>{/snippet}
{#snippet footer(close)}<Button onclick={close}>Got it</Button>{/snippet}
</Dialog>Long content
The dialog scrolls inside; the page behind it stays put.
<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>import { Dialog, DialogClose } from "@/components/vin/dialog";
import { Button } from "@/components/vin/button";
export function DialogScroll() {
return (
<Dialog
trigger={<Button variant="outline">Read the terms</Button>}
title="Terms of use"
footer={
<>
<DialogClose><Button variant="outline">Decline</Button></DialogClose>
<DialogClose><Button>Accept</Button></DialogClose>
</>
}
>
<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>
</Dialog>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import Dialog, { DialogClose } from "@/components/vin/dialog.vue";
import Button from "@/components/vin/button.vue";
</script>
<template>
<Dialog title="Terms of use">
<template #trigger><Button variant="outline">Read the terms</Button></template>
<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>
<template #footer>
<DialogClose><Button variant="outline">Decline</Button></DialogClose>
<DialogClose><Button>Accept</Button></DialogClose>
</template>
</Dialog>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import Dialog from "$lib/components/vin/dialog.svelte";
import Button from "$lib/components/vin/button.svelte";
</script>
<Dialog title="Terms of use">
{#snippet trigger(props)}<Button variant="outline" {...props}>Read the terms</Button>{/snippet}
<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>
{#snippet footer(close)}
<Button variant="outline" onclick={close}>Decline</Button>
<Button onclick={close}>Accept</Button>
{/snippet}
</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:
npx @vinish/ui add dialogimport { Dialog, DialogClose } from "@/components/vin/dialog";import Dialog, { DialogClose } from "@/components/vin/dialog.vue";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.
| Prop | Values | Default | In HTML |
|---|---|---|---|
triggerThe button that opens it. | ReactElement | — | button[commandfor="id"][command="show-modal"] |
title / description | ReactNode | — | .vin-dialog__title, .vin-dialog__description (wire them up with aria-labelledby and aria-describedby) |
footerActions at the end. Wrap a button in DialogClose to make it close the dialog. | ReactNode | — | .vin-dialog__footer; button[commandfor][command="close"] closes |
open / defaultOpenUse onOpenChange to hear about changes. | boolean | — | — |
onSubmitWraps the body and footer in a form; a submit button sends it and closes the dialog. | (data: FormData) => void | — | form[method=dialog] |
dismissibleClose on a click outside. | boolean | true | closedby="any" on the dialog |
showClose | boolean | true | button.vin-dialog__close |
Parts
| React, Vue, Svelte | HTML 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.
Customize
Saved in this browser. Style and accent are in the header.