import { Component } from '@angular/core';
import { WrSwitch } from 'ngwr/switch';
@Component({ imports: [WrSwitch] })
export class MyComponent {}Switch
Turn a setting on or off.
Installation
Import the class, and @use the entry point in your global styles: without it the component renders unstyled.
Basic usage
Bind the boolean with [(checked)] or any Angular form.
<wr-switch [(checked)]="enabled">Notifications</wr-switch>Disabled
<wr-switch [disabled]="true">Disabled</wr-switch>API
| Name | Description | Type | Default |
|---|---|---|---|
ariaLabel | Accessible name for a switch used WITHOUT projected text. The wrapping <label> names the control whenever content is projected; with none, the label is empty and the input has no name at all. | string | null | null |
id | Stable id used to associate the native input with its label. Lands on the inner <input>; the host never keeps it. | string | randomId('wr-switch') |
disabled | Disable the switch. Bound automatically from the field's disabled state when used with [formField]. | boolean | false |
readonly | Refuse edits while staying focusable and submittable. Bound automatically from the field's readonly state when used with [formField]. A native checkbox ignores the readonly attribute, so this cancels the click's activation behaviour instead — which covers Space too, since the key arrives as a click — and mirrors the state as aria-readonly, which role switch supports. | boolean | false |
size | Control size — shares the --wr-control-* contract. | WrSwitchSize | 'md' |
checked | On / off state. Bound by [formField], or two-way via [(checked)]. | boolean | false |
(touch) | Emitted on blur so a bound field can mark itself touched. | void | — |
CSS variables
Set these on the component's selector or a wrapper, not on :root: the defaults are declared on ngwr/switch's own element.
| Variable | Default | Declared on |
|---|---|---|
--wr-switch-bg | var(--wr-border-base) | .wr-switch +1 variant override |
--wr-switch-bg-checked | var(--wr-color-primary) | .wr-switch |
--wr-switch-color | var(--wr-color-on-surface) | .wr-switch +1 variant override |
--wr-switch-font-family | var(--wr-font-family-base) | .wr-switch |
--wr-switch-font-size | var(--wr-text-sm) | .wr-switch +2 variant overrides |
--wr-switch-label-gap | var(--wr-space-sm) | .wr-switch |
--wr-switch-line-height | var(--wr-control-line-height-md) | .wr-switch +2 variant overrides |
--wr-switch-thumb | var(--wr-color-surface) | .wr-switch |
--wr-switch-thumb-size | 0.875rem | .wr-switch +2 variant overrides |
--wr-switch-track-height | 1.125rem | .wr-switch +2 variant overrides |
--wr-switch-track-radius | var(--wr-border-radius-pill) | .wr-switch |
--wr-switch-track-width | 2rem | .wr-switch +2 variant overrides |
Dependencies
| Built on | Form |
|---|---|
| Used by | Schema Form |
See also
Component CheckboxA checkbox for one boolean, or a group of them for an array.Component SegmentedPick one option from a row of buttons.Component Form FieldWraps a form control with a label, a hint and its error messages.Guide FormsBind ngwr controls to any Angular form and show their errors.Component FormA plain layout wrapper for a label, a control and its error.