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
Switch
A control that allows the user to toggle between checked and not checked.
import { Component } from '@angular/core';
import { HlmLabel } from '@spartan-ng/helm/label';
import { HlmSwitch } from '@spartan-ng/helm/switch';
@Component({
selector: 'spartan-switch-preview',
imports: [HlmLabel, HlmSwitch],
template: `
<label class="flex items-center" hlmLabel>
<hlm-switch class="mr-2" />
Airplane mode
</label>
`,
})
export class SwitchPreview {}Installation
ng g @spartan-ng/cli:ui switchnx g @spartan-ng/cli:ui switchUsage
import { HlmSwitch } from '@spartan-ng/helm/switch';<hlm-switch />Examples
Description
Focus is shared across devices, and turns off when you leave the app.
import { Component } from '@angular/core';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmSwitch } from '@spartan-ng/helm/switch';
@Component({
selector: 'spartan-switch-description',
imports: [HlmFieldImports, HlmSwitch],
template: `
<hlm-field orientation="horizontal" class="max-w-sm">
<hlm-field-content>
<label hlmFieldLabel for="switch-focus-mode">Share across devices</label>
<p hlmFieldDescription>Focus is shared across devices, and turns off when you leave the app.</p>
</hlm-field-content>
<hlm-switch inputId="switch-focus-mode" />
</hlm-field>
`,
})
export class SwitchDescription {}Choice Card
import { Component } from '@angular/core';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmSwitch } from '@spartan-ng/helm/switch';
@Component({
selector: 'spartan-switch-choice-card',
imports: [HlmFieldImports, HlmSwitch],
host: { class: 'min-w-xs sm:min-w-sm' },
template: `
<hlm-field-group class="w-full max-w-sm">
<label hlmFieldLabel for="switch-share">
<hlm-field orientation="horizontal">
<hlm-field-content>
<hlm-field-title for="switch-focus-mode">Share across devices</hlm-field-title>
<p hlmFieldDescription>Focus is shared across devices, and turns off when you leave the app.</p>
</hlm-field-content>
<hlm-switch inputId="switch-share" />
</hlm-field>
</label>
<label hlmFieldLabel for="switch-notifications">
<hlm-field orientation="horizontal">
<hlm-field-content>
<hlm-field-title for="switch-focus-mode">Enable notifications</hlm-field-title>
<p hlmFieldDescription>Receive notifications when focus mode is enabled or disabled.</p>
</hlm-field-content>
<hlm-switch inputId="switch-notifications" />
</hlm-field>
</label>
</hlm-field-group>
`,
})
export class SwitchChoiceCard {}Disabled
import { Component } from '@angular/core';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmSwitch } from '@spartan-ng/helm/switch';
@Component({
selector: 'spartan-switch-disabled',
imports: [HlmFieldImports, HlmSwitch],
template: `
<hlm-field orientation="horizontal" data-disabled="true">
<hlm-switch inputId="switch-disabled" disabled />
<label hlmFieldLabel for="switch-disabled">Disabled</label>
</hlm-field>
`,
})
export class SwitchDisabled {}Invalid
You must accept the terms and conditions to continue.
import { Component } from '@angular/core';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmSwitch } from '@spartan-ng/helm/switch';
@Component({
selector: 'spartan-switch-invalid',
imports: [HlmFieldImports, HlmSwitch],
template: `
<hlm-field orientation="horizontal" class="max-w-sm" forceInvalid>
<hlm-field-content>
<label hlmFieldLabel for="switch-invalid">Accept terms and conditions</label>
<p hlmFieldDescription>You must accept the terms and conditions to continue.</p>
</hlm-field-content>
<hlm-switch inputId="switch-invalid" forceInvalid />
</hlm-field>
`,
})
export class SwitchInvalid {}Sizes
import { Component } from '@angular/core';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmSwitch } from '@spartan-ng/helm/switch';
@Component({
selector: 'spartan-switch-sizes',
imports: [HlmFieldImports, HlmSwitch],
host: { class: 'w-full max-w-40' },
template: `
<hlm-field-group>
<hlm-field orientation="horizontal">
<hlm-switch inputId="switch-size-sm" size="sm" />
<label hlmFieldLabel for="switch-size-sm">Small</label>
</hlm-field>
<hlm-field orientation="horizontal">
<hlm-switch inputId="switch-size-default" />
<label hlmFieldLabel for="switch-size-default">Default</label>
</hlm-field>
</hlm-field-group>
`,
})
export class SwitchSizes {}RTL
To enable RTL support in spartan-ng, see the RTL configuration guide.
يتم مشاركة التركيز عبر الأجهزة، ويتم إيقاف تشغيله عند مغادرة التطبيق.
import { Component, computed, inject } from '@angular/core';
import { TranslateService, Translations } from '@spartan-ng/app/app/shared/translate.service';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmSwitch } from '@spartan-ng/helm/switch';
@Component({
selector: 'spartan-switch-rtl-preview',
imports: [HlmFieldImports, HlmSwitch],
host: {
'[dir]': '_dir()',
},
template: `
<hlm-field orientation="horizontal" class="max-w-sm">
<hlm-field-content>
<label hlmFieldLabel for="switch-focus-mode-rtl">{{ _t()['label'] }}</label>
<p hlmFieldDescription>{{ _t()['description'] }}</p>
</hlm-field-content>
<hlm-switch inputId="switch-focus-mode-rtl" />
</hlm-field>
`,
})
export class SwitchRtlPreview {
private readonly _language = inject(TranslateService).language;
private readonly _translations: Translations = {
en: {
dir: 'ltr',
values: {
label: 'Share across devices',
description: 'Focus is shared across devices, and turns off when you leave the app.',
},
},
ar: {
dir: 'rtl',
values: {
label: 'المشاركة عبر الأجهزة',
description: 'يتم مشاركة التركيز عبر الأجهزة، ويتم إيقاف تشغيله عند مغادرة التطبيق.',
},
},
he: {
dir: 'rtl',
values: {
label: 'שיתוף בין מכשירים',
description: 'המיקוד משותף בין מכשירים, וכבה כשאתה עוזב את האפליקציה.',
},
},
};
private readonly _translation = computed(() => this._translations[this._language()]);
protected readonly _t = computed(() => this._translation().values);
protected readonly _dir = computed(() => this._translation().dir);
}Brain API
BrnSwitchThumb
Selector: [brnSwitchThumb],brn-switch-thumb
ExportAs: brnSwitchThumb
BrnSwitch
Selector: brn-switch
ExportAs: brnSwitch
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| checked | boolean | false | Whether switch is checked/toggled on. Can be bound with [(checked)] for two-way binding. |
| id | string | null | null | Unique identifier for switch component. When provided, inner button gets ID without '-switch' suffix. Auto-generates ID when omitted or `null`, so wrappers can forward an optional id. |
| name | string | null | null | Form control name for switch. When provided, inner button gets name without '-switch' suffix. |
| class | string | null | null | CSS classes applied to inner button element. |
| hostStyles | string | 'display: contents' | Styles applied to the host element. Bound via `[style]` so they apply through the CSSOM and are not blocked by a strict Content-Security-Policy. |
| size | BrnSwitchSize | 'default' | Size of the switch. Drives the size-keyed registry style rules via the `data-size` attribute. |
| forceInvalid | boolean | false | Whether to force the input into an invalid state. |
| aria-label | string | null | null | Accessibility label for screen readers. Use when no visible label exists. |
| aria-labelledby | string | null | null | ID of element that labels this switch for accessibility. Auto-set when switch is inside label element. |
| aria-describedby | string | null | null | ID of element that describes this switch for accessibility. |
| required | boolean | false | Whether switch is required in a form. |
| disabled | boolean | false | Whether switch is disabled. Disabled switches cannot be toggled and indicate disabled state with data attribute. |
| tabIndex | number | 0 | Keyboard tab order for switch. |
Outputs
| Prop | Type | Default | Description |
|---|---|---|---|
| checkedChange | boolean | - | Emits when checked state changes. |
| touched | void | - | Event emitted when switch is blurred (loses focus). Used for form validation. |
Helm API
HlmSwitchThumb
Selector: [hlmSwitchThumb],hlm-switch-thumb
ExportAs: hlmSwitchThumb
HlmSwitch
Selector: hlm-switch
ExportAs: hlmSwitch
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| class | ClassValue | '' | - |
| checked | boolean | false | The checked state of the switch. |
| disabled | boolean | false | The disabled state of the switch. |
| size | BrnSwitchSize | 'default' | The size of the switch. |
| inputId | string | null | null | Used to set the id on the underlying brn element. |
| forceInvalid | boolean | false | Whether to force the input into an invalid state. |
| aria-label | string | null | null | Used to set the aria-label attribute on the underlying brn element. |
| aria-labelledby | string | null | null | Used to set the aria-labelledby attribute on the underlying brn element. |
| aria-describedby | string | null | null | Used to set the aria-describedby attribute on the underlying brn element. |
Outputs
| Prop | Type | Default | Description |
|---|---|---|---|
| checkedChange | boolean | - | Emits when the checked state of the switch 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