Switch

A 34×20 on/off toggle for settings that take effect as soon as they change. It is a <button role="switch">, not a checkbox.

StableSource

Import

tsx
import { Switch } from "@merid/react";

Examples

Controlled

defaultChecked keeps state inside the switch. For controlled use pass checked and onCheckedChange, which receives the next boolean.

checked: false

Label position

Disabled and unlabelled

Without children, give the switch an aria-label.

In a form

A button does not submit a value on its own. Set name and the switch renders a hidden input that submits value (default "on") while it is on — nothing is submitted while off.

API reference

The ref and remaining button attributes go to the track <button>; className goes to the outer wrapper.

PropTypeDefaultDescription
checkedboolean–On state (controlled).
defaultCheckedbooleanfalseInitial on state (uncontrolled).
onCheckedChange(checked: boolean) => void–Called with the new state when toggled.
childrenReactNode–Visible label; becomes the accessible name. Otherwise pass aria-label.
labelPosition"start" | "end""end"Which side the label sits on.
namestring–When set, a hidden input with this name submits value while on.
valuestring"on"Submitted value when on.
disabledboolean–Disables the switch and dims the label. Inside a `Field`, inherited from it.
invalidboolean–Forces the invalid style (`data-invalid`) and `aria-invalid`. Inside a `Field`, derived from its `error`.

Styling

HookValues
.mrd-switchWrapper; receives className
.mrd-switch__trackThe <button role="switch">
.mrd-switch__thumbMoving thumb
.mrd-switch__labelVisible label
data-stateon · off (wrapper and track)
data-label-positionstart · end
data-disabledOn the wrapper when disabled
data-invalidOn the track when invalid; draws the danger ring

Switch has no component variables; the off track uses --mrd-control-off and the on track --mrd-accent.

Accessibility

  • Exposed as role="switch" with aria-checked, so screen readers announce “on” / “off”.
  • The visible label is linked with aria-labelledby; clicking it also toggles the switch.
  • Inside a Field, the switch takes the Field id (so the Field label names it), plus its aria-describedby, aria-invalid and disabled.
  • aria-invalid carries the invalid semantics; data-invalid drives the styling.
KeyAction
TabMoves focus to the switch.
SpaceToggles the switch.
EnterToggles the switch.

Guidelines

Do

Use a switch for settings that apply instantly, and label it with the setting name: “Dark mode”.

Avoid

Use a switch in a form that needs a Save button — use a Checkbox.

Do

Keep the label static; the switch itself shows the state.

Avoid

Change the label between “On” and “Off” — it duplicates the state and confuses screen reader users.