Getting Started
UI
Components
- Accordion
- Alert
- Alert Dialog
- Aspect Ratio
- Attachment
- Autocomplete
- Avatar
- Badge
- Breadcrumb
- Bubble
- Button
- Button Group
- Calendar
- Card
- Carousel
- Chart
- Checkbox
- Code Block
- Collapsible
- Combobox
- Command
- Context Menu
- Data Table
- Date Picker
- Dialog
- Drawer
- Dropdown Menu
- Empty
- Field
- Hover Card
- Input Group
- Input OTP
- Input
- Item
- Kbd
- Label
- Marker
- Menubar
- Message
- Message Scroller
- Native Select
- Navigation Menu
- Pagination
- Popover
- Progress
- Questionnaire
- Radio Group
- Resizable
- Scroll Area
- Select
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Sonner (Toast)
- Spinner
- Switch
- Table
- Tabs
- Textarea
- Toggle
- Toggle Group
- Tooltip
- Trace
Utilities
Forms
Stack
Toggle Group
A group of toggle buttons.
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { lucideBold, lucideItalic, lucideUnderline } from '@ng-icons/lucide';
import { HlmToggleGroupImports } from '@spartan-ng/helm/toggle-group';
@Component({
selector: 'spartan-toggle-group-preview',
imports: [HlmToggleGroupImports, NgIcon],
providers: [provideIcons({ lucideBold, lucideItalic, lucideUnderline })],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<hlm-toggle-group type="multiple" variant="outline">
<button hlmToggleGroupItem value="bold" aria-label="Toggle bold">
<ng-icon name="lucideBold" />
</button>
<button hlmToggleGroupItem value="italic" aria-label="Toggle italic">
<ng-icon name="lucideItalic" />
</button>
<button hlmToggleGroupItem value="underline" aria-label="Toggle underline">
<ng-icon name="lucideUnderline" />
</button>
</hlm-toggle-group>
`,
})
export class ToggleGroupPreview {}Installation
ng g @spartan-ng/cli:ui toggle-groupnx g @spartan-ng/cli:ui toggle-groupUsage
import { HlmToggleGroupImports } from '@spartan-ng/helm/toggle-group';<hlm-toggle-group type="single">
<button hlmToggleGroupItem value="a">A</button>
<button hlmToggleGroupItem value="b">B</button>
<button hlmToggleGroupItem value="c">C</button>
</hlm-toggle-group>Examples
Outline
Use variant="outline" for an outline style.
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { HlmToggleGroupImports } from '@spartan-ng/helm/toggle-group';
@Component({
selector: 'spartan-toggle-group-outline',
imports: [HlmToggleGroupImports],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<hlm-toggle-group variant="outline" value="all">
<button hlmToggleGroupItem value="all" aria-label="Toggle all">All</button>
<button hlmToggleGroupItem value="missed" aria-label="Toggle missed">Missed</button>
</hlm-toggle-group>
`,
})
export class ToggleGroupOutlinePreview {}Size
Use the size prop to change the size of the toggle group.
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { HlmToggleGroupImports } from '@spartan-ng/helm/toggle-group';
@Component({
selector: 'spartan-toggle-group-size',
imports: [HlmToggleGroupImports],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<div class="flex flex-col gap-4">
<hlm-toggle-group type="single" size="sm" variant="outline" value="top">
<button hlmToggleGroupItem value="top" aria-label="Toggle top">Top</button>
<button hlmToggleGroupItem value="bottom" aria-label="Toggle bottom">Bottom</button>
<button hlmToggleGroupItem value="left" aria-label="Toggle left">Left</button>
<button hlmToggleGroupItem value="right" aria-label="Toggle right">Right</button>
</hlm-toggle-group>
<hlm-toggle-group type="single" variant="outline" value="top">
<button hlmToggleGroupItem value="top" aria-label="Toggle top">Top</button>
<button hlmToggleGroupItem value="bottom" aria-label="Toggle bottom">Bottom</button>
<button hlmToggleGroupItem value="left" aria-label="Toggle left">Left</button>
<button hlmToggleGroupItem value="right" aria-label="Toggle right">Right</button>
</hlm-toggle-group>
</div>
`,
})
export class ToggleGroupSizePreview {}Spacing
Use spacing="2" to add spacing between toggle group items.
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { HlmToggleGroupImports } from '@spartan-ng/helm/toggle-group';
@Component({
selector: 'spartan-toggle-group-spacing',
imports: [HlmToggleGroupImports],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<hlm-toggle-group type="single" size="sm" variant="outline" spacing="2" value="top">
<button hlmToggleGroupItem value="top" aria-label="Toggle top">Top</button>
<button hlmToggleGroupItem value="bottom" aria-label="Toggle bottom">Bottom</button>
<button hlmToggleGroupItem value="left" aria-label="Toggle left">Left</button>
<button hlmToggleGroupItem value="right" aria-label="Toggle right">Right</button>
</hlm-toggle-group>
`,
})
export class ToggleGroupSpacingPreview {}Vertical
Use orientation="vertical" for vertical toggle groups.
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { lucideBold, lucideItalic, lucideUnderline } from '@ng-icons/lucide';
import { HlmToggleGroupImports } from '@spartan-ng/helm/toggle-group';
@Component({
selector: 'spartan-toggle-group-vertical',
imports: [HlmToggleGroupImports, NgIcon],
providers: [provideIcons({ lucideBold, lucideItalic, lucideUnderline })],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<hlm-toggle-group type="multiple" orientation="vertical" spacing="1" [value]="['bold', 'italic']">
<button hlmToggleGroupItem value="bold" aria-label="Toggle bold">
<ng-icon name="lucideBold" />
</button>
<button hlmToggleGroupItem value="italic" aria-label="Toggle italic">
<ng-icon name="lucideItalic" />
</button>
<button hlmToggleGroupItem value="underline" aria-label="Toggle underline">
<ng-icon name="lucideUnderline" />
</button>
</hlm-toggle-group>
`,
})
export class ToggleGroupVerticalPreview {}Disabled
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { lucideBold, lucideItalic, lucideUnderline } from '@ng-icons/lucide';
import { HlmToggleGroupImports } from '@spartan-ng/helm/toggle-group';
@Component({
selector: 'spartan-toggle-group-disabled',
imports: [HlmToggleGroupImports, NgIcon],
providers: [provideIcons({ lucideBold, lucideItalic, lucideUnderline })],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<hlm-toggle-group type="multiple" disabled>
<button hlmToggleGroupItem value="bold" aria-label="Toggle bold">
<ng-icon name="lucideBold" />
</button>
<button hlmToggleGroupItem value="italic" aria-label="Toggle italic">
<ng-icon name="lucideItalic" />
</button>
<button hlmToggleGroupItem value="underline" aria-label="Toggle underline">
<ng-icon name="lucideUnderline" />
</button>
</hlm-toggle-group>
`,
})
export class ToggleGroupDisabledPreview {}Custom
font-normal to set the font weight. import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmToggleGroupImports } from '@spartan-ng/helm/toggle-group';
import { hlmCode } from '@spartan-ng/helm/typography';
@Component({
selector: 'spartan-toggle-group-custom',
imports: [HlmToggleGroupImports, HlmFieldImports],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<hlm-field>
<label hlmFieldLabel>Font Weight</label>
<hlm-toggle-group variant="outline" value="all" spacing="2" size="lg" [(value)]="fontWeight">
@for (weight of fontWeights; track $index) {
<button
hlmToggleGroupItem
[value]="weight.value"
[attr.aria-label]="weight.label"
class="flex size-16 flex-col items-center justify-center rounded-xl"
>
<span class="text-2xl leading-none" [class]="weight.class">Aa</span>
<span class="text-muted-foreground text-xs">{{ weight.label }}</span>
</button>
}
</hlm-toggle-group>
<hlm-field-description>
Use
<code class="${hlmCode}">font-{{ fontWeight() }}</code>
to set the font weight.
</hlm-field-description>
</hlm-field>
`,
})
export class ToggleGroupCustomPreview {
public fontWeights = [
{ label: 'Light', value: 'light', class: 'font-light' },
{ label: 'Normal', value: 'normal', class: 'font-normal' },
{ label: 'Medium', value: 'medium', class: 'font-medium' },
{ label: 'Bold', value: 'bold', class: 'font-bold' },
];
public fontWeight = signal('normal');
}Form
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { form, FormField, FormRoot, required } from '@angular/forms/signals';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { lucideBookmark, lucideHeart, lucideStar } from '@ng-icons/lucide';
import { HlmButtonImports } from '@spartan-ng/helm/button';
import { HlmToggleGroupImports } from '@spartan-ng/helm/toggle-group';
@Component({
selector: 'spartan-toggle-group-form',
imports: [HlmToggleGroupImports, HlmButtonImports, NgIcon, FormRoot, FormField],
providers: [provideIcons({ lucideStar, lucideHeart, lucideBookmark })],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<form class="space-y-6" [formRoot]="form">
<hlm-toggle-group [formField]="form.action" type="single" variant="outline" spacing="2" size="sm">
<button
hlmToggleGroupItem
value="star"
aria-label="Toggle star"
class="data-[state=on]:bg-transparent data-[state=on]:*:[ng-icon]:*:[svg]:fill-yellow-500 data-[state=on]:*:[ng-icon]:*:[svg]:stroke-yellow-500"
>
<ng-icon name="lucideStar" />
Star
</button>
<button
hlmToggleGroupItem
value="heart"
aria-label="Toggle heart"
class="data-[state=on]:bg-transparent data-[state=on]:*:[ng-icon]:*:[svg]:fill-red-500 data-[state=on]:*:[ng-icon]:*:[svg]:stroke-red-500"
>
<ng-icon name="lucideHeart" />
Heart
</button>
<button
hlmToggleGroupItem
value="bookmark"
aria-label="Toggle bookmark"
class="data-[state=on]:bg-transparent data-[state=on]:*:[ng-icon]:*:[svg]:fill-blue-500 data-[state=on]:*:[ng-icon]:*:[svg]:stroke-blue-500"
>
<ng-icon name="lucideBookmark" />
Bookmark
</button>
</hlm-toggle-group>
<button hlmBtn type="submit">Submit</button>
</form>
`,
})
export class ToggleGroupSpacingForm {
protected readonly _model = signal({
action: 'star',
});
public readonly form = form(
this._model,
(schemaPath) => {
required(schemaPath.action, { message: 'Please select an action' });
},
{
submission: {
action: async () => {
const model = this._model();
console.log(model);
},
},
},
);
}RTL
To enable RTL support in spartan-ng, see the RTL configuration guide.
import { ChangeDetectionStrategy, Component, computed, inject } from '@angular/core';
import { TranslateService, Translations } from '@spartan-ng/app/app/shared/translate.service';
import { HlmToggleGroupImports } from '@spartan-ng/helm/toggle-group';
@Component({
selector: 'spartan-toggle-group-rtl',
imports: [HlmToggleGroupImports],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<hlm-toggle-group variant="outline" value="list" [dir]="_dir()">
<button hlmToggleGroupItem value="list" [aria-label]="_t()['list']">
{{ _t()['list'] }}
</button>
<button hlmToggleGroupItem value="grid" [aria-label]="_t()['grid']">
{{ _t()['grid'] }}
</button>
<button hlmToggleGroupItem value="cards" [aria-label]="_t()['cards']">
{{ _t()['cards'] }}
</button>
</hlm-toggle-group>
`,
})
export class ToggleGroupRtlPreview {
private readonly _language = inject(TranslateService).language;
private readonly _translations: Translations = {
en: {
dir: 'ltr',
values: {
list: 'List',
grid: 'Grid',
cards: 'Cards',
},
},
ar: {
dir: 'rtl',
values: {
list: 'قائمة',
grid: 'شبكة',
cards: 'بطاقات',
},
},
he: {
dir: 'rtl',
values: {
list: 'רשימה',
grid: 'רשת',
cards: 'כרטיסים',
},
},
};
private readonly _translation = computed(() => this._translations[this._language()]);
protected readonly _t = computed(() => this._translation().values);
protected readonly _dir = computed(() => this._translation().dir);
}Brain API
BrnToggleGroup
Selector: [brnToggleGroup],brn-toggle-group
ExportAs: brnToggleGroup
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| type | ToggleType | 'single' | The type of the toggle group. |
| value | ToggleValue<T> | undefined | Value of the toggle group. |
| nullable | boolean | true | Whether no button toggles need to be selected. |
| disabled | boolean | false | Whether the button toggle group is disabled. |
Outputs
| Prop | Type | Default | Description |
|---|---|---|---|
| valueChange | ToggleValue<T> | - | Emits when the value changes. |
| change | BrnButtonToggleChange<T> | - | Emit event when the group value changes. |
BrnToggleGroupItem
Selector: button[brnToggleGroupItem]
ExportAs: brnToggleGroupItem
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| id | string | `brn-toggle-group-item-${++BrnToggleGroupItem._uniqueId}` | The id of the toggle. |
| value | T | - | The value this toggle represents. |
| disabled | boolean | false | Whether the toggle is disabled. |
| type | 'button' | 'submit' | 'reset' | 'button' | The type of the button. |
| aria-label | string | null | null | Accessibility label for screen readers. Use when no visible label exists. |
| state | 'on' | 'off' | 'off' | The current state of the toggle when not used in a group. |
Outputs
| Prop | Type | Default | Description |
|---|---|---|---|
| stateChange | 'on' | 'off' | 'off' | The current state of the toggle when not used in a group. |
Helm API
HlmToggleGroupItem
Selector: button[hlmToggleGroupItem]
ExportAs: hlmToggleGroupItem
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| variant | ToggleVariants['variant'] | 'default' | - |
| size | ToggleVariants['size'] | 'default' | - |
| id | string | `brn-toggle-group-item-${++BrnToggleGroupItem._uniqueId}` | The id of the toggle. |
| value | T | - | The value this toggle represents. |
| disabled | boolean | false | Whether the toggle is disabled. |
| state | 'on' | 'off' | 'off' | The current state of the toggle when not used in a group. |
| aria-label | string | null | null | Accessibility label for screen readers. Use when no visible label exists. |
| type | 'button' | 'submit' | 'reset' | 'button' | The type of the button. |
Outputs
| Prop | Type | Default | Description |
|---|---|---|---|
| stateChange | 'on' | 'off' | - | The current state of the toggle when not used in a group. |
HlmToggleGroup
Selector: [hlmToggleGroup],hlm-toggle-group
ExportAs: hlmToggleGroup
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| variant | ToggleVariants['variant'] | 'default' | - |
| size | ToggleVariants['size'] | 'default' | - |
| spacing | number | 0 | - |
| orientation | 'horizontal' | 'vertical' | 'horizontal' | - |
| type | ToggleType | 'single' | The type of the toggle group. |
| value | ToggleValue<T> | undefined | Value of the toggle group. |
| nullable | boolean | true | Whether no button toggles need to be selected. |
| disabled | boolean | false | Whether the button toggle group is disabled. |
Outputs
| Prop | Type | Default | Description |
|---|---|---|---|
| valueChange | ToggleValue<T> | - | Emits when the value changes. |
On This Page
Stop configuring. Start shipping.
Zerops powers spartan.ng and Angular teams worldwide.
One-command deployment. Zero infrastructure headaches.
Deploy with Zerops