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
Checkbox
A control that allows the user to toggle between checked and not checked.
By clicking this checkbox, you agree to the terms and conditions.
import { Component } from '@angular/core';
import { HlmCheckboxImports } from '@spartan-ng/helm/checkbox';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmLabelImports } from '@spartan-ng/helm/label';
@Component({
selector: 'spartan-checkbox-preview',
imports: [HlmLabelImports, HlmCheckboxImports, HlmFieldImports],
template: `
<div class="flex flex-col gap-6">
<div class="flex items-center gap-3">
<hlm-checkbox inputId="terms" />
<label hlmLabel for="terms">Accept terms and conditions</label>
</div>
<div class="flex items-start gap-3">
<hlm-checkbox inputId="terms-2" [checked]="true" />
<div class="grid gap-2">
<label hlmLabel for="terms-2">Accept terms and conditions</label>
<p hlmFieldDescription>By clicking this checkbox, you agree to the terms and conditions.</p>
</div>
</div>
<div class="flex items-start gap-3">
<hlm-checkbox inputId="toggle" disabled />
<label hlmLabel for="toggle">Enable notifications</label>
</div>
<label
class="hover:bg-accent/50 flex items-start gap-3 rounded-lg border p-3 has-[[aria-checked=true]]:border-blue-600 has-[[aria-checked=true]]:bg-blue-50 dark:has-[[aria-checked=true]]:border-blue-900 dark:has-[[aria-checked=true]]:bg-blue-950"
>
<hlm-checkbox
inputId="toggle-2"
[checked]="true"
class="data-[state=checked]:border-blue-600 data-[state=checked]:bg-blue-600 data-[state=checked]:text-white dark:data-[state=checked]:border-blue-700 dark:data-[state=checked]:bg-blue-700"
/>
<div class="grid gap-1.5 font-normal">
<p class="text-sm leading-none font-medium">Enable notifications</p>
<p hlmFieldDescription>You can enable or disable notifications at any time.</p>
</div>
</label>
</div>
`,
})
export class CheckboxPreview {}Installation
ng g @spartan-ng/cli:ui checkboxnx g @spartan-ng/cli:ui checkboximport { 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 { BrnCheckbox } from '@spartan-ng/brain/checkbox';
import { BrnFieldControlDescribedBy } from '@spartan-ng/brain/field';
import { ChangeDetectionStrategy, Component, booleanAttribute, computed, forwardRef, input, linkedSignal, model, output, viewChild } from '@angular/core';
import { NG_VALUE_ACCESSOR, type ControlValueAccessor } from '@angular/forms';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { hlm } from '@spartan-ng/helm/utils';
import { lucideCheck } from '@ng-icons/lucide';
import { type BooleanInput } from '@angular/cdk/coercion';
import { type ChangeFn, type TouchFn } from '@spartan-ng/brain/forms';
import { type ClassValue } from 'cn';
export const HLM_CHECKBOX_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => HlmCheckbox),
multi: true,
};
@Component({
selector: 'hlm-checkbox',
exportAs: 'hlmCheckbox',
imports: [BrnCheckbox, NgIcon],
providers: [HLM_CHECKBOX_VALUE_ACCESSOR],
viewProviders: [provideIcons({ lucideCheck })],
changeDetection: ChangeDetectionStrategy.OnPush,
hostDirectives: [BrnFieldControlDescribedBy],
host: {
class: 'contents peer',
'data-slot': 'checkbox',
'[attr.aria-label]': 'null',
'[attr.aria-labelledby]': 'null',
'[attr.data-disabled]': '_disabled() ? "" : null',
},
template: `
<brn-checkbox
[id]="inputId()"
[name]="name()"
[class]="_computedClass()"
[checked]="checked()"
[(indeterminate)]="indeterminate"
[disabled]="_disabled()"
[required]="required()"
[aria-label]="ariaLabel()"
[aria-labelledby]="ariaLabelledby()"
[aria-describedby]="ariaDescribedby()"
[forceInvalid]="forceInvalid()"
(checkedChange)="_handleChange($event)"
(touched)="_onTouched?.()"
>
@if (checked() || indeterminate()) {
<span class="[&>ng-icon]:text-[length:--spacing(3.5)] flex items-center justify-center text-current transition-none">
<ng-icon name="lucideCheck" />
</span>
}
</brn-checkbox>
`,
})
export class HlmCheckbox implements ControlValueAccessor {
public readonly userClass = input<ClassValue>('', { alias: 'class' });
protected readonly _computedClass = computed(() =>
hlm(
'border-input dark:bg-input/30 data-checked:bg-primary data-checked:text-primary-foreground dark:data-checked:bg-primary data-checked:border-primary data-[matches-spartan-invalid=true]:aria-checked:border-primary data-[matches-spartan-invalid=true]:border-destructive dark:data-[matches-spartan-invalid=true]:border-destructive/50 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 flex size-4 items-center justify-center rounded-[4px] border transition-colors group-has-disabled/field:opacity-50 focus-visible:ring-3 data-[matches-spartan-invalid=true]:ring-3 peer shrink-0 cursor-default outline-none disabled:cursor-not-allowed disabled:opacity-50',
this.userClass(),
this._errorStateClass(),
),
);
/** Used to set the id on the underlying brn element. */
public readonly inputId = input<string | null>(null);
/** 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' });
/** The checked state of the checkbox. */
public readonly checkedInput = input<boolean, BooleanInput>(false, { alias: 'checked', transform: booleanAttribute });
public readonly checked = linkedSignal(this.checkedInput);
/** Emits when checked state changes. */
public readonly checkedChange = output<boolean>();
/**
* The indeterminate state of the checkbox.
* For example, a "select all/deselect all" checkbox may be in the indeterminate state when some but not all of its sub-controls are checked.
*/
public readonly indeterminate = model<boolean>(false);
/** The name attribute of the checkbox. */
public readonly name = input<string | null>(null);
/** Whether the checkbox is required. */
public readonly required = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
/** Whether the checkbox is disabled. */
public readonly disabled = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
/** Whether to force the checkbox into an invalid state. */
public readonly forceInvalid = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
protected readonly _disabled = linkedSignal(this.disabled);
private readonly _brnCheckbox = viewChild.required(BrnCheckbox);
private readonly _spartanInvalid = computed(() => this.forceInvalid() || this._brnCheckbox().spartanInvalid?.());
protected readonly _errorStateClass = computed(() =>
this._spartanInvalid()
? 'border-destructive focus-visible:border-destructive focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40'
: '',
);
protected _onChange?: ChangeFn<boolean>;
protected _onTouched?: TouchFn;
protected _handleChange(value: boolean): void {
if (this._disabled()) return;
this.checked.set(value);
this.checkedChange.emit(value);
this._onChange?.(value);
}
/** CONTROL VALUE ACCESSOR */
writeValue(value: boolean): void {
this.checked.set(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 HlmCheckboxImports = [HlmCheckbox] as const;import { BrnCheckbox } from '@spartan-ng/brain/checkbox';
import { BrnFieldControlDescribedBy } from '@spartan-ng/brain/field';
import { ChangeDetectionStrategy, Component, booleanAttribute, computed, forwardRef, input, linkedSignal, model, output, viewChild } from '@angular/core';
import { NG_VALUE_ACCESSOR, type ControlValueAccessor } from '@angular/forms';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { hlm } from '@spartan-ng/helm/utils';
import { lucideCheck } from '@ng-icons/lucide';
import { type BooleanInput } from '@angular/cdk/coercion';
import { type ChangeFn, type TouchFn } from '@spartan-ng/brain/forms';
import { type ClassValue } from 'cn';
export const HLM_CHECKBOX_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => HlmCheckbox),
multi: true,
};
@Component({
selector: 'hlm-checkbox',
exportAs: 'hlmCheckbox',
imports: [BrnCheckbox, NgIcon],
providers: [HLM_CHECKBOX_VALUE_ACCESSOR],
viewProviders: [provideIcons({ lucideCheck })],
changeDetection: ChangeDetectionStrategy.OnPush,
hostDirectives: [BrnFieldControlDescribedBy],
host: {
class: 'contents peer',
'data-slot': 'checkbox',
'[attr.aria-label]': 'null',
'[attr.aria-labelledby]': 'null',
'[attr.data-disabled]': '_disabled() ? "" : null',
},
template: `
<brn-checkbox
[id]="inputId()"
[name]="name()"
[class]="_computedClass()"
[checked]="checked()"
[(indeterminate)]="indeterminate"
[disabled]="_disabled()"
[required]="required()"
[aria-label]="ariaLabel()"
[aria-labelledby]="ariaLabelledby()"
[aria-describedby]="ariaDescribedby()"
[forceInvalid]="forceInvalid()"
(checkedChange)="_handleChange($event)"
(touched)="_onTouched?.()"
>
@if (checked() || indeterminate()) {
<span class="[&>ng-icon]:text-[length:--spacing(3.5)] flex items-center justify-center text-current transition-none">
<ng-icon name="lucideCheck" />
</span>
}
</brn-checkbox>
`,
})
export class HlmCheckbox implements ControlValueAccessor {
public readonly userClass = input<ClassValue>('', { alias: 'class' });
protected readonly _computedClass = computed(() =>
hlm(
'border-input dark:bg-input/30 data-checked:bg-primary data-checked:text-primary-foreground dark:data-checked:bg-primary data-checked:border-primary data-[matches-spartan-invalid=true]:aria-checked:border-primary data-[matches-spartan-invalid=true]:border-destructive dark:data-[matches-spartan-invalid=true]:border-destructive/50 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 flex size-4 items-center justify-center rounded-[4px] border shadow-xs transition-shadow group-has-disabled/field:opacity-50 focus-visible:ring-3 data-[matches-spartan-invalid=true]:ring-3 peer shrink-0 cursor-default outline-none disabled:cursor-not-allowed disabled:opacity-50',
this.userClass(),
this._errorStateClass(),
),
);
/** Used to set the id on the underlying brn element. */
public readonly inputId = input<string | null>(null);
/** 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' });
/** The checked state of the checkbox. */
public readonly checkedInput = input<boolean, BooleanInput>(false, { alias: 'checked', transform: booleanAttribute });
public readonly checked = linkedSignal(this.checkedInput);
/** Emits when checked state changes. */
public readonly checkedChange = output<boolean>();
/**
* The indeterminate state of the checkbox.
* For example, a "select all/deselect all" checkbox may be in the indeterminate state when some but not all of its sub-controls are checked.
*/
public readonly indeterminate = model<boolean>(false);
/** The name attribute of the checkbox. */
public readonly name = input<string | null>(null);
/** Whether the checkbox is required. */
public readonly required = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
/** Whether the checkbox is disabled. */
public readonly disabled = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
/** Whether to force the checkbox into an invalid state. */
public readonly forceInvalid = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
protected readonly _disabled = linkedSignal(this.disabled);
private readonly _brnCheckbox = viewChild.required(BrnCheckbox);
private readonly _spartanInvalid = computed(() => this.forceInvalid() || this._brnCheckbox().spartanInvalid?.());
protected readonly _errorStateClass = computed(() =>
this._spartanInvalid()
? 'border-destructive focus-visible:border-destructive focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40'
: '',
);
protected _onChange?: ChangeFn<boolean>;
protected _onTouched?: TouchFn;
protected _handleChange(value: boolean): void {
if (this._disabled()) return;
this.checked.set(value);
this.checkedChange.emit(value);
this._onChange?.(value);
}
/** CONTROL VALUE ACCESSOR */
writeValue(value: boolean): void {
this.checked.set(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 HlmCheckboxImports = [HlmCheckbox] as const;import { BrnCheckbox } from '@spartan-ng/brain/checkbox';
import { BrnFieldControlDescribedBy } from '@spartan-ng/brain/field';
import { ChangeDetectionStrategy, Component, booleanAttribute, computed, forwardRef, input, linkedSignal, model, output, viewChild } from '@angular/core';
import { NG_VALUE_ACCESSOR, type ControlValueAccessor } from '@angular/forms';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { hlm } from '@spartan-ng/helm/utils';
import { lucideCheck } from '@ng-icons/lucide';
import { type BooleanInput } from '@angular/cdk/coercion';
import { type ChangeFn, type TouchFn } from '@spartan-ng/brain/forms';
import { type ClassValue } from 'cn';
export const HLM_CHECKBOX_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => HlmCheckbox),
multi: true,
};
@Component({
selector: 'hlm-checkbox',
exportAs: 'hlmCheckbox',
imports: [BrnCheckbox, NgIcon],
providers: [HLM_CHECKBOX_VALUE_ACCESSOR],
viewProviders: [provideIcons({ lucideCheck })],
changeDetection: ChangeDetectionStrategy.OnPush,
hostDirectives: [BrnFieldControlDescribedBy],
host: {
class: 'contents peer',
'data-slot': 'checkbox',
'[attr.aria-label]': 'null',
'[attr.aria-labelledby]': 'null',
'[attr.data-disabled]': '_disabled() ? "" : null',
},
template: `
<brn-checkbox
[id]="inputId()"
[name]="name()"
[class]="_computedClass()"
[checked]="checked()"
[(indeterminate)]="indeterminate"
[disabled]="_disabled()"
[required]="required()"
[aria-label]="ariaLabel()"
[aria-labelledby]="ariaLabelledby()"
[aria-describedby]="ariaDescribedby()"
[forceInvalid]="forceInvalid()"
(checkedChange)="_handleChange($event)"
(touched)="_onTouched?.()"
>
@if (checked() || indeterminate()) {
<span class="[&>ng-icon]:text-[length:--spacing(3.5)] flex items-center justify-center text-current transition-none">
<ng-icon name="lucideCheck" />
</span>
}
</brn-checkbox>
`,
})
export class HlmCheckbox implements ControlValueAccessor {
public readonly userClass = input<ClassValue>('', { alias: 'class' });
protected readonly _computedClass = computed(() =>
hlm(
'border-input dark:bg-input/30 data-checked:bg-primary data-checked:text-primary-foreground dark:data-checked:bg-primary data-checked:border-primary data-[matches-spartan-invalid=true]:aria-checked:border-primary data-[matches-spartan-invalid=true]:border-destructive dark:data-[matches-spartan-invalid=true]:border-destructive/50 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 flex size-4 items-center justify-center rounded-none border transition-colors group-has-disabled/field:opacity-50 focus-visible:ring-1 data-[matches-spartan-invalid=true]:ring-1 peer shrink-0 cursor-default outline-none disabled:cursor-not-allowed disabled:opacity-50',
this.userClass(),
this._errorStateClass(),
),
);
/** Used to set the id on the underlying brn element. */
public readonly inputId = input<string | null>(null);
/** 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' });
/** The checked state of the checkbox. */
public readonly checkedInput = input<boolean, BooleanInput>(false, { alias: 'checked', transform: booleanAttribute });
public readonly checked = linkedSignal(this.checkedInput);
/** Emits when checked state changes. */
public readonly checkedChange = output<boolean>();
/**
* The indeterminate state of the checkbox.
* For example, a "select all/deselect all" checkbox may be in the indeterminate state when some but not all of its sub-controls are checked.
*/
public readonly indeterminate = model<boolean>(false);
/** The name attribute of the checkbox. */
public readonly name = input<string | null>(null);
/** Whether the checkbox is required. */
public readonly required = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
/** Whether the checkbox is disabled. */
public readonly disabled = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
/** Whether to force the checkbox into an invalid state. */
public readonly forceInvalid = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
protected readonly _disabled = linkedSignal(this.disabled);
private readonly _brnCheckbox = viewChild.required(BrnCheckbox);
private readonly _spartanInvalid = computed(() => this.forceInvalid() || this._brnCheckbox().spartanInvalid?.());
protected readonly _errorStateClass = computed(() =>
this._spartanInvalid()
? 'border-destructive focus-visible:border-destructive focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40'
: '',
);
protected _onChange?: ChangeFn<boolean>;
protected _onTouched?: TouchFn;
protected _handleChange(value: boolean): void {
if (this._disabled()) return;
this.checked.set(value);
this.checkedChange.emit(value);
this._onChange?.(value);
}
/** CONTROL VALUE ACCESSOR */
writeValue(value: boolean): void {
this.checked.set(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 HlmCheckboxImports = [HlmCheckbox] as const;import { BrnCheckbox } from '@spartan-ng/brain/checkbox';
import { BrnFieldControlDescribedBy } from '@spartan-ng/brain/field';
import { ChangeDetectionStrategy, Component, booleanAttribute, computed, forwardRef, input, linkedSignal, model, output, viewChild } from '@angular/core';
import { NG_VALUE_ACCESSOR, type ControlValueAccessor } from '@angular/forms';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { hlm } from '@spartan-ng/helm/utils';
import { lucideCheck } from '@ng-icons/lucide';
import { type BooleanInput } from '@angular/cdk/coercion';
import { type ChangeFn, type TouchFn } from '@spartan-ng/brain/forms';
import { type ClassValue } from 'cn';
export const HLM_CHECKBOX_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => HlmCheckbox),
multi: true,
};
@Component({
selector: 'hlm-checkbox',
exportAs: 'hlmCheckbox',
imports: [BrnCheckbox, NgIcon],
providers: [HLM_CHECKBOX_VALUE_ACCESSOR],
viewProviders: [provideIcons({ lucideCheck })],
changeDetection: ChangeDetectionStrategy.OnPush,
hostDirectives: [BrnFieldControlDescribedBy],
host: {
class: 'contents peer',
'data-slot': 'checkbox',
'[attr.aria-label]': 'null',
'[attr.aria-labelledby]': 'null',
'[attr.data-disabled]': '_disabled() ? "" : null',
},
template: `
<brn-checkbox
[id]="inputId()"
[name]="name()"
[class]="_computedClass()"
[checked]="checked()"
[(indeterminate)]="indeterminate"
[disabled]="_disabled()"
[required]="required()"
[aria-label]="ariaLabel()"
[aria-labelledby]="ariaLabelledby()"
[aria-describedby]="ariaDescribedby()"
[forceInvalid]="forceInvalid()"
(checkedChange)="_handleChange($event)"
(touched)="_onTouched?.()"
>
@if (checked() || indeterminate()) {
<span class="[&>ng-icon]:text-[length:--spacing(3.5)] flex items-center justify-center text-current transition-none">
<ng-icon name="lucideCheck" />
</span>
}
</brn-checkbox>
`,
})
export class HlmCheckbox implements ControlValueAccessor {
public readonly userClass = input<ClassValue>('', { alias: 'class' });
protected readonly _computedClass = computed(() =>
hlm(
'border-input dark:bg-input/30 data-checked:bg-primary data-checked:text-primary-foreground dark:data-checked:bg-primary data-checked:border-primary data-[matches-spartan-invalid=true]:aria-checked:border-primary data-[matches-spartan-invalid=true]:border-destructive dark:data-[matches-spartan-invalid=true]:border-destructive/50 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 flex size-4 items-center justify-center rounded-[6px] border transition-shadow group-has-disabled/field:opacity-50 focus-visible:ring-[3px] data-[matches-spartan-invalid=true]:ring-[3px] peer shrink-0 cursor-default outline-none disabled:cursor-not-allowed disabled:opacity-50',
this.userClass(),
this._errorStateClass(),
),
);
/** Used to set the id on the underlying brn element. */
public readonly inputId = input<string | null>(null);
/** 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' });
/** The checked state of the checkbox. */
public readonly checkedInput = input<boolean, BooleanInput>(false, { alias: 'checked', transform: booleanAttribute });
public readonly checked = linkedSignal(this.checkedInput);
/** Emits when checked state changes. */
public readonly checkedChange = output<boolean>();
/**
* The indeterminate state of the checkbox.
* For example, a "select all/deselect all" checkbox may be in the indeterminate state when some but not all of its sub-controls are checked.
*/
public readonly indeterminate = model<boolean>(false);
/** The name attribute of the checkbox. */
public readonly name = input<string | null>(null);
/** Whether the checkbox is required. */
public readonly required = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
/** Whether the checkbox is disabled. */
public readonly disabled = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
/** Whether to force the checkbox into an invalid state. */
public readonly forceInvalid = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
protected readonly _disabled = linkedSignal(this.disabled);
private readonly _brnCheckbox = viewChild.required(BrnCheckbox);
private readonly _spartanInvalid = computed(() => this.forceInvalid() || this._brnCheckbox().spartanInvalid?.());
protected readonly _errorStateClass = computed(() =>
this._spartanInvalid()
? 'border-destructive focus-visible:border-destructive focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40'
: '',
);
protected _onChange?: ChangeFn<boolean>;
protected _onTouched?: TouchFn;
protected _handleChange(value: boolean): void {
if (this._disabled()) return;
this.checked.set(value);
this.checkedChange.emit(value);
this._onChange?.(value);
}
/** CONTROL VALUE ACCESSOR */
writeValue(value: boolean): void {
this.checked.set(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 HlmCheckboxImports = [HlmCheckbox] as const;import { BrnCheckbox } from '@spartan-ng/brain/checkbox';
import { BrnFieldControlDescribedBy } from '@spartan-ng/brain/field';
import { ChangeDetectionStrategy, Component, booleanAttribute, computed, forwardRef, input, linkedSignal, model, output, viewChild } from '@angular/core';
import { NG_VALUE_ACCESSOR, type ControlValueAccessor } from '@angular/forms';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { hlm } from '@spartan-ng/helm/utils';
import { lucideCheck } from '@ng-icons/lucide';
import { type BooleanInput } from '@angular/cdk/coercion';
import { type ChangeFn, type TouchFn } from '@spartan-ng/brain/forms';
import { type ClassValue } from 'cn';
export const HLM_CHECKBOX_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => HlmCheckbox),
multi: true,
};
@Component({
selector: 'hlm-checkbox',
exportAs: 'hlmCheckbox',
imports: [BrnCheckbox, NgIcon],
providers: [HLM_CHECKBOX_VALUE_ACCESSOR],
viewProviders: [provideIcons({ lucideCheck })],
changeDetection: ChangeDetectionStrategy.OnPush,
hostDirectives: [BrnFieldControlDescribedBy],
host: {
class: 'contents peer',
'data-slot': 'checkbox',
'[attr.aria-label]': 'null',
'[attr.aria-labelledby]': 'null',
'[attr.data-disabled]': '_disabled() ? "" : null',
},
template: `
<brn-checkbox
[id]="inputId()"
[name]="name()"
[class]="_computedClass()"
[checked]="checked()"
[(indeterminate)]="indeterminate"
[disabled]="_disabled()"
[required]="required()"
[aria-label]="ariaLabel()"
[aria-labelledby]="ariaLabelledby()"
[aria-describedby]="ariaDescribedby()"
[forceInvalid]="forceInvalid()"
(checkedChange)="_handleChange($event)"
(touched)="_onTouched?.()"
>
@if (checked() || indeterminate()) {
<span class="[&>ng-icon]:text-[length:--spacing(3.5)] flex items-center justify-center text-current transition-none">
<ng-icon name="lucideCheck" />
</span>
}
</brn-checkbox>
`,
})
export class HlmCheckbox implements ControlValueAccessor {
public readonly userClass = input<ClassValue>('', { alias: 'class' });
protected readonly _computedClass = computed(() =>
hlm(
'border-input dark:bg-input/30 data-checked:bg-primary data-checked:text-primary-foreground dark:data-checked:bg-primary data-checked:border-primary data-[matches-spartan-invalid=true]:aria-checked:border-primary data-[matches-spartan-invalid=true]:border-destructive dark:data-[matches-spartan-invalid=true]:border-destructive/50 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 flex size-4 items-center justify-center rounded-[4px] border transition-shadow group-has-disabled/field:opacity-50 focus-visible:ring-2 data-[matches-spartan-invalid=true]:ring-2 peer shrink-0 cursor-default outline-none disabled:cursor-not-allowed disabled:opacity-50',
this.userClass(),
this._errorStateClass(),
),
);
/** Used to set the id on the underlying brn element. */
public readonly inputId = input<string | null>(null);
/** 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' });
/** The checked state of the checkbox. */
public readonly checkedInput = input<boolean, BooleanInput>(false, { alias: 'checked', transform: booleanAttribute });
public readonly checked = linkedSignal(this.checkedInput);
/** Emits when checked state changes. */
public readonly checkedChange = output<boolean>();
/**
* The indeterminate state of the checkbox.
* For example, a "select all/deselect all" checkbox may be in the indeterminate state when some but not all of its sub-controls are checked.
*/
public readonly indeterminate = model<boolean>(false);
/** The name attribute of the checkbox. */
public readonly name = input<string | null>(null);
/** Whether the checkbox is required. */
public readonly required = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
/** Whether the checkbox is disabled. */
public readonly disabled = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
/** Whether to force the checkbox into an invalid state. */
public readonly forceInvalid = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
protected readonly _disabled = linkedSignal(this.disabled);
private readonly _brnCheckbox = viewChild.required(BrnCheckbox);
private readonly _spartanInvalid = computed(() => this.forceInvalid() || this._brnCheckbox().spartanInvalid?.());
protected readonly _errorStateClass = computed(() =>
this._spartanInvalid()
? 'border-destructive focus-visible:border-destructive focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40'
: '',
);
protected _onChange?: ChangeFn<boolean>;
protected _onTouched?: TouchFn;
protected _handleChange(value: boolean): void {
if (this._disabled()) return;
this.checked.set(value);
this.checkedChange.emit(value);
this._onChange?.(value);
}
/** CONTROL VALUE ACCESSOR */
writeValue(value: boolean): void {
this.checked.set(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 HlmCheckboxImports = [HlmCheckbox] as const;import { BrnCheckbox } from '@spartan-ng/brain/checkbox';
import { BrnFieldControlDescribedBy } from '@spartan-ng/brain/field';
import { ChangeDetectionStrategy, Component, booleanAttribute, computed, forwardRef, input, linkedSignal, model, output, viewChild } from '@angular/core';
import { NG_VALUE_ACCESSOR, type ControlValueAccessor } from '@angular/forms';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { hlm } from '@spartan-ng/helm/utils';
import { lucideCheck } from '@ng-icons/lucide';
import { type BooleanInput } from '@angular/cdk/coercion';
import { type ChangeFn, type TouchFn } from '@spartan-ng/brain/forms';
import { type ClassValue } from 'cn';
export const HLM_CHECKBOX_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => HlmCheckbox),
multi: true,
};
@Component({
selector: 'hlm-checkbox',
exportAs: 'hlmCheckbox',
imports: [BrnCheckbox, NgIcon],
providers: [HLM_CHECKBOX_VALUE_ACCESSOR],
viewProviders: [provideIcons({ lucideCheck })],
changeDetection: ChangeDetectionStrategy.OnPush,
hostDirectives: [BrnFieldControlDescribedBy],
host: {
class: 'contents peer',
'data-slot': 'checkbox',
'[attr.aria-label]': 'null',
'[attr.aria-labelledby]': 'null',
'[attr.data-disabled]': '_disabled() ? "" : null',
},
template: `
<brn-checkbox
[id]="inputId()"
[name]="name()"
[class]="_computedClass()"
[checked]="checked()"
[(indeterminate)]="indeterminate"
[disabled]="_disabled()"
[required]="required()"
[aria-label]="ariaLabel()"
[aria-labelledby]="ariaLabelledby()"
[aria-describedby]="ariaDescribedby()"
[forceInvalid]="forceInvalid()"
(checkedChange)="_handleChange($event)"
(touched)="_onTouched?.()"
>
@if (checked() || indeterminate()) {
<span class="[&>ng-icon]:text-[length:--spacing(3.5)] flex items-center justify-center text-current transition-none">
<ng-icon name="lucideCheck" />
</span>
}
</brn-checkbox>
`,
})
export class HlmCheckbox implements ControlValueAccessor {
public readonly userClass = input<ClassValue>('', { alias: 'class' });
protected readonly _computedClass = computed(() =>
hlm(
'bg-input/90 data-checked:bg-primary data-checked:text-primary-foreground dark:data-checked:bg-primary data-checked:border-primary data-[matches-spartan-invalid=true]:aria-checked:border-primary data-[matches-spartan-invalid=true]:border-destructive dark:data-[matches-spartan-invalid=true]:border-destructive/50 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 flex size-4 items-center justify-center rounded-[5px] border border-transparent transition-shadow group-has-disabled/field:opacity-50 focus-visible:ring-3 data-[matches-spartan-invalid=true]:ring-3 peer shrink-0 cursor-default outline-none disabled:cursor-not-allowed disabled:opacity-50',
this.userClass(),
this._errorStateClass(),
),
);
/** Used to set the id on the underlying brn element. */
public readonly inputId = input<string | null>(null);
/** 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' });
/** The checked state of the checkbox. */
public readonly checkedInput = input<boolean, BooleanInput>(false, { alias: 'checked', transform: booleanAttribute });
public readonly checked = linkedSignal(this.checkedInput);
/** Emits when checked state changes. */
public readonly checkedChange = output<boolean>();
/**
* The indeterminate state of the checkbox.
* For example, a "select all/deselect all" checkbox may be in the indeterminate state when some but not all of its sub-controls are checked.
*/
public readonly indeterminate = model<boolean>(false);
/** The name attribute of the checkbox. */
public readonly name = input<string | null>(null);
/** Whether the checkbox is required. */
public readonly required = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
/** Whether the checkbox is disabled. */
public readonly disabled = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
/** Whether to force the checkbox into an invalid state. */
public readonly forceInvalid = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
protected readonly _disabled = linkedSignal(this.disabled);
private readonly _brnCheckbox = viewChild.required(BrnCheckbox);
private readonly _spartanInvalid = computed(() => this.forceInvalid() || this._brnCheckbox().spartanInvalid?.());
protected readonly _errorStateClass = computed(() =>
this._spartanInvalid()
? 'border-destructive focus-visible:border-destructive focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40'
: '',
);
protected _onChange?: ChangeFn<boolean>;
protected _onTouched?: TouchFn;
protected _handleChange(value: boolean): void {
if (this._disabled()) return;
this.checked.set(value);
this.checkedChange.emit(value);
this._onChange?.(value);
}
/** CONTROL VALUE ACCESSOR */
writeValue(value: boolean): void {
this.checked.set(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 HlmCheckboxImports = [HlmCheckbox] as const;Usage
import { HlmCheckboxImports } from '@spartan-ng/helm/checkbox';<hlm-checkbox />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 { HlmCheckboxImports } from '@spartan-ng/helm/checkbox';
import { HlmLabelImports } from '@spartan-ng/helm/label';
@Component({
selector: 'spartan-checkbox-rtl-preview',
imports: [HlmLabelImports, HlmCheckboxImports],
host: {
class: 'flex flex-col gap-4',
'[dir]': '_dir()',
},
template: `
@for (item of _items; track item.id) {
<div class="flex items-center gap-3">
<hlm-checkbox [inputId]="item.id" [checked]="item.checked" />
<label hlmLabel [for]="item.id">{{ _t()[item.labelKey] }}</label>
</div>
}
`,
})
export class CheckboxRtlPreview {
private readonly _language = inject(TranslateService).language;
private readonly _translations: Translations = {
en: {
dir: 'ltr',
values: {
terms: 'Accept terms and conditions',
newsletter: 'Subscribe to the newsletter',
notifications: 'Enable notifications',
},
},
ar: {
dir: 'rtl',
values: {
terms: 'أوافق على الشروط والأحكام',
newsletter: 'الاشتراك في النشرة الإخبارية',
notifications: 'تفعيل الإشعارات',
},
},
he: {
dir: 'rtl',
values: {
terms: 'אני מסכים לתנאים ולהגבלות',
newsletter: 'הירשם לניוזלטר',
notifications: 'הפעל התראות',
},
},
};
protected readonly _items = [
{ id: 'rtl-terms', labelKey: 'terms' as const, checked: true },
{ id: 'rtl-newsletter', labelKey: 'newsletter' as const, checked: false },
{ id: 'rtl-notifications', labelKey: 'notifications' as const, checked: false },
];
private readonly _translation = computed(() => this._translations[this._language()]);
protected readonly _t = computed(() => this._translation().values);
protected readonly _dir = computed(() => this._translation().dir);
}Brain API
BrnCheckbox
Selector: brn-checkbox
ExportAs: brnCheckbox
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| checked | boolean | false | The checked state of the checkbox. Can be bound with [(checked)] for two-way binding. |
| id | string | null | ++uniqueIdCounter + '' | Unique identifier for checkbox component. When provided, inner button gets ID without '-checkbox' suffix. Auto-generates ID if not provided. |
| name | string | null | null | Form control name for checkbox. When provided, inner button gets name without '-checkbox' 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. |
| 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 checkbox for accessibility. Auto-set when checkbox is inside label element. |
| aria-describedby | string | null | null | ID of element that describes this checkbox for accessibility. |
| required | boolean | false | Whether checkbox is required in a form. |
| disabled | boolean | false | Whether checkbox is disabled. Disabled checkboxes cannot be toggled and indicate disabled state through data-disabled attribute. |
| forceInvalid | boolean | false | Whether to force the field into an invalid state, regardless of the form control's state. Overrides both the `data-invalid` and `data-matches-spartan-invalid` attributes. |
| indeterminate | boolean | false | - |
Outputs
| Prop | Type | Default | Description |
|---|---|---|---|
| checkedChange | boolean | - | Emits when checked state changes. |
| touched | void | - | Event emitted when checkbox is blurred (loses focus). Used for form validation. |
| indeterminateChange | boolean | false | - |
Helm API
HlmCheckbox
Selector: hlm-checkbox
ExportAs: hlmCheckbox
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| class | ClassValue | '' | - |
| inputId | string | null | null | Used to set the id on the underlying brn element. |
| 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. |
| checked | boolean | false | The checked state of the checkbox. |
| name | string | null | null | The name attribute of the checkbox. |
| required | boolean | false | Whether the checkbox is required. |
| disabled | boolean | false | Whether the checkbox is disabled. |
| forceInvalid | boolean | false | Whether to force the checkbox into an invalid state. |
| indeterminate | boolean | false | The indeterminate state of the checkbox. For example, a "select all/deselect all" checkbox may be in the indeterminate state when some but not all of its sub-controls are checked. |
Outputs
| Prop | Type | Default | Description |
|---|---|---|---|
| checkedChange | boolean | - | Emits when checked state changes. |
| indeterminateChange | boolean | false | The indeterminate state of the checkbox. For example, a "select all/deselect all" checkbox may be in the indeterminate state when some but not all of its sub-controls are checked. |
On This Page
Stop configuring. Start shipping.
Zerops powers spartan.ng and Angular teams worldwide.
One-command deployment. Zero infrastructure headaches.
Deploy with Zerops