-
-
Save juanpprieto/39fca66ff24f02d22521ad1fced1d20e to your computer and use it in GitHub Desktop.
Unpic blurhash handling with React
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
function isStoryblokAsset(object: unknown): object is AssetStoryblok { | |
return (object as AssetStoryblok)?.filename !== undefined; | |
} | |
const Picture = forwardRef< | |
HTMLImageElement, | |
PictureProps & ImgHTMLAttributes<HTMLImageElement> | |
>(({ src, lazy, ...props }, ref) => { | |
const internalRef = useRef<HTMLImageElement>(null); | |
const blurHashes = useBlurHashes(); | |
const priority = useImagePriority(); | |
useImperativeHandle<HTMLImageElement | null, HTMLImageElement | null>( | |
ref, | |
() => internalRef.current | |
); | |
useEffect(() => { | |
if (internalRef.current) resetBackgroundBlurHash(internalRef.current); | |
}, []); | |
if (!src || (isStoryblokAsset(src) && !src.filename)) return; | |
const source = isStoryblokAsset(src) ? src.filename : src; | |
const fileUrl = !source.startsWith("http") ? `https:${source}` : source; | |
const [width, height] = fileUrl.match(/\/(\d+)x(\d+)\//)?.slice(1) || []; | |
return ( | |
<Image | |
ref={internalRef} | |
{...props} | |
alt={isStoryblokAsset(src) ? src.alt || "" : props.alt || ""} | |
src={priority ? `${fileUrl}/m/filters:quality(50)` : fileUrl} | |
width={parseInt(width, 10)} | |
height={parseInt(height, 10)} | |
priority={lazy === false || priority} | |
onLoad={(event) => { | |
if (event.target instanceof HTMLImageElement) { | |
resetBackgroundBlurHash(event.target); | |
} | |
}} | |
background={ | |
blurHashes[fileUrl] | |
? blurhashToCssGradientString(blurHashes[fileUrl]) | |
: undefined | |
} | |
/> | |
); | |
}); | |
const PictureProvider: FC<PropsWithChildren> = (props) => ( | |
<PictureContext.Provider {...props} value={Picture} /> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment