# Skeleton

> Track-colored blocks with a soft pulse that hold the shape of rows while they load.

Docs: https://plumbago.ochotzas.com/components/skeleton

## Installation

```sh
npx plumbago add skeleton
```

This copies `components/ui/skeleton.tsx` into your project, along with `cn`.

## Usage

```tsx
import { Skeleton, SkeletonText } from "@/components/ui/skeleton"

<Skeleton className="size-9 rounded-full" />
<SkeletonText className="w-40 text-body" />
```

## Examples

### Preview

```tsx
import { Island, ListRow, ListRowText } from "@/components/ui/island"
import { Skeleton, SkeletonText } from "@/components/ui/skeleton"

export default function SkeletonDemo() {
  return (
    <div className="flex w-full max-w-md flex-col gap-3" role="status" aria-label="Loading tonight's orders">
      <h3 className="px-1 text-title2 font-bold text-ink">Tonight's orders</h3>
      <Island variant="list">
        {["w-2/3", "w-1/2", "w-3/5"].map((width) => (
          <ListRow key={width}>
            <Skeleton className="size-[34px] shrink-0 rounded-full" />
            <ListRowText>
              <SkeletonText className={`text-body/[22px] ${width}`} />
              <SkeletonText className="w-1/3 text-footnote" />
            </ListRowText>
            <SkeletonText className="w-12 text-body" />
          </ListRow>
        ))}
      </Island>
    </div>
  )
}
```

### Loading then loaded

```tsx
import { cn } from "@/lib/cn"
import { Avatar } from "@/components/ui/avatar"
import { Button } from "@/components/ui/button"
import { Island, ListRow, ListRowSubtitle, ListRowText, ListRowTitle, ListRowValue } from "@/components/ui/island"
import { Skeleton, SkeletonText } from "@/components/ui/skeleton"
import { useCallback, useEffect, useRef, useState } from "react"

const orders = [
  { table: "Table 4", items: "Burrata, sea bream", total: "€38.50", guest: "Ana Lima", photo: "/people/ana-lima.webp" },
  { table: "Table 9", items: "Tasting menu for two", total: "€124.00", guest: "Tom Okafor", photo: "/people/tom-okafor.webp" },
  { table: "Table 12", items: "Lamb shoulder, wine", total: "€61.00", guest: "Marta Rossi", photo: "/people/marta-rossi.webp" },
]

const layer = "col-start-1 row-start-1 transition-[opacity,filter,translate,scale]"
const shown = "delay-[120ms] duration-spring ease-spring"
const hidden = "pointer-events-none opacity-0 duration-[160ms] ease-in motion-safe:-translate-y-1 motion-safe:scale-98 motion-safe:blur-[8px]"

export default function SkeletonLoadingThenLoaded() {
  const [loaded, setLoaded] = useState(false)
  const timer = useRef<ReturnType<typeof setTimeout>>(undefined)

  const load = useCallback(() => {
    setLoaded(false)
    clearTimeout(timer.current)
    timer.current = setTimeout(() => setLoaded(true), 1600)
  }, [])

  useEffect(() => {
    load()
    return () => clearTimeout(timer.current)
  }, [load])

  return (
    <div className="flex w-full max-w-md flex-col gap-3">
      <div className="flex items-center justify-between px-1">
        <h3 className="text-title2 font-bold text-ink">Tonight's orders</h3>
        <Button variant="secondary" size="sm" onClick={load} disabled={!loaded}>
          Refresh
        </Button>
      </div>
      <div className="grid">
        <Island variant="list" className={cn(layer, loaded ? hidden : shown)} role="status" aria-label="Loading tonight's orders" inert={loaded}>
          {orders.map((order) => (
            <ListRow key={order.table}>
              <Skeleton className="size-[34px] shrink-0 rounded-full" />
              <ListRowText>
                <SkeletonText className="w-1/3 text-body/[22px]" />
                <SkeletonText className="w-1/2 text-footnote" />
              </ListRowText>
              <SkeletonText className="w-14 text-body" />
            </ListRow>
          ))}
        </Island>
        <Island variant="list" className={cn(layer, loaded ? shown : hidden)} inert={!loaded}>
          {orders.map((order) => (
            <ListRow key={order.table}>
              <Avatar name={order.guest} src={order.photo} />
              <ListRowText>
                <ListRowTitle>{order.table}</ListRowTitle>
                <ListRowSubtitle>{order.items}</ListRowSubtitle>
              </ListRowText>
              <ListRowValue className="tabular-nums">{order.total}</ListRowValue>
            </ListRow>
          ))}
        </Island>
      </div>
    </div>
  )
}
```

## Source

```tsx
import { cn } from "@/lib/cn"
import type { ComponentProps } from "react"

export type SkeletonProps = Omit<ComponentProps<"div">, "className"> & {
  className?: string
}

export function Skeleton({ className, ...props }: SkeletonProps) {
  return <div data-slot="skeleton" aria-hidden="true" className={cn("animate-skeleton rounded-sm bg-track motion-reduce:animate-none", className)} {...props} />
}

export type SkeletonTextProps = Omit<ComponentProps<"span">, "className"> & {
  className?: string
}

export function SkeletonText({ className, ...props }: SkeletonTextProps) {
  return (
    <span
      data-slot="skeleton-text"
      aria-hidden="true"
      className={cn(
        "relative block h-lh animate-skeleton text-body motion-reduce:animate-none",
        "before:absolute before:inset-x-0 before:inset-y-[0.22lh] before:rounded-full before:bg-track before:content-['']",
        className,
      )}
      {...props}
    />
  )
}
```
