Skip to content
// EFFECT DAYS 2026

Switch unstable

A control that turns a setting on or off.

Switch is a view function. It keeps no state, so it takes no key: it owns the ARIA, the data attributes, the form field and the click handling, and the consumer draws the markup in view. The consumer owns the value, passes it as checked, and stores what onToggle reports.

Anatomy

Part Element
root <button role="switch">, with the hidden input
thumb <span> inside the root

Each part takes the consumer's builder and returns it, so the consumer goes on chaining: parts.root(h.button.class("switch")).children(...). root adds the hidden <input> that carries the form value to the button, after the children already on the builder it gets and before those chained on its result, so a switch with a name can't be drawn without it. A part's result is also another kit call's element.

Naming the switch 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, parts.root(field.control(h.button)). A disabled field disables the button, but the switch's other parts follow the disabled prop, so pass disabled: field.disabled as well.

Data attributes

On root and thumb, each present with an empty value or absent:

  • data-checked / data-unchecked
  • data-disabled
  • data-readonly
  • data-required

Keyboard

The root is a native <button>, so Space and Enter toggle it and Tab moves focus, without a key handler. A click on a <label> that names it toggles it too.

Form behavior

The form value is a hidden input inside the root. The required prop sets aria-required and data-required but does not block submission.

Example A labelled switch whose value lives in the consumer's Model

import { UiSchema } from "effect/ui"
import { Html, HtmlServer } from "effect/ui/html"
import { Switch } from "effect/ui/kit"
import { Effect, Schema } from "effect"
const SettingsMessage = UiSchema.TaggedEnum({ ToggledNotify: { checked: Schema.Boolean } })
const Settings = Html.component({
name: "Settings",
model: Schema.Struct({ notify: Schema.Boolean }),
message: SettingsMessage
}).make({
init: () => ({ notify: false }),
update: {
ToggledNotify: ($, { checked }) => $.evolve({ notify: checked })
},
view: (h, model) =>
Switch(h, {
props: { checked: model.notify, name: "notify" },
onToggle: ({ checked }) => SettingsMessage.ToggledNotify({ checked }),
view: (parts) =>
h.label.class("setting").children(
parts.root(h.button.class("switch")).children(parts.thumb(h.span.class("thumb"))),
"Notifications"
)
})
})
// The root keeps the consumer's class and gets the switch's role.
const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Settings, {}))
if (!/<button class="switch"[^>]* role="switch"/.test(html)) throw new Error("the root isn't a styled switch")
5 exports Since v4.2.0 Source

Components

Switch

unstable
Since v4.2.0

A switch. Call it with the consumer's h, the props, and a view closure that draws the root with parts.root(h.button), usually with parts.thumb(h.span.class("thumb")) as a child. It returns what view returns.

Details

onToggle is what a click sends: it gets the value the user asked for, which the consumer stores and passes back as checked. It never cancels the click. A read-only switch, or one without onToggle, sets no click handler, so its click goes to the nearest ancestor that handles one. A disabled switch sets none either, and the browser gives a disabled button no click.

The switch's own attributes win over the ones the consumer set on the builders, before the part or after it: type, role, disabled, the ARIA state (aria-checked, aria-disabled, aria-readonly, aria-required), the thumb's aria-hidden="true", which keeps its content out of the switch'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.

Signature

declare function Switch<Msg, A extends Content<Msg, any>>(_h: Helper<Msg, unknown>, options: {
readonly onToggle?: (event: {
readonly checked: boolean;
}) => NoInfer<Msg> | undefined;
readonly props: SwitchProps;
readonly view: (parts: SwitchParts<Msg>) => A;
}): A

Models

SwitchButton

type unstable
Since v4.2.0

A button the switch's root accepts, from the consumer's h.button.

Signature

type SwitchButton<Msg, R = never> = Html.Element<Msg, R, Html.EventsOf<"button">, HTMLButtonElement, "button">

SwitchParts

interface unstable
Since v4.2.0

What a Switch gives its view: the parts, and the state resolved to booleans for markup that depends on it.

Details

root(button) and thumb(span) each return the builder they were given, so set its classes, attributes, other handlers and children before the part or after it. The switch writes its own attributes when the element is built, so they win over the consumer's either way.

A switch with an onToggle sets the button's click handler, so a root that handles click itself fails the render, in every state, whether the handler was set before the part or chained after it. Without onToggle the switch sets none, and the consumer's click handler stays.

Signature

interface SwitchParts<Msg> {
readonly checked: boolean;
readonly disabled: boolean;
readonly readOnly: boolean;
readonly required: boolean;
readonly root: <R = never>(element: SwitchButton<Msg, R>) => SwitchButton<Msg, R>;
readonly thumb: <R = never>(element: SwitchSpan<Msg, R>) => SwitchSpan<Msg, R>;
}

SwitchProps

interface unstable
Since v4.2.0

The controlled value, interaction state and form settings for Switch.

Details

  • checked: whether the switch is on. Required: the consumer owns it.
  • disabled: sets the native disabled attribute, so the switch is skipped by Tab and ignores clicks.
  • readOnly: ignores clicks but stays focusable, and sets aria-readonly.
  • required: sets aria-required and data-required.
  • name, value and uncheckedValue: the form field. An on switch submits value, "on" by default. An off switch submits uncheckedValue, or nothing when it is unset. A disabled switch, or one without a name, submits nothing.

Signature

interface SwitchProps {
readonly checked: boolean;
readonly disabled?: boolean;
readonly name?: string;
readonly readOnly?: boolean;
readonly required?: boolean;
readonly uncheckedValue?: string;
readonly value?: string;
}

SwitchSpan

type unstable
Since v4.2.0

A span the switch's thumb accepts, from the consumer's h.span.

Signature

type SwitchSpan<Msg, R = never> = Html.Element<Msg, R, Html.EventsOf<"span">, HTMLSpanElement, "span">