Primitives
Selection within a group.
Indie Plan For those who want to are just starting out Growth Plan For those who want to grow their business Unicorn Plan For those who are going to the moonimport { Component, signal } from "@angular/core";
import {
NgpRadioGroup,
NgpRadioIndicator,
NgpRadioItem,
} from "ng-primitives/radio";
@Component({
selector: "app-radio",
imports: [NgpRadioGroup, NgpRadioItem, NgpRadioIndicator],
styles: `
[ngpRadioGroup] {
display: flex;
flex-direction: column;
row-gap: 1rem;
}
[ngpRadioItem] {
display: grid;
cursor: pointer;
grid-template-columns: auto 1fr;
grid-template-rows: repeat(2, auto);
column-gap: 0.625rem;
row-gap: 0.125rem;
border-radius: 0.5rem;
background-color: var(--ngp-background);
padding: 0.75rem 1rem;
box-shadow: var(--ngp-button-shadow);
outline: none;
}
[ngpRadioItem][data-hover] {
background-color: var(--ngp-background-hover);
}
[ngpRadioItem][data-focus-visible] {
outline: 2px solid var(--ngp-focus-ring);
outline-offset: 2px;
}
[ngpRadioItem][data-press] {
background-color: var(--ngp-background-active);
}
[ngpRadioItem][data-checked] {
background-color: var(--ngp-background-inverse);
}
[ngpRadioIndicator] {
display: inline-flex;
grid-column-start: 1;
grid-row-start: 1;
justify-content: center;
align-items: center;
align-self: center;
border-radius: 9999px;
width: 1rem;
height: 1rem;
border: 1px solid var(--ngp-border);
}
.indicator-dot {
width: 0.5rem;
height: 0.5rem;
border-radius: 9999px;
background-color: transparent;
}
[ngpRadioItem][data-checked] .indicator-dot {
background-color: var(--ngp-background);
}
.title {
grid-column-start: 2;
grid-row-start: 1;
font-weight: 500;
color: var(--ngp-text-primary);
margin: 0;
}
[ngpRadioItem][data-checked] .title {
color: var(--ngp-text-inverse);
}
.description {
grid-column-start: 2;
grid-row-start: 2;
font-size: 0.75rem;
color: var(--ngp-text-secondary);
line-height: 1rem;
margin: 0;
}
[ngpRadioItem][data-checked] .description {
color: var(--ngp-text-inverse);
}
`,
template: `
<div
[(ngpRadioGroupValue)]="plan"
ngpRadioGroup
ngpRadioGroupOrientation="vertical"
>
<div ngpRadioItem ngpRadioItemValue="indie">
<div ngpRadioIndicator>
<span class="indicator-dot"></span>
</div>
<p class="title">Indie Plan</p>
<p class="description">For those who want to are just starting out</p>
</div>
<div ngpRadioItem ngpRadioItemValue="growth">
<div ngpRadioIndicator>
<span class="indicator-dot"></span>
</div>
<p class="title">Growth Plan</p>
<p class="description">For those who want to grow their business</p>
</div>
<div ngpRadioItem ngpRadioItemValue="unicorn">
<div ngpRadioIndicator>
<span class="indicator-dot"></span>
</div>
<p class="title">Unicorn Plan</p>
<p class="description">For those who are going to the moon</p>
</div>
</div>
`,
})
export default class RadioExample {
/**
* Store the selected plan.
*/
readonly plan = signal<Plan>("indie");
}
type Plan = "indie" | "growth" | "unicorn";
Import the Radio primitives from ng-primitives/radio
.
import { NgpRadioGroup, NgpRadioItem, NgpRadioIndicator } from 'ng-primitives/radio';
Assemble the radio directives in your template.
<div ngpRadioGroup [(ngpRadioGroupValue)]="value">
<button ngpRadioItem ngpRadioItemValue="Option 1">
<ng-icon ngpRadioIndicator name="dot" />
Option 1
</button>
<button ngpRadioItem ngpRadioItemValue="Option 2">
<ng-icon ngpRadioIndicator name="dot" />
Option 2
</button>
<button ngpRadioItem ngpRadioItemValue="Option 3">
<ng-icon ngpRadioIndicator name="dot" />
Option 3
</button>
</div>
Create a reusable component that uses the radio directives.
import { Component, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { RadioGroup } from './radio-group';
import { RadioItem } from './radio-item';
@Component({
selector: 'app-root',
imports: [RadioGroup, RadioItem, FormsModule],
template: `
<app-radio-group [(ngModel)]="value" orientation="vertical">
<app-radio-item value="1">One</app-radio-item>
<app-radio-item value="2">Two</app-radio-item>
<app-radio-item value="3">Three</app-radio-item>
</app-radio-group>
`,
})
export default class App {
value = signal('1');
}
Generate a reusable radio component using the Angular CLI.
ng g ng-primitives:primitive radio
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
.The following directives are available to import from the ng-primitives/radio
package:
Apply the `ngpRadioGroup` directive to an element that represents the group of radio items. The value of the radio group.
Whether the radio group is disabled.
The orientation of the radio group.
The comparator function for the radio group. This is useful if values are objects and you want to compare them by value, not by reference.
Event emitted when the radio group value changes.
[ngpRadioGroup]
The following data attributes are applied to the ngpRadioGroup
directive:
Attribute | Description | Value |
---|---|---|
data-orientation |
The orientation of the radio group. | vertical | horizontal |
data-disabled |
Applied when the radio group is disabled. | - |
Apply the `ngpRadioItem` directive to an element that represents a radio item. This would typically be a `button` element. The value of the radio item.
Whether the radio item is disabled.
[ngpRadioItem]
The following data attributes are applied to the ngpRadioItem
directive:
Attribute | Description |
---|---|
data-checked |
Applied when the radio item is checked. |
data-disabled |
Applied when the radio item is disabled. |
data-hover |
Applied when the radio item is hovered. |
data-focus-visible |
Applied when the radio item is focused. |
data-press |
Applied when the radio item is pressed. |
Apply the `ngpRadioIndicator` directive to an element that represents the radio indicator (i.e. the dot).[ngpRadioIndicator]
The following data attributes are applied to the ngpRadioItem
directive:
Attribute | Description |
---|---|
data-checked |
Applied when the radio item is checked. |
data-disabled |
Applied when the radio item is disabled. |
data-hover |
Applied when the radio item is hovered. |
data-press |
Applied when the radio item is pressed. |
Adheres to the Radio Group WAI-ARIA design pattern.
Copyright © 2025 Angular Primitives