Spaces:
Sleeping
Sleeping
File size: 5,156 Bytes
4995d62 | 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 | /**
* Skeleton - Configurable placeholder loader with shimmer animation.
*
* Variants:
* text - Horizontal line (for text placeholders)
* circle - Circle (for avatars)
* card - Full card placeholder with header, body lines, footer
* rect - Custom rectangle
*
* Supports repeating via the `count` prop and custom dimensions.
*/
import PropTypes from "prop-types";
// ---------------------------------------------------------------------------
// Base Skeleton Block
// ---------------------------------------------------------------------------
function SkeletonBlock({ width, height, rounded = "rounded", className = "" }) {
return (
<div
className={`bg-neutral-200 ${rounded} relative overflow-hidden ${className}`}
style={{ width, height }}
aria-hidden="true"
>
<div className="absolute inset-0 shimmer" />
</div>
);
}
SkeletonBlock.propTypes = {
width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
rounded: PropTypes.string,
className: PropTypes.string,
};
// ---------------------------------------------------------------------------
// Preset: Text Lines
// ---------------------------------------------------------------------------
function SkeletonText({ lines = 3, className = "" }) {
const widths = ["100%", "90%", "75%", "85%", "60%"];
return (
<div className={`space-y-2.5 ${className}`} aria-hidden="true">
{Array.from({ length: lines }).map((_, i) => (
<SkeletonBlock
key={i}
width={widths[i % widths.length]}
height={12}
rounded="rounded-md"
/>
))}
</div>
);
}
SkeletonText.propTypes = {
lines: PropTypes.number,
className: PropTypes.string,
};
// ---------------------------------------------------------------------------
// Preset: Card
// ---------------------------------------------------------------------------
function SkeletonCard({ className = "" }) {
return (
<div
className={`rounded-xl border border-neutral-200 bg-white overflow-hidden ${className}`}
aria-hidden="true"
>
{/* Header area */}
<div className="p-5 space-y-3">
<div className="flex items-center gap-3">
<SkeletonBlock width={40} height={40} rounded="rounded-lg" />
<div className="flex-1 space-y-2">
<SkeletonBlock width="60%" height={14} rounded="rounded-md" />
<SkeletonBlock width="40%" height={10} rounded="rounded-md" />
</div>
</div>
</div>
{/* Body lines */}
<div className="px-5 pb-5">
<SkeletonText lines={3} />
</div>
{/* Footer */}
<div className="border-t border-neutral-100 px-5 py-3 bg-neutral-50 flex justify-between">
<SkeletonBlock width={80} height={28} rounded="rounded-md" />
<SkeletonBlock width={60} height={28} rounded="rounded-md" />
</div>
</div>
);
}
SkeletonCard.propTypes = {
className: PropTypes.string,
};
// ---------------------------------------------------------------------------
// Main Skeleton Component
// ---------------------------------------------------------------------------
function Skeleton({
variant = "text",
width,
height,
count = 1,
className = "",
}) {
if (variant === "circle") {
const size = width || height || 48;
return (
<div
className={`flex flex-wrap gap-3 ${className}`}
role="status"
aria-label="Loading"
>
{Array.from({ length: count }).map((_, i) => (
<SkeletonBlock
key={i}
width={size}
height={size}
rounded="rounded-full"
/>
))}
</div>
);
}
if (variant === "card") {
return (
<div
className={`grid gap-4 ${count > 1 ? "sm:grid-cols-2 lg:grid-cols-3" : ""} ${className}`}
role="status"
aria-label="Loading"
>
{Array.from({ length: count }).map((_, i) => (
<SkeletonCard key={i} />
))}
</div>
);
}
if (variant === "rect") {
return (
<div
className={className}
role="status"
aria-label="Loading"
>
{Array.from({ length: count }).map((_, i) => (
<SkeletonBlock
key={i}
width={width || "100%"}
height={height || 100}
rounded="rounded-lg"
className={i < count - 1 ? "mb-3" : ""}
/>
))}
</div>
);
}
// Default: text
return (
<div
className={className}
role="status"
aria-label="Loading"
>
{Array.from({ length: count }).map((_, i) => (
<SkeletonText key={i} className={i < count - 1 ? "mb-6" : ""} />
))}
</div>
);
}
Skeleton.propTypes = {
variant: PropTypes.oneOf(["text", "circle", "card", "rect"]),
width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
count: PropTypes.number,
className: PropTypes.string,
};
export default Skeleton;
|