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 { HlmCheckbox } from '@spartan-ng/helm/checkbox';
import { HlmLabel } from '@spartan-ng/helm/label';
@Component({
selector: 'spartan-checkbox-preview',
imports: [HlmLabel, HlmCheckbox],
template: `
<div class="flex flex-col gap-6">
<div class="flex items-center gap-3">
<hlm-checkbox id="terms" />
<label hlmLabel for="terms">Accept terms and conditions</label>
</div>
<div class="flex items-start gap-3">
<hlm-checkbox id="terms-2" [checked]="true" />
<div class="grid gap-2">
<label hlmLabel for="terms-2">Accept terms and conditions</label>
<p class="text-muted-foreground text-sm">By clicking this checkbox, you agree to the terms and conditions.</p>
</div>
</div>
<div class="flex items-start gap-3">
<hlm-checkbox id="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
id="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 font-medium leading-none">Enable notifications</p>
<p class="text-muted-foreground text-sm">You can enable or disable notifications at any time.</p>
</div>
</label>
</div>
`,
})
export class CheckboxPreview {}
Installation
npx nx g @spartan-ng/cli:ui checkbox
ng g @spartan-ng/cli:ui checkbox
Usage
import { HlmCheckbox } from '@spartan-ng/helm/checkbox';
<hlm-checkbox />
Brain API
BrnCheckbox
Selector: brn-checkbox
Inputs
Prop | Type | Default | Description |
---|---|---|---|
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. |
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. |
checked | BrnCheckboxValue | false | Current checked state of checkbox. Can be boolean (true/false) or 'indeterminate'. Can be bound with [(checked)] for two-way binding. |
Outputs
Prop | Type | Default | Description |
---|---|---|---|
changed | BrnCheckboxValue | - | Event emitted when checkbox value changes. Emits new checked state (true/false/'indeterminate'). |
touched | void | - | Event emitted when checkbox is blurred (loses focus). Used for form validation. |
checkedChanged | BrnCheckboxValue | false | Current checked state of checkbox. Can be boolean (true/false) or 'indeterminate'. Can be bound with [(checked)] for two-way binding. |
Helm API
HlmCheckbox
Selector: hlm-checkbox
Inputs
Prop | Type | Default | Description |
---|---|---|---|
class | ClassValue | - | - |
id | 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. |
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. |
checked | CheckboxValue | false | The checked state of the checkbox. |
Outputs
Prop | Type | Default | Description |
---|---|---|---|
changed | boolean | - | - |
checkedChanged | CheckboxValue | false | The checked state of the checkbox. |