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