Skip to content

g1t/apps/web/app/components/ui/skeleton.tsx

199 lines7,304 bytesCodeBlame
1import type { ComponentProps, ReactNode } from "react";
2
3import { cn } from "../../lib/cn";
4
5// The shape of something still loading: a quiet raised block that pulses
6// (still for people who ask for less motion). Give it the size of what it
7// stands for, so nothing moves when the real thing arrives. Hidden from
8// screen readers; the region it sits in says it is busy (`aria-busy`),
9// which `Loading` does, with a "Loading…" for them to hear.
10//
11// The pieces below are the app's common shapes, each the height of the
12// real thing: compose a page's outline from them rather than drawing one.
13
14/** One block. Size it with classes: `h-3 w-24`, `size-8 rounded-full`. */
15export function Skeleton({ className, ...props }: ComponentProps<"span">) {
16 return (
17 <span
18 aria-hidden="true"
19 className={cn("block rounded bg-raised motion-safe:animate-pulse", className)}
20 {...props}
21 />
22 );
23}
24
25/**
26 * A bar standing in for one line of text, as tall as the line it sits in
27 * (`1lh`), so a row keeps its height when the words arrive.
28 */
29export function SkeletonLine({ className, barClassName, width }: { className?: string; barClassName?: string; width?: string }) {
30 return (
31 <span aria-hidden="true" className={cn("flex h-[1lh] items-center", className)}>
32 <Skeleton className={cn("h-3 w-full", barClassName)} style={width ? { width } : undefined} />
33 </span>
34 );
35}
36
37/**
38 * A region whose content is on its way: busy for assistive technology,
39 * with "Loading…" said once. The skeleton inside is hidden from them.
40 */
41export function Loading({
42 label = "Loading…",
43 className,
44 children,
45 ...props
46}: { label?: string; children: ReactNode } & ComponentProps<"div">) {
47 return (
48 <div aria-busy="true" className={className} {...props}>
49 <span role="status" className="sr-only">
50 {label}
51 </span>
52 {children}
53 </div>
54 );
55}
56
57/**
58 * Lines of text: `lines` bars `h-3` apart by `gap`, the last one shorter,
59 * as a paragraph or a list's titles would sit.
60 */
61export function SkeletonText({ lines = 3, className, lineClassName }: { lines?: number; className?: string; lineClassName?: string }) {
62 return (
63 <span aria-hidden="true" className={cn("flex flex-col gap-2", className)}>
64 {Array.from({ length: lines }, (_, index) => (
65 <Skeleton key={index} className={cn("h-3", index === lines - 1 && lines > 1 ? "w-3/5" : "w-full", lineClassName)} />
66 ))}
67 </span>
68 );
69}
70
71/**
72 * Rows of a list, each `rowClassName` tall (`h-10` by default) with a
73 * leading dot and a title, as the app's lists are laid out.
74 */
75export function SkeletonRows({ rows = 3, className, rowClassName }: { rows?: number; className?: string; rowClassName?: string }) {
76 return (
77 <div aria-hidden="true" className={cn("divide-y divide-line", className)}>
78 {Array.from({ length: rows }, (_, index) => (
79 <div key={index} className={cn("flex h-10 items-center gap-3 px-3", rowClassName)}>
80 <Skeleton className="size-4 shrink-0 rounded-full" />
81 <Skeleton className="h-3 grow" style={{ maxWidth: varied(index) }} />
82 <Skeleton className="h-3 w-12 shrink-0" />
83 </div>
84 ))}
85 </div>
86 );
87}
88
89/** A width for row `index` that varies, so a list of bars does not look ruled. */
90function varied(index: number, widest = 70, spread = 30) {
91 return `${widest - ((index * 17) % spread)}%`;
92}
93
94/**
95 * Rows of a table: `columns` cells across, the first wide, the rest as
96 * `cellClassName` sizes them, in rows as tall as text rows (`py-2`).
97 */
98export function SkeletonTableRows({
99 rows = 3,
100 columns = 3,
101 className,
102 rowClassName,
103}: {
104 rows?: number;
105 columns?: number;
106 className?: string;
107 rowClassName?: string;
108}) {
109 return (
110 <div aria-hidden="true" className={cn("divide-y divide-line text-sm", className)}>
111 {Array.from({ length: rows }, (_, row) => (
112 <div key={row} className={cn("flex items-center gap-4 px-4 py-2", rowClassName)}>
113 <SkeletonLine className="grow" width={varied(row)} />
114 {Array.from({ length: columns - 1 }, (_, column) => (
115 <SkeletonLine key={column} className="w-16 shrink-0" />
116 ))}
117 </div>
118 ))}
119 </div>
120 );
121}
122
123/**
124 * An item of an issue's or pull request's timeline (components/work.tsx
125 * `TimelineItem`): the avatar beside a card with its header and `lines`
126 * of body.
127 */
128export function SkeletonTimelineItem({ lines = 2, className }: { lines?: number; className?: string }) {
129 return (
130 <div aria-hidden="true" className={cn("flex gap-3", className)}>
131 <Skeleton className="mt-1 hidden size-8 shrink-0 rounded-full sm:block" />
132 <div className="min-w-0 grow overflow-hidden rounded-xl border border-line bg-surface">
133 <div className="flex items-center gap-2 border-b border-line bg-raised/40 px-4 py-2 text-sm">
134 <SkeletonLine className="w-20" />
135 <SkeletonLine className="w-32" />
136 </div>
137 {lines > 0 && <SkeletonText lines={lines} className="px-4 py-4" />}
138 </div>
139 </div>
140 );
141}
142
143/** A card: a title and `lines` of text, in the app's bordered box. */
144export function SkeletonCard({ lines = 2, className }: { lines?: number; className?: string }) {
145 return (
146 <div aria-hidden="true" className={cn("rounded-xl border border-line bg-surface p-4", className)}>
147 <SkeletonLine className="w-1/3 text-sm" />
148 {lines > 0 && <SkeletonText lines={lines} className="mt-3" />}
149 </div>
150 );
151}
152
153/** A sidebar section: its small heading and `rows` lines under it. */
154export function SkeletonSidebarSection({ rows = 2, className }: { rows?: number; className?: string }) {
155 return (
156 <div aria-hidden="true" className={cn("text-sm", className)}>
157 <SkeletonLine className="w-24" />
158 <div className="mt-2 space-y-1.5">
159 {Array.from({ length: rows }, (_, index) => (
160 <div key={index} className="flex items-center gap-2">
161 <Skeleton className="size-5 shrink-0 rounded-full" />
162 <SkeletonLine className="grow" barClassName="w-28" />
163 </div>
164 ))}
165 </div>
166 </div>
167 );
168}
169
170/**
171 * A stat tile, as mission control's tiles sit in their grid: its label,
172 * the number, and the line under it, each the height of its text.
173 */
174export function SkeletonStat({ className }: { className?: string }) {
175 return (
176 <div aria-hidden="true" className={cn("bg-surface px-4 py-3.5 sm:px-5 sm:py-4", className)}>
177 <SkeletonLine className="w-16 text-xs" />
178 <SkeletonLine className="mt-1 w-12 text-2xl sm:text-[1.75rem]" barClassName="h-6" />
179 <SkeletonLine className="mt-0.5 w-24 text-xs" />
180 </div>
181 );
182}
183
184/**
185 * Lines of code, numbered, as `CodeLines` and a README's code blocks set
186 * them: `lines` rows of the monospace line height.
187 */
188export function SkeletonCode({ lines = 6, className }: { lines?: number; className?: string }) {
189 return (
190 <div aria-hidden="true" className={cn("py-2 font-mono text-[0.8125rem] leading-6", className)}>
191 {Array.from({ length: lines }, (_, index) => (
192 <div key={index} className="flex items-center gap-4 px-4">
193 <SkeletonLine className="w-6 shrink-0" />
194 <SkeletonLine className="grow" width={varied(index, 80, 50)} />
195 </div>
196 ))}
197 </div>
198 );
199}