checkbox

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

PreviousNext

By clicking this checkbox, you agree to the terms.

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

import { ZardCheckboxComponent } from '@/shared/components/checkbox/checkbox.component';
import { ZardFieldImports } from '@/shared/components/field/field.imports';

@Component({
  selector: 'z-demo-checkbox-default',
  imports: [ZardCheckboxComponent, ...ZardFieldImports, FormsModule],
  template: `
    <div z-field-group class="min-w-sm">
      <div z-field zOrientation="horizontal">
        <z-checkbox zId="terms-checkbox" [(ngModel)]="terms" />
        <label z-field-label for="terms-checkbox">Accept terms and conditions</label>
      </div>
      <div z-field zOrientation="horizontal">
        <z-checkbox zId="terms-checkbox-2" [(ngModel)]="termsWithDesc" />
        <div z-field-content>
          <label z-field-label for="terms-checkbox-2">Accept terms and conditions</label>
          <p z-field-description>By clicking this checkbox, you agree to the terms.</p>
        </div>
      </div>
      <div z-field zOrientation="horizontal" data-disabled="true">
        <z-checkbox zId="toggle-checkbox" zDisabled />
        <label z-field-label for="toggle-checkbox">Enable notifications</label>
      </div>
      <label z-field-label>
        <div z-field zOrientation="horizontal">
          <z-checkbox zId="toggle-checkbox-2" [(ngModel)]="notifications" />
          <div z-field-content>
            <div z-field-title>Enable notifications</div>
            <p z-field-description>You can enable or disable notifications at any time.</p>
          </div>
        </div>
      </label>
    </div>
  `,
})
export class ZardDemoCheckboxDefaultComponent {
  terms = false;
  termsWithDesc = true;
  notifications = false;
}

Installation

Copy
npx zard-cli@latest add checkbox

Usage

import { ZardCheckboxComponent } from '@/shared/components/checkbox/checkbox.component';
Copy
<z-checkbox zLabel="Accept terms and conditions"></z-checkbox>
Copy

Examples

invalid

Set aria-invalid on the checkbox and data-invalid on the field wrapper to show the invalid styles.

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

import { ZardCheckboxComponent } from '@/shared/components/checkbox/checkbox.component';
import { ZardFieldImports } from '@/shared/components/field/field.imports';

@Component({
  selector: 'z-demo-checkbox-invalid',
  imports: [ZardCheckboxComponent, ...ZardFieldImports, FormsModule],
  template: `
    <div z-field-group class="mx-auto w-56">
      <div z-field zOrientation="horizontal" data-invalid="true">
        <z-checkbox zId="terms-checkbox-invalid" [(ngModel)]="terms" zInvalid />
        <label z-field-label for="terms-checkbox-invalid">Accept terms and conditions</label>
      </div>
    </div>
  `,
})
export class ZardDemoCheckboxInvalidComponent {
  terms = false;
}

basic

Pair the checkbox with Field and FieldLabel for proper layout and labeling.

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

import { ZardCheckboxComponent } from '@/shared/components/checkbox/checkbox.component';
import { ZardFieldImports } from '@/shared/components/field/field.imports';

@Component({
  selector: 'z-demo-checkbox-basic',
  imports: [ZardCheckboxComponent, ...ZardFieldImports, FormsModule],
  template: `
    <div z-field-group class="mx-auto w-56">
      <div z-field zOrientation="horizontal">
        <z-checkbox zId="terms-checkbox-basic" [(ngModel)]="terms" />
        <label z-field-label for="terms-checkbox-basic">Accept terms and conditions</label>
      </div>
    </div>
  `,
})
export class ZardDemoCheckboxBasicComponent {
  terms = false;
}

description

Use FieldContent and FieldDescription for helper text.

By clicking this checkbox, you agree to the terms and conditions.

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

import { ZardCheckboxComponent } from '@/shared/components/checkbox/checkbox.component';
import { ZardFieldImports } from '@/shared/components/field/field.imports';

@Component({
  selector: 'z-demo-checkbox-description',
  imports: [ZardCheckboxComponent, ...ZardFieldImports, FormsModule],
  template: `
    <div z-field-group class="mx-auto w-72">
      <div z-field zOrientation="horizontal">
        <z-checkbox zId="terms-checkbox-desc" [(ngModel)]="terms" />
        <div z-field-content>
          <label z-field-label for="terms-checkbox-desc">Accept terms and conditions</label>
          <p z-field-description>By clicking this checkbox, you agree to the terms and conditions.</p>
        </div>
      </div>
    </div>
  `,
})
export class ZardDemoCheckboxDescriptionComponent {
  terms = true;
}

disabled

Use the disabled prop to prevent interaction and add the data-disabled attribute to the Field component for disabled styles.

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

import { ZardCheckboxComponent } from '@/shared/components/checkbox/checkbox.component';
import { ZardFieldImports } from '@/shared/components/field/field.imports';

@Component({
  selector: 'z-demo-checkbox-disabled',
  imports: [ZardCheckboxComponent, ...ZardFieldImports],
  template: `
    <div z-field-group class="mx-auto w-56">
      <div z-field zOrientation="horizontal" data-disabled="true">
        <z-checkbox zId="toggle-checkbox-disabled" zDisabled />
        <label z-field-label for="toggle-checkbox-disabled">Enable notifications</label>
      </div>
    </div>
  `,
})
export class ZardDemoCheckboxDisabledComponent {}

group

Use multiple fields to create a checkbox list.

Show these items on the desktop:

