Switch

An on and off toggle for settings that apply at once.

A summary of your team's activity every Monday.

Tell me when someone mentions me in a comment.

Play a sound when a notification arrives.

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/switch

Usage

import { Switch } from "./ui/uicontext/components/Switch.ts"
const Message = UiSchema.TaggedEnum({ ToggledDigest: { checked: Schema.Boolean } })export const DigestSwitch = (h: Html.H<typeof Message.Type>, digest: boolean) =>	Switch(h, {		checked: digest,		label: "Weekly digest",		onToggle: (checked) => Message.ToggledDigest({ checked }),	})

Examples

Saving, and a refused change

The switch moves at once and is read-only while the save runs. When the server refuses, it moves back and says why.

Only members can view.

API reference

Options

Switch options
NameTypeDefaultDescription
keystring—Names the switch among others in the same view; needed for more than one
checkedrequiredboolean—Whether it's on. The caller keeps it and passes back what onToggle asks for.
disabledboolean—Can't be pressed or focused, and is skipped when its form submits.
readOnlyboolean—Shows the value and stays focusable, but a press doesn't change it: a setting only an admin can change.
requiredboolean—Must be on before its form submits.
namestring—The form field's name: an on switch submits its value under it
valuestringonWhat an on switch submits under name; on by default.
size"sm" | "md"mdThe track's size; md by default.
labelstring—The visible label beside the switch; leave it out inside a FormField
ariaLabelstring—The accessible name when there's no visible label
onTogglerequired(checked: boolean) => Message—What a press asks for: the value to store and pass back as checked
fieldFieldControlAttrs—A FormField's attributes: its label names the switch, its description and error describe it
styleStyle—StyleX styles applied after the switch’s own.

Accessibility

  • A button with role="switch": Tab reaches it, and Space or Enter flips it. Screen readers say on or off.
  • 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.

Source