switch

A control that allows the user to toggle between checked and unchecked.

PreviousNext
import { Component } from '@angular/core';

import { ZardSwitchComponent } from '@/shared/components/switch/switch.component';

@Component({
  selector: 'zard-demo-switch',
  imports: [ZardSwitchComponent],
  template: `
    <z-switch zId="airplane-mode">Airplane Mode</z-switch>
  `,
})
export class ZardDemoSwitchDefaultComponent {}

Installation

Copy
npx zard-cli@latest add switch

Usage

import { ZardSwitchComponent } from '@/shared/components/switch/switch.component';
Copy
<z-switch></z-switch>
Copy

Examples

description

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

import { Component } from '@angular/core';

import { ZardFieldImports } from '@/shared/components/field/field.imports';
import { ZardSwitchComponent } from '@/shared/components/switch/switch.component';

@Component({
  selector: 'z-demo-switch-description',
  imports: [...ZardFieldImports, ZardSwitchComponent],
  template: `
    <div z-field zOrientation="horizontal" class="max-w-sm">
      <div z-field-content>
        <label z-field-label for="switch-focus-mode">Share across devices</label>
        <p z-field-description>Focus is shared across devices, and turns off when you leave the app.</p>
      </div>
      <z-switch zId="switch-focus-mode" />
    </div>
  `,
})
export class ZardDemoSwitchDescriptionComponent {}

choice card

Card-style selection where FieldLabel wraps the entire Field for a clickable card pattern.

import { Component } from '@angular/core';

import { ZardFieldImports } from '@/shared/components/field/field.imports';
import { ZardSwitchComponent } from '@/shared/components/switch/switch.component';

@Component({
  selector: 'z-demo-switch-choice-card',
  imports: [...ZardFieldImports, ZardSwitchComponent],
  template: `
    <div z-field-group class="w-full min-w-sm">
      <label z-field-label for="switch-share">
        <div z-field zOrientation="horizontal">
          <div z-field-content>
            <div z-field-title>Share across devices</div>
            <p z-field-description>Focus is shared across devices, and turns off when you leave the app.</p>
          </div>
          <z-switch zId="switch-share" />
        </div>
      </label>
      <label z-field-label for="switch-notifications">
        <div z-field zOrientation="horizontal">
          <div z-field-content>
            <div z-field-title>Enable notifications</div>
            <p z-field-description>Receive notifications when focus mode is enabled or disabled.</p>
          </div>
          <z-switch zId="switch-notifications" [zChecked]="true" />
        </div>
      </label>
    </div>
  `,
})
export class ZardDemoSwitchChoiceCardComponent {}

disabled

Add the zDisabled prop to the Switch component to disable the switch. Add the data-disabled prop to the Field component for styling.

import { Component } from '@angular/core';

import { ZardFieldImports } from '@/shared/components/field/field.imports';
import { ZardSwitchComponent } from '@/shared/components/switch/switch.component';

@Component({
  selector: 'z-demo-switch-disabled',
  imports: [...ZardFieldImports, ZardSwitchComponent],
  template: `
    <div z-field zOrientation="horizontal" data-disabled="true" class="w-fit">
      <z-switch zId="switch-disabled-unchecked" zDisabled />
      <label z-field-label for="switch-disabled-unchecked">Disabled</label>
    </div>
  `,
})
export class ZardDemoSwitchDisabledComponent {}

invalid

Add the zInvalid prop to the Switch component to indicate an invalid state. Add the data-invalid prop to the Field component for styling.

You must accept the terms and conditions to continue.

import { Component } from '@angular/core';

import { ZardFieldImports } from '@/shared/components/field/field.imports';
import { ZardSwitchComponent } from '@/shared/components/switch/switch.component';

@Component({
  selector: 'z-demo-switch-invalid',
  imports: [...ZardFieldImports, ZardSwitchComponent],
  template: `
    <div z-field zOrientation="horizontal" data-invalid="true" class="max-w-sm">
      <div z-field-content>
        <label z-field-label for="switch-terms">Accept terms and conditions</label>
        <p z-field-description>You must accept the terms and conditions to continue.</p>
      </div>
      <z-switch zId="switch-terms" zInvalid />
    </div>
  `,
})
export class ZardDemoSwitchInvalidComponent {}

size

Use the zSize prop to change the size of the switch.

import { Component } from '@angular/core';

import { ZardSwitchComponent } from '@/shared/components/switch/switch.component';

@Component({
  selector: 'z-demo-switch-size',
  imports: [ZardSwitchComponent],
  template: `
    <div class="grid w-full min-w-sm items-center justify-center gap-6">
      <z-switch zSize="sm">Small</z-switch>
      <z-switch>Default</z-switch>
    </div>
  `,
})
export class ZardDemoSwitchSizeComponent {}

API Reference

[z-switch]Component

A customizable switch with minimal configuration.

PropertyDescriptionTypeDefault
[class] Additional CSS classes ClassValue ''
[(zChecked)] Switch state (two-way binding) boolean false
[zDisabled] Switch disabled state boolean false
[zInvalid] Switch invalid state (sets aria-invalid) boolean false
[zId] Switch id string -
[zSize] Switch size 'default' | 'sm' 'default'
github iconwhatsapp icondiscord iconX icon

Made with in Brazil. Open source and available on GitHub .