|
| 1 | +import { useState, useRef, useEffect } from 'react' |
| 2 | + |
| 3 | +const LazyImage = ({ |
| 4 | + src, |
| 5 | + alt, |
| 6 | + className = '', |
| 7 | + placeholder = '/placeholder.svg', |
| 8 | + onLoad, |
| 9 | + onError, |
| 10 | + ...props |
| 11 | +}) => { |
| 12 | + const [isLoaded, setIsLoaded] = useState(false) |
| 13 | + const [isInView, setIsInView] = useState(false) |
| 14 | + const [hasError, setHasError] = useState(false) |
| 15 | + const imgRef = useRef(null) |
| 16 | + |
| 17 | + useEffect(() => { |
| 18 | + const observer = new IntersectionObserver( |
| 19 | + ([entry]) => { |
| 20 | + if (entry.isIntersecting) { |
| 21 | + setIsInView(true) |
| 22 | + observer.disconnect() |
| 23 | + } |
| 24 | + }, |
| 25 | + { |
| 26 | + threshold: 0.1, |
| 27 | + rootMargin: '50px' |
| 28 | + } |
| 29 | + ) |
| 30 | + |
| 31 | + if (imgRef.current) { |
| 32 | + observer.observe(imgRef.current) |
| 33 | + } |
| 34 | + |
| 35 | + return () => observer.disconnect() |
| 36 | + }, []) |
| 37 | + |
| 38 | + const handleLoad = (e) => { |
| 39 | + setIsLoaded(true) |
| 40 | + if (onLoad) onLoad(e) |
| 41 | + } |
| 42 | + |
| 43 | + const handleError = (e) => { |
| 44 | + setHasError(true) |
| 45 | + if (onError) onError(e) |
| 46 | + } |
| 47 | + |
| 48 | + return ( |
| 49 | + <div ref={imgRef} className={`relative overflow-hidden ${className}`}> |
| 50 | + {/* Placeholder */} |
| 51 | + {!isLoaded && !hasError && ( |
| 52 | + <div className="absolute inset-0 bg-gray-200 dark:bg-gray-700 animate-pulse flex items-center justify-center"> |
| 53 | + <div className="w-8 h-8 text-gray-400"> |
| 54 | + <svg fill="currentColor" viewBox="0 0 20 20"> |
| 55 | + <path fillRule="evenodd" d="M4 3a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V5a2 2 0 00-2-2H4zm12 12H4l4-8 3 6 2-4 3 6z" clipRule="evenodd" /> |
| 56 | + </svg> |
| 57 | + </div> |
| 58 | + </div> |
| 59 | + )} |
| 60 | + |
| 61 | + {/* Error state */} |
| 62 | + {hasError && ( |
| 63 | + <div className="absolute inset-0 bg-gray-100 dark:bg-gray-800 flex items-center justify-center"> |
| 64 | + <div className="text-center text-gray-500 dark:text-gray-400"> |
| 65 | + <div className="w-8 h-8 mx-auto mb-2"> |
| 66 | + <svg fill="currentColor" viewBox="0 0 20 20"> |
| 67 | + <path fillRule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z" clipRule="evenodd" /> |
| 68 | + </svg> |
| 69 | + </div> |
| 70 | + <span className="text-xs">Failed to load</span> |
| 71 | + </div> |
| 72 | + </div> |
| 73 | + )} |
| 74 | + |
| 75 | + {/* Actual image */} |
| 76 | + {isInView && ( |
| 77 | + <img |
| 78 | + src={src} |
| 79 | + alt={alt} |
| 80 | + className={`transition-opacity duration-300 ${ |
| 81 | + isLoaded ? 'opacity-100' : 'opacity-0' |
| 82 | + } ${className}`} |
| 83 | + onLoad={handleLoad} |
| 84 | + onError={handleError} |
| 85 | + loading="lazy" |
| 86 | + {...props} |
| 87 | + /> |
| 88 | + )} |
| 89 | + </div> |
| 90 | + ) |
| 91 | +} |
| 92 | + |
| 93 | +export default LazyImage |
0 commit comments