Primitives
Perform state toggling.
import { Component } from "@angular/core";
import { NgpSwitch, NgpSwitchThumb } from "ng-primitives/switch";
@Component({
selector: "app-switch",
imports: [NgpSwitch, NgpSwitchThumb],
styles: `
[ngpSwitch] {
position: relative;
width: 2.5rem;
height: 1.5rem;
border-radius: 999px;
background-color: var(--ngp-background-secondary);
border: 1px solid var(--ngp-border);
padding: 0;
outline: none;
transition-property: color, background-color, border-color,
text-decoration-color, fill, stroke;
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
transition-duration: 150ms;
}
[ngpSwitch][data-focus-visible] {
outline: 2px solid var(--ngp-focus-ring);
}
[ngpSwitch][data-checked] {
background-color: var(--ngp-background-inverse);
border-color: var(--ngp-border-inverse);
}
[ngpSwitchThumb] {
display: block;
height: 1.25rem;
width: 1.25rem;
border-radius: 999px;
background-color: white;
box-shadow: var(--ngp-button-shadow);
outline: none;
transition: transform 150ms cubic-bezier(0.4, 0, 0.2, 1);
transform: translateX(1px);
}
[ngpSwitchThumb][data-checked] {
transform: translateX(17px);
}
`,
template: `
<button ngpSwitch>
<span ngpSwitchThumb></span>
</button>
`,
})
export default class SwitchExample {}
Import the Switch primitives from ng-primitives/switch
.
import { NgpSwitch, NgpSwitchThumb } from 'ng-primitives/switch';
Assemble the switch directives in your template.
<button ngpSwitch [(ngpSwitchChecked)]="checked">
<span ngpSwitchThumb></span>
</button>
Create a reusable component that uses the switch directives.
import { Component, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { Switch } from './switch';
@Component({
selector: 'app-root',
imports: [Switch, FormsModule],
template: `
<app-switch [(ngModel)]="checked" aria-label="Toggle Switch" />
`,
})
export default class App {
checked = signal(true);
}
Generate a reusable switch component using the Angular CLI.
ng g ng-primitives:primitive switch
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
.Here are some additional examples of how to use the Switch primitives.
The switch automatically integrates with the form field primitives.
import { Component } from "@angular/core";
import { NgpFormField, NgpLabel } from "ng-primitives/form-field";
import { NgpSwitch, NgpSwitchThumb } from "ng-primitives/switch";
@Component({
selector: "app-switch-form-field",
imports: [NgpSwitch, NgpSwitchThumb, NgpFormField, NgpLabel],
styles: `
[ngpFormField] {
display: flex;
align-items: center;
gap: 1rem;
}
[ngpLabel] {
font-weight: 500;
color: var(--ngp-text-primary);
}
[ngpSwitch] {
position: relative;
width: 2.5rem;
height: 1.5rem;
border-radius: 999px;
background-color: var(--ngp-background-secondary);
border: 1px solid var(--ngp-border);
padding: 0;
outline: none;
transition-property: color, background-color, border-color,
text-decoration-color, fill, stroke;
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
transition-duration: 150ms;
}
[ngpSwitch][data-focus-visible] {
outline: 2px solid var(--ngp-focus-ring);
}
[ngpSwitch][data-checked] {
background-color: var(--ngp-background-blue);
border-color: var(--ngp-border-blue);
}
[ngpSwitchThumb] {
display: block;
height: 1.25rem;
width: 1.25rem;
border-radius: 999px;
background-color: white;
box-shadow: var(--ngp-button-shadow);
outline: none;
transition: transform 150ms cubic-bezier(0.4, 0, 0.2, 1);
transform: translateX(1px);
}
[ngpSwitchThumb][data-checked] {
transform: translateX(17px);
}
`,
template: `
<div ngpFormField>
<label ngpLabel>Mobile Data</label>
<button ngpSwitch>
<span ngpSwitchThumb></span>
</button>
</div>
`,
})
export default class SwitchFormFieldExample {}
The following directives are available to import from the ng-primitives/switch
package:
Apply the `ngpSwitch` directive to an element to manage the checked state. Determine if the switch is checked.
Determine if the switch is disabled.
Emits when the checked state changes.
[ngpSwitch]
ngpSwitch
The following data attributes are applied to the ngpSwitch
directive:
Attribute | Description |
---|---|
data-checked |
Applied when the switch is checked. |
data-disabled |
Applied when the switch is disabled. |
data-hover |
Applied when the switch is hovered. |
data-focus-visible |
Applied when the switch is focused. |
data-press |
Applied when the switch is pressed. |
Apply the `ngpSwitchThumb` directive to an element within a switch to represent the thumb.[ngpSwitchThumb]
The following data attributes are available to style the thumb:
Attribute | Description |
---|---|
data-checked |
Applied when the switch is checked. |
data-disabled |
Applied when the switch is disabled. |
data-hover |
Applied when the switch thumb is hovered. |
data-focus-visible |
Applied when the switch thumb is focused. |
data-press |
Applied when the switch thumb is pressed. |
Adheres to the WAI-ARIA switch design pattern.
Copyright © 2025 Angular Primitives