Components
Skeleton
Quiet shapes that hold the place of content that has not arrived, in seven effects, so the page does not jump when it does.
Introduction
Skeleton is a gray shape where something will be: a line where a title comes, a circle where a photo comes. It shows the layout before the content arrives, so the page feels quicker and does not jump when the real thing takes its place.
- Seven effects, chosen with
effect:pulse,shimmer,wave,glow,stripes,breatheandnone. The gray follows the theme, and works in the light and the dark one. - Shapes: a rectangle, a pill and a circle. And ready-made ones for a paragraph (
SkeletonText) and an avatar (SkeletonAvatar). - Wrap real content in a
Skeletonand give itisLoaded: it keeps the size of the content, covers it, and fades the content in when it is loaded. - The shapes are silent for screen readers.
SkeletonGroupmarks the region busy and says "Loading" once. For people who prefer reduced motion every effect stands still. - It has no client code, so it works in Server Components, for example in a
loading.tsxfile.
Draw the skeleton the same size as the content, or the page will still jump. For a wait with no shape to show, such as a button that is saving, use Loader.
Installation
centerUI has no CLI or registry yet: components are copied into your project.
# bun
bun add tailwind-variants tailwind-merge clsx
# pnpm
pnpm add tailwind-variants tailwind-merge clsx
# npm
npm install tailwind-variants tailwind-merge clsxManual installation
Copy these files into your project and keep the paths, or adjust the imports.
1. Add the styles
src/types/skeleton-styles.tsShow code
import { tv } from "tailwind-variants";
// Styles for Skeleton: a quiet shape that stands in for content that has not
// arrived. `effect` is how it moves. The animations are named in globals.css
// (`animate-skeleton-*`), and stand still for people who prefer reduced motion.
//
// - pulse: fades in and out.
// - shimmer: a band of light crosses it.
// - wave: a softer, slanted band crosses it, more slowly.
// - glow: brightens, with a faint glow, and settles.
// - stripes: diagonal stripes move sideways.
// - breathe: swells in and out a little.
// - none: still.
const light = "after:pointer-events-none after:absolute after:content-['']";
export const skeletonStyles = tv({
slots: {
root: "block bg-fg/10",
text: "flex w-full flex-col",
line: "block bg-fg/10",
},
variants: {
effect: {
pulse: { root: "animate-skeleton-pulse", line: "animate-skeleton-pulse" },
shimmer: {
root: `relative overflow-hidden ${light} after:inset-0 after:bg-linear-to-r after:from-transparent after:via-fg/12 after:to-transparent after:animate-skeleton-shimmer`,
line: `relative overflow-hidden ${light} after:inset-0 after:bg-linear-to-r after:from-transparent after:via-fg/12 after:to-transparent after:animate-skeleton-shimmer`,
},
wave: {
root: `relative overflow-hidden ${light} after:inset-y-0 after:left-0 after:w-1/3 after:bg-linear-to-r after:from-transparent after:via-fg/16 after:to-transparent after:blur-[2px] after:animate-skeleton-wave`,
line: `relative overflow-hidden ${light} after:inset-y-0 after:left-0 after:w-1/3 after:bg-linear-to-r after:from-transparent after:via-fg/16 after:to-transparent after:blur-[2px] after:animate-skeleton-wave`,
},
glow: { root: "animate-skeleton-glow", line: "animate-skeleton-glow" },
stripes: {
root: "animate-skeleton-stripes bg-[length:1.75rem_1.75rem] bg-[linear-gradient(45deg,color-mix(in_oklab,var(--fg)_8%,transparent)_25%,transparent_25%,transparent_50%,color-mix(in_oklab,var(--fg)_8%,transparent)_50%,color-mix(in_oklab,var(--fg)_8%,transparent)_75%,transparent_75%)]",
line: "animate-skeleton-stripes bg-[length:1.75rem_1.75rem] bg-[linear-gradient(45deg,color-mix(in_oklab,var(--fg)_8%,transparent)_25%,transparent_25%,transparent_50%,color-mix(in_oklab,var(--fg)_8%,transparent)_50%,color-mix(in_oklab,var(--fg)_8%,transparent)_75%,transparent_75%)]",
},
breathe: {
root: "animate-skeleton-breathe",
line: "animate-skeleton-breathe",
},
none: {},
},
shape: {
rect: { root: "rounded-md" },
pill: { root: "rounded-full" },
circle: { root: "aspect-square rounded-full" },
},
// How tall a line of SkeletonText is, and the space between the lines.
size: {
sm: { text: "gap-1.5", line: "h-2.5" },
md: { text: "gap-2", line: "h-3" },
lg: { text: "gap-2.5", line: "h-4" },
},
},
defaultVariants: { effect: "shimmer", shape: "rect", size: "md" },
});
export const skeletonAvatarSizes = {
xs: "size-5",
sm: "size-6",
md: "size-8",
lg: "size-10",
xl: "size-14",
} as const;2. Add the Skeleton file
src/components/ui/skeleton/skeleton.tsxShow code
import type { CSSProperties, ReactNode } from "react";
import { cn } from "@/lib/cn";
import { skeletonAvatarSizes, skeletonStyles } from "@/types/skeleton-styles";
export type SkeletonEffect =
| "pulse"
| "shimmer"
| "wave"
| "glow"
| "stripes"
| "breathe"
| "none";
export interface SkeletonProps {
/**
* The content this stands in for. With children, the skeleton covers them
* until `isLoaded` is `true`, then they fade in. Without, it is a shape of
* its own, sized with `width`, `height` or classes.
*/
children?: ReactNode;
/** With children: show them instead of the skeleton. Defaults to `false`. */
isLoaded?: boolean;
/**
* How it moves:
* - `pulse`: fades in and out.
* - `shimmer`: a band of light crosses it (the default).
* - `wave`: a softer, slanted band crosses it, more slowly.
* - `glow`: brightens, with a faint glow, and settles.
* - `stripes`: diagonal stripes move sideways.
* - `breathe`: swells in and out a little.
* - `none`: still.
*/
effect?: SkeletonEffect;
/** Rounded corners, a pill, or a circle (which is as tall as it is wide). */
shape?: "rect" | "pill" | "circle";
/** A number is pixels. */
width?: number | string;
/** A number is pixels. */
height?: number | string;
className?: string;
}
const size = (value: number | string | undefined) =>
typeof value === "number" ? `${value}px` : value;
/**
* A quiet shape that holds the place of content that has not arrived, so the
* page does not jump when it does. Give it the size of what it replaces.
*
* It moves in one of seven ways (`effect`), and stands still for people who
* prefer reduced motion. It is decorative and hidden from screen readers: mark
* the region that is loading with `SkeletonGroup`, which says so once.
*
* @example
* <Skeleton className="h-4 w-40" />
* <Skeleton effect="wave" shape="circle" width={48} />
* <Skeleton isLoaded={!isLoading}><h2>{title}</h2></Skeleton>
*/
export function Skeleton({
children,
isLoaded = false,
effect = "shimmer",
shape,
width,
height,
className,
}: SkeletonProps) {
const styles = skeletonStyles({ effect, shape });
const style: CSSProperties = { width: size(width), height: size(height) };
// Around content: the content keeps its size, hidden, and the skeleton lies
// over it, so nothing moves when it is loaded.
if (children !== undefined) {
if (isLoaded) {
return <div className={cn("animate-fade-in", className)}>{children}</div>;
}
return (
<div className={cn("relative", className)}>
<div aria-hidden="true" className="invisible">
{children}
</div>
<span
aria-hidden="true"
data-skeleton=""
data-effect={effect}
className={cn(styles.root(), "absolute inset-0")}
/>
</div>
);
}
return (
<span
aria-hidden="true"
data-skeleton=""
data-effect={effect}
style={style}
// A circle is as tall as it is wide, so it only gets a default width.
className={cn(
styles.root(),
shape === "circle" ? "w-10" : "h-4 w-full",
className,
)}
/>
);
}
export interface SkeletonTextProps {
/** How many lines. Defaults to 3. */
lines?: number;
/** The width of the last line, so the block does not look like a box. Defaults to `"60%"`. */
lastLineWidth?: string;
/** How tall a line is. */
size?: "sm" | "md" | "lg";
effect?: SkeletonEffect;
className?: string;
}
/** Lines of text, the last one shorter, as a paragraph looks before it is read. */
export function SkeletonText({
lines = 3,
lastLineWidth = "60%",
size: lineSize,
effect = "shimmer",
className,
}: SkeletonTextProps) {
const styles = skeletonStyles({ effect, size: lineSize });
return (
<span aria-hidden="true" className={cn(styles.text(), className)}>
{Array.from({ length: lines }, (_, i) => (
<span
// biome-ignore lint/suspicious/noArrayIndexKey: a fixed count of identical lines
key={i}
data-skeleton=""
data-effect={effect}
className={cn(styles.line(), "rounded-full")}
style={{
width: i === lines - 1 && lines > 1 ? lastLineWidth : "100%",
}}
/>
))}
</span>
);
}
export interface SkeletonAvatarProps {
/** The same sizes as `Avatar`. Defaults to `md`. */
size?: keyof typeof skeletonAvatarSizes;
shape?: "circle" | "square";
effect?: SkeletonEffect;
className?: string;
}
/** The place of an avatar. */
export function SkeletonAvatar({
size: avatarSize = "md",
shape = "circle",
effect = "shimmer",
className,
}: SkeletonAvatarProps) {
const styles = skeletonStyles({
effect,
shape: shape === "circle" ? "circle" : "rect",
});
return (
<span
aria-hidden="true"
data-skeleton=""
data-effect={effect}
className={cn(styles.root(), skeletonAvatarSizes[avatarSize], className)}
/>
);
}
export interface SkeletonGroupProps {
children: ReactNode;
/** What a screen reader hears while it is loading. Defaults to "Loading". */
label?: string;
className?: string;
}
/**
* Wraps the skeletons of a region that is loading. It marks the region busy
* and announces "Loading" once, since the skeletons themselves are silent.
* Replace it with the real content when it arrives.
*/
export function SkeletonGroup({
children,
label = "Loading",
className,
}: SkeletonGroupProps) {
return (
<output aria-busy="true" className={cn("block", className)}>
<span className="sr-only">{label}</span>
{children}
</output>
);
}Wrapping content uses the animate-fade-in class of the theme for the moment it is loaded.
3. Add the animations
In src/app/globals.css copy the six --animate-skeleton-* lines of the theme, the six skeleton-* keyframes, and the [data-skeleton] rule inside the reduced-motion query. Without them the shapes are still.
4. Use it
import {
Skeleton,
SkeletonAvatar,
SkeletonGroup,
SkeletonText,
} from "@/components/ui/skeleton/skeleton";Anatomy
Shapes, ready-made shapes for text and avatars, and a group that says the region is loading.
<SkeletonGroup label="Loading profile">
<SkeletonAvatar size="lg" />
<Skeleton className="h-4 w-40" />
<SkeletonText lines={3} />
</SkeletonGroup>| Part | Rendered as | Notes |
|---|---|---|
| Skeleton | span | A quiet shape, sized with width, height or classes. Around children it covers them until isLoaded. |
| SkeletonText | span with lines | Lines of text, the last one shorter. |
| SkeletonAvatar | span | The place of an Avatar, in the same sizes. |
| SkeletonGroup | output | Wraps a region that is loading. Marks it busy and announces "Loading" once. The shapes inside are silent. |
Effects
How a skeleton moves. Shimmer is the default, and the most familiar.
The seven effects
Pulse fades. Shimmer sends a band of light across. Wave is a softer, slanted band, more slowly. Glow brightens with a faint glow. Stripes are diagonal and move like a barber pole. Breathe swells a little. None is still.
On a real layout
The same row in every effect, to see which one suits the place. The quiet ones (pulse, glow, breathe) suit small shapes and lists. The travelling ones (shimmer, wave) suit big blocks.
Try them
Pick an effect and watch it on a card: an avatar, two lines, a block and a paragraph.
Examples
Usage
Shapes
A rectangle with rounded corners, a pill, and a circle, which is as tall as it is wide. Size them with className, or with width and height, where a number is pixels.
Text
Lines of text, with the last one shorter so the block looks like a paragraph. lines sets how many, and lastLineWidth how short.
Text sizes
How tall the lines are, to match the size of the text they replace.
Avatar
The place of an avatar, in the same five sizes, round or square.
Card
A card of content that is loading: an image, a title, a paragraph and an author.
List
Rows of an avatar and two lines, for a list of messages or people.
Table
Rows with the same columns as the table they stand in for.
Around real content
Put the real content inside a Skeleton and set isLoaded. Until then the content keeps its size, hidden, under the skeleton, so nothing moves when it arrives, and then it fades in. This one loads after two seconds.
Group
The shapes are hidden from screen readers, so wrap the region in a SkeletonGroup. It is marked busy and announces the label once. Replace it with the content when it arrives.
Component API
Skeleton is made of the parts below.
Skeleton
| Prop | Type | Default |
|---|---|---|
| children | ReactNode | - |
| isLoaded | boolean | false |
| effect | "pulse" | "shimmer" | "wave" | "glow" | "stripes" | "breathe" | "none" | "shimmer" |
| shape | "rect" | "pill" | "circle" | "rect" |
| width | number | string | - |
| height | number | string | - |
| className | string | - |
SkeletonText
| Prop | Type | Default |
|---|---|---|
| lines | number | 3 |
| lastLineWidth | string | "60%" |
| size | "sm" | "md" | "lg" | "md" |
| effect | as Skeleton | "shimmer" |
| className | string | - |
SkeletonAvatar
| Prop | Type | Default |
|---|---|---|
| size | "xs" | "sm" | "md" | "lg" | "xl" | "md" |
| shape | "circle" | "square" | "circle" |
| effect | as Skeleton | "shimmer" |
| className | string | - |
SkeletonGroup
| Prop | Type | Default |
|---|---|---|
| children | ReactNode | - (required) |
| label | string | "Loading" |
| className | string | - |