Created
May 16, 2023 23:07
-
-
Save HectorBlisS/f2911b03710c58bca883a0ee3ff29418 to your computer and use it in GitHub Desktop.
Youtube_useBlurImage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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