Placeholder shapes that match the layout they stand in for, with one sweep of light that respects reduced motion.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Avatar } from "./ui/uicontext/components/Avatar.ts"import { Skeleton, SkeletonGroup } from "./ui/uicontext/components/Skeleton.ts"import { Switch } from "./ui/uicontext/components/Switch.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, radius, space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ stack: { display: "grid", gap: space.xl, inlineSize: "min(100%, 30rem)" }, list: { margin: 0, padding: 0, listStyle: "none", borderWidth: 1, borderStyle: "solid", borderColor: color.line, borderRadius: radius.lg, backgroundColor: color.panel, }, // the row is the same box loaded or not: the skeleton fills the slots the content will take row: { display: "grid", gridTemplateColumns: "auto minmax(0, 1fr) auto", alignItems: "center", columnGap: space.lg, paddingInline: space.xl, paddingBlock: space.lg, borderTopWidth: { default: 1, ":first-child": 0 }, borderTopStyle: "solid", borderTopColor: color.line, }, text: { display: "grid", minInlineSize: 0 }, name: { fontSize: font.base, lineHeight: 1.5, fontWeight: 500, color: color.fg, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }, email: { fontSize: font.sm, lineHeight: 1.4, color: color.muted, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }, role: { fontSize: font.sm, lineHeight: 1.4, color: color.muted }, // sizes taken from the real content: a name, an email, a role nameBar: { inlineSize: "45%" }, emailBar: { inlineSize: "70%" }, roleBar: { inlineSize: 48 }, avatar: { inlineSize: 32, blockSize: 32 },})const people = [ { id: "ada", name: "Ada Lovelace", email: "[email protected]", role: "Owner" }, { id: "ben", name: "Ben Okafor", email: "[email protected]", role: "Admin" }, { id: "mira", name: "Mira Patel", email: "[email protected]", role: "Member" },]const Message = UiSchema.TaggedEnum({ ToggledLoading: { loading: Schema.Boolean } })export const MembersLoading = Html.component({ name: "MembersLoading", model: Schema.Struct({ loading: Schema.Boolean }), message: Message,}).make({ init: () => ({ loading: true }), update: { ToggledLoading: ($, { loading }) => $.evolve({ loading }) }, view: (h, { loading }) => h.div.attrs(sx(styles.stack)).children( Switch(h, { checked: loading, label: "Loading", onToggle: (on) => Message.ToggledLoading({ loading: on }) }), loading ? SkeletonGroup(h, { label: "Loading members", delay: false }).children( h.ul.attrs(sx(styles.list)).children( people.map((p) => h.li.key(p.id).attrs(sx(styles.row)).children( Skeleton(h, { shape: "circle", style: styles.avatar }), h.div.attrs(sx(styles.text)).children( Skeleton(h, { text: "base", style: styles.nameBar }), Skeleton(h, { text: "sm", style: styles.emailBar }), ), Skeleton(h, { text: "sm", style: styles.roleBar }), )), ), ) : h.ul.attrs(sx(styles.list)).children( people.map((p) => h.li.key(p.id).attrs(sx(styles.row)).children( Avatar(h, { key: p.id, id: p.id, name: p.name, size: "md" }), h.div.attrs(sx(styles.text)).children(h.span.attrs(sx(styles.name)).children(p.name), h.span.attrs(sx(styles.email)).children(p.email)), h.span.attrs(sx(styles.role)).children(p.role), )), ), ),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/skeletonUsage
import { Skeleton, SkeletonGroup } from "./ui/uicontext/components/Skeleton.ts"export const CommentLoading = <M>(h: Html.H<M>) => SkeletonGroup(h, { label: "Loading comments" }).children( Skeleton(h, { shape: "circle" }), Skeleton(h, { text: "base", lines: 3 }), )Examples
Shapes
A block keeps the cover's aspect ratio, each text line takes its font size, and a circle stands in for a face.
import type { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Skeleton, SkeletonGroup } from "./ui/uicontext/components/Skeleton.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, radius, space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ grid: { display: "grid", gridTemplateColumns: "repeat(auto-fill, minmax(12rem, 1fr))", gap: space.xl, inlineSize: "min(100%, 40rem)" }, card: { display: "grid", gap: space.md, padding: space.lg, borderWidth: 1, borderStyle: "solid", borderColor: color.line, borderRadius: radius.lg, backgroundColor: color.panel, }, cover: { aspectRatio: "16 / 9", blockSize: "auto" }, byline: { display: "flex", alignItems: "center", gap: space.sm }, face: { inlineSize: 20, blockSize: 20 }, name: { inlineSize: "40%" }, title: { inlineSize: "80%" },})// a gallery of docs: the cover keeps its aspect ratio, the title is a heading's line, the byline a small oneexport const DocCards = <M>(h: Html.H<M>) => SkeletonGroup(h, { label: "Loading documents", delay: false, style: styles.grid }).children( ["a", "b", "c"].map((key) => h.div.key(key).attrs(sx(styles.card)).children( Skeleton(h, { style: styles.cover }), Skeleton(h, { text: "lg", style: styles.title }), Skeleton(h, { text: "sm", lines: 2 }), h.div.attrs(sx(styles.byline)).children(Skeleton(h, { shape: "circle", style: styles.face }), Skeleton(h, { text: "xs", style: styles.name })), )), )API reference
SkeletonView function
Options
| Name | Type | Default | Description |
|---|---|---|---|
shape | "text" | "circle" | "rect" | — | text for a line of text (pass text for its size), circle for an avatar or an icon, rect for an image, a chart or a field. text when text or lines is given, rect otherwise. |
text | "xs" | "sm" | "base" | "lg" | "xl" | "xxl" | base | The font size of the text it stands in for, so the line is as tall as the text will be; base by default. |
lines | number | 1 | Lines of a paragraph, the last one shorter; 1 by default. |
style | Style | — | StyleX styles for the size the real content takes: { inlineSize: 120 } for a short title, { inlineSize: 40, blockSize: 40 } for a larger avatar, an aspect ratio for an image. |
SkeletonGroupView function
Options
| Name | Type | Default | Description |
|---|---|---|---|
labelrequired | string | — | What's loading, read once to a screen reader: "Loading issues". |
delay | boolean | true | Wait 300 ms before showing, so a load that finishes quickly shows nothing at all; true by default. Turn it off when the placeholder is the first paint of a page. |
style | Style | — | StyleX styles for the group's box; it adds none of its own, so the shapes sit in your layout. |