Skip to content

Button

A native button or link with variants and sizes.

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 button

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

Keyboard

KeyAction
Enter / SpaceActivate

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.

  • Styles a native <button> or <a>, so focus, disabled and form behavior are the browser's own.
  • An icon-only button needs an aria-label.

Source

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

button.ts
import { Directive, booleanAttribute, computed, input } from '@angular/core';

const base =
  'inline-flex h-(--fz-h) items-center justify-center gap-2 whitespace-nowrap rounded-md text-(length:--fz-text) font-medium transition-colors ' +
  'outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 ring-offset-background ' +
  'disabled:pointer-events-none disabled:opacity-50';

const variants = {
  default: 'bg-primary text-primary-foreground hover:bg-primary/90',
  secondary: 'bg-secondary text-secondary-foreground hover:bg-secondary/80',
  outline: 'border bg-background hover:bg-accent hover:text-accent-foreground',
  ghost: 'hover:bg-accent hover:text-accent-foreground',
  destructive: 'bg-destructive text-destructive-foreground hover:bg-destructive/90',
};

type FzSize = 'xs' | 'sm' | 'md' | 'lg';

@Directive({
  selector: 'button[fzButton], a[fzButton]',
  host: { '[class]': 'classes()', '[attr.data-size]': 'size() ?? null' },
})
export class FzButton {
  readonly variant = input<keyof typeof variants>('default');
  /** xs, sm, md, lg (the scale in fizzy.css). Unset, it takes the size of the nearest `data-size` container, else md. */
  readonly size = input<FzSize>();
  /** Square, for an icon-only button (give it an aria-label). */
  readonly icon = input(false, { transform: booleanAttribute });
  protected readonly classes = computed(() => `${base} ${variants[this.variant()]}${this.icon() ? ' w-(--fz-h)' : ' px-(--fz-px)'}`);
}

export const FZ_BUTTON = [FzButton] as const;