mediahub-fe/components/ui/image-blurry.tsx

78 lines
1.8 KiB
TypeScript
Raw Normal View History

2025-07-28 08:16:03 +00:00
import React, { useEffect, useState } from "react";
2025-01-12 17:22:47 +00:00
interface ImageBlurryProps {
src: string;
alt?: string;
className?: string;
2025-01-12 17:22:47 +00:00
key?: string | number;
style?: React.CSSProperties;
2025-07-28 08:16:03 +00:00
priority?: boolean;
placeholder?: string;
2025-01-12 17:22:47 +00:00
}
const ImageBlurry: React.FC<ImageBlurryProps> = ({
src,
alt,
className,
key,
style,
priority = false,
placeholder,
}) => {
2025-01-12 17:22:47 +00:00
const [imgSrc, setImgSrc] = useState<string>(src);
const [isError, setIsError] = useState<boolean>(false);
useEffect(() => {
setImgSrc(src);
}, [src]);
const handleImage = () => {
2025-07-28 08:16:03 +00:00
const pics = document.querySelectorAll<HTMLImageElement>("img");
2025-01-12 17:22:47 +00:00
pics.forEach((pic) => {
if (pic.complete) {
checkImage(pic);
} else {
2025-07-28 08:16:03 +00:00
pic.addEventListener("load", function () {
checkImage(this as HTMLImageElement);
2025-01-12 17:22:47 +00:00
});
}
});
};
const checkImage = (img: HTMLImageElement) => {
if (img.naturalHeight > img.naturalWidth) {
2025-07-28 08:16:03 +00:00
img.classList.add("portrait");
2025-01-12 17:22:47 +00:00
} else if (Math.abs(img.naturalHeight - img.naturalWidth) < 10) {
2025-07-28 08:16:03 +00:00
img.classList.add("square");
2025-01-12 17:22:47 +00:00
} else {
2025-07-28 08:16:03 +00:00
img.classList.add("landscape");
2025-01-12 17:22:47 +00:00
}
};
const onLoad = () => {
2025-07-28 08:16:03 +00:00
console.log("Image loaded:", src);
2025-01-12 17:22:47 +00:00
handleImage();
};
return (
2025-07-28 08:16:03 +00:00
<div
className={`blurry-wrapper relative overflow-hidden bg-[#e9e9e9] rounded-t-lg ${
className || ""
}`}
2025-07-28 08:16:03 +00:00
key={key}
style={style}
>
<div
className="absolute -top-6 -bottom-6 -left-0 -right-6 bg-[#e9e9e9] blur-[8px] scale-[1.8] bg-center bg-no-repeat bg-contain blur-bg"
style={{ backgroundImage: `url(${imgSrc})` }}
></div>
<div
className="absolute inset-0 bg-transparent bg-center bg-no-repeat bg-contain image-bg"
style={{ backgroundImage: `url(${imgSrc})` }}
></div>
2025-01-12 17:22:47 +00:00
</div>
);
};
export default ImageBlurry;