forked from Movalabs-crew/mova-store
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptimizedImage.tsx
More file actions
165 lines (154 loc) · 3.21 KB
/
Copy pathOptimizedImage.tsx
File metadata and controls
165 lines (154 loc) · 3.21 KB
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
"use client";
import Image, { ImageProps } from "next/image";
import { useState } from "react";
interface OptimizedImageProps extends Omit<ImageProps, "onError"> {
fallbackSrc?: string;
showSkeleton?: boolean;
}
/**
* OptimizedImage Component
*
* A wrapper around Next.js Image with:
* - Loading skeleton
* - Error fallback
* - Blur placeholder
* - Lazy loading by default
*
* Usage:
* ```tsx
* <OptimizedImage
* src="/product.jpg"
* alt="Product"
* width={400}
* height={300}
* fallbackSrc="/placeholder.jpg"
* />
* ```
*/
export default function OptimizedImage({
src,
alt,
fallbackSrc = "/images/placeholder.png",
showSkeleton = true,
className = "",
...props
}: OptimizedImageProps) {
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState(false);
const imageSrc = error ? fallbackSrc : src;
return (
<div className={`relative overflow-hidden ${className}`}>
{/* Loading skeleton */}
{showSkeleton && isLoading && (
<div
className="absolute inset-0 bg-gray-200 animate-pulse"
aria-hidden="true"
/>
)}
<Image
src={imageSrc}
alt={alt}
className={`
transition-opacity duration-300
${isLoading ? "opacity-0" : "opacity-100"}
${className}
`}
onLoad={() => setIsLoading(false)}
onError={() => {
setError(true);
setIsLoading(false);
}}
loading="lazy"
{...props}
/>
</div>
);
}
/**
* ProductImage Component
*
* Optimized for product images with consistent sizing.
*/
export function ProductImage({
src,
alt,
size = "medium",
className = "",
}: {
src: string;
alt: string;
size?: "small" | "medium" | "large";
className?: string;
}) {
const sizes = {
small: { width: 150, height: 150 },
medium: { width: 300, height: 300 },
large: { width: 500, height: 500 },
};
const { width, height } = sizes[size];
return (
<OptimizedImage
src={src}
alt={alt}
width={width}
height={height}
className={`object-cover rounded-lg ${className}`}
sizes={`(max-width: 768px) 100vw, ${width}px`}
/>
);
}
/**
* Avatar Component
*
* Optimized for user avatars with circular shape.
*/
export function Avatar({
src,
alt,
size = 40,
className = "",
}: {
src?: string | null;
alt: string;
size?: number;
className?: string;
}) {
const fallback = `https://ui-avatars.com/api/?name=${encodeURIComponent(alt)}&size=${size * 2}&background=dc2626&color=fff`;
return (
<OptimizedImage
src={src || fallback}
alt={alt}
width={size}
height={size}
className={`rounded-full object-cover ${className}`}
fallbackSrc={fallback}
/>
);
}
/**
* HeroImage Component
*
* Full-width hero images with priority loading.
*/
export function HeroImage({
src,
alt,
className = "",
}: {
src: string;
alt: string;
className?: string;
}) {
return (
<div className={`relative w-full h-[60vh] min-h-[400px] ${className}`}>
<Image
src={src}
alt={alt}
fill
priority
className="object-cover"
sizes="100vw"
/>
</div>
);
}