A checkbox with a mixed state, a label and form field wiring.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Checkbox } from "./ui/uicontext/components/Checkbox.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ stack: { display: "grid", gap: 2 }, children: { display: "grid", gap: 2, paddingInlineStart: 26 },})const channels = [ { value: "email", label: "Email" }, { value: "push", label: "Push notifications" }, { value: "sms", label: "Text messages" },] as constconst Message = UiSchema.TaggedEnum({ ToggledAll: { checked: Schema.Boolean }, ToggledChannel: { value: Schema.String, checked: Schema.Boolean },})export const NotificationChannels = Html.component({ name: "NotificationChannels", model: Schema.Struct({ channels: Schema.Array(Schema.String) }), message: Message,}).make({ init: () => ({ channels: ["email"] }), update: { ToggledAll: ($, { checked }) => $.evolve({ channels: checked ? channels.map((c) => c.value) : [] }), ToggledChannel: ($, { value, checked }) => $.evolve({ channels: (list) => (checked ? [...list.filter((v) => v !== value), value] : list.filter((v) => v !== value)) }), }, view: (h, model) => { const all = model.channels.length === channels.length return h.div.attrs(sx(styles.stack)).children( // the parent shows a dash while only some channels are on Checkbox(h, { key: "all", checked: all, indeterminate: model.channels.length > 0 && !all, label: "Notify me everywhere", onToggle: (checked) => Message.ToggledAll({ checked }), }), h.div.attrs(sx(styles.children)).children( channels.map((channel) => Checkbox(h, { key: channel.value, checked: model.channels.includes(channel.value), label: channel.label, onToggle: (checked) => Message.ToggledChannel({ value: channel.value, checked }), }), ), ), ) },})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/checkboxUsage
import { Checkbox } from "./ui/uicontext/components/Checkbox.ts"const Message = UiSchema.TaggedEnum({ ToggledArchive: { checked: Schema.Boolean } })export const ArchiveDone = (h: Html.H<typeof Message.Type>, archive: boolean) => Checkbox(h, { checked: archive, label: "Archive finished issues", onToggle: (checked) => Message.ToggledArchive({ checked }), })Examples
In a form
Inside a FormField, the label, description and error are wired to the box. Submit without ticking it to see the error.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Button } from "./ui/uicontext/components/Button.ts"import { Checkbox } from "./ui/uicontext/components/Checkbox.ts"import { FormField } from "./ui/uicontext/components/FormControls.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ form: { display: "grid", gap: 16, justifyItems: "start", inlineSize: "min(100%, 26rem)" },})const Message = UiSchema.TaggedEnum({ ToggledTerms: { checked: Schema.Boolean }, Submitted: {} })export const TermsField = Html.component({ name: "TermsField", model: Schema.Struct({ terms: Schema.Boolean, tried: Schema.Boolean }), message: Message,}).make({ init: () => ({ terms: false, tried: false }), update: { ToggledTerms: ($, { checked }) => $.evolve({ terms: checked }), Submitted: ($) => $.evolve({ tried: true }), }, view: (h, { terms, tried }) => h.form.attrs({ novalidate: "", ...sx(styles.form) }).on("submit", Message.Submitted()).children( // the field's label names the box, and its description and error are read with it FormField( h, { id: "signup-terms", label: "I accept the terms of service", description: "You can read them any time in Settings.", layout: "inline", ...(tried && !terms ? { error: "Accept the terms to create the workspace." } : {}), }, (field) => Checkbox(h, { checked: terms, required: true, name: "terms", field, onToggle: (checked) => Message.ToggledTerms({ checked }) }), ), Button(h, { tone: "primary", type: "submit" }).children("Create workspace"), ),})Read-only and disabled
Read-only stays in the Tab order and is announced as read-only, for a setting someone else controls. Disabled leaves the Tab order.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Checkbox } from "./ui/uicontext/components/Checkbox.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ row: { display: "flex", flexWrap: "wrap", columnGap: 24, rowGap: 8 },})const Message = UiSchema.TaggedEnum({ ToggledSso: { checked: Schema.Boolean } })export const SecuritySettings = Html.component({ name: "SecuritySettings", model: Schema.Struct({ sso: Schema.Boolean }), message: Message,}).make({ init: () => ({ sso: true }), update: { ToggledSso: ($, { checked }) => $.evolve({ sso: checked }) }, view: (h, { sso }) => { const toggled = (checked: boolean) => Message.ToggledSso({ checked }) return h.div.attrs(sx(styles.row)).children( Checkbox(h, { key: "sso", checked: sso, label: "Require single sign-on", onToggle: toggled }), // an owner set it: members can read and focus it, but not change it Checkbox(h, { key: "two-factor", checked: true, readOnly: true, label: "Two-factor sign-in", onToggle: toggled }), // the plan doesn't include it Checkbox(h, { key: "audit", checked: false, disabled: true, label: "Export audit log", onToggle: toggled }), ) },})API reference
Options
| Name | Type | Default | Description |
|---|---|---|---|
key | string | — | Names the checkbox among others in the same view; needed for more than one |
checkedrequired | boolean | — | Whether it's ticked. The caller keeps it and passes back what onToggle asks for. |
indeterminate | boolean | — | Some of what it stands for is ticked: shown as a dash, announced as mixed |
disabled | boolean | — | Can't be pressed or focused, and is skipped when its form submits. |
readOnly | boolean | — | Shows the value and stays focusable, but a press doesn't change it: a setting only an admin can change. |
required | boolean | — | Must be ticked before its form submits. |
name | string | — | The form field's name: a ticked checkbox submits its value under it |
value | string | on | What a ticked box submits under name; on by default. |
label | string | — | The visible label beside the box; leave it out inside a FormField |
ariaLabel | string | — | The accessible name when there's no visible label (a row's selection box) |
onTogglerequired | (checked: boolean) => Message | — | What a press asks for: the value to store and pass back as checked |
field | FieldControlAttrs | — | A FormField's attributes: its label names the box, its description and error describe it |
style | Style | — | StyleX styles applied after the checkbox’s own. |
Accessibility
- A button with
role="checkbox": Tab reaches it and Space toggles it. A mixed box is announced as mixed. - The visible label names it; without one, give it an
ariaLabel. Inside a field, the field's label, hint and error are tied to it. - A disabled checkbox can't be focused and is skipped when its form submits.