AlertDialog unstable
A modal dialog that interrupts the user with something they must answer before going on, such as confirming that a draft is discarded.
AlertDialog is a function that declares a headless Dialog with three
things fixed: the dialog has role="alertdialog", it always opens as a
modal, and a press outside the popup doesn't ask it to close. Everything
else is the Dialog's: the parts, the events, focus, transitions and
nesting. The consumer owns whether it is open, passes it as open, and
stores what the RequestedOpen and RequestedClose events ask for.
Anatomy
| Part | Element |
|---|---|
trigger |
<button> that opens the alert dialog |
portal |
<div> in the top layer, around the parts below |
backdrop |
<div> that covers the page |
viewport |
<div> that positions the popup |
popup |
<div> with the alertdialog role and the content |
title |
a heading, <h1> to <h6>, that labels it |
description |
<p> that describes it |
close |
<button> that closes it |
initialFocus |
any element, focused when it opens |
finalFocus |
any element, focused when it closes |
Each part takes the consumer's builder and returns it with the part
applied, and the consumer adds the children, as for a Dialog. The portal
returns undefined while the alert dialog is closed, unless
keepMounted. Give it a title and a description: the popup is
labelled by the one and described by the other, which a screen reader
announces as the alert dialog opens.
Data attributes
The same as a Dialog's, each present with an empty value or absent:
trigger:data-popup-openbackdrop:data-open/data-closed,data-starting-style,data-ending-styleviewportandpopup: the backdrop's, anddata-nestedanddata-nested-dialog-open
Keyboard
The trigger and the close button are native buttons, so Enter and Space
press them. Escape asks the alert dialog to close, with escape-key.
Focus moves into the popup when it opens, stays inside while it shows, and
goes back to the trigger as soon as it starts closing, as in a modal
Dialog. Put initialFocus on the least destructive answer, such as
Cancel.
Dismissal
The alert dialog asks to close only from its trigger, its close part,
Escape and, on Android, the back gesture, with close-watcher as in a
Dialog. A press outside the popup, on the backdrop included, asks
nothing. Answers other than closing, such as Discard, are the consumer's
buttons, which send the consumer's messages.
An alert dialog drawn inside a Dialog's popup is nested in it, as a
Dialog would be: the outer popup gets data-nested-dialog-open, and only
the alert dialog hears Escape. A DialogTrigger opens one drawn
elsewhere, paired with it by its id. It renders a Dialog instance, so
an alert dialog and a dialog under one parent each take a key, and a
Scene of the consumer names it Dialog.
Example Confirming that a draft is discarded
import { UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { AlertDialog } from "effect/ui/kit"import { Effect, Schema } from "effect"
const EditorMessage = UiSchema.TaggedEnum({ RequestedOpenConfirm: {}, RequestedCloseConfirm: {}, ClickedDiscard: {} })
const Editor = Html.component({ name: "Editor", model: Schema.Struct({ confirming: Schema.Boolean }), message: EditorMessage}).make({ init: () => ({ confirming: true }), update: { RequestedOpenConfirm: ($) => $.evolve({ confirming: true }), RequestedCloseConfirm: ($) => $.evolve({ confirming: false }), ClickedDiscard: ($) => $.evolve({ confirming: false }) }, view: (h, model) => AlertDialog(h, { props: { open: model.confirming }, onEmit: { RequestedOpen: () => EditorMessage.RequestedOpenConfirm(), RequestedClose: () => EditorMessage.RequestedCloseConfirm(), Settled: () => undefined }, view: (alertDialog) => h.div( alertDialog.trigger(h.button).children("Discard draft"), alertDialog.portal(h.div)?.children( alertDialog.popup(h.div.class("popup")).children( alertDialog.title(h.h2).children("Discard draft?"), alertDialog.description(h.p).children("You can't undo this."), alertDialog.initialFocus(alertDialog.close(h.button)).children("Cancel"), h.button.on("click", EditorMessage.ClickedDiscard()).children("Discard") ) ) ) })})
const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Editor, {}))const title = /<h2 id="([^"]+)"/.exec(html)?.[1]if (title === undefined || !/<div[^>]* role="alertdialog"/.test(html) || !html.includes(`aria-labelledby="${title}"`)) { throw new Error("the popup isn't an alert dialog labelled by its title")}Components
AlertDialog
unstableA headless alert dialog. Call it as a Dialog, with the consumer's h, a
key when it needs one, props, onEmit and a view closure that draws
the trigger with alertDialog.trigger and the alert dialog with alertDialog.portal and the
parts inside it.
Details
It declares a Dialog with modal: true and disablePointerDismissal: true, so it emits the Dialog's events. RequestedClose carries
trigger-press, escape-key, close-press or close-watcher, never
outside-press or focus-out.
On top of the Dialog's attributes, the popup's role="alertdialog" wins
over the consumer's, whether the consumer set it on the builder it passes
in or chained it after the part.
Signature
declare function AlertDialog<Msg, RV = never, RF = never>(h: Helper<Msg, unknown>, options: { readonly key?: string; readonly onEmit?: (events: { readonly _tag: "Settled"; readonly open: boolean; } | { readonly _tag: "RequestedOpen"; readonly triggerId: string | null; } | { readonly _tag: "RequestedClose"; readonly reason: "trigger-press" | "outside-press" | "escape-key" | "close-press" | "focus-out" | "close-watcher"; }) => NoInfer<Msg> | undefined | { RequestedClose: (event: { readonly _tag: "RequestedClose"; readonly reason: "trigger-press" | "outside-press" | "escape-key" | "close-press" | "focus-out" | "close-watcher"; }) => NoInfer<Msg> | undefined; RequestedOpen: (event: { readonly _tag: "RequestedOpen"; readonly triggerId: string | null; }) => NoInfer<Msg> | undefined; Settled: (event: { readonly _tag: "Settled"; readonly open: boolean; }) => NoInfer<Msg> | undefined; }; readonly onFailure?: (failure: Failure<never, unknown>) => NoInfer<Msg> | undefined;} & { readonly props: { readonly finalFocus?: boolean; readonly forceRenderBackdrop?: boolean; readonly holdExit?: boolean; readonly id?: string; readonly initialFocus?: boolean; readonly keepMounted?: boolean; readonly nested?: boolean; readonly open: boolean; readonly openMethod?: "mouse" | "touch" | "pen" | "keyboard" | null; readonly triggerId?: string | null; };} & { readonly fallback?: (result: Failure<never, unknown>) => Content<NoInfer<Msg>, RF>; readonly view: (parts: { backdrop: <B extends PartTarget<"div">>(element: B) => ReturnType<B["attr"]> | undefined & Pick<Part<"div", [], never>, "~R">; close: Part<"button", [], never>; closeMethod: "mouse" | "touch" | "pen" | "keyboard" | null; description: <B extends PartTarget<"p">>(element: B) => ReturnType<B["attr"]> & Pick<Part<"p", [id: string], never>, "~R">; finalFocus: Part<string, [], never>; initialFocus: Part<string, [], never>; mounted: boolean; nested: boolean; nestedDialogs: number; open: boolean; openMethod: "mouse" | "touch" | "pen" | "keyboard" | null; popup: Part<"div", [], never>; portal: <B extends PartTarget<"div">>(element: B) => ReturnType<B["attr"]> | undefined & Pick<Part<"div", [], never>, "~R">; title: <B extends PartTarget<"h1" | "h2" | "h3" | "h4" | "h5" | "h6">>(element: B) => ReturnType<B["attr"]> & Pick<Part<"h1" | "h2" | "h3" | "h4" | "h5" | "h6", [id: string], never>, "~R">; transitionStatus: DialogTransitionStatus; trigger: <B extends PartTarget<"button">>(element: B) => ReturnType<B["attr"]> & Pick<Part<"button", [triggerId: string | null, key: string, disabled: boolean], never>, "~R">; triggerId: string | null; viewport: Part<"div", [], never>; }) => Content<NoInfer<Msg>, RV>;}): Child<Msg>Models
Schemas
AlertDialogProps
unstableControls whether the alert dialog is open and where focus goes: a
Dialog's props without modal and disablePointerDismissal, which an
alert dialog fixes, and without topLayer.
Details
open: whether the alert dialog shows. Required: the consumer owns it.triggerId,keepMounted,forceRenderBackdrop,initialFocus,finalFocus,id,openMethodandholdExit: as for aDialog.
An alert dialog is always modal, so it always renders in the top layer.
Signature
declare const AlertDialogProps: Struct<{ readonly finalFocus: optional<Boolean>; readonly forceRenderBackdrop: optional<Boolean>; readonly holdExit: optional<Boolean>; readonly id: optional<String>; readonly initialFocus: optional<Boolean>; readonly keepMounted: optional<Boolean>; readonly nested: optional<Boolean>; readonly open: Boolean; readonly openMethod: optional<NullOr<Literals<readonly ["mouse", "touch", "pen", "keyboard"]>>>; readonly triggerId: optional<NullOr<String>>;}>