import { ChangeDetectionStrategy, Component, input, model } from '@angular/core'; import { uniqueId } from '../core/a11y'; import { AuFormControl, provideValueAccessor } from '../core/form-control'; /** * Native checkbox with a custom visual. Using the real `` keeps Space * toggling, form semantics and the "mixed" state (indeterminate) announced correctly. */ @Component({ selector: 'au-checkbox', changeDetection: ChangeDetectionStrategy.OnPush, providers: [provideValueAccessor(() => AuCheckbox)], template: ` @if (hint()) {

{{ hint() }}

} `, styles: ` :host { display: block; font-family: var(--au-font); color: var(--au-text); } .au-check { display: inline-flex; align-items: flex-start; gap: 0.6rem; cursor: pointer; position: relative; line-height: 1.4; } .au-check--disabled { cursor: not-allowed; opacity: 0.6; } .au-check__input { position: absolute; opacity: 0; width: 1.15rem; height: 1.15rem; margin: 0.1rem 0 0; cursor: inherit; } .au-check__box { flex: none; width: 1.15rem; height: 1.15rem; margin-top: 0.1rem; box-sizing: border-box; display: grid; place-items: center; border: 1.5px solid var(--au-border-strong); border-radius: calc(var(--au-radius) * 0.4); background: var(--au-surface); transition: background var(--au-duration), border-color var(--au-duration); } .au-check__box svg { width: 0.9rem; height: 0.9rem; fill: none; stroke: var(--au-accent-contrast); stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; } .au-check__input:checked + .au-check__box, .au-check__input:indeterminate + .au-check__box { background: var(--au-accent); border-color: var(--au-accent); } .au-check__input:focus-visible + .au-check__box { outline: 2px solid var(--au-focus); outline-offset: 2px; } .au-check__hint { margin: 0.2rem 0 0 1.75rem; font-size: 0.8rem; color: var(--au-muted); } `, }) export class AuCheckbox extends AuFormControl { readonly value = model(false); protected readonly emptyValue = false; /** "Mixed" state, e.g. for a select-all box when only some items are selected. Cleared on click. */ readonly indeterminate = model(false); readonly name = input(''); readonly hint = input(''); readonly inputId = uniqueId('au-checkbox'); protected readonly hintId = `${this.inputId}-hint`; protected toggle(event: Event): void { this.indeterminate.set(false); this.commit((event.target as HTMLInputElement).checked); } }