Skip to content

Instantly share code, notes, and snippets.

@HectorBlisS
Created May 16, 2023 23:07
Show Gist options
  • Save HectorBlisS/f2911b03710c58bca883a0ee3ff29418 to your computer and use it in GitHub Desktop.
Save HectorBlisS/f2911b03710c58bca883a0ee3ff29418 to your computer and use it in GitHub Desktop.
Youtube_useBlurImage
import { useEffect, useState } from "react";
export const useDynamicSrc = (hd: string, low: string) => {
const [src, set] = useState(low ?? "");
useEffect(() => {
const img = new Image();
img.src = src;
img.onload = () => {
set(hd);
};
}, [hd, src]);
return [src, src !== hd];
};
export const BlurImage = ({
src,
lowSrc,
}: {
src: string;
lowSrc?: string;
}) => {
const [dSrc, blur] = useDynamicSrc(src, lowSrc);
return (
<img
style={{
width: "100%",
filter: blur ? "blur(20px)" : "none",
clipPath: "inset(0)",
transition: "filter 1s ease-out",
}}
src={dSrc}
alt="zelda"
/>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment