Components · Forms

Switch

Takes effect the moment it moves. If the change needs a review step, it is not a switch.

Stable · v1.0 shadcn/ui WCAG 2.2 AA

Overview

A switch is the only control in this set that does something on contact. That is its definition and its entire hazard: it looks like a Checkbox, sits in the same forms, and commits where a checkbox stages.

Which of the three to reach for — how many may be chosen, and when the choice takes effect — is decided on Selection controls.

What may sit on a switch

The rule

A switch carries a view setting, never a clinical fact. Anything that is recorded, signed, or acted on by somebody else needs a moment at which a person reviews it — and a switch has no such moment.

What the component enforces, and what it only asks for

The type carries the one constraint that matters, and it carries it by having exactly one permitted value.

RuleHeld by How
A deferred switch cannot be declaredThe component effect accepts "immediate" and nothing else. The constraint is in the type; there is no runtime check to forget.
Asserted: Switch: effect has exactly one permitted value
It reports as a switch, not a checkboxThe component role="switch" on a real input, so it is announced as on or off.
Asserted: AX tree: the switch toggles its checked state, and stays a switch
The whole label row is the targetThe component 44 px, from the shared base file — a control that acts on contact is the worst one to make small.
Asserted: Switch: the label row is the target, not the control
Its state is shown in wordsThe component “On” or “Off” beside the label. The switch drew the same square as a checkbox, and the two are opposites in this system — one stages a change, one applies it. Driven in a browser.
Asserted: Switch: its state is shown in words, and follows the switch
An unavailable switch says why, and stays reachableThe component disabled is not a prop, as on Button. unavailable={{ reason }} keeps the control focusable, describes it by the reason, and ignores Space and clicks. It accepted a bare native disabled: off, unreachable by keyboard, and silent about why.
Asserted: Checkbox, radio and switch cannot be switched off without a reason
What sits on it is a view setting, not a clinical factYou label is a string. “Patient consents to data sharing” on a switch is a record entry with no moment at which anybody reviews it, and the type permits it.
Nothing destructive or irreversibleYou onChange is yours. A control that acts on contact cannot also ask whether you are sure.
The change really is immediateYou If your handler defers, the label is now lying and the component cannot tell.
Why this is written down

A rule stated on a component page reads as a rule the component keeps. 3 of these 6 are; 3 are obligations this page was quietly handing to the reader. The component makes the wrong kind of switch unexpressible. It cannot stop the wrong content being put on a correct one.

Do's and don'ts

Do
Show model annotation Applies immediately

A view setting that genuinely takes effect at once, and says so.

Don't
Patient consents to data sharing

A recorded clinical fact on an immediate-effect control, pre-set to yes. There is no moment at which anyone reviews it.

Do

Show model annotation   off

applies now

A view setting. It changes what the reader sees and nothing in the record, and the label says it takes effect at once.

Don't

Patient consents to data sharing   on

A recorded clinical fact on a control that acts on contact. There is no moment at which anyone reviews it, and it is already true.

Do

Night mode   on

Reversible, immediate, harmless to get wrong. Exactly what a switch is for.

Don't

Discharge patient   off

Irreversible and consequential on a control that cannot ask “are you sure”. That is a Button and a Dialog.

Accessibility

Outcomes of use

Clinical safety notes

Risk controls this page specifies

Trace these in your risk file (ISO 14971) and usability engineering file (IEC 62366-1). Which of these the component keeps and which are yours is stated in the guarantees table; a control the component does not keep is one your risk file must show you keep.

Implementation

$ npx shadcn@latest add https://md.notjustany.tech/r/switch.json
<Switch
  label="Show model annotation"
  effect="immediate"   // the only permitted value
  hint="Applies now" />

// A value that needs a review step is a Checkbox inside a form. The
// constraint is in the type; nothing throws at runtime.
NotJustAnyMed.Tech Design System · Selection controls · v1.0 · draft for review
Covers Checkbox, Radio and Switch. Reference applications named in this system are fictional.