Skip to content
refs
History
171 lines · 5.2 KBRaw
1import type { ReactNode } from "react";
2import { cn } from "./cn";
3
4/** A placeholder for one piece of content that is loading. A soft light sweeps across it. */
5export function Shimmer({ className }: { className?: string }) {
6 return <span aria-hidden className={cn("shimmer block h-3", className)} />;
7}
8
9/** The shapes a page can take while it loads. The skeleton shows the layout to come, never fake content. */
10export type SkeletonKind = "overview" | "list" | "detail" | "code" | "form" | "section";
11
12const WIDTHS = ["w-[62%]", "w-[48%]", "w-[71%]", "w-[55%]", "w-[66%]", "w-[43%]", "w-[58%]", "w-[51%]"];
13
14function Card({ children, className }: { children: ReactNode; className?: string }) {
15 return <div className={cn("flex flex-col overflow-hidden rounded-panel bg-panel shadow-card", className)}>{children}</div>;
16}
17
18function CardHeader() {
19 return (
20 <div className="flex h-10 items-center px-4 shadow-[inset_0_-1px_0_var(--hairline)]">
21 <Shimmer className="w-32" />
22 </div>
23 );
24}
25
26function Rows({ count, icon = false }: { count: number; icon?: boolean }) {
27 return Array.from({ length: count }, (_, index) => (
28 <div key={index} className="flex items-start gap-3 px-4 py-3 shadow-[inset_0_-1px_0_var(--hairline)] last:shadow-none">
29 {icon && <Shimmer className="mt-0.5 size-4 shrink-0 rounded-full" />}
30 <div className="flex min-w-0 flex-1 flex-col gap-2">
31 <Shimmer className={WIDTHS[index % WIDTHS.length]} />
32 <Shimmer className={cn("h-2.5", WIDTHS[(index + 3) % WIDTHS.length])} />
33 </div>
34 </div>
35 ));
36}
37
38function Title({ meta = false }: { meta?: boolean }) {
39 return (
40 <div className="mb-6 flex flex-col gap-3">
41 <Shimmer className="h-7 w-64 max-w-[70%] rounded-control" />
42 {meta && <Shimmer className="w-80 max-w-[85%]" />}
43 </div>
44 );
45}
46
47function Toolbar() {
48 return (
49 <div className="flex items-center justify-between gap-3">
50 <Shimmer className="h-8 w-40 rounded-control" />
51 <Shimmer className="h-8 w-24 rounded-control" />
52 </div>
53 );
54}
55
56function Shape({ kind }: { kind: SkeletonKind }) {
57 if (kind === "overview") {
58 return (
59 <div className="grid items-start gap-6 lg:grid-cols-[minmax(0,1fr)_340px]">
60 <div className="flex min-w-0 flex-col gap-6">
61 <Card>
62 <CardHeader />
63 <Rows count={3} icon />
64 </Card>
65 <Card>
66 <CardHeader />
67 <Rows count={4} />
68 </Card>
69 </div>
70 <div className="flex flex-col gap-6">
71 <Card>
72 <CardHeader />
73 <Rows count={2} />
74 </Card>
75 <Card>
76 <CardHeader />
77 <Rows count={3} />
78 </Card>
79 </div>
80 </div>
81 );
82 }
83 if (kind === "list") {
84 return (
85 <div className="flex flex-col gap-4">
86 <Toolbar />
87 <Card>
88 <Rows count={6} icon />
89 </Card>
90 </div>
91 );
92 }
93 if (kind === "detail") {
94 return (
95 <div className="grid items-start gap-8 lg:grid-cols-[minmax(0,1fr)_17rem]">
96 <div className="flex min-w-0 flex-col gap-3">
97 <Card>
98 <CardHeader />
99 <Rows count={2} />
100 </Card>
101 <Card>
102 <CardHeader />
103 <Rows count={1} />
104 </Card>
105 </div>
106 <div className="flex flex-col gap-6 max-lg:hidden">
107 {[0, 1, 2].map((index) => (
108 <div key={index} className="flex flex-col gap-2.5">
109 <Shimmer className="h-2.5 w-20" />
110 <Shimmer className={WIDTHS[index + 1]} />
111 </div>
112 ))}
113 </div>
114 </div>
115 );
116 }
117 if (kind === "code") {
118 return (
119 <div className="flex flex-col gap-4">
120 <Toolbar />
121 <Card>
122 <CardHeader />
123 {Array.from({ length: 10 }, (_, index) => (
124 <div key={index} className="flex items-center gap-3 px-4 py-2.5 shadow-[inset_0_-1px_0_var(--hairline)] last:shadow-none">
125 <Shimmer className="size-4 shrink-0" />
126 <Shimmer className={cn("max-w-56", WIDTHS[index % WIDTHS.length])} />
127 </div>
128 ))}
129 </Card>
130 </div>
131 );
132 }
133 if (kind === "form") {
134 return (
135 <Card className="max-w-xl gap-5 p-5">
136 {[0, 1, 2].map((index) => (
137 <div key={index} className="flex flex-col gap-2">
138 <Shimmer className="h-2.5 w-24" />
139 <Shimmer className="h-8 w-full rounded-control" />
140 </div>
141 ))}
142 <Shimmer className="h-8 w-28 rounded-control" />
143 </Card>
144 );
145 }
146 return (
147 <div className="flex flex-col gap-3">
148 <Card>
149 <CardHeader />
150 <Rows count={2} />
151 </Card>
152 <Card>
153 <CardHeader />
154 <Rows count={3} />
155 </Card>
156 </div>
157 );
158}
159
160/**
161 * The shape of a page while it loads. `title` adds the page heading, for a whole page; leave it out when the
162 * heading above stays on screen.
163 */
164export function PageSkeleton({ kind, title = false }: { kind: SkeletonKind; title?: boolean }) {
165 return (
166 <div role="status" aria-label="Loading" className="fade-in">
167 {(title || kind === "detail") && <Title meta={kind === "detail"} />}
168 <Shape kind={kind} />
169 </div>
170 );
171}