Skip to content

Spinner

A small indicator that something is loading.

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.

Loadingxs
Loadingsm
Loadingmd
Loadinglg

Installation

npx fizzy-ui add spinner

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

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 status region with hidden text (label, "Loading" by default), so it is announced when it appears.
  • Inside a button, also set the button text or aria-busy so the button itself says what is happening.

Source

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

spinner.ts
import { Component, input } from '@angular/core';

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

/**
 * A loading indicator: `<fz-spinner />`, or `<fz-spinner label="Saving" class="size-6" />`.
 * A `status` region with hidden text, so screen readers say "Loading" when it appears. It takes
 * the text color; size it with `size` (xs–lg), or let it follow its container (a button's size). Under Reduce Motion it pulses instead of spinning.
 */
@Component({
  selector: 'fz-spinner',
  host: { role: 'status', class: 'inline-block size-(--fz-spinner) shrink-0 text-muted-foreground', '[attr.data-size]': 'size() ?? null' },
  template: `
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" aria-hidden="true" class="size-full animate-spin motion-reduce:animate-pulse">
      <circle cx="12" cy="12" r="9" opacity="0.25" />
      <path d="M21 12a9 9 0 0 0-9-9" />
    </svg>
    <span class="sr-only">{{ label() }}</span>
  `,
})
export class FzSpinner {
  readonly label = input('Loading');
  /** xs, sm, md, lg (its own scale in fizzy.css). Unset, it takes the size of the nearest `data-size` container, else md. */
  readonly size = input<FzSize>();
}

export const FZ_SPINNER = [FzSpinner] as const;