1import React, { useEffect, useMemo } from 'react'; 2import ColorTintFilter, { getTintColorStyle } from './ColorTintFilter'; 3import { getImageWrapperEventHandler } from './getImageWrapperEventHandler'; 4import { absoluteFilledPosition, getObjectPositionFromContentPositionObject } from './positioning'; 5import { useBlurhash } from '../utils/blurhash/useBlurhash'; 6import { isBlurhashString, isThumbhashString } from '../utils/resolveSources'; 7import { thumbHashStringToDataURL } from '../utils/thumbhash/thumbhash'; 8function getFetchPriorityFromImagePriority(priority = 'normal') { 9 return priority && ['low', 'high'].includes(priority) ? priority : 'auto'; 10} 11function getImgPropsFromSource(source) { 12 if (source && 'srcset' in source) { 13 return { 14 srcSet: source.srcset, 15 sizes: source.sizes, 16 }; 17 } 18 return {}; 19} 20const ImageWrapper = React.forwardRef(({ source, events, contentPosition, hashPlaceholderContentPosition, priority, style, hashPlaceholderStyle, tintColor, className, accessibilityLabel, ...props }, ref) => { 21 useEffect(() => { 22 events?.onMount?.forEach((e) => e?.()); 23 }, []); 24 const isBlurhash = isBlurhashString(source?.uri || ''); 25 const isThumbhash = isThumbhashString(source?.uri || ''); 26 const isHash = isBlurhash || isThumbhash; 27 // Thumbhash uri always has to start with 'thumbhash:/' 28 const thumbhash = source?.uri?.replace(/thumbhash:\//, ''); 29 const thumbhashUri = useMemo(() => (isThumbhash ? thumbHashStringToDataURL(thumbhash ?? '') : null), [thumbhash]); 30 const blurhashUri = useBlurhash(isBlurhash ? source?.uri : null, source?.width, source?.height); 31 if (!source) { 32 return null; 33 } 34 const objectPosition = getObjectPositionFromContentPositionObject(isHash ? hashPlaceholderContentPosition : contentPosition); 35 const uri = isHash ? blurhashUri ?? thumbhashUri : source?.uri; 36 return (React.createElement(React.Fragment, null, 37 React.createElement(ColorTintFilter, { tintColor: tintColor }), 38 React.createElement("img", { ref: ref, alt: accessibilityLabel, className: className, src: uri || undefined, key: source?.uri, style: { 39 objectPosition, 40 ...absoluteFilledPosition, 41 ...getTintColorStyle(tintColor), 42 ...(isHash ? hashPlaceholderStyle : {}), 43 ...style, 44 }, 45 // @ts-ignore 46 // eslint-disable-next-line react/no-unknown-property 47 fetchpriority: getFetchPriorityFromImagePriority(priority || 'normal'), ...getImageWrapperEventHandler(events, source), ...getImgPropsFromSource(source), ...props }))); 48}); 49export default ImageWrapper; 50//# sourceMappingURL=ImageWrapper.js.map