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 switchimport { OVERLAY_DEFAULT_CONFIG } from '@angular/cdk/overlay';
import { brnClasses, type BrnClassesOptions } from '@spartan-ng/brain/core';
import { cn, type ClassValue } from 'cn';
import { createCn } from 'cn/config';
import { makeEnvironmentProviders, type EnvironmentProviders } from '@angular/core';
import { provideSpartanHlm } from '@spartan-ng/helm/utils';
/**
* Merges class values and resolves Tailwind CSS conflicts.
*
* To customize the merge (e.g. register custom theme values), create your own instance:
*
* @example
* ```ts
*
*
* export const hlm = createCn({
* extend: { classGroups: { 'font-size': [{ text: ['hero'] }] } },
* });
* ```
*/
export const hlm = cn;
/**
* This function dynamically adds and removes classes for a given element without requiring
* the a class binding (e.g. `[class]="..."`) which may interfere with other class bindings.
* The classes are merged with `hlm`.
*/
export function classes(computed: () => ClassValue[] | string, options: Omit<BrnClassesOptions, 'merge'> = {}) {
brnClasses(computed, { ...options, merge: hlm });
}
/**
* Provides default configuration for Spartan Helm components.
*
* This utility configures the Angular CDK overlay to disable the `usePopover`
* behavior introduced in Angular 21, which causes CDK overlay-based components
* (sheets, dialogs, tooltips, etc.) to render above `position: fixed` elements
* like `<hlm-toaster>`.
*
* @returns {EnvironmentProviders} Environment providers to be added to the application config.
*
* @example
* ```ts
* // app.config.ts
*
*
* export const appConfig: ApplicationConfig = {
* providers: [
* provideSpartanHlm(),
* // ... other providers
* ],
* };
* ```
*/
export function provideSpartanHlm(): EnvironmentProviders {
return makeEnvironmentProviders([
{
provide: OVERLAY_DEFAULT_CONFIG,
useValue: { usePopover: false },
},
]);
}import { BrnSwitch, BrnSwitchThumb, type BrnSwitchSize } from '@spartan-ng/brain/switch';
import { ChangeDetectionStrategy, Component, Directive, booleanAttribute, computed, forwardRef, input, linkedSignal, output } from '@angular/core';
import { NG_VALUE_ACCESSOR, type ControlValueAccessor } from '@angular/forms';
import { classes, hlm } from '@spartan-ng/helm/utils';
import { type BooleanInput } from '@angular/cdk/coercion';
import { type ChangeFn, type TouchFn } from '@spartan-ng/brain/forms';
import { type ClassValue } from 'cn';
@Directive({
selector: '[hlmSwitchThumb],hlm-switch-thumb',
exportAs: 'hlmSwitchThumb',
hostDirectives: [BrnSwitchThumb],
host: { 'data-slot': 'switch-thumb' },
})
export class HlmSwitchThumb {
constructor() {
classes(() => 'bg-background dark:data-unchecked:bg-foreground dark:data-checked:bg-primary-foreground rounded-full group-data-[size=default]/switch:size-4 group-data-[size=sm]/switch:size-3 data-unchecked:translate-x-0 data-checked:ltr:translate-x-[calc(100%-2px)] data-checked:rtl:-translate-x-[calc(100%-2px)] pointer-events-none block ring-0 transition-transform');
}
}
export const HLM_SWITCH_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => HlmSwitch),
multi: true,
};
@Component({
selector: 'hlm-switch',
exportAs: 'hlmSwitch',
imports: [BrnSwitch, HlmSwitchThumb],
providers: [HLM_SWITCH_VALUE_ACCESSOR],
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
'data-slot': 'switch',
class: 'contents',
'[attr.aria-label]': 'null',
'[attr.aria-labelledby]': 'null',
'[attr.aria-describedby]': 'null',
},
template: `
<brn-switch
[class]="_computedClass()"
[size]="size()"
[checked]="checked()"
[disabled]="_disabled()"
[id]="inputId()"
[forceInvalid]="forceInvalid()"
[aria-label]="ariaLabel()"
[aria-labelledby]="ariaLabelledby()"
[aria-describedby]="ariaDescribedby()"
(checkedChange)="handleChange($event)"
(touched)="_onTouched?.()"
>
<hlm-switch-thumb />
</brn-switch>
`,
})
export class HlmSwitch implements ControlValueAccessor {
public readonly userClass = input<ClassValue>('', { alias: 'class' });
protected readonly _computedClass = computed(() =>
hlm(
'data-checked:bg-primary data-unchecked:bg-input focus-visible:border-ring focus-visible:ring-ring/50 data-[matches-spartan-invalid=true]:ring-destructive/20 dark:data-[matches-spartan-invalid=true]:ring-destructive/40 data-[matches-spartan-invalid=true]:border-destructive dark:data-[matches-spartan-invalid=true]:border-destructive/50 dark:data-unchecked:bg-input/80 rounded-full border border-transparent focus-visible:ring-3 data-[matches-spartan-invalid=true]:ring-3 data-[size=default]:h-[18.4px] data-[size=default]:w-[32px] data-[size=sm]:h-[14px] data-[size=sm]:w-[24px] group/switch inline-flex shrink-0 items-center transition-all outline-none data-disabled:cursor-not-allowed data-disabled:opacity-50',
this.userClass(),
),
);
/** The checked state of the switch. */
public readonly checkedInput = input<boolean, BooleanInput>(false, { alias: 'checked', transform: booleanAttribute });
public readonly checked = linkedSignal(this.checkedInput);
/** Emits when the checked state of the switch changes. */
public readonly checkedChange = output<boolean>();
/** The disabled state of the switch. */
public readonly disabled = input<boolean, BooleanInput>(false, {
transform: booleanAttribute,
});
/** The size of the switch. */
public readonly size = input<BrnSwitchSize>('default');
/** Used to set the id on the underlying brn element. */
public readonly inputId = input<string | null>(null);
/** Whether to force the input into an invalid state. */
public readonly forceInvalid = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
/** Used to set the aria-label attribute on the underlying brn element. */
public readonly ariaLabel = input<string | null>(null, { alias: 'aria-label' });
/** Used to set the aria-labelledby attribute on the underlying brn element. */
public readonly ariaLabelledby = input<string | null>(null, { alias: 'aria-labelledby' });
/** Used to set the aria-describedby attribute on the underlying brn element. */
public readonly ariaDescribedby = input<string | null>(null, { alias: 'aria-describedby' });
protected readonly _disabled = linkedSignal(this.disabled);
protected _onChange?: ChangeFn<boolean>;
protected _onTouched?: TouchFn;
protected handleChange(value: boolean): void {
this.checked.set(value);
this._onChange?.(value);
this.checkedChange.emit(value);
}
/** CONTROL VALUE ACCESSOR */
writeValue(value: boolean): void {
this.checked.set(Boolean(value));
}
registerOnChange(fn: ChangeFn<boolean>): void {
this._onChange = fn;
}
registerOnTouched(fn: TouchFn): void {
this._onTouched = fn;
}
setDisabledState(isDisabled: boolean): void {
this._disabled.set(isDisabled);
}
}
export const HlmSwitchImports = [HlmSwitch, HlmSwitchThumb] as const;import { BrnSwitch, BrnSwitchThumb, type BrnSwitchSize } from '@spartan-ng/brain/switch';
import { ChangeDetectionStrategy, Component, Directive, booleanAttribute, computed, forwardRef, input, linkedSignal, output } from '@angular/core';
import { NG_VALUE_ACCESSOR, type ControlValueAccessor } from '@angular/forms';
import { classes, hlm } from '@spartan-ng/helm/utils';
import { type BooleanInput } from '@angular/cdk/coercion';
import { type ChangeFn, type TouchFn } from '@spartan-ng/brain/forms';
import { type ClassValue } from 'cn';
@Directive({
selector: '[hlmSwitchThumb],hlm-switch-thumb',
exportAs: 'hlmSwitchThumb',
hostDirectives: [BrnSwitchThumb],
host: { 'data-slot': 'switch-thumb' },
})
export class HlmSwitchThumb {
constructor() {
classes(() => 'bg-background dark:data-unchecked:bg-foreground dark:data-checked:bg-primary-foreground rounded-full group-data-[size=default]/switch:size-4 group-data-[size=sm]/switch:size-3 data-unchecked:translate-x-0 data-checked:ltr:translate-x-[calc(100%-2px)] data-checked:rtl:-translate-x-[calc(100%-2px)] pointer-events-none block ring-0 transition-transform');
}
}
export const HLM_SWITCH_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => HlmSwitch),
multi: true,
};
@Component({
selector: 'hlm-switch',
exportAs: 'hlmSwitch',
imports: [BrnSwitch, HlmSwitchThumb],
providers: [HLM_SWITCH_VALUE_ACCESSOR],
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
'data-slot': 'switch',
class: 'contents',
'[attr.aria-label]': 'null',
'[attr.aria-labelledby]': 'null',
'[attr.aria-describedby]': 'null',
},
template: `
<brn-switch
[class]="_computedClass()"
[size]="size()"
[checked]="checked()"
[disabled]="_disabled()"
[id]="inputId()"
[forceInvalid]="forceInvalid()"
[aria-label]="ariaLabel()"
[aria-labelledby]="ariaLabelledby()"
[aria-describedby]="ariaDescribedby()"
(checkedChange)="handleChange($event)"
(touched)="_onTouched?.()"
>
<hlm-switch-thumb />
</brn-switch>
`,
})
export class HlmSwitch implements ControlValueAccessor {
public readonly userClass = input<ClassValue>('', { alias: 'class' });
protected readonly _computedClass = computed(() =>
hlm(
'data-checked:bg-primary data-unchecked:bg-input focus-visible:border-ring focus-visible:ring-ring/50 data-[matches-spartan-invalid=true]:ring-destructive/20 dark:data-[matches-spartan-invalid=true]:ring-destructive/40 data-[matches-spartan-invalid=true]:border-destructive dark:data-[matches-spartan-invalid=true]:border-destructive/50 dark:data-unchecked:bg-input/80 rounded-full border border-transparent shadow-xs focus-visible:ring-3 data-[matches-spartan-invalid=true]:ring-3 data-[size=default]:h-[18.4px] data-[size=default]:w-[32px] data-[size=sm]:h-[14px] data-[size=sm]:w-[24px] group/switch inline-flex shrink-0 items-center transition-all outline-none data-disabled:cursor-not-allowed data-disabled:opacity-50',
this.userClass(),
),
);
/** The checked state of the switch. */
public readonly checkedInput = input<boolean, BooleanInput>(false, { alias: 'checked', transform: booleanAttribute });
public readonly checked = linkedSignal(this.checkedInput);
/** Emits when the checked state of the switch changes. */
public readonly checkedChange = output<boolean>();
/** The disabled state of the switch. */
public readonly disabled = input<boolean, BooleanInput>(false, {
transform: booleanAttribute,
});
/** The size of the switch. */
public readonly size = input<BrnSwitchSize>('default');
/** Used to set the id on the underlying brn element. */
public readonly inputId = input<string | null>(null);
/** Whether to force the input into an invalid state. */
public readonly forceInvalid = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
/** Used to set the aria-label attribute on the underlying brn element. */
public readonly ariaLabel = input<string | null>(null, { alias: 'aria-label' });
/** Used to set the aria-labelledby attribute on the underlying brn element. */
public readonly ariaLabelledby = input<string | null>(null, { alias: 'aria-labelledby' });
/** Used to set the aria-describedby attribute on the underlying brn element. */
public readonly ariaDescribedby = input<string | null>(null, { alias: 'aria-describedby' });
protected readonly _disabled = linkedSignal(this.disabled);
protected _onChange?: ChangeFn<boolean>;
protected _onTouched?: TouchFn;
protected handleChange(value: boolean): void {
this.checked.set(value);
this._onChange?.(value);
this.checkedChange.emit(value);
}
/** CONTROL VALUE ACCESSOR */
writeValue(value: boolean): void {
this.checked.set(Boolean(value));
}
registerOnChange(fn: ChangeFn<boolean>): void {
this._onChange = fn;
}
registerOnTouched(fn: TouchFn): void {
this._onTouched = fn;
}
setDisabledState(isDisabled: boolean): void {
this._disabled.set(isDisabled);
}
}
export const HlmSwitchImports = [HlmSwitch, HlmSwitchThumb] as const;import { BrnSwitch, BrnSwitchThumb, type BrnSwitchSize } from '@spartan-ng/brain/switch';
import { ChangeDetectionStrategy, Component, Directive, booleanAttribute, computed, forwardRef, input, linkedSignal, output } from '@angular/core';
import { NG_VALUE_ACCESSOR, type ControlValueAccessor } from '@angular/forms';
import { classes, hlm } from '@spartan-ng/helm/utils';
import { type BooleanInput } from '@angular/cdk/coercion';
import { type ChangeFn, type TouchFn } from '@spartan-ng/brain/forms';
import { type ClassValue } from 'cn';
@Directive({
selector: '[hlmSwitchThumb],hlm-switch-thumb',
exportAs: 'hlmSwitchThumb',
hostDirectives: [BrnSwitchThumb],
host: { 'data-slot': 'switch-thumb' },
})
export class HlmSwitchThumb {
constructor() {
classes(() => 'bg-background dark:data-unchecked:bg-foreground dark:data-checked:bg-primary-foreground rounded-full group-data-[size=default]/switch:size-4 group-data-[size=sm]/switch:size-3 data-unchecked:translate-x-0 data-checked:ltr:translate-x-[calc(100%-2px)] data-checked:rtl:-translate-x-[calc(100%-2px)] pointer-events-none block ring-0 transition-transform');
}
}
export const HLM_SWITCH_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => HlmSwitch),
multi: true,
};
@Component({
selector: 'hlm-switch',
exportAs: 'hlmSwitch',
imports: [BrnSwitch, HlmSwitchThumb],
providers: [HLM_SWITCH_VALUE_ACCESSOR],
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
'data-slot': 'switch',
class: 'contents',
'[attr.aria-label]': 'null',
'[attr.aria-labelledby]': 'null',
'[attr.aria-describedby]': 'null',
},
template: `
<brn-switch
[class]="_computedClass()"
[size]="size()"
[checked]="checked()"
[disabled]="_disabled()"
[id]="inputId()"
[forceInvalid]="forceInvalid()"
[aria-label]="ariaLabel()"
[aria-labelledby]="ariaLabelledby()"
[aria-describedby]="ariaDescribedby()"
(checkedChange)="handleChange($event)"
(touched)="_onTouched?.()"
>
<hlm-switch-thumb />
</brn-switch>
`,
})
export class HlmSwitch implements ControlValueAccessor {
public readonly userClass = input<ClassValue>('', { alias: 'class' });
protected readonly _computedClass = computed(() =>
hlm(
'data-checked:bg-primary data-unchecked:bg-input focus-visible:border-ring focus-visible:ring-ring/50 data-[matches-spartan-invalid=true]:ring-destructive/20 dark:data-[matches-spartan-invalid=true]:ring-destructive/40 data-[matches-spartan-invalid=true]:border-destructive dark:data-[matches-spartan-invalid=true]:border-destructive/50 dark:data-unchecked:bg-input/80 rounded-full border border-transparent focus-visible:ring-1 data-[matches-spartan-invalid=true]:ring-1 data-[size=default]:h-[18.4px] data-[size=default]:w-[32px] data-[size=sm]:h-[14px] data-[size=sm]:w-[24px] group/switch inline-flex shrink-0 items-center transition-all outline-none data-disabled:cursor-not-allowed data-disabled:opacity-50',
this.userClass(),
),
);
/** The checked state of the switch. */
public readonly checkedInput = input<boolean, BooleanInput>(false, { alias: 'checked', transform: booleanAttribute });
public readonly checked = linkedSignal(this.checkedInput);
/** Emits when the checked state of the switch changes. */
public readonly checkedChange = output<boolean>();
/** The disabled state of the switch. */
public readonly disabled = input<boolean, BooleanInput>(false, {
transform: booleanAttribute,
});
/** The size of the switch. */
public readonly size = input<BrnSwitchSize>('default');
/** Used to set the id on the underlying brn element. */
public readonly inputId = input<string | null>(null);
/** Whether to force the input into an invalid state. */
public readonly forceInvalid = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
/** Used to set the aria-label attribute on the underlying brn element. */
public readonly ariaLabel = input<string | null>(null, { alias: 'aria-label' });
/** Used to set the aria-labelledby attribute on the underlying brn element. */
public readonly ariaLabelledby = input<string | null>(null, { alias: 'aria-labelledby' });
/** Used to set the aria-describedby attribute on the underlying brn element. */
public readonly ariaDescribedby = input<string | null>(null, { alias: 'aria-describedby' });
protected readonly _disabled = linkedSignal(this.disabled);
protected _onChange?: ChangeFn<boolean>;
protected _onTouched?: TouchFn;
protected handleChange(value: boolean): void {
this.checked.set(value);
this._onChange?.(value);
this.checkedChange.emit(value);
}
/** CONTROL VALUE ACCESSOR */
writeValue(value: boolean): void {
this.checked.set(Boolean(value));
}
registerOnChange(fn: ChangeFn<boolean>): void {
this._onChange = fn;
}
registerOnTouched(fn: TouchFn): void {
this._onTouched = fn;
}
setDisabledState(isDisabled: boolean): void {
this._disabled.set(isDisabled);
}
}
export const HlmSwitchImports = [HlmSwitch, HlmSwitchThumb] as const;import { BrnSwitch, BrnSwitchThumb, type BrnSwitchSize } from '@spartan-ng/brain/switch';
import { ChangeDetectionStrategy, Component, Directive, booleanAttribute, computed, forwardRef, input, linkedSignal, output } from '@angular/core';
import { NG_VALUE_ACCESSOR, type ControlValueAccessor } from '@angular/forms';
import { classes, hlm } from '@spartan-ng/helm/utils';
import { type BooleanInput } from '@angular/cdk/coercion';
import { type ChangeFn, type TouchFn } from '@spartan-ng/brain/forms';
import { type ClassValue } from 'cn';
@Directive({
selector: '[hlmSwitchThumb],hlm-switch-thumb',
exportAs: 'hlmSwitchThumb',
hostDirectives: [BrnSwitchThumb],
host: { 'data-slot': 'switch-thumb' },
})
export class HlmSwitchThumb {
constructor() {
classes(() => 'bg-background dark:data-unchecked:bg-foreground dark:data-checked:bg-primary-foreground rounded-full group-data-[size=default]/switch:size-4 group-data-[size=sm]/switch:size-3 data-unchecked:translate-x-0 data-checked:ltr:translate-x-[calc(100%-2px)] data-checked:rtl:-translate-x-[calc(100%-2px)] pointer-events-none block ring-0 transition-transform');
}
}
export const HLM_SWITCH_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => HlmSwitch),
multi: true,
};
@Component({
selector: 'hlm-switch',
exportAs: 'hlmSwitch',
imports: [BrnSwitch, HlmSwitchThumb],
providers: [HLM_SWITCH_VALUE_ACCESSOR],
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
'data-slot': 'switch',
class: 'contents',
'[attr.aria-label]': 'null',
'[attr.aria-labelledby]': 'null',
'[attr.aria-describedby]': 'null',
},
template: `
<brn-switch
[class]="_computedClass()"
[size]="size()"
[checked]="checked()"
[disabled]="_disabled()"
[id]="inputId()"
[forceInvalid]="forceInvalid()"
[aria-label]="ariaLabel()"
[aria-labelledby]="ariaLabelledby()"
[aria-describedby]="ariaDescribedby()"
(checkedChange)="handleChange($event)"
(touched)="_onTouched?.()"
>
<hlm-switch-thumb />
</brn-switch>
`,
})
export class HlmSwitch implements ControlValueAccessor {
public readonly userClass = input<ClassValue>('', { alias: 'class' });
protected readonly _computedClass = computed(() =>
hlm(
'data-checked:bg-primary data-unchecked:bg-input focus-visible:border-ring focus-visible:ring-ring/50 data-[matches-spartan-invalid=true]:ring-destructive/20 dark:data-[matches-spartan-invalid=true]:ring-destructive/40 data-[matches-spartan-invalid=true]:border-destructive dark:data-[matches-spartan-invalid=true]:border-destructive/50 dark:data-unchecked:bg-input/80 rounded-full border border-transparent focus-visible:ring-[3px] data-[matches-spartan-invalid=true]:ring-[3px] data-[size=default]:h-[18.4px] data-[size=default]:w-[32px] data-[size=sm]:h-[14px] data-[size=sm]:w-[24px] group/switch inline-flex shrink-0 items-center transition-all outline-none data-disabled:cursor-not-allowed data-disabled:opacity-50',
this.userClass(),
),
);
/** The checked state of the switch. */
public readonly checkedInput = input<boolean, BooleanInput>(false, { alias: 'checked', transform: booleanAttribute });
public readonly checked = linkedSignal(this.checkedInput);
/** Emits when the checked state of the switch changes. */
public readonly checkedChange = output<boolean>();
/** The disabled state of the switch. */
public readonly disabled = input<boolean, BooleanInput>(false, {
transform: booleanAttribute,
});
/** The size of the switch. */
public readonly size = input<BrnSwitchSize>('default');
/** Used to set the id on the underlying brn element. */
public readonly inputId = input<string | null>(null);
/** Whether to force the input into an invalid state. */
public readonly forceInvalid = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
/** Used to set the aria-label attribute on the underlying brn element. */
public readonly ariaLabel = input<string | null>(null, { alias: 'aria-label' });
/** Used to set the aria-labelledby attribute on the underlying brn element. */
public readonly ariaLabelledby = input<string | null>(null, { alias: 'aria-labelledby' });
/** Used to set the aria-describedby attribute on the underlying brn element. */
public readonly ariaDescribedby = input<string | null>(null, { alias: 'aria-describedby' });
protected readonly _disabled = linkedSignal(this.disabled);
protected _onChange?: ChangeFn<boolean>;
protected _onTouched?: TouchFn;
protected handleChange(value: boolean): void {
this.checked.set(value);
this._onChange?.(value);
this.checkedChange.emit(value);
}
/** CONTROL VALUE ACCESSOR */
writeValue(value: boolean): void {
this.checked.set(Boolean(value));
}
registerOnChange(fn: ChangeFn<boolean>): void {
this._onChange = fn;
}
registerOnTouched(fn: TouchFn): void {
this._onTouched = fn;
}
setDisabledState(isDisabled: boolean): void {
this._disabled.set(isDisabled);
}
}
export const HlmSwitchImports = [HlmSwitch, HlmSwitchThumb] as const;import { BrnSwitch, BrnSwitchThumb, type BrnSwitchSize } from '@spartan-ng/brain/switch';
import { ChangeDetectionStrategy, Component, Directive, booleanAttribute, computed, forwardRef, input, linkedSignal, output } from '@angular/core';
import { NG_VALUE_ACCESSOR, type ControlValueAccessor } from '@angular/forms';
import { classes, hlm } from '@spartan-ng/helm/utils';
import { type BooleanInput } from '@angular/cdk/coercion';
import { type ChangeFn, type TouchFn } from '@spartan-ng/brain/forms';
import { type ClassValue } from 'cn';
@Directive({
selector: '[hlmSwitchThumb],hlm-switch-thumb',
exportAs: 'hlmSwitchThumb',
hostDirectives: [BrnSwitchThumb],
host: { 'data-slot': 'switch-thumb' },
})
export class HlmSwitchThumb {
constructor() {
classes(() => 'bg-background dark:data-unchecked:bg-foreground dark:data-checked:bg-primary-foreground rounded-full group-data-[size=default]/switch:size-3.5 group-data-[size=sm]/switch:size-3 data-unchecked:translate-x-0 data-checked:ltr:translate-x-[calc(100%-2px)] data-checked:rtl:-translate-x-[calc(100%-2px)] pointer-events-none block ring-0 transition-transform');
}
}
export const HLM_SWITCH_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => HlmSwitch),
multi: true,
};
@Component({
selector: 'hlm-switch',
exportAs: 'hlmSwitch',
imports: [BrnSwitch, HlmSwitchThumb],
providers: [HLM_SWITCH_VALUE_ACCESSOR],
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
'data-slot': 'switch',
class: 'contents',
'[attr.aria-label]': 'null',
'[attr.aria-labelledby]': 'null',
'[attr.aria-describedby]': 'null',
},
template: `
<brn-switch
[class]="_computedClass()"
[size]="size()"
[checked]="checked()"
[disabled]="_disabled()"
[id]="inputId()"
[forceInvalid]="forceInvalid()"
[aria-label]="ariaLabel()"
[aria-labelledby]="ariaLabelledby()"
[aria-describedby]="ariaDescribedby()"
(checkedChange)="handleChange($event)"
(touched)="_onTouched?.()"
>
<hlm-switch-thumb />
</brn-switch>
`,
})
export class HlmSwitch implements ControlValueAccessor {
public readonly userClass = input<ClassValue>('', { alias: 'class' });
protected readonly _computedClass = computed(() =>
hlm(
'data-checked:bg-primary data-unchecked:bg-input focus-visible:border-ring focus-visible:ring-ring/30 data-[matches-spartan-invalid=true]:ring-destructive/20 dark:data-[matches-spartan-invalid=true]:ring-destructive/40 data-[matches-spartan-invalid=true]:border-destructive dark:data-[matches-spartan-invalid=true]:border-destructive/50 dark:data-unchecked:bg-input/80 rounded-full border border-transparent focus-visible:ring-2 data-[matches-spartan-invalid=true]:ring-2 data-[size=default]:h-[16.6px] data-[size=default]:w-[28px] data-[size=sm]:h-[14px] data-[size=sm]:w-[24px] group/switch inline-flex shrink-0 items-center transition-all outline-none data-disabled:cursor-not-allowed data-disabled:opacity-50',
this.userClass(),
),
);
/** The checked state of the switch. */
public readonly checkedInput = input<boolean, BooleanInput>(false, { alias: 'checked', transform: booleanAttribute });
public readonly checked = linkedSignal(this.checkedInput);
/** Emits when the checked state of the switch changes. */
public readonly checkedChange = output<boolean>();
/** The disabled state of the switch. */
public readonly disabled = input<boolean, BooleanInput>(false, {
transform: booleanAttribute,
});
/** The size of the switch. */
public readonly size = input<BrnSwitchSize>('default');
/** Used to set the id on the underlying brn element. */
public readonly inputId = input<string | null>(null);
/** Whether to force the input into an invalid state. */
public readonly forceInvalid = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
/** Used to set the aria-label attribute on the underlying brn element. */
public readonly ariaLabel = input<string | null>(null, { alias: 'aria-label' });
/** Used to set the aria-labelledby attribute on the underlying brn element. */
public readonly ariaLabelledby = input<string | null>(null, { alias: 'aria-labelledby' });
/** Used to set the aria-describedby attribute on the underlying brn element. */
public readonly ariaDescribedby = input<string | null>(null, { alias: 'aria-describedby' });
protected readonly _disabled = linkedSignal(this.disabled);
protected _onChange?: ChangeFn<boolean>;
protected _onTouched?: TouchFn;
protected handleChange(value: boolean): void {
this.checked.set(value);
this._onChange?.(value);
this.checkedChange.emit(value);
}
/** CONTROL VALUE ACCESSOR */
writeValue(value: boolean): void {
this.checked.set(Boolean(value));
}
registerOnChange(fn: ChangeFn<boolean>): void {
this._onChange = fn;
}
registerOnTouched(fn: TouchFn): void {
this._onTouched = fn;
}
setDisabledState(isDisabled: boolean): void {
this._disabled.set(isDisabled);
}
}
export const HlmSwitchImports = [HlmSwitch, HlmSwitchThumb] as const;import { BrnSwitch, BrnSwitchThumb, type BrnSwitchSize } from '@spartan-ng/brain/switch';
import { ChangeDetectionStrategy, Component, Directive, booleanAttribute, computed, forwardRef, input, linkedSignal, output } from '@angular/core';
import { NG_VALUE_ACCESSOR, type ControlValueAccessor } from '@angular/forms';
import { classes, hlm } from '@spartan-ng/helm/utils';
import { type BooleanInput } from '@angular/cdk/coercion';
import { type ChangeFn, type TouchFn } from '@spartan-ng/brain/forms';
import { type ClassValue } from 'cn';
@Directive({
selector: '[hlmSwitchThumb],hlm-switch-thumb',
exportAs: 'hlmSwitchThumb',
hostDirectives: [BrnSwitchThumb],
host: { 'data-slot': 'switch-thumb' },
})
export class HlmSwitchThumb {
constructor() {
classes(() => 'bg-background dark:data-unchecked:bg-foreground dark:data-checked:bg-primary-foreground rounded-full shadow-sm not-dark:bg-clip-padding group-data-[size=default]/switch:h-4 group-data-[size=default]/switch:w-6 group-data-[size=sm]/switch:h-3 group-data-[size=sm]/switch:w-4 data-unchecked:translate-x-0 data-checked:ltr:translate-x-[calc(100%-8px)] data-checked:rtl:-translate-x-[calc(100%-8px)] pointer-events-none block ring-0 transition-transform');
}
}
export const HLM_SWITCH_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => HlmSwitch),
multi: true,
};
@Component({
selector: 'hlm-switch',
exportAs: 'hlmSwitch',
imports: [BrnSwitch, HlmSwitchThumb],
providers: [HLM_SWITCH_VALUE_ACCESSOR],
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
'data-slot': 'switch',
class: 'contents',
'[attr.aria-label]': 'null',
'[attr.aria-labelledby]': 'null',
'[attr.aria-describedby]': 'null',
},
template: `
<brn-switch
[class]="_computedClass()"
[size]="size()"
[checked]="checked()"
[disabled]="_disabled()"
[id]="inputId()"
[forceInvalid]="forceInvalid()"
[aria-label]="ariaLabel()"
[aria-labelledby]="ariaLabelledby()"
[aria-describedby]="ariaDescribedby()"
(checkedChange)="handleChange($event)"
(touched)="_onTouched?.()"
>
<hlm-switch-thumb />
</brn-switch>
`,
})
export class HlmSwitch implements ControlValueAccessor {
public readonly userClass = input<ClassValue>('', { alias: 'class' });
protected readonly _computedClass = computed(() =>
hlm(
'data-checked:bg-primary data-unchecked:bg-input/90 data-checked:border-primary focus-visible:border-ring focus-visible:ring-ring/30 data-[matches-spartan-invalid=true]:ring-destructive/20 dark:data-[matches-spartan-invalid=true]:ring-destructive/40 data-[matches-spartan-invalid=true]:border-destructive dark:data-[matches-spartan-invalid=true]:border-destructive/50 rounded-full border-2 focus-visible:ring-3 data-unchecked:border-transparent data-[matches-spartan-invalid=true]:ring-3 data-[size=default]:h-5 data-[size=default]:w-11 data-[size=sm]:h-4 data-[size=sm]:w-7 group/switch inline-flex shrink-0 items-center transition-all outline-none data-disabled:cursor-not-allowed data-disabled:opacity-50',
this.userClass(),
),
);
/** The checked state of the switch. */
public readonly checkedInput = input<boolean, BooleanInput>(false, { alias: 'checked', transform: booleanAttribute });
public readonly checked = linkedSignal(this.checkedInput);
/** Emits when the checked state of the switch changes. */
public readonly checkedChange = output<boolean>();
/** The disabled state of the switch. */
public readonly disabled = input<boolean, BooleanInput>(false, {
transform: booleanAttribute,
});
/** The size of the switch. */
public readonly size = input<BrnSwitchSize>('default');
/** Used to set the id on the underlying brn element. */
public readonly inputId = input<string | null>(null);
/** Whether to force the input into an invalid state. */
public readonly forceInvalid = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
/** Used to set the aria-label attribute on the underlying brn element. */
public readonly ariaLabel = input<string | null>(null, { alias: 'aria-label' });
/** Used to set the aria-labelledby attribute on the underlying brn element. */
public readonly ariaLabelledby = input<string | null>(null, { alias: 'aria-labelledby' });
/** Used to set the aria-describedby attribute on the underlying brn element. */
public readonly ariaDescribedby = input<string | null>(null, { alias: 'aria-describedby' });
protected readonly _disabled = linkedSignal(this.disabled);
protected _onChange?: ChangeFn<boolean>;
protected _onTouched?: TouchFn;
protected handleChange(value: boolean): void {
this.checked.set(value);
this._onChange?.(value);
this.checkedChange.emit(value);
}
/** CONTROL VALUE ACCESSOR */
writeValue(value: boolean): void {
this.checked.set(Boolean(value));
}
registerOnChange(fn: ChangeFn<boolean>): void {
this._onChange = fn;
}
registerOnTouched(fn: TouchFn): void {
this._onTouched = fn;
}
setDisabledState(isDisabled: boolean): void {
this._disabled.set(isDisabled);
}
}
export const HlmSwitchImports = [HlmSwitch, HlmSwitchThumb] as const;Usage
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 | ++uniqueIdCounter + '' | Unique identifier for switch component. When provided, inner button gets ID without '-switch' suffix. Auto-generates ID if not provided. |
| 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