Skeleton

Loading placeholders in the shape of the real content, captured by boneyard.

Wrap the content in a Skeleton, name it, and set loading. boneyard’s CLI renders the page in a browser at a few widths, measures every piece of the content and saves those measurements as bones. While loading, the bones stand in, in the system’s muted color and pulse, and the content keeps its space so nothing moves when it arrives. Turn the switch off to see what they stand in for.

Preview
basic.tsx
import { useState } from "react"

import { Button } from "~/components/ui/button"
import {
  Card,
  CardContent,
  CardDescription,
  CardFooter,
  CardHeader,
  CardTitle,
} from "~/components/ui/card"
import { Label } from "~/components/ui/label"
import { Skeleton } from "~/components/ui/skeleton"
import { Switch } from "~/components/ui/switch"
import { Text } from "~/components/ui/text"

export default function Example() {
  const [loading, setLoading] = useState(true)
  return (
    <div className="grid w-full max-w-sm gap-lg">
      <Label>
        <Switch checked={loading} onCheckedChange={setLoading} />
        Loading
      </Label>
      <Skeleton name="story-card" loading={loading}>
        <Card>
          <CardHeader>
            <CardTitle>Harbour reopens after the storm</CardTitle>
            <CardDescription>Weekend · Page A1</CardDescription>
          </CardHeader>
          <CardContent>
            <Text tone="muted">
              Ferries run again from Monday, on a reduced timetable while the pier is repaired.
            </Text>
          </CardContent>
          <CardFooter className="justify-end">
            <Button variant="outline" size="sm">
              Save
            </Button>
            <Button size="sm">Read</Button>
          </CardFooter>
        </Card>
      </Skeleton>
    </div>
  )
}

Capturing bones

Capture once, and again when the content’s layout changes. Point the CLI at a page that shows the skeleton, then import the registry it writes once, where your app starts.

terminal
npx boneyard-js build http://localhost:5173 --out ./src/bones
main.tsx
import "./bones/registry"

Vite apps can use boneyardPlugin() from boneyard-js/vite instead, which captures as the dev server runs. Without bones, a Skeleton shows its fallback while loading.

Single bones

Where there’s nothing to measure, like an iframe or an image still downloading, size a SkeletonBone with classes. Bones are hidden from screen readers, so give the group a status role and a label.

Preview
bone.tsx
import { SkeletonBone } from "~/components/ui/skeleton"

export default function Example() {
  return (
    // oxlint-disable-next-line jsx-a11y/prefer-tag-over-role -- <output> is for a form's computed result, not a loading region
    <div className="flex w-full max-w-sm items-center gap-lg" role="status" aria-label="Loading">
      <SkeletonBone className="size-10 shrink-0" />
      <div className="grid flex-1 gap-sm">
        <SkeletonBone className="h-4 w-3/4" />
        <SkeletonBone className="h-4 w-1/2" />
      </div>
    </div>
  )
}

Dependencies

What it's built from and what's built from it in Mocha, read from the imports in its source.

UsesNothing else in the system.
Used byNothing yet.
Packages boneyard-js@1.10.0

Installation

Set the project up for Mocha once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/mocha/skeleton.json

Manually

Install its packages:

terminal
npm install boneyard-js@1.10.0

Then copy the source:

components/ui/skeleton.tsx
import { Skeleton as Boneyard, type SkeletonProps as BoneyardProps } from "boneyard-js/react"
import type * as React from "react"

import { cn } from "~/lib/utils"

/*
 * Loading placeholders in the shape of the real thing, from boneyard
 * (https://github.com/0xGF/boneyard). Wrap the content and name it; boneyard's
 * CLI renders it in a browser, measures every piece and saves those "bones".
 * While `loading`, the bones show in its place, colored and pulsing like the
 * rest of the system.
 *
 * <Skeleton name="profile-card" loading={isLoading}>
 *   <ProfileCard />
 * </Skeleton>
 *
 * Capture the bones with `npx boneyard-js build <url>` and import the
 * registry it writes once in your app. Where there's nothing to measure, like
 * an iframe, size a `SkeletonBone` by hand.
 */

type SkeletonProps = Omit<
  BoneyardProps,
  "color" | "darkColor" | "animate" | "boneClass" | "stagger" | "transition"
>

/**
 * Shows its captured bones while `loading`, and its children after. Name it so
 * the CLI can find it, or pass `initialBones`. The content keeps its space
 * while hidden, so nothing moves when it arrives.
 */
function Skeleton(props: SkeletonProps) {
  return (
    <Boneyard
      // The system's muted color, which a CSS variable keeps right in both modes.
      color="var(--color-muted)"
      darkColor="var(--color-muted)"
      // The system's pulse: boneyard's own can't lighten a CSS variable, so its bones stay solid.
      animate="solid"
      // boneyard has no setting for corners: it rounds unrounded pieces by 8px. Its styles are
      // inline, so the system's radius wins with !important. Circles, like avatars, stay round.
      boneClass="rounded-sm! animate-pulse motion-reduce:animate-none [&[style*='border-radius:_50%']]:rounded-full! [&[style*='border-radius:_9999px']]:rounded-full!"
      transition={150}
      // The CLI captures at window widths, so pick bones by the window, not the space the content gets.
      select="viewport"
      {...props}
    />
  )
}

/** A single bone you size with classes, for content there's nothing to capture from. Hidden from screen readers. */
function SkeletonBone({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="skeleton-bone"
      aria-hidden="true"
      className={cn("animate-pulse rounded-md bg-muted motion-reduce:animate-none", className)}
      {...props}
    />
  )
}

export { Skeleton, SkeletonBone }
export type { SkeletonProps }

API reference

Generated from Mocha's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

Skeleton

Shows its captured bones while loading, and its children after. Name it so the CLI can find it, or pass initialBones. The content keeps its space while hidden, so nothing moves when it arrives.

children*

Your component — rendered when not loading.

Type ReactNode

className

Additional className for the container

Type string

fallback

Shown when loading is true and no bones are available.

Type ReactNode

fixture

Mock content rendered during npx boneyard-js build so the CLI can capture bone positions even when real data isn't available. Only rendered when the CLI sets window.__BONEYARD_BUILD = true.

Type ReactNode

initialBones

Pre-generated bones. Accepts a single SkeletonResult or a ResponsiveBones map.

Type SkeletonResult | ResponsiveBones

loading*

When true, shows the skeleton. When false, shows children.

Type boolean

name

Name this skeleton. Used by npx boneyard-js build to identify and capture bones. Also used to auto-resolve pre-generated bones from the registry.

Type string

select

Which width selects the responsive breakpoint: 'container' (default, the skeleton's measured width) or 'viewport' (window.innerWidth, how the CLI keys captures). Use 'viewport' for app-shell layouts where the container is narrower than the window. See #92.

Type "container" | "viewport"

snapshotConfig

Controls how npx boneyard-js build extracts bones from the fixture. Stored as a data attribute — the CLI reads it during capture.

Type SnapshotConfig

SkeletonBone

A single bone you size with classes, for content there's nothing to capture from. Hidden from screen readers.

Takes the props of a native div.