Primitives

Switch

Perform state toggling.

Import

Import the Switch primitives from ng-primitives/switch.

import { NgpSwitch, NgpSwitchThumb } from 'ng-primitives/switch';

Usage

Assemble the switch directives in your template.

<button ngpSwitch [(ngpSwitchChecked)]="checked">
  <span ngpSwitchThumb></span>
</button>

Reusable Component

Create a reusable component that uses the switch directives.

Schematics

Generate a reusable switch component using the Angular CLI.

ng g ng-primitives:primitive switch

Options

  • path: The path at which to create the component file.
  • prefix: The prefix to apply to the generated component selector.
  • component-suffix: The suffix to apply to the generated component class name.
  • file-suffix: The suffix to apply to the generated component file name. Defaults to component.
  • styles: How component styles should be generated. css (default) includes the full example styles; unstyled omits them entirely so you can style the component yourself.
  • example-styles (deprecated): still supported for compatibility - true maps to styles: css, false maps to styles: unstyled.

Examples

Here are some additional examples of how to use the Switch primitives.

Switch Form Field

The switch automatically integrates with the form field primitives.

API Reference

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

NgpSwitch

NgpSwitchThumb

Accessibility

Adheres to the WAI-ARIA switch design pattern.

Keyboard Interactions

  • Space - Toggle the switch state (when switch is a button).
  • Enter - Toggle the switch state.

Copyright © 2026 Angular Primitives

This site is powered by Netlify