Dismissal unstable
Lets custom widgets share Dialog's dismissal stack. TrackDismissal
reports close requests; the consumer decides whether to close the widget.
Ignoring a request does not pass it to the layer below. Focus trapping and
focus return remain separate responsibilities.
Layer ordering
Mount TrackDismissal on the widget's element while it is visible. A
nested layer takes precedence over its ancestor; otherwise, the later
mount takes precedence. Dialog, AlertDialog, Popover and Drawer
share this stack. They claim every reason, even when configured to ignore
it, such as an outside press with disablePointerDismissal. An open
Tooltip, PreviewCard, NavigationMenu or root Menu joins it for
escape-key only, so Escape closes it before the dialog around it.
A modal dialog still traps Tab while a custom layer inside it is open.
Each event selects the topmost layer that accepts its reason. Closing that layer during the event does not pass the event to another layer.
Dismissal reasons
escape-key: Escape, unless already prevented or an input method is composing. The selected layer prevents the key's default behavior.outside-press: a primary-button press outside the element. Mouse and pen presses request dismissal on pointerdown; touch waits for a click so scrolling does not dismiss. Scrollbars and toasts do not count.focus-out: focus moving outside the element. Focus moved by the kit or by a claimed outside press, through its click, does not request dismissal from any layer.close-watcher: Android's back gesture in browsers that supportCloseWatcher.
Example An inline editor that keeps unsaved changes
import { Mount, UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { Dismissal } from "effect/ui/kit"import { Effect, Schema } from "effect"
const NoteMessage = UiSchema.TaggedEnum({ RequestedDismiss: { request: Dismissal.Request }})
const Note = Html.component({ name: "Note", model: Schema.Struct({ editing: Schema.Boolean, dirty: Schema.Boolean }), message: NoteMessage}).make({ init: () => ({ editing: true, dirty: false }), update: { // Refuse while there are unsaved changes: the layer below still doesn't hear it. RequestedDismiss: ($) => $.model.dirty ? $.model : $.evolve({ editing: false }) }, view: (h, model) => h.div( h.button.attr("id", "edit").children("Edit"), model.editing ? h.div.class("editor").onMount( Dismissal.TrackDismissal({ insideSelector: "#edit", reasons: ["escape-key", "outside-press"] }).pipe( Mount.map((request) => NoteMessage.RequestedDismiss({ request })) ) ).children(h.input) : null )})
const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Note, {}))if (!html.includes("class=\"editor\"")) throw new Error("the editor isn't drawn")Models
Why a layer is asked to close.
Signature
type Reason = typeof Reason.TypeA request to close a layer.
Signature
type Request = typeof Request.TypeThe args of TrackDismissal.
Signature
type TrackDismissalArgs = typeof TrackDismissalArgs.TypeMounts
trackDismissal
unstableReturns a stream of close requests for node. Running the stream registers
the layer; stopping it removes the layer.
Details
An element accepts one mount. To combine dismissal with positioning, use
Mount.makeStream and merge this stream with Positioning.trackPosition.
Signature
declare function trackDismissal(node: HTMLElement, args: { readonly insideSelector?: string; readonly reasons?: readonly Array<"outside-press" | "escape-key" | "focus-out" | "close-watcher">;}, changes: Stream<{ readonly insideSelector?: string; readonly reasons?: readonly Array<"outside-press" | "escape-key" | "focus-out" | "close-watcher">;}>): Stream<{ readonly pointerType: string; readonly reason: "outside-press" | "escape-key" | "focus-out" | "close-watcher";}>TrackDismissal
unstableRegisters its element in the dismissal stack for the mount's lifetime.
Map each emitted Request to a message so your update handler can decide
whether to close the widget.
Details
Argument updates apply from the next event. See the module documentation for layer ordering and dismissal reasons.
A Scene has no document, so send a Request there with
mounted(Dismissal.TrackDismissal, request).
Signature
declare const TrackDismissal: Factory<{ readonly insideSelector?: string; readonly reasons?: readonly Array<"outside-press" | "escape-key" | "focus-out" | "close-watcher">;}, { readonly pointerType: string; readonly reason: "outside-press" | "escape-key" | "focus-out" | "close-watcher";}, HTMLElement, never>Schemas
Dismissal reasons shared with Dialog's DialogDismissReason.
Signature
declare const Reason: Literals<readonly ["escape-key", "outside-press", "focus-out", "close-watcher"]>A request to close a layer: the reason, and the press's pointer type for
outside-press, "" otherwise.
Signature
declare const Request: Struct<{ readonly pointerType: String; readonly reason: Literals<readonly ["escape-key", "outside-press", "focus-out", "close-watcher"]>;}>TrackDismissalArgs
unstableOptions for TrackDismissal.
Details
insideSelector: elements outside the layer's element that count as inside it, such as its anchor, its trigger or a part drawn elsewhere:[id="${anchorId}"].reasons: accepted reasons, defaulting to all four. Omitted reasons remain available to lower layers.
Signature
declare const TrackDismissalArgs: Struct<{ readonly insideSelector: optional<String>; readonly reasons: optional<$Array<Literals<readonly ["escape-key", "outside-press", "focus-out", "close-watcher"]>>>;}>