An on/off switch (WAI-ARIA Switch pattern, HTML checkbox variant). It is a real <input type="checkbox" role="switch">: the DOM checked is the optimistic state, the input carries data-pending while checkedChangeAction runs, and it takes part in forms like any checkbox.
"use client";
import { Switch } from "@funstack/ui/switch";
import { DemoErrorBoundary } from "./DemoErrorBoundary.js";
import { fakeSave } from "./fake-api.js";
// Autosave on change. Each Switch is uncontrolled with an async action: the input flips at once
// (its DOM `checked` is the optimistic value), carries data-pending for the 800 ms the save
// takes, then keeps the value. The second save always fails: the switch flips back and the
// error reaches the Error Boundary around it.
export function SwitchSettings() {
return (
<div className="demo-stack">
<label className="demo-field">
<Switch
className="demo-switch"
defaultChecked
checkedChangeAction={(next) => fakeSave(`notifications=${next}`)}
/>
Email notifications
</label>
<DemoErrorBoundary>
<label className="demo-field">
<Switch
className="demo-switch"
checkedChangeAction={(next) => fakeSave(`beta=${next}`, { fail: true })}
/>
Beta features (the save always fails)
</label>
</DemoErrorBoundary>
</div>
);
}/* Switch: the input is the track, ::before is the thumb. `:checked` and `[data-checked]` agree,
because the DOM `checked` is the optimistic state. */
.demo-switch {
appearance: none;
flex: none;
margin: 0;
inline-size: 2.5em;
block-size: 1.5em;
border-radius: 1em;
background: var(--color-border);
cursor: inherit;
transition: background-color 120ms;
}
.demo-switch::before {
content: "";
display: block;
inline-size: 1.2em;
block-size: 1.2em;
margin: 0.15em;
border-radius: 50%;
background: #fff;
box-shadow: 0 1px 2px rgb(0 0 0 / 0.3);
transition: translate 120ms;
}
/* The flip is a sync commit: a plain CSS transition animates it, and the rollback too. */
.demo-switch:checked {
background: var(--color-primary);
}
.demo-switch:checked::before {
translate: 1em 0;
}
.demo-switch[data-pending]::before {
background: radial-gradient(circle, var(--color-primary) 0 25%, #fff 30%);
}
.demo-switch:focus-visible {
outline: 2px solid var(--color-primary);
outline-offset: 2px;
}
.demo-switch:disabled {
opacity: 0.5;
}
@media (prefers-reduced-motion: reduce) {
.demo-switch,
.demo-switch::before {
transition: none;
}
}import { Switch } from "@funstack/ui/switch";
<label>
<Switch checkedChangeAction={saveNotifications} />
Email notifications
</label>One plain component, no parts. It renders a single void element, so there is no Thumb: the input is the track and ::before the thumb (appearance: none), or hide the input with a clip and style a sibling through label:has(:checked). There is no Label part either: your <label> (wrapping, or htmlFor with an id) names it and forwards clicks, before hydration too.
| Prop | Type | Default | Description |
|---|---|---|---|
checked | boolean | – | Controlled checked state. undefined means uncontrolled. |
defaultChecked | boolean | false | Initial checked state in uncontrolled mode. Also what a form reset returns to: the latest value is mirrored into the input's defaultChecked. |
checkedChangeAction | ChangeAction<boolean, SwitchCheckedChangeDetails> | – | Called with the next checked state on every change, inside the Switch's transition. Sync, async or a Server Function; rejections reach the nearest Error Boundary. details.reason is "activate" for the user (event is the native click, for Space and label clicks too) or "platform" for a form reset (event is the reset event) or a change found at hydration (event is undefined). details.valid: the control would be valid with the requested value (native constraints and the Field's validate); information only, the change is dispatched anyway. |
disabled | boolean | false | Native disabled, or the enclosing Field.Root's; never dispatches. A disabled switch is not submitted. |
readOnly | boolean | false | The library's read-only, or the enclosing Field.Root's: the switch stays focusable and submitted, and a click, Space or a label click changes nothing (the click is cancelled, so no input or change fires) and dispatches nothing. Rendered as aria-readonly="true" and data-readonly, never the HTML attribute (which does not apply to a checkbox input). Native required is not rendered while read-only, so a locked box never blocks a submission. A click made before hydration is undone at mount. disabled wins when both are set. |
value | string | "on" | Submitted with the form when checked (platform default). |
className | string | ((state: SwitchState) => string | undefined) | – | A class name, or a function of the part's state. |
style | CSSProperties | ((state: SwitchState) => CSSProperties | undefined) | – | Inline style, or a function of the part's state. |
render | ReactElement | ((props, state: SwitchState) => ReactNode) | – | Replace the default <input> (element form) or take over rendering (function form). It must keep an <input type="checkbox"> (development warning otherwise); the function form may render siblings next to it. |
SwitchState is { checked, pending, disabled, readOnly, invalid }; checked is optimistic; invalid is true while the enclosing Field.Root shows an error (data-invalid), false outside a Field.type, role, checked, defaultChecked, disabled, aria-readonly (while read-only), data-checked, data-pending, data-disabled, data-readonly; aria-checked and readOnly are removed, and required while read-only; onChange is merged (a render element's own handler runs first). Inside a Field.Root also id, name, required, aria-invalid, aria-describedby and data-invalid, and disabled / name / required default from the Field.Every other <input> prop passes through (name, required, form, id, autoFocus, ...). A render element's own onChange or onClick runs first. readOnly is the library's prop (above), not the HTML attribute. There is no transition prop: animate the flip with CSS on :checked. In development a switch with no accessible name (no <label>, aria-label or aria-labelledby) logs a warning.
| Attribute | On | Pseudo-class | Meaning |
|---|---|---|---|
data-checked | Switch | :checked | Optimistic checked state. Identical, because the DOM holds the optimistic value. |
data-pending | Switch | – | checkedChangeAction is in flight. |
data-disabled | Switch | :disabled | The disabled prop (or the Field's). Only :disabled also sees <fieldset disabled>. |
data-readonly | Switch | – | The readOnly prop (or the Field's). :read-only always matches a checkbox input, so it tells nothing. |
data-invalid | Switch | :user-invalid | Inside a Field.Root: the Field shows an error for the switch. |
| – | Switch | :required, :focus-visible | Platform only; no attribute. |
Data attributes are still emitted so that a wrapper can use them; on a wrapper, :has(:checked) works without JavaScript.
| Key | Behaviour |
|---|---|
| Space | Toggles (native). |
| Click (the switch or its label) | Toggles (native). |
| Enter | Does not toggle (native); may submit the form. |
| Tab | Focusable unless disabled (read-only included). |
| Read-only | Space, a click and a label click change nothing and fire no input / change; focus stays. |
click.role="switch" on a checkbox input is allowed by ARIA in HTML. aria-checked is never set (it is forbidden on native inputs): the native checked state is the ARIA state, and since it is the optimistic value, assistive technology hears the flip at once.Concept: Transitions & ViewTransition
fsui-switch plus fsui-switch-check or fsui-switch-uncheck, the kinds of every checked state (Checkbox, CheckboxGroup, Menu.CheckboxItem). There is no transition prop: a consumer's own <ViewTransition> keys a type map on them.
Concept: Pending & optimistic state
The native input renders the optimistic state, so :checked and data-checked change at the click and assistive technology hears it at once. data-pending is on the Switch while checkedChangeAction is in flight. Animate the flip with CSS on :checked; your own <ViewTransition> around content next to the Switch can animate the commit that ends the action.
checkedChangeAction(next, { reason: "activate" }) in its transition; data-checked and data-pending update in the same frame.checked decides. data-pending goes.checked back to the input, and the error reaches the nearest Error Boundary (the "Beta features" row above).The input shows the optimistic value because react-dom restores a controlled input after every event (Native inputs). So details.event.preventDefault() is not a veto: it makes the browser undo the click after React has rendered it, and the input disagrees with data-checked while the action is pending (development warning). To veto, use controlled mode and do not adopt the value.
Concept: Suspense
No content region; nothing the Switch renders suspends.
Concept: Browser-owned state
A native <input>: the browser flips it before any JavaScript runs. A form reset and clicks before hydration reach checkedChangeAction with reason: "platform".
The library owns checked, but the browser can still change it on its own. Both cases reach checkedChangeAction with reason: "platform":
form.reset(), or React's automatic reset after a successful <form action>. Uncontrolled, the reset target is the latest defaultChecked (so a form re-rendered with saved data resets to it, as in the example). Controlled, the browser resets to the mount-time value and the change is offered to the parent, which adopts it or not; the input then follows the parent.Concept: Server Components
A native checkbox: clicking, Space, labels, required and form submission work. On hydration a changed value is reconciled with one checkedChangeAction call (reason: "platform").
If a switch only posts with its form, a Server Component can render the native input itself, with no library code for it:
// A Server Component: no import from @funstack/ui.
<label>
<input type="checkbox" role="switch" name="notifications" defaultChecked={user.notifications} />
Email notifications
</label>role="switch" gives switch semantics in every engine. To draw it as a switch, set appearance: none and style it on :checked, :disabled and :focus-visible. The HTML switch attribute only changes the look: it draws a native switch in Safari 17.4+ and nowhere else (not in Chrome or Firefox, and not yet in the HTML standard). If you add it, write switch="": react-dom drops true for an attribute it does not know, and @types/react does not type it.
Compared with <Switch>, the plain input has no checkedChangeAction, so no transition, data-pending or rollback on rejection; no data-checked / data-disabled; no React state that tracks it (clicks before hydration and form resets live only in the DOM); and no Field integration (the Field's id, label, description, invalid and required state must be wired by hand).
"use client";
import { startTransition, useState } from "react";
import { useFormStatus } from "react-dom";
import { Switch } from "@funstack/ui/switch";
import { fakeSave } from "./fake-api.js";
function SubmitButton() {
const { pending } = useFormStatus();
return (
<button type="submit" className="demo-button demo-primary">
{pending && <span className="demo-spinner" aria-hidden />}
Save
</button>
);
}
// No change actions: the switches are native checkboxes that post `name=value` with the form.
// After the form action succeeds React resets the form; the saved values are the new
// `defaultChecked`, so the reset lands on what was saved. The Reset button goes through the same
// path (the browser restores `defaultChecked`; the Switch follows with reason "platform").
export function SwitchForm() {
const [saved, setSaved] = useState({ newsletter: false, digest: true });
const [submitted, setSubmitted] = useState("(nothing yet)");
return (
<form
className="demo-stack"
action={async (formData) => {
await fakeSave("the settings");
const next = {
newsletter: formData.get("newsletter") === "on",
digest: formData.get("digest") === "weekly",
};
startTransition(() => {
setSaved(next);
setSubmitted(JSON.stringify([...formData]));
});
}}
>
<label className="demo-field">
<Switch className="demo-switch" name="newsletter" defaultChecked={saved.newsletter} />
Newsletter
</label>
<label className="demo-field">
<Switch
className="demo-switch"
name="digest"
value="weekly"
defaultChecked={saved.digest}
/>
Weekly digest
</label>
<div className="demo-row">
<SubmitButton />
<button type="reset" className="demo-button">
Reset
</button>
</div>
<output className="demo-readout">
last submitted: <code>{submitted}</code>
</output>
</form>
);
}/* Switch: the input is the track, ::before is the thumb. `:checked` and `[data-checked]` agree,
because the DOM `checked` is the optimistic state. */
.demo-switch {
appearance: none;
flex: none;
margin: 0;
inline-size: 2.5em;
block-size: 1.5em;
border-radius: 1em;
background: var(--color-border);
cursor: inherit;
transition: background-color 120ms;
}
.demo-switch::before {
content: "";
display: block;
inline-size: 1.2em;
block-size: 1.2em;
margin: 0.15em;
border-radius: 50%;
background: #fff;
box-shadow: 0 1px 2px rgb(0 0 0 / 0.3);
transition: translate 120ms;
}
/* The flip is a sync commit: a plain CSS transition animates it, and the rollback too. */
.demo-switch:checked {
background: var(--color-primary);
}
.demo-switch:checked::before {
translate: 1em 0;
}
.demo-switch[data-pending]::before {
background: radial-gradient(circle, var(--color-primary) 0 25%, #fff 30%);
}
.demo-switch:focus-visible {
outline: 2px solid var(--color-primary);
outline-offset: 2px;
}
.demo-switch:disabled {
opacity: 0.5;
}
@media (prefers-reduced-motion: reduce) {
.demo-switch,
.demo-switch::before {
transition: none;
}
}A Switch with a name posts name=value when checked, exactly like a checkbox. No change action is needed for that.
The Switch's own action and the form channel are independent. A Server Component can use either, or both on the same Switch:
// Autosave on change: a per-switch action with data-pending.
<Switch defaultChecked={user.notifications} checkedChangeAction={setNotifications} />
// Native submission: posted with the form, pending is the form's.
<form action={saveSettings}>
<Switch name="notifications" defaultChecked={user.notifications} />
<SubmitButton />
</form>| checkedChangeAction={serverFn} | <form action={serverFn}> + name | |
|---|---|---|
| Runs | on every change, in the Switch's transition | on submit, in the form's |
| Pending | data-pending on the Switch | useFormStatus() in the form; the Switch shows nothing |
| Receives | (next, details), details.event undefined | FormData |
| Rollback | optimistic value reverts on rejection | n/a |
"Submit on change" is checkedChangeAction={() => formRef.current?.requestSubmit()}.
A Switch with no action and no checked is a working native input before hydration and with JavaScript off.