uchill/front_material/components/OptimizedImage.tsx

68 lines
1.4 KiB
TypeScript

/**
* Оптимизированный компонент изображения
*/
'use client';
import Image from 'next/image';
import { useState } from 'react';
interface OptimizedImageProps {
src: string;
alt: string;
width?: number;
height?: number;
className?: string;
priority?: boolean;
placeholder?: 'blur' | 'empty';
blurDataURL?: string;
}
export function OptimizedImage({
src,
alt,
width,
height,
className,
priority = false,
placeholder = 'empty',
blurDataURL,
}: OptimizedImageProps) {
const [error, setError] = useState(false);
if (error) {
return (
<div
className={className}
style={{
width: width || '100%',
height: height || 'auto',
backgroundColor: 'var(--md-sys-color-surface-variant)',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
color: 'var(--md-sys-color-on-surface-variant)',
}}
>
{alt}
</div>
);
}
return (
<Image
src={src}
alt={alt}
width={width}
height={height}
className={className}
priority={priority}
placeholder={placeholder}
blurDataURL={blurDataURL}
onError={() => setError(true)}
loading={priority ? undefined : 'lazy'}
decoding="async"
/>
);
}