HtmlCommand unstable
The HTML Commands: element ref operations (focus, blur,
scrollIntoView, measure, select, requestSubmit, showPicker,
waitForAnimations) and waitForPaint.
The element operations run after the render caused by the issuing update
commits. Synchronous ref operations (focus, blur, scrollIntoView, measure,
select, requestSubmit and showPicker) run in that same task, so the
next browser event sees their DOM updates.
Element operations use Html.Host, which tests can replace with
Layer.succeed(Html.Host, ...). waitForAnimations reads getAnimations
directly from the element. It and waitForPaint wait for frames on
HtmlSubscription.AnimationFrames.
Commands
Takes focus away from the element ref names, and sends f's message.
Signature
declare function blur<N extends HTMLElement | SVGElement, Msg>(ref: Ref<N>, f: (result: Found) => Msg): Issued<Msg, never>The Command behind blur.
Signature
declare const Blur: Command.Factory<{ readonly ref: Renderer.Ref<any>;}, Found>Focuses the element ref names, once the render the issuing update
caused is committed, and sends f's message.
Details
A ref that isn't rendered gives { found: false }. The ref resolves within
the issuing instance. Refs and Html.Host are framework services, so
this adds nothing to a component's requirements.
Signature
declare function focus<N extends HTMLElement | SVGElement, Msg>(ref: Ref<N>, f: (result: Found) => Msg, options?: FocusOptions): Issued<Msg, never>Example Focusing the new-todo field after adding
import { Html, HtmlCommand } from "effect/ui/html"import { UiSchema } from "effect/ui"import { Schema } from "effect"
const Message = UiSchema.TaggedEnum({ SubmittedForm: {}, CompletedFocusNewTodo: {} })const NewTodo = Html.ref("NewTodo", "input")
const command = HtmlCommand.focus(NewTodo, () => Message.CompletedFocusNewTodo())The Command behind focus, for a story's .resolve(HtmlCommand.Focus, { found }).
Signature
declare const Focus: Command.Factory<{ readonly options?: Html.FocusOptions; readonly ref: Renderer.Ref<any>;}, Found>Measures the element ref names, and sends f's message with its box, or
Option.none() when the ref isn't rendered.
Signature
declare function measure<N extends Element, Msg>(ref: Ref<N>, f: (rect: Option<Rect>) => Msg): Issued<Msg, never>The Command behind measure.
Signature
declare const Measure: Command.Factory<{ readonly ref: Renderer.Ref<any>;}, Option.Option<Html.Rect>>requestSubmit
unstableSubmits the form of the element ref names, as a press on its submit
button would, and sends f's message.
Details
ref names a form, or a control that belongs to one, by nesting or by
its form attribute. The browser validates the form first, unless it has
novalidate, and then fires submit, which reaches the form's
submit handler. submitted is false when the element has no form.
Signature
declare function requestSubmit<N extends HTMLElement, Msg>(ref: Ref<N>, f: (result: Submitted) => Msg): Issued<Msg, never>RequestSubmit
unstableThe Command behind requestSubmit.
Signature
declare const RequestSubmit: Command.Factory<{ readonly ref: Renderer.Ref<any>;}, Submitted>scrollIntoView
unstableScrolls the element ref names into view, and sends f's message.
Signature
declare function scrollIntoView<N extends Element, Msg>(ref: Ref<N>, f: (result: Found) => Msg, options?: ScrollOptions): Issued<Msg, never>ScrollIntoView
unstableThe Command behind scrollIntoView.
Signature
declare const ScrollIntoView: Command.Factory<{ readonly options?: Html.ScrollOptions; readonly ref: Renderer.Ref<any>;}, Found>Selects the text in the input or textarea ref names, once the
render the issuing update caused is committed, and sends f's message.
Without options it selects all the text; { start, end } selects that
range, and { start } places the caret.
Details
The field already holds the value the same update set, even over a
rejected edit. The range is in UTF-16 code units, clamped to the value's
length. A field without a selection, such as an email input, is left as
it is and still reports { found: true }. Issue focus if the field
needs focus: only Safari focuses it.
Signature
declare function select<N extends HTMLInputElement | HTMLTextAreaElement, Msg>(ref: Ref<N>, f: (result: Found) => Msg, options?: SelectOptions): Issued<Msg, never>Example Selecting a field's text, or placing the caret
import { Html, HtmlCommand } from "effect/ui/html"import { UiSchema } from "effect/ui"
const Message = UiSchema.TaggedEnum({ CompletedSelectAmount: {} })const Amount = Html.ref("Amount", "input")
const all = HtmlCommand.select(Amount, () => Message.CompletedSelectAmount())const caret = HtmlCommand.select(Amount, () => Message.CompletedSelectAmount(), { start: 2 })The Command behind select.
Signature
declare const Select: Command.Factory<{ readonly options?: Html.SelectOptions; readonly ref: Renderer.Ref<any>;}, Found>showPicker
unstableOpens the picker of the input or select ref names, as a click on
it would, and sends f's message.
When to use
Use to open a file input the view renders from a menu item or a keyboard
shortcut. The files arrive in the input's change handler. To choose files
without rendering an input, use HtmlFiles.pick.
Details
It calls the element's showPicker(), or click() on a file input in a
browser without it. opened is false when the browser refuses, as it
does outside a user's action and for a disabled element. A Command issued
from a click or key press runs within that action.
Signature
declare function showPicker<N extends HTMLInputElement | HTMLSelectElement, Msg>(ref: Ref<N>, f: (result: Opened) => Msg): Issued<Msg, never>Example Opening a rendered file input's picker
import { Html, HtmlCommand } from "effect/ui/html"import { UiSchema } from "effect/ui"import { Schema } from "effect"
const Message = UiSchema.TaggedEnum({ CompletedShowPicker: { opened: Schema.Boolean } })const Attachment = Html.ref("Attachment", "input")
const command = HtmlCommand.showPicker(Attachment, ({ opened }) => Message.CompletedShowPicker({ opened }))ShowPicker
unstableThe Command behind showPicker.
Signature
declare const ShowPicker: Command.Factory<{ readonly ref: Renderer.Ref<any>;}, Opened>waitForAnimations
unstableWaits for finite animations on ref's element and its descendants, then sends
f's message.
When to use
Use to keep an exiting element mounted until its exit transition finishes,
then remove it in update.
Details
After commit, it awaits .finished on the finite animations returned by
element.getAnimations({ subtree: true }), including CSS transitions.
Infinite animations, such as spinners, are skipped. If an animation is
cancelled or replaced, it collects animations again on the next
HtmlSubscription.AnimationFrames frame, unless the element has disconnected.
A missing ref returns { found: false }. An element with no finite animations
or no getAnimations returns { found: true }. If an animation rejects after
the element disconnects, it returns { found: true } without waiting a frame.
Signature
declare function waitForAnimations<N extends Element, Msg>(ref: Ref<N>, f: (result: Found) => Msg): Issued<Msg, never>Example Keeping a panel mounted during its exit animation
import { Html, HtmlCommand } from "effect/ui/html"import { UiSchema } from "effect/ui"import { Schema } from "effect"
const Panel = Html.ref("Panel", "div")const Message = UiSchema.TaggedEnum({ Opened: {}, Closed: {}, ExitSettled: { generation: Schema.Number }})
const Drawer = Html.component({ name: "Drawer", model: Schema.Struct({ phase: Schema.Literals(["closed", "open", "closing"]), generation: Schema.Number }), message: Message}).make({ init: () => ({ phase: "closed" as const, generation: 0 }), update: { Opened: ($) => $.evolve({ phase: "open" as const }), Closed: ($) => { if ($.model.phase !== "open") return $.model const generation = $.model.generation + 1 return $.set({ phase: "closing" as const, generation }).run( HtmlCommand.waitForAnimations(Panel, () => Message.ExitSettled({ generation }) ) ) }, // Ignore a stale settle if the drawer reopened or started a newer close. ExitSettled: ($, { generation }) => $.model.phase === "closing" && generation === $.model.generation ? $.evolve({ phase: "closed" as const }) : $.model }, view: (h, { phase }) => phase === "closed" ? h.div() : h.div.ref(Panel).attr( "data-ending-style", phase === "closing" ? "" : undefined )})WaitForAnimations
unstableThe Command behind waitForAnimations, for a story's
.resolve(HtmlCommand.WaitForAnimations, { found }).
Signature
declare const WaitForAnimations: Command.Factory<{ readonly ref: Renderer.Ref<any>;}, Found>waitForPaint
unstableWaits two animation frames after the render commit, then sends f's message.
When to use
Use when an enter transition needs the from-state (such as a
data-starting-style attribute) to paint before update applies the
to-state. measure and scrollIntoView already run after commit and do not
need this wait.
Details
It waits two frames on HtmlSubscription.AnimationFrames: the renderer
flushes in a microtask rather than inside requestAnimationFrame, so the
first frame arrives before the render's paint.
Gotchas
The default HtmlSubscription.AnimationFrames never fires. A whole-app test
provides TestAnimationFrames.layer and fires two frames; a Story or
HtmlScene resolves the Command with
.resolve(HtmlCommand.WaitForPaint, undefined).
Signature
declare function waitForPaint<Msg>(f: () => Msg): Issued<Msg, never>Example Triggering an enter transition after paint
import { HtmlCommand } from "effect/ui/html"import { UiSchema } from "effect/ui"
const Message = UiSchema.TaggedEnum({ Opened: {}, Painted: {} })
const command = HtmlCommand.waitForPaint(() => Message.Painted())WaitForPaint
unstableThe Command behind waitForPaint, for a story's
.resolve(HtmlCommand.WaitForPaint, undefined).
Signature
declare const WaitForPaint: Command.Factory<{}, void>Models
What focus, blur, scrollIntoView and select report: whether the
ref was rendered.
Signature
interface Found { readonly found: boolean;}What showPicker reports: whether the ref was rendered, and whether the
browser opened its picker.
Signature
interface Opened extends Found { readonly opened: boolean;}What requestSubmit reports: whether the ref was rendered, and whether
it had a form to submit.
Signature
interface Submitted extends Found { readonly submitted: boolean;}