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
Settings Row
Disabled
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
| Variant | What it is |
|---|---|
| off / on | Grey border-strong track, success when on. |
| row | 56px row with a top border, as in portal settings. |
| description | Muted explanation. |
| disabled | 45% 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
| Prop | Type | Default | Description |
|---|---|---|---|
label * | ReactNode | — | Visible label. Required |
checked | boolean | — | On state (controlled) |
defaultChecked | boolean | false | Initial on state (uncontrolled) |
description | ReactNode | none | Help text under the label |
row | boolean | false | Settings-row layout with a top border and 56px height |
disabled | boolean | false | Disables the switch |
onChange | (on: boolean) => void | none | Called with the new state |
className | string | — | 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>
| Kind | Name | Description |
|---|---|---|
| attribute | checked | On state (controlled) |
| attribute | default-checked / defaultChecked | Initial on state (uncontrolled); default false |
| attribute | row | Settings-row layout with a top border and 56px height; default false |
| attribute | disabled | Disables the switch; default false |
| attribute | class-name / className | Class on the wrapping row. |
| property / JSON attribute | default-value / defaultValue | |
| attribute | auto-focus / autoFocus | |
| attribute | name | |
| property / JSON attribute | value | |
| event | co-change | Called with the new state; default none (React: onChange) |
| event | co-input | (React: onInput) |
| slot | label | Visible label. Required Plain text also works as the `label` attribute. |
| slot | description | Help text under the label; default none Plain text also works as the `description` attribute. |