Primitives

Toggle

Toggle a button on and off.

Import

Import the Toggle primitives from ng-primitives/toggle.

import { NgpToggle } from 'ng-primitives/toggle';

Usage

Assemble the toggle directives in your template.

<button ngpToggle [(ngpToggleSelected)]="selected">Toggle</button>

Reusable Component

Create a reusable component that uses the toggle directives.

import { Component, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { Toggle } from './toggle';

@Component({
  selector: 'app-root',
  imports: [Toggle, FormsModule],
  template: `
    <button [(ngModel)]="selected" app-toggle>Toggle</button>
  `,
})
export default class App {
  selected = signal(false);
}

Schematics

Generate a reusable toggle component using the Angular CLI.

ng g ng-primitives:primitive toggle

Options

  • path: The path at which to create the component file.
  • prefix: The prefix to apply to the generated component selector.
  • componentSuffix: The suffix to apply to the generated component class name.
  • fileSuffix: The suffix to apply to the generated component file name. Defaults to component.
  • exampleStyles: Whether to include example styles in the generated component file. Defaults to true.

API Reference

The following directives are available to import from the ng-primitives/toggle package:

NgpToggle

Apply the ngpToggle directive to an element to manage the toggle state. This must be applied to a button element.

  • Selector: [ngpToggle]
  • Exported As: ngpToggle
ngpToggleSelected
boolean
default: "false"

Define the selected state.

ngpToggleDisabled
boolean
default: "false"

Define the disabled state.

ngpToggleSelectedChange
boolean

Event emitted when the selected state changes.

Data Attributes

The following data attributes are applied to the ngpToggle directive:

Attribute Description
data-selected Applied when the toggle is selected.
data-disabled Applied when the toggle is disabled.

Accessibility

Keyboard Interaction

  • Space: Toggles the selected state.
  • Enter: Toggles the selected state.

Copyright © 2025 Angular Primitives