A modal window with a title, a description and actions.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Button } from "./ui/uicontext/components/Button.ts"import { Dialog, DialogFooter, DialogHeader } from "./ui/uicontext/components/Dialog.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ body: { display: "grid", gap: 20 },})const Message = UiSchema.TaggedEnum({ ClickedArchive: {}, ClickedCancel: {}, ClickedConfirm: {},})export const ArchiveDialog = Html.component({ name: "ArchiveDialog", model: Schema.Struct({ open: Schema.Boolean, archived: Schema.Boolean }), message: Message,}).make({ init: () => ({ open: false, archived: false }), update: { ClickedArchive: ($) => $.evolve({ open: true }), ClickedCancel: ($) => $.evolve({ open: false }), ClickedConfirm: ($) => $.evolve({ open: false, archived: true }), }, view: (h, { open, archived }) => h.div.children( Button(h, { emphasis: "outline" }).on("click", Message.ClickedArchive()).children(archived ? "Archived" : "Archive project…"), Dialog(h, { open, onClose: Message.ClickedCancel(), labelledBy: "archive-title", describedBy: "archive-description", size: "sm", children: [ h.div.attrs(sx(styles.body)).children( DialogHeader(h, { title: "Archive this project?", titleId: "archive-title", description: "Its issues and run history stay available to search. You can restore it any time.", descriptionId: "archive-description", }), DialogFooter(h, [ Button(h, { emphasis: "outline" }).on("click", Message.ClickedCancel()).children("Cancel"), Button(h, { tone: "danger" }).on("click", Message.ClickedConfirm()).children("Archive"), ]), ), ], }), ),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/dialogUsage
import { Button } from "./ui/uicontext/components/Button.ts"import { Dialog, DialogFooter, DialogHeader } from "./ui/uicontext/components/Dialog.ts"const Message = UiSchema.TaggedEnum({ ClickedOpen: {}, ClickedClose: {} })// `open` lives in your Model: ClickedOpen sets it, ClickedClose clears itexport const ShareDialog = (h: Html.H<typeof Message.Type>, open: boolean) => h.div.children( Button(h).on("click", Message.ClickedOpen()).children("Share"), Dialog(h, { open, onClose: Message.ClickedClose(), labelledBy: "share-title", children: [ DialogHeader(h, { title: "Share this view", titleId: "share-title" }), DialogFooter(h, [Button(h).on("click", Message.ClickedClose()).children("Done")]), ], }), )Examples
With a form
Enter submits. An invalid email keeps the dialog open and says what to fix.
No invitations sent yet.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Button } from "./ui/uicontext/components/Button.ts"import { Dialog, DialogBody, DialogFooter, DialogHeader } from "./ui/uicontext/components/Dialog.ts"import { FormField, Input, Select } from "./ui/uicontext/components/FormControls.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ stack: { display: "grid", justifyItems: "center", gap: 12 }, form: { display: "grid", gap: 16 }, note: { margin: 0, color: color.muted, fontSize: font.sm },})const Role = Schema.Literals(["member", "admin", "guest"])const Message = UiSchema.TaggedEnum({ ClickedInvite: {}, ClickedCancel: {}, ChangedEmail: { email: Schema.String }, PickedRole: { role: Schema.String }, Submitted: {},})const problemWith = (email: string) => (/^[^\s@]+@[^\s@]+\.[^\s@]+$/u.test(email.trim()) ? undefined : "Enter an email address, like [email protected].")export const InviteDialog = Html.component({ name: "InviteDialog", model: Schema.Struct({ open: Schema.Boolean, email: Schema.String, role: Role, tried: Schema.Boolean, invited: Schema.NullOr(Schema.String), }), message: Message,}).make({ init: () => ({ open: false, email: "", role: "member", tried: false, invited: null }), update: { ClickedInvite: ($) => $.evolve({ open: true, email: "", role: "member", tried: false }), ClickedCancel: ($) => $.evolve({ open: false }), ChangedEmail: ($, { email }) => $.evolve({ email }), PickedRole: ($, { role }) => (Schema.is(Role)(role) ? $.evolve({ role }) : $.model), // the dialog stays open until the form is valid Submitted: ($) => problemWith($.model.email) === undefined ? $.evolve({ open: false, invited: `${$.model.email.trim()} as ${$.model.role}` }) : $.evolve({ tried: true }), }, view: (h, { open, email, role, tried, invited }) => { const error = tried ? problemWith(email) : undefined return h.div.attrs(sx(styles.stack)).children( Button(h, { tone: "primary" }).on("click", Message.ClickedInvite()).children("Invite people"), h.p.attrs({ role: "status", ...sx(styles.note) }).children(invited === null ? "No invitations sent yet." : `Invited ${invited}.`), Dialog(h, { open, onClose: Message.ClickedCancel(), labelledBy: "invite-title", describedBy: "invite-description", size: "sm", children: [ h.form.attrs({ novalidate: "", ...sx(styles.form) }).on("submit", Message.Submitted()).children( DialogHeader(h, { title: "Invite to Acme Studio", titleId: "invite-title", description: "They'll get an email with a link to join.", descriptionId: "invite-description", }), DialogBody(h, [ FormField(h, { id: "invite-email", label: "Email", ...(error === undefined ? {} : { error }) }, (attrs) => Input(h, { invalid: error !== undefined }) .attrs({ ...attrs, type: "email", autocomplete: "off", placeholder: "[email protected]" }) .value(email) .on("input", ({ value }) => Message.ChangedEmail({ email: value }))), FormField(h, { id: "invite-role", label: "Role" }, (attrs) => Select(h).attrs(attrs).value(role).on("change", ({ value }) => Message.PickedRole({ role: value })).children( h.option.attr("value", "member").children("Member: create and edit"), h.option.attr("value", "admin").children("Admin: also manage people and billing"), h.option.attr("value", "guest").children("Guest: only what's shared with them"), )), ]), DialogFooter(h, [ Button(h, { emphasis: "outline" }).on("click", Message.ClickedCancel()).children("Cancel"), Button(h, { tone: "primary", type: "submit" }).children("Send invitation"), ]), ), ], }), ) },})Alert
For a destructive confirmation: alert makes it an alertdialog, and focus starts on the safe choice.
“Overdue invoices”, shared with 6 people
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Button } from "./ui/uicontext/components/Button.ts"import { Dialog, DialogFooter, DialogHeader } from "./ui/uicontext/components/Dialog.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ stack: { display: "grid", justifyItems: "center", gap: 12 }, body: { display: "grid", gap: 20 }, note: { margin: 0, color: color.muted, fontSize: font.sm },})const Message = UiSchema.TaggedEnum({ ClickedDelete: {}, ClickedKeep: {}, ClickedConfirm: {},})export const DeleteViewAlert = Html.component({ name: "DeleteViewAlert", model: Schema.Struct({ open: Schema.Boolean, deleted: Schema.Boolean }), message: Message,}).make({ init: () => ({ open: false, deleted: false }), update: { ClickedDelete: ($) => $.evolve({ open: true }), ClickedKeep: ($) => $.evolve({ open: false }), ClickedConfirm: ($) => $.evolve({ open: false, deleted: true }), }, view: (h, { open, deleted }) => h.div.attrs(sx(styles.stack)).children( Button(h, { tone: "danger", emphasis: "soft" }).disabled(deleted).on("click", Message.ClickedDelete()).children("Delete view…"), h.p.attrs({ role: "status", ...sx(styles.note) }).children(deleted ? "“Overdue invoices” was deleted." : "“Overdue invoices”, shared with 6 people"), // an alert dialog waits for an answer: a click outside doesn't dismiss it, Escape still does Dialog(h, { open, alert: true, onClose: Message.ClickedKeep(), labelledBy: "delete-view-title", describedBy: "delete-view-description", size: "sm", children: [ h.div.attrs(sx(styles.body)).children( DialogHeader(h, { title: "Delete “Overdue invoices”?", titleId: "delete-view-title", description: "The 6 people it's shared with lose it too. The invoices themselves aren't deleted.", descriptionId: "delete-view-description", }), DialogFooter(h, [ Button(h, { emphasis: "outline" }).attr("autofocus", "").on("click", Message.ClickedKeep()).children("Keep view"), Button(h, { tone: "danger" }).on("click", Message.ClickedConfirm()).children("Delete view"), ]), ), ], }), ),})API reference
DialogView function
Options
| Name | Type | Default | Description |
|---|---|---|---|
key | string | — | Names the dialog among others in the same view; label (or labelledBy) by default, so give a key when the label changes while the dialog is open |
openrequired | boolean | — | Whether the dialog shows. The caller keeps it. |
onCloserequired | Message | — | Sent for Escape and presses on the dimmed page; the Model closes the dialog, or keeps it open |
label | string | — | The dialog's accessible name, when it has no DialogTitle |
labelledBy | string | — | The ids of the elements that name and describe it, in place of label |
describedBy | string | — | The id of the element that describes it, such as DialogHeader's description. |
alert | boolean | — | An alertdialog: it interrupts to ask for an answer (a confirmation), rather than holding a task |
size | "sm" | "md" | "lg" | md | The dialog's width; md by default. |
childrenrequired | View.Children | — | What the dialog holds: usually DialogHeader, the body and DialogFooter. |
style | Style | — | StyleX styles applied after the dialog's own. |
instant | boolean | — | Opens and closes at once, without fading or growing: for a dialog people open many times a day from the keyboard, such as a command menu. |
DialogHeaderView function
Options
| Name | Type | Description |
|---|---|---|
titlerequired | string | The dialog's title, which names it. |
titleId | string | The title's id, for the dialog's labelledBy. |
description | string | A sentence under the title on what the dialog is for. |
descriptionId | string | The description's id, for the dialog's describedBy. |
Accessibility
- A modal
<dialog>in the top layer: focus moves inside and stays there, and the page behind takes no presses and is hidden from screen readers. - Escape and a click on the dimmed page send
onClose; your Model decides whether it closes. An alert dialog closes only on Escape or its buttons. - Menus opened from inside it and toasts posted while it's open show above it and stay usable.
- Name it with a title and describe it with
labelledByanddescribedBy, or give it alabel.