# Skeleton

Placeholder blocks that sketch the layout of content while it loads, so the page does not jump when the data arrives. Use it for content areas; use a spinner or a button's loading state for actions.

- Package: [@vigdx/ui](https://www.npmjs.com/package/@vigdx/ui)
- Selectors: `vig-skeleton`, `vigSkeleton`
- Source: [libs/ui/src/lib/atoms/skeleton/skeleton.ts](https://github.com/bakyurekk/vigdx/blob/main/libs/ui/src/lib/atoms/skeleton/skeleton.ts)

```typescript title="Import"
import { Skeleton, VigSkeleton } from '@vigdx/ui/skeleton';
```

## Shapes

`shape` is `text` (default), `rect` or `circle`. Text skeletons render `lines` bars, the last one shorter; `width` and `height` take any CSS length.

```typescript title="skeleton-shapes.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Skeleton } from '@vigdx/ui/skeleton';

@Component({
  selector: 'app-skeleton-shapes',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Skeleton],
  template: `
    <div class="grid gap-6 sm:grid-cols-3">
      <div class="flex flex-col gap-2">
        <span class="text-xs text-muted-foreground">text</span>
        <vig-skeleton width="70%" />
        <vig-skeleton [lines]="3" />
      </div>
      <div class="flex flex-col gap-2">
        <span class="text-xs text-muted-foreground">rect</span>
        <vig-skeleton shape="rect" height="5rem" />
      </div>
      <div class="flex flex-col gap-2">
        <span class="text-xs text-muted-foreground">circle</span>
        <div class="flex items-center gap-3">
          <vig-skeleton shape="circle" />
          <vig-skeleton shape="circle" width="3.5rem" />
        </div>
      </div>
    </div>
  `,
})
export class SkeletonShapesDemo {}
```

## Sizing with the font

Text bars are `1em` tall, so wrap them in the text style they stand in for. Set `animated` to `false` for a static placeholder; the pulse also stops for users who prefer reduced motion.

```typescript title="skeleton-sizing.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Skeleton } from '@vigdx/ui/skeleton';

@Component({
  selector: 'app-skeleton-sizing',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Skeleton],
  template: `
    <div class="flex max-w-md flex-col gap-4">
      <div class="text-h3"><vig-skeleton width="60%" /></div>
      <div class="text-sm"><vig-skeleton [lines]="2" /></div>
      <div class="text-sm"><vig-skeleton [lines]="2" [animated]="false" /></div>
    </div>
  `,
})
export class SkeletonSizingDemo {}
```

## Loading a card

Mirror the final layout with skeletons and swap them for the content when it arrives. One `ariaLabel` announces the whole placeholder; the other skeletons stay silent.

```typescript title="skeleton-loading.ts"
import {
  ChangeDetectionStrategy,
  Component,
  DestroyRef,
  afterNextRender,
  inject,
  signal,
} from '@angular/core';
import { Avatar } from '@vigdx/ui/avatar';
import { Button } from '@vigdx/ui/button';
import { Card } from '@vigdx/ui/card';
import { Skeleton } from '@vigdx/ui/skeleton';

@Component({
  selector: 'app-skeleton-loading',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Avatar, Button, Card, Skeleton],
  template: `
    <vig-card class="max-w-sm" [attr.aria-busy]="loading()">
      @if (loading()) {
        <div class="flex items-center gap-3">
          <vig-skeleton shape="circle" ariaLabel="Loading profile" />
          <div class="flex flex-1 flex-col gap-2">
            <vig-skeleton width="50%" />
            <span class="text-xs"><vig-skeleton width="70%" /></span>
          </div>
        </div>
        <div class="mt-4"><vig-skeleton [lines]="3" /></div>
      } @else {
        <div class="flex items-center gap-3">
          <vig-avatar name="Grace Hopper" variant="primary" />
          <div class="flex flex-col">
            <span class="font-semibold">Grace Hopper</span>
            <span class="text-xs text-muted-foreground">Rear admiral, computer scientist</span>
          </div>
        </div>
        <p class="mt-4">
          Pioneer of machine-independent programming languages and co-designer of COBOL.
        </p>
      }
    </vig-card>
    <div>
      <vig-button
        label="Reload"
        variant="secondary"
        size="sm"
        [loading]="loading()"
        (click)="reload()"
      />
    </div>
  `,
})
export class SkeletonLoadingDemo {
  protected readonly loading = signal(true);
  private timer: ReturnType<typeof setTimeout> | undefined;

  constructor() {
    // Simulates a request; start it in the browser only.
    afterNextRender(() => this.reload());
    inject(DestroyRef).onDestroy(() => clearTimeout(this.timer));
  }

  protected reload(): void {
    this.loading.set(true);
    clearTimeout(this.timer);
    this.timer = setTimeout(() => this.loading.set(false), 1500);
  }
}
```

## Skeleton directive

Put `vigSkeleton` on the real markup and it becomes its own placeholder while the data loads, with no second skeleton tree to keep in sync:

```html
<h2 [vigSkeleton]="loading()">{{ user()?.name }}</h2>
<p [vigSkeleton]="loading()" vigSkeletonLines="3">{{ user()?.bio }}</p>
<img [vigSkeleton]="loading()" vigSkeletonShape="circle"
     [attr.src]="user()?.avatar" alt="" width="48" height="48" />
```

While `vigSkeleton` is `true` the element keeps its box, hides its content and shows the skeleton look: one bar per line for text, a filled block for `vigSkeletonShape="rect"` and `"circle"`. An empty element is `vigSkeletonLines` lines tall (`1lh` each); `vigSkeletonWidth` and `vigSkeletonHeight` size it while loading, and `[vigSkeletonAnimated]="false"` stops the pulse. When it is `false` the directive adds nothing to the element.

## Loading in place

The card renders the same avatar, name, role and bio while loading and after. Stale content from before a reload keeps the layout and turns into bars; the status line under the card announces each loading state once.

```typescript title="skeleton-directive.ts"
import {
  ChangeDetectionStrategy,
  Component,
  DestroyRef,
  afterNextRender,
  computed,
  inject,
  signal,
} from '@angular/core';
import { Button } from '@vigdx/ui/button';
import { Card } from '@vigdx/ui/card';
import { VigSkeleton } from '@vigdx/ui/skeleton';

interface Profile {
  name: string;
  role: string;
  bio: string;
}

@Component({
  selector: 'app-skeleton-directive',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button, Card, VigSkeleton],
  template: `
    <vig-card class="max-w-sm">
      <div class="flex items-center gap-3">
        <span
          class="grid size-10 shrink-0 place-items-center rounded-avatar bg-primary font-semibold text-primary-foreground"
          [vigSkeleton]="loading()"
          vigSkeletonShape="circle"
          aria-hidden="true"
          >{{ initials() }}</span
        >
        <div class="flex min-w-0 flex-1 flex-col">
          <span class="font-semibold" [vigSkeleton]="loading()" vigSkeletonWidth="50%">{{
            user()?.name
          }}</span>
          <span
            class="text-xs text-muted-foreground"
            [vigSkeleton]="loading()"
            vigSkeletonWidth="70%"
            >{{ user()?.role }}</span
          >
        </div>
      </div>
      <p class="mt-4" [vigSkeleton]="loading()" vigSkeletonLines="3">{{ user()?.bio }}</p>
    </vig-card>
    <p class="sr-only" role="status">{{ status() }}</p>
    <div>
      <vig-button label="Reload" variant="secondary" size="sm" (click)="reload()" />
    </div>
  `,
})
export class SkeletonDirectiveDemo {
  protected readonly loading = signal(true);
  protected readonly user = signal<Profile | null>(null);
  // Empty when rendered, filled afterwards: a live region announces changes only.
  protected readonly status = signal('');
  protected readonly initials = computed(
    () =>
      this.user()
        ?.name.split(' ')
        .map((word) => word[0])
        .join('') ?? '',
  );
  private timer: ReturnType<typeof setTimeout> | undefined;

  constructor() {
    // Simulates a request; start it in the browser only.
    afterNextRender(() => this.reload());
    inject(DestroyRef).onDestroy(() => clearTimeout(this.timer));
  }

  protected reload(): void {
    this.loading.set(true);
    this.status.set('Loading profile');
    clearTimeout(this.timer);
    this.timer = setTimeout(() => {
      this.user.set({
        name: 'Grace Hopper',
        role: 'Rear admiral, computer scientist',
        bio: 'Pioneer of machine-independent programming languages and co-designer of COBOL.',
      });
      this.loading.set(false);
      this.status.set('Profile loaded');
    }, 1500);
  }
}
```

### Customizing the directive

- It follows the same tokens as `vig-skeleton`: `--color-muted` for the fill and `--radius-skeleton` for the corners.
- While loading, the element carries `[data-scope="skeleton"][data-part="root"][data-state="loading"]` and `aria-busy="true"`; style that selector to adjust the placeholder. Text bars are background images in the element's content box, so its padding, border and background colour stay; `rect` and `circle` fill the element.
- Use it on plain HTML elements. Kit components have their own scope and state on their host, so wrap them, or use their loading input. An inline element such as `<span>` has no box while empty: make it `inline-block` (a flex or grid item already is a block).

## Styling hooks

Internal elements carry a stable `data-part` and the component's `data-scope`; both are public API, so removing one is a breaking change. Select a part by both, e.g. `[data-scope="skeleton"][data-part="bar"] { … }`: the pair stays within this component when kit components nest, and also reaches parts in overlay panels, which render outside the host. State is exposed through `aria-*` and, where listed, a `data-state`; literal values are checked by the API gate, values computed from the component's state follow its documented types. Other `data-*` attributes listed below (such as `data-opening` while an overlay animates) are API too. See [Customizing](https://vigdx.com/docs/theming.md#customizing).

### Skeleton `[data-scope="skeleton"]`

| Part | Element | data-state |
| --- | --- | --- |
| `bar` | One placeholder block or text line. |   |

### VigSkeleton `[data-scope="skeleton"]`

| Part | Element | data-state |
| --- | --- | --- |
| `root` | The `[vigSkeleton]` element while loading (`inert`, `aria-busy="true"`); nothing while not. | `loading` |

## Accessibility

- Skeletons are hidden from assistive technology by default, so a group of them does not produce a series of empty announcements.
- Give one skeleton per loading region an `ariaLabel`, e.g. “Loading profile”: it becomes a `role="status"` region with that text.
- Mark the container that is loading with `aria-busy="true"` and set it back to `false` once the content is in place.
- A `vigSkeleton` element is `inert` while loading: stale or empty content is neither read nor focusable, and focusable children leave the tab order instead of being hidden while focusable. It also gets `aria-busy="true"`. It announces nothing by itself, so a page of placeholders is not a series of announcements: give the loading region one status message, as the demo above does.
- `inert` also takes focus away: when loading starts while focus is inside the element, the browser moves it to the page. Move focus to something that stays, such as the loading region itself (`tabindex="-1"`), before reloading content the user is working in; dev mode warns once when this happens.
- An element's own `inert`, `aria-busy` and `data-scope`/`data-part`/`data-state` values come back when loading ends.
- In forced-colors mode (Windows High Contrast) the placeholders are drawn in `GrayText`, since forced colors drop author backgrounds.
- The pulse animation stops when the user prefers reduced motion (`prefers-reduced-motion`).

## API

### Skeleton

Selector: `vig-skeleton` · Kind: component

Placeholder block shown while content loads. Hidden from assistive tech by default; pass `ariaLabel` to announce it as a `role="status"` region.

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `animated` | `boolean` | `true` | Pulses while loading; static with reduced motion. |
| `ariaLabel` | `string \| undefined` | `undefined` | Announces the placeholder (e.g. "Loading profile") as a status region. |
| `height` | `string \| undefined` | `undefined` | Any CSS length. Text lines are `1em` tall; rect defaults to `1em`. |
| `lines` | `number` | `1` | Number of text bars (`shape="text"` only); the last of several is shorter. |
| `shape` | `SkeletonShape` | `'text'` | — |
| `width` | `string \| undefined` | `undefined` | Any CSS length, e.g. `'12rem'` or `'60%'`. Text and rect fill their container by default. |

### VigSkeleton

Selector: `[vigSkeleton]` · Kind: directive

Turns the element it sits on into its own loading placeholder, so a loading state needs no parallel skeleton markup:

**Example :**

```html
<h2 [vigSkeleton]="loading()">{{ user()?.name }}</h2>
<p [vigSkeleton]="loading()" vigSkeletonLines="3">{{ user()?.bio }}</p>
<img [vigSkeleton]="loading()" vigSkeletonShape="circle" [attr.src]="user()?.avatar" alt="" />
```

While `vigSkeleton` is `true` the host keeps its layout box, its content is hidden (text transparent, child elements `visibility: hidden`, an image's picture moved out of its box) and it shows the `vig-skeleton` look instead: one bar per line for `text`, a filled block for `rect` and `circle`. A text host with no content yet (whitespace aside) is `vigSkeletonLines` lines tall. The host is `inert` (out of the tab order and the accessibility tree, so stale or empty content is not read; a focused element inside loses focus) and carries `aria-busy="true"`, `data-scope="skeleton"`, `data-part="root"` and `data-state="loading"`. The host's own values of these attributes come back when loading ends; while `false` the directive adds nothing.

It announces nothing: give the loading region one announcement, e.g. a `vig-skeleton` with an `ariaLabel`. Use it on plain HTML elements, not on kit components, whose hosts carry their own scope and state. An inline element (`<span>`) has no box while empty: make it `inline-block`. root skeleton loading

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `vigSkeleton` | `boolean` | `false` | Shows the placeholder while `true` (a bare `vigSkeleton` attribute counts as `true`). |
| `vigSkeletonAnimated` | `boolean` | `true` | Pulses while loading; static with reduced motion. |
| `vigSkeletonHeight` | `string \| undefined` | `undefined` | Height while loading, any CSS length; a `circle` with only a width stays square. |
| `vigSkeletonLines` | `number` | `1` | Lines an empty `text` host shows (`1lh` each); the last of several is shorter. |
| `vigSkeletonShape` | `SkeletonShape` | `'text'` | `text` (default) draws one bar per line; `rect` and `circle` fill the host. |
| `vigSkeletonWidth` | `string \| undefined` | `undefined` | Width while loading, any CSS length (e.g. `'70%'`); the host's own width otherwise. |
