Skip to content

Toggle

A button that stays on or off.

View as Markdown

Example

Sizes

xs, sm, md (default) and lg: set size="sm" on it, or data-size="sm" on any container, as here. See Theming.

xs
sm
md
lg

Installation

npx fizzy-ui add toggle

Copies toggle.ts into src/app/ui/ and installs what it needs. Run init once first.

Keyboard

KeyAction
Enter / SpaceTurn on or off

Accessibility

No axe-core violations axe-core 4.14.0 checks this example, closed and with its panels open, on every build; the site is not published otherwise.

  • A native <button> with aria-pressed.
  • An icon-only toggle needs an aria-label that does not change with the state.

Testing

FzToggleHarness drives it in your tests, the way a user would, with the Angular CDK harness API. --harness also copies toggle.harness.ts; its methods are documented in the file.

npx fizzy-ui add toggle --harness
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { FzToggleHarness } from './ui/toggle.harness';

const toggle = await TestbedHarnessEnvironment.loader(fixture).getHarness(FzToggleHarness);

Source

The exact file add copies. It is yours to edit.

toggle.ts
import { Directive, ElementRef, booleanAttribute, computed, inject, input, model, output } from '@angular/core';
import { Toolbar, ToolbarWidget } from '@angular/aria/toolbar';

const base =
  'inline-flex h-(--fz-h) min-w-(--fz-h) cursor-pointer items-center justify-center gap-2 whitespace-nowrap rounded-md px-[calc(var(--fz-px)/2)] text-(length:--fz-text) font-medium transition-colors ' +
  'outline-none focus-visible:ring-2 focus-visible:ring-ring hover:bg-muted hover:text-muted-foreground ' +
  'aria-pressed:bg-accent aria-pressed:text-accent-foreground ' +
  'disabled:pointer-events-none disabled:opacity-50 aria-disabled:pointer-events-none aria-disabled:opacity-50';
const variants = { default: 'bg-transparent', outline: 'border bg-transparent shadow-xs hover:bg-accent hover:text-accent-foreground' };
type Variant = keyof typeof variants;
type FzSize = 'xs' | 'sm' | 'md' | 'lg';

/** An on/off button: `<button fzToggle [(pressed)]="bold" aria-label="Bold">`. A native button with `aria-pressed`. */
@Directive({
  selector: 'button[fzToggle]',
  host: { type: 'button', '[attr.aria-pressed]': 'pressed()', '(click)': 'pressed.set(!pressed())', '[class]': 'classes()', '[attr.data-size]': 'size() ?? null' },
})
export class FzToggle {
  readonly pressed = model(false);
  readonly variant = input<Variant>('default');
  /** xs, sm, md, lg. Unset, it takes the size of the nearest `data-size` container, else md. */
  readonly size = input<FzSize>();
  protected readonly classes = computed(() => `${base} ${variants[this.variant()]}`);
}

/**
 * A set of toggles with one Tab stop (arrows, Home, End move between them):
 * `<div fzToggleGroup [(value)]="align" aria-label="Alignment">` + `<button fzToggleGroupItem value="left">`.
 * One value at a time (pressing it again clears it, unless `required`), or `multiple` for an array of values.
 * Every item is an `aria-pressed` button in both modes.
 */
@Directive({
  selector: '[fzToggleGroup]',
  hostDirectives: [{ directive: Toolbar, inputs: ['orientation', 'disabled'] }],
  host: { class: 'inline-flex w-fit items-center gap-1 aria-[orientation=vertical]:flex-col', '[attr.data-size]': 'size() ?? null', '(focusout)': 'onFocusOut($event)' },
})
export class FzToggleGroup<V = unknown> {
  readonly value = model<V | V[] | undefined>();
  readonly multiple = input(false, { transform: booleanAttribute });
  /** Single mode: one item always stays pressed. */
  readonly required = input(false, { transform: booleanAttribute });
  readonly variant = input<Variant>('default');
  /** Sizes every item (they inherit it through data-size). */
  readonly size = input<FzSize>();
  /** Emits when focus leaves it: forms mark the field touched. */
  readonly touch = output();
  private readonly el: HTMLElement = inject(ElementRef).nativeElement;

  protected onFocusOut(event: FocusEvent) {
    if (!this.el.contains(event.relatedTarget as Node | null)) this.touch.emit();
  }

  isPressed(v: V) {
    const value = this.value();
    return this.multiple() ? Array.isArray(value) && value.includes(v) : value === v;
  }

  toggle(v: V) {
    if (!this.multiple()) return this.value.set(this.isPressed(v) && !this.required() ? undefined : v);
    const list = Array.isArray(this.value()) ? (this.value() as V[]) : [];
    this.value.set(list.includes(v) ? list.filter((x) => x !== v) : [...list, v]);
  }
}

@Directive({
  selector: '[fzToggleGroupItem]',
  hostDirectives: [{ directive: ToolbarWidget, inputs: ['id', 'disabled'] }],
  host: { '[attr.aria-pressed]': 'group.isPressed(value())', '(click)': 'toggle()', '[class]': 'classes()' },
})
export class FzToggleGroupItem<V = unknown> {
  readonly value = input.required<V>();
  protected readonly group = inject<FzToggleGroup<V>>(FzToggleGroup);
  private readonly el: HTMLElement = inject(ElementRef).nativeElement;
  protected readonly classes = computed(() => `${base} ${variants[this.group.variant()]}`);

  protected toggle() {
    // Soft-disabled items stay focusable and get Space/Enter clicks: ignore them (own or group).
    if (!this.el.closest('[aria-disabled=true]')) this.group.toggle(this.value());
  }
}

export const FZ_TOGGLE = [FzToggle] as const;
export const FZ_TOGGLE_GROUP = [FzToggleGroup, FzToggleGroupItem] as const;