// TODO: We can probably detect if it's preloaded by using the dpr_auto setting // in whatever image proxy we wind up using and then testing for natural dimensions. // Another option would be to defer applying the ready state until after there's an initial // paint so we can detect whether or not it should be faded in. // TODO: Implement a cropped responsive image that supports aspect ratios as config. import * as React from 'react'; import classNames from 'classnames'; import {scale} from 'proportional-scale'; import {LazyRetinaImage, RetinaImage} from '../retina_image'; import {ResponsiveImagePlaceholder} from './responsive_image_placeholder'; import {FallbackImage} from '../fallback_image'; import './responsive_image.scss'; export const DEFAULT_PRODUCT_ASPECT_WIDTH = 128; export const DEFAULT_PRODUCT_ASPECT_HEIGHT = 205; export const DEFAULT_PRODUCT_ASPECT_RATIO = DEFAULT_PRODUCT_ASPECT_HEIGHT / DEFAULT_PRODUCT_ASPECT_WIDTH; export interface Props extends React.HTMLAttributes { src: null | string; aspectWidth?: number; aspectHeight?: number; maxWidth: number; maxHeight: number; alt: string; quality?: number; usePlaceholder?: boolean; lazyLoad?: boolean; forwardedRef?: React.Ref; handleErrorCallback?: (src: string) => void; } enum Mode { 'Loading', 'Ready', 'Error' } interface State { mode: Mode; } type Action = {type: 'READY'} | {type: 'ERROR'}; const reducer = (state: State, action: Action): State => { switch (action.type) { case 'READY': { // Once we are in error mode, do not recover. // This may happen for instance when the browser // returns a complete event on a cached errored image. if (state.mode === Mode.Error) { return state; } return { ...state, mode: Mode.Ready }; } case 'ERROR': return { ...state, mode: Mode.Error }; } }; const __ResponsiveImage__: React.FC = ({ className, src, aspectWidth = DEFAULT_PRODUCT_ASPECT_WIDTH, aspectHeight = DEFAULT_PRODUCT_ASPECT_HEIGHT, maxWidth, maxHeight, alt, quality = 90, lazyLoad = false, usePlaceholder = false, forwardedRef, handleErrorCallback, ...rest }) => { const [state, dispatch] = React.useReducer(reducer, { mode: Mode.Loading }); const handleLoad = React.useCallback(() => dispatch({type: 'READY'}), []); const handleError = React.useCallback(() => { handleErrorCallback && handleErrorCallback(src); dispatch({type: 'ERROR'}); }, [handleErrorCallback, src]); const {width, height, paddingBottom} = scale({ width: aspectWidth, height: aspectHeight, maxWidth, maxHeight }); const img = React.useRef(null); const Image = lazyLoad ? LazyRetinaImage : RetinaImage; React.useEffect(() => { if (img.current.complete) { dispatch({type: 'READY'}); } }, [img]); return (
{src && usePlaceholder && state.mode !== Mode.Error && ( )} {(state.mode === Mode.Error || (!src && !usePlaceholder)) && ( )} {alt}
); }; export const ResponsiveImage = React.memo( // eslint-disable-next-line react/display-name React.forwardRef((props: Props, ref: React.Ref) => ( // eslint-disable-next-line react/jsx-pascal-case <__ResponsiveImage__ {...props} forwardedRef={ref} /> )) ); ResponsiveImage.displayName = 'ResponsiveImage';