Skip to content
Ndop UI

Toggle

A button that stays pressed or released, such as Bold in a text editor.

import { Component } from '@angular/core';
import { NdIcon } from '@ndop/core';
import { Toggle } from '@/ui/toggle/toggle';

/** An icon-only toggle, named for screen readers. */
@Component({
  selector: 'example-toggle-demo',
  imports: [NdIcon, Toggle],
  template: `
    <button ndToggle aria-label="Toggle bold">
      <svg ndIcon="bold"></svg>
    </button>
  `,
})
export class ToggleDemoExample {}

A toggle is a button that stays pressed or released, such as Bold in a text editor. It's announced with its pressed state, comes in a default and an outline style and three sizes, and binds with [(pressed)]. For several related toggles, such as formatting or alignment, use a Toggle Group, which adds arrow-key navigation.

Installation

npx @ndop/cli add toggle

Usage

import { Toggle } from '@/ui/toggle/toggle';
<button ndToggle aria-label="Toggle bold">
  <svg ndIcon="bold"></svg>
</button>

Examples

Toggle outline

The outline variant.

import { Component } from '@angular/core';
import { NdIcon } from '@ndop/core';
import { Toggle } from '@/ui/toggle/toggle';

/** The outline variant. */
@Component({
  selector: 'example-toggle-outline',
  imports: [NdIcon, Toggle],
  template: `
    <button ndToggle variant="outline" aria-label="Toggle italic">
      <svg ndIcon="italic"></svg>
    </button>
  `,
})
export class ToggleOutlineExample {}

Toggle with text

A toggle with an icon and a label.

import { Component, signal } from '@angular/core';
import { NdIcon } from '@ndop/core';
import { Toggle } from '@/ui/toggle/toggle';

/**
 * A toggle with an icon and a label, whose icon fills while it's pressed.
 */
@Component({
  selector: 'example-toggle-with-text',
  imports: [NdIcon, Toggle],
  template: `
    <button
      ndToggle
      variant="outline"
      size="sm"
      class="data-[state=on]:bg-transparent data-[state=on]:*:[svg]:fill-current"
      [(pressed)]="saved"
    >
      <svg ndIcon="bookmark"></svg>
      Bookmark
    </button>
  `,
})
export class ToggleWithTextExample {
  protected readonly saved = signal(false);
}

Toggle sizes

The small, default and large sizes.

import { Component } from '@angular/core';
import { NdIcon } from '@ndop/core';
import { Toggle } from '@/ui/toggle/toggle';

/** The small, default and large sizes. */
@Component({
  selector: 'example-toggle-sizes',
  imports: [NdIcon, Toggle],
  template: `
    <div class="flex items-center gap-2">
      <button ndToggle size="sm" aria-label="Toggle underline, small">
        <svg ndIcon="underline"></svg>
      </button>
      <button ndToggle aria-label="Toggle underline">
        <svg ndIcon="underline"></svg>
      </button>
      <button ndToggle size="lg" aria-label="Toggle underline, large">
        <svg ndIcon="underline"></svg>
      </button>
    </div>
  `,
})
export class ToggleSizesExample {}

Toggle disabled

A disabled toggle.

import { Component } from '@angular/core';
import { NdIcon } from '@ndop/core';
import { Toggle } from '@/ui/toggle/toggle';

/** A disabled toggle. */
@Component({
  selector: 'example-toggle-disabled',
  imports: [NdIcon, Toggle],
  template: `
    <button ndToggle disabled aria-label="Toggle underline">
      <svg ndIcon="underline"></svg>
    </button>
  `,
})
export class ToggleDisabledExample {}

Accessibility

Implements the Button pattern of the WAI-ARIA Authoring Practices.

Keyboard

Keys the component handles.

Key Action
SpacePresses or releases it.
EnterPresses or releases it.

Focus

  • Unchanged from a native button: activating it keeps focus on it.

Right-to-left

Nothing mirrors.

API reference

Toggle

button[ndToggle]

A toggle button (@ndop/primitives/toggle): pressed or released, announced with aria-pressed. Bind it with [(pressed)]. Keep its name the same in both states, such as aria-label="Toggle bold".

Manages aria-pressed.

Toggle binding Type Default
[variant]
Visual style.
'default' | 'outline''default'
[size]
Size.
'default' | 'sm' | 'lg''default'
[class]
Extra classes, merged last so they win.
string''
[defaultPressed]
Initial pressed when it isn't bound.
booleanfalse
[disabled]
Disables it. A disabled bound field disables it too.
booleanfalse
[(pressed)]
Whether it's pressed. Two-way bindable with [(pressed)].
booleanfalse
Data attribute Values
data-slottoggle
data-variant —
data-size —
data-state —
data-disabled —

Agent notes

Do

  • Give an icon-only toggle an aria-label, such as "Toggle bold".
  • Keep the same name in both states.

Don't

  • Don't use a toggle for an on/off setting; use a Switch.
  • Don't use it to trigger an action; use a Button.