# Skeleton

Pulsing placeholder for content that is loading, sized through classes.

- Category: Feedback
- Status: stable
- Registry: https://markas.jenderalcorp.com/r/skeleton.json
- CLI: `npx markasui@latest add skeleton`

## When to use

Holding the place of loading content so the layout does not shift.

- Match the shape and size of the real content
- Use shape circle for avatars, not the rounded-full class
- Add sr-only text to the wrapper so the loading state is announced

## Code

### Blade

Save at `resources/views/components/markas/skeleton.blade.php`. No extra dependencies.

```blade
@props([
    'shape' => 'rect',
])

@php
    $base = 'animate-pulse bg-subtle motion-reduce:animate-none';
    $shapes = [
        'rect' => 'rounded-md',
        'circle' => 'rounded-full',
    ];
@endphp

<div {{ $attributes->merge(['aria-hidden' => 'true'])->class([$base, $shapes[$shape]]) }}></div>
```

```blade
<div class="flex items-center gap-4" role="status">
    <span class="sr-only">Loading profile</span>
    <x-markas.skeleton shape="circle" class="size-12 shrink-0" />
    <div class="grid flex-1 gap-2">
        <x-markas.skeleton class="h-4 w-2/3" />
        <x-markas.skeleton class="h-4 w-1/2" />
    </div>
</div>
```

### React

Save at `src/components/markas/Skeleton.tsx`. No extra dependencies.

```tsx
import type { ComponentProps } from "react";

const base = "animate-pulse bg-subtle motion-reduce:animate-none";
const shapes = {
  rect: "rounded-md",
  circle: "rounded-full",
};
const cx = (...c: (string | false | null | undefined)[]) =>
  c.filter(Boolean).join(" ");

export type SkeletonProps = ComponentProps<"div"> & {
  shape?: keyof typeof shapes;
};

export function Skeleton({
  shape = "rect",
  className,
  ...props
}: SkeletonProps) {
  return (
    <div
      aria-hidden="true"
      className={cx(base, shapes[shape], className)}
      {...props}
    />
  );
}
```

```tsx
import { Skeleton } from "@/components/markas/Skeleton";

<div className="flex items-center gap-4" role="status">
  <span className="sr-only">Loading profile</span>
  <Skeleton shape="circle" className="size-12 shrink-0" />
  <div className="grid flex-1 gap-2">
    <Skeleton className="h-4 w-2/3" />
    <Skeleton className="h-4 w-1/2" />
  </div>
</div>;
```

### Vue

Save at `src/components/markas/Skeleton.vue`. No extra dependencies.

```vue
<script setup lang="ts">
const props = withDefaults(
  defineProps<{
    shape?: "rect" | "circle";
  }>(),
  { shape: "rect" },
);

const base = "animate-pulse bg-subtle motion-reduce:animate-none";
const shapes = {
  rect: "rounded-md",
  circle: "rounded-full",
};
</script>

<template>
  <div aria-hidden="true" :class="[base, shapes[props.shape]]" />
</template>
```

```vue
<script setup lang="ts">
import Skeleton from "@/components/markas/Skeleton.vue";
</script>

<template>
  <div class="flex items-center gap-4" role="status">
    <span class="sr-only">Loading profile</span>
    <Skeleton shape="circle" class="size-12 shrink-0" />
    <div class="grid flex-1 gap-2">
      <Skeleton class="h-4 w-2/3" />
      <Skeleton class="h-4 w-1/2" />
    </div>
  </div>
</template>
```

### Svelte

Save at `src/lib/components/markas/Skeleton.svelte`. No extra dependencies.

```svelte
<script lang="ts">
  import type { HTMLAttributes } from "svelte/elements";

  const base = "animate-pulse bg-subtle motion-reduce:animate-none";
  const shapes = {
    rect: "rounded-md",
    circle: "rounded-full",
  };

  type Props = HTMLAttributes<HTMLDivElement> & {
    shape?: keyof typeof shapes;
  };

  let { shape = "rect", class: className, ...rest }: Props = $props();
</script>

<div
  aria-hidden="true"
  class={[base, shapes[shape], className]}
  {...rest}
></div>
```

```svelte
<script lang="ts">
  import Skeleton from "$lib/components/markas/Skeleton.svelte";
</script>

<div class="flex items-center gap-4" role="status">
  <span class="sr-only">Loading profile</span>
  <Skeleton shape="circle" class="size-12" />
  <Skeleton class="h-4 w-1/2" />
</div>
```

### Astro

Save at `src/components/markas/Skeleton.astro`. No extra dependencies.

```astro
---
import type { HTMLAttributes } from "astro/types";

const base = "animate-pulse bg-subtle motion-reduce:animate-none";
const shapes = {
  rect: "rounded-md",
  circle: "rounded-full",
};

type Props = HTMLAttributes<"div"> & {
  shape?: keyof typeof shapes;
};

const { shape = "rect", class: className, ...rest } = Astro.props;
---

<div aria-hidden="true" class:list={[base, shapes[shape], className]} {...rest}></div>
```

```astro
---
import Skeleton from "@/components/markas/Skeleton.astro";
---

<div class="flex items-center gap-4" role="status">
  <span class="sr-only">Loading profile</span>
  <Skeleton shape="circle" class="size-12" />
  <Skeleton class="h-4 w-1/2" />
</div>
```

### Angular

Save at `src/app/markas/skeleton.component.ts`. No extra dependencies.

```ts
import {
  ChangeDetectionStrategy,
  Component,
  computed,
  input,
} from "@angular/core";

const base = "animate-pulse bg-subtle motion-reduce:animate-none";
const shapes = {
  rect: "rounded-md",
  circle: "rounded-full",
};

export type SkeletonShape = keyof typeof shapes;

/**
 * Attribute selector on a native div: the user class (size, width) stays on the placeholder
 * itself. The placeholder is hidden from assistive technology.
 */
@Component({
  selector: "div[markas-skeleton]",
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: { "aria-hidden": "true", "[class]": "classes()" },
  template: ``,
})
export class Skeleton {
  readonly shape = input<SkeletonShape>("rect");

  protected readonly classes = computed(
    () => `${base} ${shapes[this.shape()]}`,
  );
}
```

```ts
import { Component } from "@angular/core";
import { Skeleton } from "./markas/skeleton.component";

@Component({
  selector: "app-profile-loading",
  imports: [Skeleton],
  template: `
    <div class="flex items-center gap-4" role="status">
      <span class="sr-only">Loading profile</span>
      <div markas-skeleton shape="circle" class="size-12"></div>
      <div markas-skeleton class="h-4 w-1/2"></div>
    </div>
  `,
})
export class ProfileLoading {}
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `shape` | `'rect' \| 'circle'` | rect | 6px rounded corners, or a full circle for avatars. |
| `class` | `string` | - | Placeholder size, for example h-4 w-1/2 or size-12. |

## Accessibility

- Every skeleton is aria-hidden, so screen readers do not announce empty boxes.
- Wrap a group of skeletons in role status with sr-only text such as Loading profile.
- The pulse animation is turned off under prefers-reduced-motion.

## Notes

- Set the size with classes (`h-4 w-1/2`, `size-12`). For a circle use `shape="circle"`, not the `rounded-full` class, because without `tailwind-merge` conflicting radius classes depend on CSS order.
