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.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { FormField } from "./ui/uicontext/components/FormControls.ts"import { Switch } from "./ui/uicontext/components/Switch.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, radius, space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ list: { display: "flex", flexDirection: "column", inlineSize: "min(100%, 30rem)", borderWidth: 1, borderStyle: "solid", borderColor: color.line, borderRadius: radius.lg, backgroundColor: color.panel, }, row: { paddingInline: space.xl, paddingBlock: space.lg, borderTopWidth: { default: 1, ":first-child": 0 }, borderTopStyle: "solid", borderTopColor: color.line, },})const Setting = Schema.Literals(["digest", "mentions", "sounds"])type Setting = typeof Setting.Typeconst rows: ReadonlyArray<{ readonly setting: Setting; readonly label: string; readonly description: string }> = [ { setting: "digest", label: "Weekly digest", description: "A summary of your team's activity every Monday." }, { setting: "mentions", label: "Mentions", description: "Tell me when someone mentions me in a comment." }, { setting: "sounds", label: "Sounds", description: "Play a sound when a notification arrives." },]const Message = UiSchema.TaggedEnum({ ToggledSetting: { setting: Setting, checked: Schema.Boolean } })export const NotificationSettings = Html.component({ name: "NotificationSettings", model: Schema.Struct({ digest: Schema.Boolean, mentions: Schema.Boolean, sounds: Schema.Boolean }), message: Message,}).make({ init: () => ({ digest: true, mentions: true, sounds: false }), update: { ToggledSetting: ($, { setting, checked }) => ({ ...$.model, [setting]: checked }) }, view: (h, model) => h.div.attrs(sx(styles.list)).children( rows.map((row) => h.div.key(row.setting).attrs(sx(styles.row)).children( // a settings row: the label and description on the left, the switch on the right FormField(h, { id: `settings-${row.setting}`, label: row.label, description: row.description, layout: "inline-end" }, (field) => Switch(h, { key: row.setting, checked: model[row.setting], field, onToggle: (checked) => Message.ToggledSetting({ setting: row.setting, checked }) })), )), ),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/switchUsage
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.
import { Command, UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Effect, Schema } from "effect"import { Switch } from "./ui/uicontext/components/Switch.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ stack: { display: "grid", gap: 8, justifyItems: "start" }, note: { margin: 0, fontSize: font.sm, color: color.muted, minBlockSize: "1.4em" }, error: { color: color.danger },})const Message = UiSchema.TaggedEnum({ ToggledPublic: { checked: Schema.Boolean }, SucceededSavePublic: {}, FailedSavePublic: { reason: Schema.String },})// stands in for the server: turning the page public is refused, to show the rollbackconst SavePublic = Command.make("Example.SavePublic", { args: { checked: Schema.Boolean } }, ({ checked }) => Effect.sleep("700 millis").pipe( Effect.as(checked ? Message.FailedSavePublic({ reason: "Only workspace owners can publish pages." }) : Message.SucceededSavePublic()), ))export const PublishSwitch = Html.component({ name: "PublishSwitch", // `saved` is what the server holds; `shown` is what the switch shows while a save runs model: Schema.Struct({ saved: Schema.Boolean, shown: Schema.Boolean, saving: Schema.Boolean, error: Schema.NullOr(Schema.String) }), message: Message,}).make({ init: () => ({ saved: false, shown: false, saving: false, error: null }), update: { // optimistic: the switch moves at once, and goes back if the save is refused ToggledPublic: ($, { checked }) => $.model.saving ? $.model : $.evolve({ shown: checked, saving: true, error: null }).run(SavePublic.make({ checked })), SucceededSavePublic: ($) => $.evolve({ saved: $.model.shown, saving: false }), FailedSavePublic: ($, { reason }) => $.evolve({ shown: $.model.saved, saving: false, error: reason }), }, view: (h, { shown, saving, error }) => h.div.attrs(sx(styles.stack)).children( Switch(h, { checked: shown, readOnly: saving, label: "Share to the web", onToggle: (checked) => Message.ToggledPublic({ checked }) }), h.p.attrs({ role: "status", ...sx(styles.note, error !== null && styles.error) }).children( error ?? (saving ? "Saving…" : shown ? "Anyone with the link can view." : "Only members can view."), ), ),})API reference
Options
| Name | Type | Default | Description |
|---|---|---|---|
key | string | — | Names the switch among others in the same view; needed for more than one |
checkedrequired | boolean | — | Whether it's on. The caller keeps it and passes back what onToggle asks for. |
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 on before its form submits. |
name | string | — | The form field's name: an on switch submits its value under it |
value | string | on | What an on switch submits under name; on by default. |
size | "sm" | "md" | md | The track's size; md by default. |
label | string | — | The visible label beside the switch; leave it out inside a FormField |
ariaLabel | string | — | 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 |
field | FieldControlAttrs | — | A FormField's attributes: its label names the switch, its description and error describe it |
style | Style | — | 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.