Skip to main content

Switch

BasicSelection<co-switch>

Switch turns a setting on or off immediately, such as "Sidebar starts collapsed" or "Allow online booking".

import { Switch } from 'health-components';

Examples​

Showcase​

Storybook

Settings Row​

Storybook

Disabled​

Storybook

Usage​

<Switch label="Sidebar starts collapsed" description="Clinical Sidebar only." checked={collapsed} onChange={setCollapsed} />

When to use​

  • Settings that apply straight away.

When not to use​

  • A choice that only applies after Save in a form: Checkbox.

Variants and states​

VariantWhat it is
off / onGrey border-strong track, success when on.
row56px row with a top border, as in portal settings.
descriptionMuted explanation.
disabled45% opacity.

Accessibility​

  • role switch with aria-checked; labelled by its text.
  • The on state is green AND the knob moves right, so colour is not the only cue.

Do and don't​

  • Do: "Start collapsed" under the Sidebar option in Appearance.
  • Don't: A switch inside a modal form that still needs Save.

Props​

PropTypeDefaultDescription
label *ReactNode—Visible label. Required
checkedboolean—On state (controlled)
defaultCheckedbooleanfalseInitial on state (uncontrolled)
descriptionReactNodenoneHelp text under the label
rowbooleanfalseSettings-row layout with a top border and 56px height
disabledbooleanfalseDisables the switch
onChange(on: boolean) => voidnoneCalled with the new state
classNamestring—Class on the wrapping row.

Also accepts native attributes, including defaultValue, autoFocus, onInput, name, value, plus className and event handlers on the root element.

Angular, Vue and HTML​

Register the elements once (Angular guide), then use <co-switch>. Attributes take strings; arrays and objects are set as properties (Angular [prop], Vue :prop) or as JSON attributes. Callbacks are DOM events with the value in event.detail.

Angular template
<co-switch checked default-checked [defaultValue]="defaultValue" (co-change)="onChange($event.detail)"></co-switch>
KindNameDescription
attributecheckedOn state (controlled)
attributedefault-checked / defaultCheckedInitial on state (uncontrolled); default false
attributerowSettings-row layout with a top border and 56px height; default false
attributedisabledDisables the switch; default false
attributeclass-name / classNameClass on the wrapping row.
property / JSON attributedefault-value / defaultValue
attributeauto-focus / autoFocus
attributename
property / JSON attributevalue
eventco-changeCalled with the new state; default none (React: onChange)
eventco-input (React: onInput)
slotlabelVisible label. Required Plain text also works as the `label` attribute.
slotdescriptionHelp text under the label; default none Plain text also works as the `description` attribute.