Switch

A native checkbox shown as a switch, drawn by itself, in the look of the app's style.

FormsExperimental

Usage

import { Component, ViewEncapsulation, signal } from '@angular/core';import { FormField, form } from '@angular/forms/signals';import { ScField, ScLabel, ScSwitch } from '@semantic-components/ui';
/** A switch beside its label. */@Component({  selector: 'app-switch-usage-demo',  imports: [FormField, ScLabel, ScSwitch, ScField],  template: `    <div scField orientation="horizontal">      <input scSwitch [formField]="modeForm.airplane" />      <label scLabel>Airplane Mode</label>    </div>  `,  host: { class: 'flex w-full justify-center' },  encapsulation: ViewEncapsulation.None,})export class SwitchUsageDemo {  private readonly modeForm = form(signal({ airplane: false }));}

Anatomy

A native checkbox with role="switch" that draws its track and thumb: alone, or in a horizontal field beside its text — the field is the label, a label is inside the field, or the field is inside a label (a choice card: the whole card clicks). Written first, the switch is at the start; last, at the end. A [formField] or [(checked)] binds it.

import { ScSwitch, ScField, ScFieldContentWrapper, ScFieldDescription, ScFieldTitle, ScLabel } from '@semantic-components/ui';
<!-- The field is the label: the control and its text --><label scField orientation="horizontal">  <input scSwitch [formField]="…" />  …</label>
<!-- A label inside the field, with a description --><div scField orientation="horizontal">  <label scLabel>…</label>  <p scFieldDescription>…</p>  <input scSwitch [formField]="…" /></div>
<!-- The field inside a label: a choice card, the whole card clicks --><label scLabel>  <div scField orientation="horizontal">    <div scFieldContentWrapper>      <div scFieldTitle>…</div>      <p scFieldDescription>…</p>    </div>    <input scSwitch [formField]="…" />  </div></label>

Accessibility

  • A native checkbox with role="switch": announced as a switch, on or off; Tab reaches it, Space toggles it.
  • Name it by its text: a label round both (label scField orientation="horizontal"), or a scLabel in a div field; a switch with no text by aria-label. Its name stays the same as it toggles.
  • A description is read with its switch; invalid once touched (aria-invalid, from Signal Forms).

Examples

Description

Focus is shared across devices, and turns off when you leave the app.

import { Component, ViewEncapsulation, signal } from '@angular/core';import { FormField, form } from '@angular/forms/signals';import {  ScField,  ScFieldDescription,  ScLabel,  ScSwitch,} from '@semantic-components/ui';
/** A description under the label, read with the switch; the switch at the end. */

Choice card

import { Component, ViewEncapsulation, signal } from '@angular/core';import { FormField, form } from '@angular/forms/signals';import {  ScField,  ScFieldContentWrapper,  ScFieldDescription,  ScFieldGroup,  ScFieldTitle,  ScLabel,  ScSwitch,

Disabled

import { Component, ViewEncapsulation } from '@angular/core';import { ScField, ScLabel, ScSwitch } from '@semantic-components/ui';
/** Disabled: dimmed, out of the Tab order. */@Component({  selector: 'app-disabled-switch-demo',  imports: [ScLabel, ScSwitch, ScField],  template: `    <div scField orientation="horizontal" class="w-fit">      <input scSwitch disabled />

Invalid

You must accept the terms and conditions to continue.

import { Component, ViewEncapsulation, signal } from '@angular/core';import { FormField, form, required } from '@angular/forms/signals';import {  ScField,  ScFieldDescription,  ScLabel,  ScSwitch,} from '@semantic-components/ui';
/** A required switch, touched and off: marked invalid (Signal Forms). */

Sizes

import { Component, ViewEncapsulation } from '@angular/core';import {  ScField,  ScFieldGroup,  ScLabel,  ScSwitch,} from '@semantic-components/ui';
/** size: default, or sm. */@Component({

Placement

import { Component, ViewEncapsulation, signal } from '@angular/core';import {  ScCard,  ScCardBody,  ScField,  ScFieldDescription,  ScFieldGroup,  ScLabelText,  ScSwitch,} from '@semantic-components/ui';

State

import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScField, ScSwitch } from '@semantic-components/ui';
/** Its state in its text: two-way, [(checked)]. */@Component({  selector: 'app-state-switch-demo',  imports: [ScSwitch, ScField],  template: `    <label scField orientation="horizontal">      <input scSwitch [(checked)]="notifications" />

Signal Forms

{
  "notifications": true,
  "darkMode": false
}
import { JsonPipe } from '@angular/common';import { Component, ViewEncapsulation, signal } from '@angular/core';import { FormField, form } from '@angular/forms/signals';import {  ScField,  ScFieldGroup,  ScFrame,  ScLabel,  ScSwitch,} from '@semantic-components/ui';

RTL

يتم مشاركة التركيز عبر الأجهزة، ويتم إيقاف تشغيله عند مغادرة التطبيق.

import { Dir } from '@angular/cdk/bidi';import { Component, LOCALE_ID, ViewEncapsulation, signal } from '@angular/core';import { FormField, form } from '@angular/forms/signals';import {  ScField,  ScFieldDescription,  ScLabel,  ScSwitch,} from '@semantic-components/ui';

API reference

Each part takes class, merged over the style (cn: a later class wins).

PartSelectorDescription
ScSwitchinput[scSwitch] A native checkbox with role="switch", its track and thumb drawn by itself in the style's look: checked (two-way, or a [formField]); size ('default', 'sm'); invalid once touched.