Checkbox unstable
A controlled checkbox. Pass the current value as checked and update
it when the checkbox emits Toggled. Your view supplies the markup;
the component adds ARIA and data attributes, handles user input and
supplies the form field.
Checkbox is a headless component, although its Model is empty: it
reads an enclosing CheckboxGroup and reports its toggles to it, which
needs an instance.
Inside a CheckboxGroup, a checkbox with a value is a member and one
with parent: true is the group's parent. The group ticks each from its
value prop, the list the consumer stores from the group's Updated
event. The group itself remembers only the mixed selection that its
parent checkbox brings back.
Anatomy
| Part | Element |
|---|---|
root |
<button role="checkbox"> |
indicator |
<span> inside the root |
Each part takes the consumer's builder and returns it, so the consumer
goes on chaining and adds children with .children(...):
checkbox.root(h.button).children(checkbox.indicator(h.span)). When the checkbox has
a form value to submit, root appends its hidden input after any
children already on the button and before children you chain afterward,
so the form value can't be left out. indicator adds no markup.
A part's result is still a builder, so it can be another kit call's
element, and both share one element.
Naming the checkbox is the consumer's job: a <label> around the root or
one pointing at its id, an aria-label or aria-labelledby on the
button, or text inside it. A Field does it, and adds a description,
an error and the validity state: draw the root on its control,
checkbox.root(field.control(h.button)). A disabled field disables the button, but
the checkbox's other parts follow the disabled prop, so pass
disabled: field.disabled as well.
Data attributes
On root and indicator, each present with an empty value or absent:
data-checked/data-unchecked, neither while indeterminatedata-indeterminatedata-disableddata-readonlydata-required
On root only:
data-parent: the group's parent checkbox
Keyboard
The root is a native <button>, so the browser's defaults do the work:
Space toggles it and Tab moves focus. A click on a <label> that names
it toggles it too. Enter doesn't toggle it, as it doesn't toggle a
native checkbox: the root's keydown handler cancels Enter and leaves
every other key's default alone.
That handler also means the root gets every keydown while it has
focus. A host gives an event only to the innermost element with a
handler for it, so an ancestor's keydown handler never sees Escape
or the arrow keys pressed on the checkbox. To handle them, add a
keydown handler to the root's button, which runs after the checkbox's own.
Forms
Place the checkbox inside the form that should submit its value. A
form attribute on the button does not associate its hidden input
with that form. The required prop sets aria-required and
data-required but does not block submission when unchecked.
Indicator visibility
The indicator stays in the DOM whenever your view renders it. Hide it
with [data-unchecked] or render it only when checkbox.checked || checkbox.indeterminate.
Example A labelled checkbox whose value lives in the consumer's Model
import { UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { Checkbox } from "effect/ui/kit"import { Effect, Schema } from "effect"
const SignupMessage = UiSchema.TaggedEnum({ ToggledTerms: { checked: Schema.Boolean } })
const Signup = Html.component({ name: "Signup", model: Schema.Struct({ terms: Schema.Boolean }), message: SignupMessage}).make({ init: () => ({ terms: false }), update: { ToggledTerms: ($, { checked }) => $.evolve({ terms: checked }) }, view: (h, model) => Checkbox(h, { props: { checked: model.terms, name: "terms", required: true }, onEmit: { Toggled: ({ checked }) => SignupMessage.ToggledTerms({ checked }) }, view: (checkbox) => h.label.class("terms").children( checkbox.root(h.button.class("checkbox")).children(checkbox.indicator(h.span.class("tick"))), "I accept the terms" ) })})
// The root keeps the consumer's class and gets the checkbox's role.const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Signup, {}))if (!/<button class="checkbox"[^>]* role="checkbox"/.test(html)) throw new Error("the root isn't a styled checkbox")Components
A headless checkbox. Call it with the consumer's h, the props, and a
view closure that draws the root with checkbox.root(h.button), usually with
checkbox.indicator(h.span) as a child.
Details
The parts also return checked, indeterminate, disabled, readOnly
and required, resolved to booleans, for markup that depends on them.
Inside a CheckboxGroup they are what the group decided.
root(button) and indicator(span) each return the builder they were
given, so the consumer chains classes, attributes, handlers and children
on it. When the checkbox has a form value to submit, root appends its
hidden input after any children already on the button and before
children you chain afterward. A click on a disabled or read-only
checkbox emits nothing.
A handler the consumer adds to the root runs after the checkbox's own.
The checkbox's own attributes win over the ones the consumer set on the
builder before the part: type, role, disabled, the ARIA state
(aria-checked, aria-disabled, aria-readonly, aria-required),
the indicator's aria-hidden="true", which keeps a glyph inside it out
of the checkbox's name, and the data attributes. That includes the ones
a state leaves absent, so a disabled on the consumer's button is
removed unless the disabled prop is set. A part writes them when it is
applied, so an attribute the consumer chains after checkbox.root(...)
or checkbox.indicator(...) is the consumer's to answer for. The root
writes id as the id prop says, and the group's parent adds the
members' ids to the consumer's aria-controls.
The checkbox reads the nearest CheckboxGroup above it, if there is one.
Signature
declare const Checkbox: Headless<{ readonly checked?: boolean; readonly disabled?: boolean; readonly id?: string; readonly indeterminate?: boolean; readonly name?: string; readonly parent?: boolean; readonly readOnly?: boolean; readonly required?: boolean; readonly uncheckedValue?: string; readonly value?: string;}, { readonly _tag: "Toggled"; readonly checked: boolean;}, { checked: boolean; disabled: boolean; indeterminate: boolean; indicator: Part<"span", [], never>; readOnly: boolean; required: boolean; root: Part<"button", [], never>;}, never, never, never, never, never, never, {}, { readonly _tag: "ClickedRoot";} | { readonly _tag: "PressedEnter";}, QueryEntries<{}, {}>>Models
Schemas
CheckboxEvents
unstableToggled asks for the value in checked. Store that value and pass it
back as the checked prop to update the checkbox. Inside a
CheckboxGroup, a member and the parent emit it too, and the group's
Updated carries the list to store.
Signature
declare const CheckboxEvents: TaggedEnum<{ Toggled: TaggedStruct<"Toggled", { readonly checked: Boolean; }>;}>CheckboxProps
unstableProps for controlling the checkbox state and the value it submits.
Details
checked: whether the checkbox is ticked. The consumer owns it, and left out it is unticked. Inside aCheckboxGroup, a member or the parent ignores it: the group decides.indeterminate: shows the checkbox as neither ticked nor unticked, witharia-checked="mixed". It overridescheckedin the ARIA and data attributes, but not in the form value. A click still emits the opposite ofchecked, so clearindeterminatewhen storing it. On a group's parent it adds to the group's own, for a member that is itself the parent of a nested group.disabled: sets the nativedisabledattribute, so the checkbox is skipped by Tab and ignores clicks. A disabledCheckboxGroupdisables every checkbox inside it.readOnly: ignores clicks but stays focusable, and setsaria-readonly.required: setsaria-requiredanddata-required.name,valueanduncheckedValue: the form field. A ticked checkbox submitsvalue,"on"by default. An unticked one submitsuncheckedValue, or nothing when it is unset. A disabled checkbox, or one without aname, submits nothing. Inside aCheckboxGroup,valuemakes the checkbox a member, and a member without anametakes the group's. An unticked checkbox inside a group submits nothing, even withuncheckedValue.parent: makes the checkbox the parent of theCheckboxGrouparound it. It submits nothing.id: the root'sid. A group parent lists this ID, or a generated fallback, inaria-controls. A member preserves an existing element ID when this prop is omitted, but the parent cannot read that ID. For a member drawn onfield.control, pass the sameidtoFieldandCheckboxso both the label and parent reference the control. Outside a group, the root writesidonly when this prop is set.
Signature
declare const CheckboxProps: Struct<{ readonly checked: optional<Boolean>; readonly disabled: optional<Boolean>; readonly id: optional<String>; readonly indeterminate: optional<Boolean>; readonly name: optional<String>; readonly parent: optional<Boolean>; readonly readOnly: optional<Boolean>; readonly required: optional<Boolean>; readonly uncheckedValue: optional<String>; readonly value: optional<String>;}>