FUNSTACK UI

HomeWhyGetting startedConceptsComponentsRoadmap
GitHub

FUNSTACK UI is in beta. Part of the FUNSTACK family with FUNSTACK Router and FUNSTACK Static, which build this site.

Introduction

  • Why FUNSTACK UI
  • Overview
  • Getting started

Concepts

  • Action props
  • Pending & optimistic state
  • Transitions & ViewTransition
  • Suspense
  • Browser-owned state
  • Server Components

Guides

  • Replacing the element (render)
  • Direction and locales
  • Experimental features
  • Next.js App Router
  • React Router framework mode
  • Content Security Policy
  • Using with AI agents

Components

  • Buttons
    • Button
    • Toggle
    • ToggleGroup
    • Toolbar
  • Disclosure
    • Tabs
    • Accordion
    • Collapsible
    • NavigationMenu
  • Overlays
    • Tooltip
    • HoverCard
    • Popover
    • Dialog
    • AlertDialog
    • Drawer
    • Menu
    • Context menu (Menu)
    • Menubar (Menu)
    • Toast
  • Forms
    • Field and Form
    • Checkbox
    • CheckboxGroup
    • RadioGroup
    • Switch
    • Select
    • Combobox
    • MultiSelect
    • Slider
    • NumberField
    • OTPField
    • TextField
  • Output
    • Progress
    • Meter
    • Avatar
  • Layout
    • Separator
    • Scroll areas (recipe)

Resources

  • Roadmap & changelog
  • GitHub ↗

Switch

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.

Show code
SwitchSettings.tsx
"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.css
/* 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;
  }
}
Async and platform behaviour: Transition types · Optimistic and pending · Suspense · Browser-owned state · Before hydration

Anatomy

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.

Props

PropTypeDefaultDescription
checkedboolean–Controlled checked state. undefined means uncontrolled.
defaultCheckedbooleanfalseInitial checked state in uncontrolled mode. Also what a form reset returns to: the latest value is mirrored into the input's defaultChecked.
checkedChangeActionChangeAction<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.
disabledbooleanfalseNative disabled, or the enclosing Field.Root's; never dispatches. A disabled switch is not submitted.
readOnlybooleanfalseThe 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.
valuestring"on"Submitted with the form when checked (platform default).
classNamestring | ((state: SwitchState) => string | undefined)–A class name, or a function of the part's state.
styleCSSProperties | ((state: SwitchState) => CSSProperties | undefined)–Inline style, or a function of the part's state.
renderReactElement | ((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.
State
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.
Owned
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.

Data attributes

AttributeOnPseudo-classMeaning
data-checkedSwitch:checkedOptimistic checked state. Identical, because the DOM holds the optimistic value.
data-pendingSwitch–checkedChangeAction is in flight.
data-disabledSwitch:disabledThe disabled prop (or the Field's). Only :disabled also sees <fieldset disabled>.
data-readonlySwitch–The readOnly prop (or the Field's). :read-only always matches a checkbox input, so it tells nothing.
data-invalidSwitch:user-invalidInside a Field.Root: the Field shows an error for the switch.
–Switch:required, :focus-visiblePlatform only; no attribute.

Data attributes are still emitted so that a wrapper can use them; on a wrapper, :has(:checked) works without JavaScript.

Keyboard and ARIA

KeyBehaviour
SpaceToggles (native).
Click (the switch or its label)Toggles (native).
EnterDoes not toggle (native); may submit the form.
TabFocusable unless disabled (read-only included).
Read-onlySpace, a click and a label click change nothing and fire no input / change; focus stays.
  • The library installs no key handler. Every activation reaches it as a native 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.
  • Keep the label stable: the state is already conveyed.
  • The input is not disabled while pending. A second click reverses the first; the latest wins.

Transition types

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.

Optimistic and pending

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.

Walkthrough

  1. Click: the browser flips the input; the Switch dispatches checkedChangeAction(next, { reason: "activate" }) in its transition; data-checked and data-pending update in the same frame.
  2. Resolve: uncontrolled, the value is committed; controlled, the parent's checked decides. data-pending goes.
  3. Reject: the optimistic value reverts, React writes the old 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.

Suspense

Concept: Suspense

No content region; nothing the Switch renders suspends.

Browser-owned state

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 platform paths

The library owns checked, but the browser can still change it on its own. Both cases reach checkedChangeAction with reason: "platform":

  • Form reset: a reset button, 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.
  • Clicks before hydration (and Firefox restoring form state on reload): the server-rendered input works natively; on hydration the Switch finds the DOM differs from its state and dispatches once, with no event.

Before hydration

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").

Plain HTML

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).

Forms

last submitted: (nothing yet)
Show code
SwitchForm.tsx
"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.css
/* 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.

Server Functions and forms

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
Runson every change, in the Switch's transitionon submit, in the form's
Pendingdata-pending on the SwitchuseFormStatus() in the form; the Switch shows nothing
Receives(next, details), details.event undefinedFormData
Rollbackoptimistic value reverts on rejectionn/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.