# API of `@koobiq/components/button-toggle`

Documented on [Button toggle](https://koobiq.io/en/components/button-toggle.md). NgModule: `KbqButtonToggleModule`.

## Components

### KbqButtonToggle (component)

Single button inside of a toggle group.

```ts
@Component({ selector: 'kbq-button-toggle', exportAs: 'kbqButtonToggle' })
class KbqButtonToggle {
    checked = input<boolean | undefined>();
    value = input<NonNullable<unknown> | null | undefined>();
    readonly ariaLabel = input<string | null>(null, {
        alias: 'aria-label'
    });
    readonly ariaLabelledby = input<string | null>(null, {
        alias: 'aria-labelledby'
    });
    disabled = input<boolean | undefined>();
    readonly change = output<KbqButtonToggleChange>();
    get type(): ToggleType;
    get iconType(): string;
    focus(): void;
    focusViaKeyboard(): void;
    onToggleClick(): void;
    markForCheck(): void;
}
```

- `[checked]`: `boolean | undefined` — Whether the button is checked.
- `[value]`: `NonNullable<unknown> | null | undefined` — Value of the toggle. The value of a selected toggle is the value of its group.
- `[aria-label]`: `string | null` — Accessible name of the toggle. Required when the toggle projects nothing but icons.
- `[aria-labelledby]`: `string | null` — Id of the element that labels the toggle.
- `[disabled]`: `boolean | undefined` — Whether the toggle is disabled. A toggle inside a disabled group is disabled as well.
- `(change)`: `KbqButtonToggleChange` — Event emitted when the group value changes.
- `type`: `ToggleType` — Whether the toggle acts as a radio button (a single-selection group) or as a checkbox
  (a multiple-selection group, or a standalone toggle).
- `iconType`: `string` — Whether the toggle holds icons only (`-icon`), icons beside a label (`-icon-text`) or no icon
  at all (`''`). Rendered as the `.kbq-button-toggle-icon`/`.kbq-button-toggle-icon-text` host
  class, which is the only signal a stylesheet gets: CSS cannot see text nodes, so a selector can
  never tell an icon-only toggle from a label that happens to contain an icon.
- `focus()`: `void` — Focuses the button.
- `focusViaKeyboard()`: `void` — Focuses the button as if the user had reached it with the keyboard, showing the focus ring.
- `onToggleClick()`: `void` — Checks the button toggle due to an interaction with the underlying native button.
- `markForCheck()`: `void` — Marks the button toggle as needing checking for change detection.

  Kept for back-compatibility. A toggle now derives `checked` and `disabled` from signals owned
  by its group, so it re-renders on its own and nothing in the library calls this any more.

## Directives

### KbqButtonToggleGroup (directive)

Selection group for `KbqButtonToggle`.

By default it behaves like a radio-button group — one toggle at a time, announced as a
`radiogroup` and walked with the arrow keys. With `multiple` the toggles become independent
toggle buttons and the group is announced as a plain `group`.

Name it with a plain `aria-label`/`aria-labelledby` attribute. The host is the very element the
consumer writes, so an input aliased to the same name would only write back what is already
there — and would wipe an `[attr.aria-label]` binding, which never reaches the input.

```ts
@Directive({
    selector: 'kbq-button-toggle-group',
    exportAs: 'kbqButtonToggleGroup'
})
class KbqButtonToggleGroup implements ControlValueAccessor {
    readonly vertical = input<boolean>(false);
    readonly stretched = input<boolean>(false);
    readonly multiple = input<boolean>(false);
    value = model<NonNullable<unknown> | null | undefined>();
    disabled = input<boolean | undefined>();
    readonly change = output<KbqButtonToggleChange>();
    get selected(): KbqButtonToggle | KbqButtonToggle[] | null;
    readonly buttonToggles: Signal<readonly KbqButtonToggle[]>;
    emitChangeEvent(source: KbqButtonToggle): void;
    syncButtonToggle(
        toggle: KbqButtonToggle,
        select: boolean,
        isUserInput?: boolean
    ): void;
    isSelected(toggle: KbqButtonToggle): boolean;
    isPrechecked(toggle: KbqButtonToggle): boolean;
}
```

- `[vertical]`: `boolean` — Whether the toggle group is vertical.
- `[stretched]`: `boolean` — Whether the toggle group stretches to fill its container width, with toggles sharing equal space.
- `[multiple]`: `boolean` — Whether multiple button toggles can be selected.
- `[(value)]`: `NonNullable<unknown> | null | undefined` — Value of the toggle group: an array in multiple-selection mode, a single value otherwise.

  An accessor rather than a `model()`: writing it walks the toggles to find the ones that match,
  and reading it reports the selection. `[(value)]` works all the same.

  An assigned value that corresponds to no toggle is kept and reported as is, so that it applies
  to a toggle rendered later — the same contract as `KbqRadioGroup`. It follows that `value` can
  name a toggle `selected` does not hold: `selected` only ever reports toggles that exist.
- `[disabled]`: `boolean | undefined` — Whether the whole button toggle group is disabled.
- `(change)`: `KbqButtonToggleChange` — Event emitted when the group's value changes.
- `selected`: `KbqButtonToggle | KbqButtonToggle[] | null` — Selected button toggles in the group: an array in multiple-selection mode, one toggle otherwise.
  Only toggles that exist, so a `value` waiting for its toggle is not represented here.
- `buttonToggles`: `Signal<readonly KbqButtonToggle[]>` — Child button toggle buttons.
- `emitChangeEvent()`: `void` — Dispatches a change event for the group value.
  - `source`: `KbqButtonToggle` — Toggle the change originated from. Taken from the interaction rather than from the
    selection, which is empty whenever the last selected toggle of a multiple-selection group is
    unchecked — there is no "last selected" toggle left to report then.
- `syncButtonToggle()`: `void` — Syncs a button toggle's selected state with the model value.
  - `toggle`: `KbqButtonToggle` — Toggle to be synced.
  - `select`: `boolean` — Whether the toggle should be selected.
  - `isUserInput`: `boolean` — Whether the change was a result of a user interaction.
- `isSelected()`: `boolean` — Checks whether a button toggle is selected.
- `isPrechecked()`: `boolean` — Determines whether a button toggle should be checked on init, from the value waiting for it.

## Classes

### KbqButtonToggleChange (class)

Change event object emitted by KbqButtonToggle.

```ts
class KbqButtonToggleChange {
    source: KbqButtonToggle;
    value: any;
    constructor(source: KbqButtonToggle, value: any);
}
```

- `source`: `KbqButtonToggle` — The KbqButtonToggle that emits the event.
- `value`: `any` — The value assigned to the KbqButtonToggle.
- `constructor()`
  - `source`: `KbqButtonToggle` — The KbqButtonToggle that emits the event.
  - `value`: `any` — The value assigned to the KbqButtonToggle.

## Types

### ToggleType (type)

Acceptable types for a button toggle.

```ts
type ToggleType = 'checkbox' | 'radio';
```
