ComponentUpdated in v15

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.

import { Component } from '@angular/core';
import { WrSwitch } from 'ngwr/switch';

@Component({ imports: [WrSwitch] })
export class MyComponent {}

Basic usage

Bind the boolean with [(checked)] or any Angular form.

Value: true
<wr-switch [(checked)]="enabled">Notifications</wr-switch>

Disabled

<wr-switch [disabled]="true">Disabled</wr-switch>

API

NameDescriptionTypeDefault
ariaLabelAccessible 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 | nullnull
idStable id used to associate the native input with its label. Lands on the inner <input>; the host never keeps it.stringrandomId('wr-switch')
disabledDisable the switch. Bound automatically from the field's disabled state when used with [formField].booleanfalse
readonlyRefuse 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.booleanfalse
sizeControl size — shares the --wr-control-* contract.WrSwitchSize'md'
checkedOn / off state. Bound by [formField], or two-way via [(checked)].booleanfalse
(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.

VariableDefaultDeclared on
--wr-switch-bgvar(--wr-border-base).wr-switch +1 variant override
--wr-switch-bg-checkedvar(--wr-color-primary).wr-switch
--wr-switch-colorvar(--wr-color-on-surface).wr-switch +1 variant override
--wr-switch-font-familyvar(--wr-font-family-base).wr-switch
--wr-switch-font-sizevar(--wr-text-sm).wr-switch +2 variant overrides
--wr-switch-label-gapvar(--wr-space-sm).wr-switch
--wr-switch-line-heightvar(--wr-control-line-height-md).wr-switch +2 variant overrides
--wr-switch-thumbvar(--wr-color-surface).wr-switch
--wr-switch-thumb-size0.875rem.wr-switch +2 variant overrides
--wr-switch-track-height1.125rem.wr-switch +2 variant overrides
--wr-switch-track-radiusvar(--wr-border-radius-pill).wr-switch
--wr-switch-track-width2rem.wr-switch +2 variant overrides

Dependencies

Built onForm
Used bySchema Form

See also