TestHtmlHost unstable
A test Html.Host that tracks focus on TestHost and sends focus events.
HtmlCommand.focus moves focus to an attached, focusable element. Its
target must support focus and be neither disabled, hidden nor inert. The
previous element receives blur and focusout; the new element receives
focus and focusin, with BrowserHost payloads. HtmlCommand.blur
clears focus only when its target has focus.
Removing the focused element or making it unfocusable clears focus
without events on its tree's next commit. Restoring the element does
not restore focus. Other operations are no-ops: measure returns a
zero-sized box; requestSubmit and showPicker return false.
HtmlPlay creates this host when no Html.Host is provided. Provide
layer() alongside TestHost.layer() to use it with TestHost.mount
or override operations such as measure.
Example Following a focus Command
import { Component } from "effect/ui"import { Html, HtmlCommand } from "effect/ui/html"import { TestHtmlHost } from "effect/ui/html/testing"import { TestHost } from "effect/ui/testing"import { Effect, Layer, Schema } from "effect"
const Field = Html.ref("Field", "input")
const Search = Html.component({ name: "Search", model: Schema.Null, message: Schema.Literal("open")}).make({ init: () => null, update: ($) => $.run(HtmlCommand.focus(Field, () => Component.noop)), view: (h) => h.div( h.button.on("click", "open").children("Search"), h.input.ref(Field).attr("aria-label", "Query") )})
Effect.gen(function*() { const root = yield* TestHost.mount(Search, {}) yield* TestHost.settle root.send("open") yield* TestHost.settle const host = yield* TestHtmlHost.TestHtmlHost host.activeElement()?.tag // => "input"}).pipe( Effect.provide(Layer.merge(TestHost.layer(), TestHtmlHost.layer())), Effect.scoped, Effect.runPromise)Constructors
Creates a test host with focus tracking and optional operation overrides.
Signature
declare function make(overrides?: Partial<Omit<{ readonly blur: (element: Element) => void; readonly focus: (element: Element, options: FocusOptions) => void; readonly measure: (element: Element) => Rect; readonly requestSubmit: (element: Element) => boolean; readonly scrollIntoView: (element: Element, options: ScrollOptions) => void; readonly select: (element: Element, options?: SelectOptions) => void; readonly showPicker: (element: Element) => boolean;}, "focus" | "blur">>): { readonly blur: (element: Element) => void; readonly focus: (element: Element, options: FocusOptions) => void; readonly measure: (element: Element) => Rect; readonly requestSubmit: (element: Element) => boolean; readonly scrollIntoView: (element: Element, options: ScrollOptions) => void; readonly select: (element: Element, options?: SelectOptions) => void; readonly showPicker: (element: Element) => boolean;} & TestHtmlHostLayers
Provides the same host as Html.Host and TestHtmlHost.
Signature
declare function layer(overrides?: Partial<Omit<{ readonly blur: (element: Element) => void; readonly focus: (element: Element, options: FocusOptions) => void; readonly measure: (element: Element) => Rect; readonly requestSubmit: (element: Element) => boolean; readonly scrollIntoView: (element: Element, options: ScrollOptions) => void; readonly select: (element: Element, options?: SelectOptions) => void; readonly showPicker: (element: Element) => boolean;}, "focus" | "blur">>): Layer<Host | TestHtmlHost>Models
Services
TestHtmlHost
unstableAccesses the focus state of the provided test host.
Signature
declare const TestHtmlHost: Service<TestHtmlHost, TestHtmlHost>Provides read access to the test host's focused element.
Signature
interface TestHtmlHost { readonly activeElement: () => TestElement | undefined;}