Select the items you want to show on the desktop.

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

import { ZardCheckboxComponent } from '@/shared/components/checkbox/checkbox.component';
import { ZardFieldImports } from '@/shared/components/field/field.imports';

@Component({
  selector: 'z-demo-checkbox-group',
  imports: [ZardCheckboxComponent, ...ZardFieldImports, FormsModule],
  template: `
    <fieldset z-field-set>
      <legend z-field-legend zVariant="label">Show these items on the desktop:</legend>
      <p z-field-description>Select the items you want to show on the desktop.</p>
      <div z-field-group class="gap-3">
        <div z-field zOrientation="horizontal">
          <z-checkbox zId="finder-pref-9k2-hard-disks-ljj-checkbox" [(ngModel)]="hardDisks" />
          <label z-field-label for="finder-pref-9k2-hard-disks-ljj-checkbox" class="font-normal">Hard disks</label>
        </div>
        <div z-field zOrientation="horizontal">
          <z-checkbox zId="finder-pref-9k2-external-disks-1yg-checkbox" [(ngModel)]="externalDisks" />
          <label z-field-label for="finder-pref-9k2-external-disks-1yg-checkbox" class="font-normal">
            External disks
          </label>
        </div>
        <div z-field zOrientation="horizontal">
          <z-checkbox zId="finder-pref-9k2-cds-dvds-fzt-checkbox" [(ngModel)]="cds" />
          <label z-field-label for="finder-pref-9k2-cds-dvds-fzt-checkbox" class="font-normal">
            CDs, DVDs, and iPods
          </label>
        </div>
        <div z-field zOrientation="horizontal">
          <z-checkbox zId="finder-pref-9k2-connected-servers-6l2-checkbox" [(ngModel)]="connectedServers" />
          <label z-field-label for="finder-pref-9k2-connected-servers-6l2-checkbox" class="font-normal">
            Connected servers
          </label>
        </div>
      </div>
    </fieldset>
  `,
})
export class ZardDemoCheckboxGroupComponent {
  hardDisks = true;
  externalDisks = true;
  cds = false;
  connectedServers = false;
}

table

Combine the checkbox with the Table component for selectable rows.

NameEmailRole
Sarah Chensarah.chen@example.comAdmin
Marcus Rodriguezmarcus.rodriguez@example.comUser
Priya Patelpriya.patel@example.comUser
David Kimdavid.kim@example.comEditor
import { Component, computed, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { ZardCheckboxComponent } from '@/shared/components/checkbox/checkbox.component';
import { ZardTableImports } from '@/shared/components/table/table.imports';

interface Row {
  id: string;
  name: string;
  email: string;
  role: string;
}

const TABLE_DATA: readonly Row[] = [
  { id: '1', name: 'Sarah Chen', email: 'sarah.chen@example.com', role: 'Admin' },
  { id: '2', name: 'Marcus Rodriguez', email: 'marcus.rodriguez@example.com', role: 'User' },
  { id: '3', name: 'Priya Patel', email: 'priya.patel@example.com', role: 'User' },
  { id: '4', name: 'David Kim', email: 'david.kim@example.com', role: 'Editor' },
];

@Component({
  selector: 'z-demo-checkbox-table',
  imports: [ZardCheckboxComponent, ...ZardTableImports, FormsModule],
  template: `
    <table z-table>
      <thead z-table-header>
        <tr z-table-row>
          <th z-table-head class="w-8">
            <z-checkbox zId="select-all-checkbox" [ngModel]="allSelected()" (checkChange)="toggleAll($event)" />
          </th>
          <th z-table-head>Name</th>
          <th z-table-head>Email</th>
          <th z-table-head>Role</th>
        </tr>
      </thead>
      <tbody z-table-body>
        @for (row of rows; track row.id) {
          <tr z-table-row [attr.data-state]="isSelected(row.id) ? 'selected' : null">
            <td z-table-cell>
              <z-checkbox
                [zId]="'row-' + row.id + '-checkbox'"
                [ngModel]="isSelected(row.id)"
                (checkChange)="toggleRow(row.id, $event)"
              />
            </td>
            <td z-table-cell class="font-medium">{{ row.name }}</td>
            <td z-table-cell>{{ row.email }}</td>
            <td z-table-cell>{{ row.role }}</td>
          </tr>
        }
      </tbody>
    </table>
  `,
})
export class ZardDemoCheckboxTableComponent {
  protected readonly rows = TABLE_DATA;
  private readonly selectedRows = signal<ReadonlySet<string>>(new Set(['1']));

  protected readonly allSelected = computed(() => this.selectedRows().size === this.rows.length);

  protected isSelected(id: string): boolean {
    return this.selectedRows().has(id);
  }

  protected toggleAll(checked: boolean): void {
    this.selectedRows.set(checked ? new Set(this.rows.map(row => row.id)) : new Set());
  }

  protected toggleRow(id: string, checked: boolean): void {
    const next = new Set(this.selectedRows());
    if (checked) {
      next.add(id);
    } else {
      next.delete(id);
    }
    this.selectedRows.set(next);
  }
}

API Reference

z-checkboxComponent

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

PropertyDescriptionTypeDefault
[class] Additional CSS classes ClassValue ''
[zDisabled] Checkbox disabled state boolean false
[zInvalid] Checkbox invalid state (sets aria-invalid) boolean false
[zId] Checkbox id string auto-generated
(checkChange) Emits when the checkbox value changes EventEmitter<boolean> -
github iconwhatsapp icondiscord iconX icon

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