|
| 1 | +import * as React from "react"; |
| 2 | + |
1 | 3 | import { cn } from "@/shared/lib/cn"; |
2 | 4 |
|
3 | | -function Skeleton({ |
| 5 | +type SkeletonProps = React.HTMLAttributes<HTMLDivElement> & { |
| 6 | + pulsing?: boolean; |
| 7 | +}; |
| 8 | + |
| 9 | +function Skeleton({ className, pulsing = true, ...props }: SkeletonProps) { |
| 10 | + return ( |
| 11 | + <div |
| 12 | + aria-hidden="true" |
| 13 | + className={cn( |
| 14 | + "t-skel-bar rounded-md bg-primary/10", |
| 15 | + pulsing && "is-pulsing", |
| 16 | + className, |
| 17 | + )} |
| 18 | + {...props} |
| 19 | + /> |
| 20 | + ); |
| 21 | +} |
| 22 | + |
| 23 | +type SkeletonRevealProps = React.HTMLAttributes<HTMLDivElement> & { |
| 24 | + contentClassName?: string; |
| 25 | + layout?: "absolute" | "flow"; |
| 26 | + loading: boolean; |
| 27 | + skeleton: React.ReactNode; |
| 28 | + skeletonClassName?: string; |
| 29 | +}; |
| 30 | + |
| 31 | +function SkeletonReveal({ |
| 32 | + children, |
4 | 33 | className, |
| 34 | + contentClassName, |
| 35 | + layout = "flow", |
| 36 | + loading, |
| 37 | + skeleton, |
| 38 | + skeletonClassName, |
5 | 39 | ...props |
6 | | -}: React.HTMLAttributes<HTMLDivElement>) { |
| 40 | +}: SkeletonRevealProps) { |
| 41 | + const rootRef = React.useRef<HTMLDivElement>(null); |
| 42 | + const previousLoadingRef = React.useRef(loading); |
| 43 | + const [isResetting, setIsResetting] = React.useState(false); |
| 44 | + |
| 45 | + React.useLayoutEffect(() => { |
| 46 | + const wasLoading = previousLoadingRef.current; |
| 47 | + previousLoadingRef.current = loading; |
| 48 | + |
| 49 | + if (!loading || wasLoading) return; |
| 50 | + |
| 51 | + setIsResetting(true); |
| 52 | + rootRef.current?.getBoundingClientRect(); |
| 53 | + |
| 54 | + const reset = () => setIsResetting(false); |
| 55 | + const frameId = globalThis.requestAnimationFrame |
| 56 | + ? globalThis.requestAnimationFrame(reset) |
| 57 | + : globalThis.setTimeout(reset, 0); |
| 58 | + |
| 59 | + return () => { |
| 60 | + if (typeof frameId === "number") { |
| 61 | + if (globalThis.cancelAnimationFrame) { |
| 62 | + globalThis.cancelAnimationFrame(frameId); |
| 63 | + } else { |
| 64 | + globalThis.clearTimeout(frameId); |
| 65 | + } |
| 66 | + } |
| 67 | + }; |
| 68 | + }, [loading]); |
| 69 | + |
7 | 70 | return ( |
8 | 71 | <div |
9 | | - className={cn("animate-pulse rounded-md bg-primary/10", className)} |
| 72 | + className={cn( |
| 73 | + "t-skel", |
| 74 | + !loading && "is-revealed", |
| 75 | + isResetting && "is-resetting", |
| 76 | + className, |
| 77 | + )} |
| 78 | + data-layout={layout} |
| 79 | + data-state={loading ? "loading" : "loaded"} |
| 80 | + ref={rootRef} |
10 | 81 | {...props} |
11 | | - /> |
| 82 | + > |
| 83 | + <div |
| 84 | + aria-hidden="true" |
| 85 | + className={cn("t-skel-skeleton is-pulsing", skeletonClassName)} |
| 86 | + > |
| 87 | + {skeleton} |
| 88 | + </div> |
| 89 | + <div |
| 90 | + aria-hidden={loading} |
| 91 | + className={cn("t-skel-content", contentClassName)} |
| 92 | + > |
| 93 | + {children} |
| 94 | + </div> |
| 95 | + </div> |
12 | 96 | ); |
13 | 97 | } |
14 | 98 |
|
15 | | -export { Skeleton }; |
| 99 | +export { Skeleton, SkeletonReveal }; |
0 commit comments