File size: 4,930 Bytes
65d68f5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | "use client";
import { HTMLAttributes } from "react";
import { clsx } from "clsx";
// =============================================================================
// Skeleton Types
// =============================================================================
export type SkeletonVariant =
| "text"
| "heading"
| "paragraph"
| "card"
| "button"
| "avatar"
| "image"
| "list"
| "circular";
export interface SkeletonProps extends HTMLAttributes<HTMLDivElement> {
variant?: SkeletonVariant;
lines?: number;
width?: string | number;
height?: string | number;
animated?: boolean;
}
// =============================================================================
// SharedSkeleton Component
// =============================================================================
export function SharedSkeleton({
variant = "text",
lines = 1,
width,
height,
animated = true,
className,
...props
}: SkeletonProps) {
// Default dimensions for each variant
const defaults: Record<SkeletonVariant, { width: string; height: string }> = {
text: { width: "100%", height: "1em" },
heading: { width: "60%", height: "1.5em" },
paragraph: { width: "100%", height: "1em" },
card: { width: "100%", height: "120px" },
button: { width: "120px", height: "40px" },
avatar: { width: "40px", height: "40px" },
image: { width: "100%", height: "200px" },
list: { width: "100%", height: "40px" },
circular: { width: "40px", height: "40px" },
};
const d = defaults[variant];
const w = width || d.width;
const h = height || d.height;
// Build style object
const style: React.CSSProperties = {
width: typeof w === "number" ? `${w}px` : w,
height: typeof h === "number" ? `${h}px` : h,
};
// Animation classes
const animationClasses = animated ? "animate-skeleton-pulse" : "";
// Variant-specific classes (reserved)
const _variantClasses = clsx({
circular: "rounded-full",
card: "rounded-lg",
button: "rounded-lg",
avatar: "rounded-full",
image: "rounded-lg",
// Default to rounded for text/paragraph/heading/list
});
if (variant === "paragraph" && lines > 1) {
return (
<div className={clsx("space-y-2", className)} {...props}>
{Array.from({ length: lines }).map((_, i) => (
<SharedSkeleton
key={i}
variant="text"
animated={animated}
width={i === 0 ? "80%" : i === lines - 1 ? "60%" : "100%"}
/>
))}
</div>
);
}
if (variant === "list" && lines > 1) {
return (
<div className={clsx("space-y-2", className)} {...props}>
{Array.from({ length: lines }).map((_, i) => (
<SharedSkeleton key={i} variant="list" animated={animated} />
))}
</div>
);
}
return (
<div
className={clsx(
"bg-[var(--bg-elevated)]",
"border",
"border-[var(--border-subtle)]",
variant !== "circular" && variant !== "avatar" && "rounded-md",
variant === "circular" && "rounded-full",
variant === "avatar" && "rounded-full",
animationClasses,
className,
)}
style={style}
{...props}
/>
);
}
// =============================================================================
// Skeleton Containers
// =============================================================================
// Card skeleton (matching SharedCard)
export function SkeletonCard({
lines = 3,
className,
...props
}: { lines?: number } & Omit<SkeletonProps, "variant">) {
return (
<SharedSkeleton
variant="card"
className={clsx("rounded-xl", className)}
{...props}
>
<div className="space-y-3 p-7">
<SharedSkeleton variant="heading" animated={props.animated} />
{Array.from({ length: lines }).map((_, i) => (
<SharedSkeleton
key={i}
variant="text"
animated={props.animated}
width={i === lines - 1 ? "75%" : "100%"}
/>
))}
</div>
</SharedSkeleton>
);
}
// Button skeleton
export function SkeletonButton({ className, ...props }: Omit<SkeletonProps, "variant">) {
return (
<SharedSkeleton
variant="button"
className={clsx("rounded-lg", className)}
{...props}
/>
);
}
// Input skeleton
export function SkeletonInput({ className, ...props }: Omit<SkeletonProps, "variant">) {
return (
<SharedSkeleton
variant="text"
height="44px"
className={clsx("rounded-lg w-full", className)}
{...props}
/>
);
}
// Avatar skeleton
export function SkeletonAvatar({
size = 40,
className,
...props
}: { size?: number } & Omit<SkeletonProps, "variant" | "width" | "height">) {
return (
<SharedSkeleton
variant="circular"
width={size}
height={size}
className={className}
{...props}
/>
);
}
|