Command palette

@yasinsensoy/web

Web · Components

Skeleton

Faded box holding the place of content while it loads; prevents layout shift (CLS) when the content arrives.

4 exports

Live example

Preview

Code

import { Skeleton } from "@yasinsensoy/web";

<div aria-busy="true" className="flex items-center gap-3">
  <Skeleton shape="circle" width={48} />
  <Skeleton lines={2} className="flex-1" />
</div>

Events

  1. Events appear here when you interact with the preview.

Import

import { Skeleton } from "@yasinsensoy/web";

Skeleton

componentServer component

Faded box holding the place of content while it loads; prevents layout shift (CLS) when the content arrives. It is a server component.

Three uses:

  • Box: give the size with classes or width/height; shape="circle" for an avatar, shape="text" for a single line.
  • Paragraph: lines={3}; the last line stays short.
  • Automatic: wrap the real component with loading; texts, images and buttons turn into faded boxes in the same place. No need to write a separate placeholder layout.

Hidden from screen readers; announce the loading state on the container with aria-busy. With the "reduced motion" preference the motion stops.

Skeleton({ shape: shapeProp, lines, width, height, animation, loading, children, className, style, ...props }: SkeletonProps): string | number | bigint | boolean | JSX.Element | Iterable<ReactNode> | Promise<string | number | bigint | boolean | Iterable<ReactNode> | ReactElement<unknown, string | JSXElementConstructor<any>> | ReactPortal | null | undefined> | null

Examples

<div aria-busy="true" className="flex items-center gap-3">
  <Skeleton shape="circle" width={40} />
  <Skeleton lines={2} className="flex-1" />
</div>

<Skeleton className="aspect-video w-full" animation="shimmer" />

// Automatic: the product card is drawn with sample data; the real one comes when loaded.
<Skeleton loading={!product}>
  <ProductCard product={product ?? sampleProduct} />
</Skeleton>

Shape of the placeholder.

  • rect: box with slightly rounded corners (card, image, button).
  • circle: circle (avatar); if width is given, the height equals it.
  • text: a bar the height of a text line; follows the font size.
type SkeletonShape = "rect" | "circle" | "text"

Motion of the placeholder.

  • pulse: slowly fades and darkens.
  • shimmer: a shine moving from left to right.
  • none: still.

With the "reduced motion" preference the motion always stops.

type SkeletonAnimation = "pulse" | "shimmer" | "none"

SkeletonProps

interface

Props of the Skeleton component.

Extends: Omit<ComponentProps<"div">, "children">

NameTypeDefaultDescription
shape?SkeletonShape"rect"`, `"text"` if `lines` is givenShape. See SkeletonShape.
lines?numberText paragraph: draws this many lines; the last line is shorter. Lines follow the container's width.
width?number | stringWidth: a number is pixels, text is a CSS value ("60%", "12rem").
height?number | stringHeight: a number is pixels, text is a CSS value.
animation?SkeletonAnimation"pulse"Animation. See SkeletonAnimation.
loading?booleantrueWith children: while true the content is drawn invisible and its texts, images and buttons turn into faded boxes of the same size; when false the content shows as is. No need to write a separate placeholder layout, and the page doesn't shift when the content arrives. Ideal for rendering with placeholder content (sample data). No effect without children.
children?ReactNodeContent whose shape is taken with loading. While loading it is non-interactive (inert) and hidden from screen readers.