Skeleton
A placeholder pencilled in with hatching the pen keeps going back over.
import { Skeleton } from "@/components/ui/skeleton";
export default function SkeletonDemo() {
return (
<div className="flex items-center gap-4">
<Skeleton className="size-12 rounded-full" seed="sk-avatar" />
<div className="flex flex-col gap-2">
<Skeleton className="h-4 w-56" seed="sk-line-1" />
<Skeleton className="h-4 w-40" seed="sk-line-2" />
</div>
</div>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ skeleton"use client";
import { useMemo, type ComponentProps, type CSSProperties } from "react";
import { cn } from "@/lib/utils";
import { penRadius, useInkBox, useInkSeed, usePen } from "@/hooks/use-ink-box";
import { InkSvg, Stroke } from "@/lib/ink";
import { hatchStrokes } from "@/lib/ink-sketch";
/**
* A placeholder pencilled in with light hatching that the pen keeps going
* back over while you wait. With reduced motion the hatching just sits there.
*/
function Skeleton({ className, seed, ...props }: ComponentProps<"div"> & { seed?: string | number }) {
const s = useInkSeed(seed);
const pen = usePen({});
const [ref, [w, h]] = useInkBox([160, 20], { step: 4 });
const strokes = useMemo(() => hatchStrokes(s, w, h, { gap: 4.2, angle: -55, jitter: 0.5 }), [s, w, h]);
return (
// Clipped by its own border-radius, which is the pen's, so rounded-full makes a round placeholder.
<div data-slot="skeleton" aria-hidden="true" className={cn("relative overflow-hidden rounded-(--pen-r)", className)} {...props} style={{ "--pen-r": `${penRadius(pen, 8)}px`, ...props.style } as CSSProperties}>
<InkSvg ref={ref} box={[0, 0, w, h]} stretch className="inset-0 size-full text-ink-4">
<g>
{strokes.map((d, i) => (
<Stroke
key={i}
d={d}
width={1}
opacity={0.8}
className="ink-scribble [stroke-dasharray:1.05_1.05] [animation:ink-scribble_1.8s_var(--ease-write)_infinite_alternate] [animation-delay:var(--ink-dd)]"
delay={(i % 12) * 70}
/>
))}
</g>
</InkSvg>
</div>
);
}
/**
* A paragraph's worth of placeholder: `lines` lines, the last one shorter, each
* a different length so it reads as text and not as a block.
*/
function SkeletonText({ lines = 3, className, seed }: { lines?: number; className?: string; seed?: string | number }) {
const widths = ["100%", "92%", "97%", "84%", "95%", "88%"];
return (
<div aria-hidden="true" className={cn("flex flex-col gap-2.5", className)}>
{Array.from({ length: lines }, (_, i) => (
<Skeleton key={i} className="h-4" style={{ width: i === lines - 1 && lines > 1 ? "58%" : widths[i % widths.length] }} seed={`${seed ?? "skeleton-text"}-${i}`} />
))}
</div>
);
}
export { Skeleton, SkeletonText };Usage
import { Skeleton } from "@/components/ui/skeleton"
<Skeleton className="h-4 w-48" />Examples
Card placeholder
The shape of a card while its content loads.
import { Card, CardContent, CardHeader } from "@/components/ui/card";
import { Skeleton } from "@/components/ui/skeleton";
export default function SkeletonCard() {
return (
<Card className="w-full max-w-sm" seed="sc-card">
<CardHeader>
<Skeleton className="h-5 w-40" seed="sc-title" />
<Skeleton className="h-4 w-56" seed="sc-sub" />
</CardHeader>
<CardContent className="flex flex-col gap-3">
<Skeleton className="h-32 w-full" seed="sc-image" />
<Skeleton className="h-4 w-full" seed="sc-l1" />
<Skeleton className="h-4 w-2/3" seed="sc-l2" />
</CardContent>
</Card>
);
}Text
A heading and paragraphs of placeholder lines, each a different length.
import { Skeleton, SkeletonText } from "@/components/ui/skeleton";
export default function SkeletonTextExample() {
return (
<article className="flex w-full max-w-md flex-col gap-4">
<Skeleton className="h-7 w-2/3" seed="st-title" />
<SkeletonText lines={4} seed="st-body" />
<SkeletonText lines={2} seed="st-more" />
</article>
);
}A list
Round avatars with a name and two lines of text, three times over.
import { Skeleton, SkeletonText } from "@/components/ui/skeleton";
export default function SkeletonProfile() {
return (
<ul className="flex w-full max-w-md flex-col gap-6">
{[0, 1, 2].map((i) => (
<li key={i} className="flex items-start gap-4">
<Skeleton className="size-12 shrink-0 rounded-full" seed={`sp-avatar-${i}`} />
<div className="flex flex-1 flex-col gap-3">
<Skeleton className="h-5 w-1/3" seed={`sp-name-${i}`} />
<SkeletonText lines={2} seed={`sp-text-${i}`} />
</div>
</li>
))}
</ul>
);
}API reference
- className
stringSize and shape it: rounded-full makes a round placeholder.
- SkeletonText linesdefault
3 numberA paragraph of placeholder: this many lines, the last one shorter, each a different width.
- seed
string | numberPins the hatching.