import { isFunction } from '@ballerine/common'; import { forwardRef, ReactEventHandler, useCallback, useEffect, useState } from 'react'; import { ctw } from '../../../utils/ctw/ctw'; import { PhotoSvg } from '../icons'; import { IBallerineImageProps } from './interfaces'; /** * @description An img element with default styling, and a Skeleton placeholder. * * @param props * @constructor */ export const BallerineImage = forwardRef( ({ withPlaceholder, placeholder, alt, src, className, isLoading, onError, ...rest }, ref) => { const [error, setError] = useState(false); const isError = !src || error; const isPlaceholder = withPlaceholder && isError; const handleError: ReactEventHandler = useCallback( e => { setError(true); if (!isFunction(onError)) { return; } onError(e); }, [onError], ); useEffect(() => { if (!error || !src) { return; } setError(false); }, [src]); if (isLoading || isPlaceholder) { return (
{isLoading ? null : placeholder ? (
{placeholder}
) : (
)}
); } return ( {alt} ); }, ); BallerineImage.displayName = 'BallerineImage';