Raw
1 import { Container, Heading, Text, clx } from "@medusajs/ui"
2 import { CSSProperties, ComponentPropsWithoutRef } from "react"
3
4 type SkeletonProps = {
5 className?: string
6 style?: CSSProperties
7 }
8
9 export const Skeleton = ({ className, style }: SkeletonProps) => {
10 return (
11 <div
12 aria-hidden
13 className={clx(
14 "bg-ui-bg-component h-3 w-3 animate-pulse rounded-[4px]",
15 className
16 )}
17 style={style}
18 />
19 )
20 }
21
22 type TextSkeletonProps = {
23 size?: ComponentPropsWithoutRef<typeof Text>["size"]
24 leading?: ComponentPropsWithoutRef<typeof Text>["leading"]
25 characters?: number
26 }
27
28 type HeadingSkeletonProps = {
29 level?: ComponentPropsWithoutRef<typeof Heading>["level"]
30 characters?: number
31 }
32
33 export const HeadingSkeleton = ({
34 level = "h1",
35 characters = 10,
36 }: HeadingSkeletonProps) => {
37 let charWidth = 9
38
39 switch (level) {
40 case "h1":
41 charWidth = 11
42 break
43 case "h2":
44 charWidth = 10
45 break
46 case "h3":
47 charWidth = 9
48 break
49 }
50
51 return (
52 <Skeleton
53 className={clx({
54 "h-7": level === "h1",
55 "h-6": level === "h2",
56 "h-5": level === "h3",
57 })}
58 style={{
59 width: `${charWidth * characters}px`,
60 }}
61 />
62 )
63 }
64
65 export const TextSkeleton = ({
66 size = "small",
67 leading = "compact",
68 characters = 10,
69 }: TextSkeletonProps) => {
70 let charWidth = 9
71
72 switch (size) {
73 case "xlarge":
74 charWidth = 13
75 break
76 case "large":
77 charWidth = 11
78 break
79 case "base":
80 charWidth = 10
81 break
82 case "small":
83 charWidth = 9
84 break
85 case "xsmall":
86 charWidth = 8
87 break
88 }
89
90 return (
91 <Skeleton
92 className={clx({
93 "h-5": size === "xsmall",
94 "h-6": size === "small",
95 "h-7": size === "base",
96 "h-8": size === "xlarge",
97 "!h-5": leading === "compact",
98 })}
99 style={{
100 width: `${charWidth * characters}px`,
101 }}
102 />
103 )
104 }
105
106 export const IconButtonSkeleton = () => {
107 return <Skeleton className="h-7 w-7 rounded-md" />
108 }
109
110 type GeneralSectionSkeletonProps = {
111 rowCount?: number
112 }
113
114 export const GeneralSectionSkeleton = ({
115 rowCount,
116 }: GeneralSectionSkeletonProps) => {
117 const rows = Array.from({ length: rowCount ?? 0 }, (_, i) => i)
118
119 return (
120 <Container className="divide-y p-0" aria-hidden>
121 <div className="flex items-center justify-between px-6 py-4">
122 <HeadingSkeleton characters={16} />
123 <IconButtonSkeleton />
124 </div>
125 {rows.map((row) => (
126 <div
127 key={row}
128 className="grid grid-cols-2 items-center px-6 py-4"
129 aria-hidden
130 >
131 <TextSkeleton size="small" leading="compact" characters={12} />
132 <TextSkeleton size="small" leading="compact" characters={24} />
133 </div>
134 ))}
135 </Container>
136 )
137 }
138
139 export const TableFooterSkeleton = ({ layout }: { layout: "fill" | "fit" }) => {
140 return (
141 <div
142 className={clx("flex items-center justify-between p-4", {
143 "border-t": layout === "fill",
144 })}
145 >
146 <Skeleton className="h-7 w-[138px]" />
147 <div className="flex items-center gap-x-2">
148 <Skeleton className="h-7 w-24" />
149 <Skeleton className="h-7 w-11" />
150 <Skeleton className="h-7 w-11" />
151 </div>
152 </div>
153 )
154 }
155
156 type TableSkeletonProps = {
157 rowCount?: number
158 search?: boolean
159 filters?: boolean
160 orderBy?: boolean
161 pagination?: boolean
162 layout?: "fit" | "fill"
163 }
164
165 export const TableSkeleton = ({
166 rowCount = 10,
167 search = true,
168 filters = true,
169 orderBy = true,
170 pagination = true,
171 layout = "fit",
172 }: TableSkeletonProps) => {
173 // Row count + header row
174 const totalRowCount = rowCount + 1
175
176 const rows = Array.from({ length: totalRowCount }, (_, i) => i)
177 const hasToolbar = search || filters || orderBy
178
179 return (
180 <div
181 aria-hidden
182 className={clx({
183 "flex h-full flex-col overflow-hidden": layout === "fill",
184 })}
185 >
186 {hasToolbar && (
187 <div className="flex items-center justify-between px-6 py-4">
188 {filters && <Skeleton className="h-7 w-full max-w-[135px]" />}
189 {(search || orderBy) && (
190 <div className="flex items-center gap-x-2">
191 {search && <Skeleton className="h-7 w-[160px]" />}
192 {orderBy && <Skeleton className="h-7 w-7" />}
193 </div>
194 )}
195 </div>
196 )}
197 <div className="flex flex-col divide-y border-y">
198 {rows.map((row) => (
199 <Skeleton key={row} className="h-10 w-full rounded-none" />
200 ))}
201 </div>
202 {pagination && <TableFooterSkeleton layout={layout} />}
203 </div>
204 )
205 }
206
207 export const TableSectionSkeleton = (props: TableSkeletonProps) => {
208 return (
209 <Container className="divide-y p-0" aria-hidden>
210 <div className="flex items-center justify-between px-6 py-4" aria-hidden>
211 <HeadingSkeleton level="h2" characters={16} />
212 <IconButtonSkeleton />
213 </div>
214 <TableSkeleton {...props} />
215 </Container>
216 )
217 }
218
219 export const JsonViewSectionSkeleton = () => {
220 return (
221 <Container className="divide-y p-0" aria-hidden>
222 <div className="flex items-center justify-between px-6 py-4" aria-hidden>
223 <div aria-hidden className="flex items-center gap-x-4">
224 <HeadingSkeleton level="h2" characters={16} />
225 <Skeleton className="h-5 w-12 rounded-md" />
226 </div>
227 <IconButtonSkeleton />
228 </div>
229 </Container>
230 )
231 }
232
233 type SingleColumnPageSkeletonProps = {
234 sections?: number
235 showJSON?: boolean
236 showMetadata?: boolean
237 }
238
239 export const SingleColumnPageSkeleton = ({
240 sections = 2,
241 showJSON = false,
242 showMetadata = false,
243 }: SingleColumnPageSkeletonProps) => {
244 return (
245 <div className="flex flex-col gap-y-3">
246 {Array.from({ length: sections }, (_, i) => i).map((section) => {
247 return (
248 <Skeleton
249 key={section}
250 className={clx("h-full max-h-[460px] w-full rounded-lg", {
251 // First section is smaller on most pages, this gives us less
252 // layout shifting in general,
253 "max-h-[219px]": section === 0,
254 })}
255 />
256 )
257 })}
258 {showMetadata && <Skeleton className="h-[60px] w-full rounded-lg" />}
259 {showJSON && <Skeleton className="h-[60px] w-full rounded-lg" />}
260 </div>
261 )
262 }
263
264 type TwoColumnPageSkeletonProps = {
265 mainSections?: number
266 sidebarSections?: number
267 showJSON?: boolean
268 showMetadata?: boolean
269 }
270
271 export const TwoColumnPageSkeleton = ({
272 mainSections = 2,
273 sidebarSections = 1,
274 showJSON = false,
275 showMetadata = true,
276 }: TwoColumnPageSkeletonProps) => {
277 const showExtraData = showJSON || showMetadata
278
279 return (
280 <div className="flex flex-col gap-y-3">
281 <div className="flex flex-col gap-x-4 gap-y-3 xl:flex-row xl:items-start">
282 <div className="flex w-full flex-col gap-y-3">
283 {Array.from({ length: mainSections }, (_, i) => i).map((section) => {
284 return (
285 <Skeleton
286 key={section}
287 className={clx("h-full max-h-[460px] w-full rounded-lg", {
288 "max-h-[219px]": section === 0,
289 })}
290 />
291 )
292 })}
293 {showExtraData && (
294 <div className="hidden flex-col gap-y-3 xl:flex">
295 {showMetadata && (
296 <Skeleton className="h-[60px] w-full rounded-lg" />
297 )}
298 {showJSON && <Skeleton className="h-[60px] w-full rounded-lg" />}
299 </div>
300 )}
301 </div>
302 <div className="flex w-full max-w-[100%] flex-col gap-y-3 xl:mt-0 xl:max-w-[440px]">
303 {Array.from({ length: sidebarSections }, (_, i) => i).map(
304 (section) => {
305 return (
306 <Skeleton
307 key={section}
308 className={clx("h-full max-h-[320px] w-full rounded-lg", {
309 "max-h-[140px]": section === 0,
310 })}
311 />
312 )
313 }
314 )}
315 {showExtraData && (
316 <div className="flex flex-col gap-y-3 xl:hidden">
317 {showMetadata && (
318 <Skeleton className="h-[60px] w-full rounded-lg" />
319 )}
320 {showJSON && <Skeleton className="h-[60px] w-full rounded-lg" />}
321 </div>
322 )}
323 </div>
324 </div>
325 </div>
326 )
327 }