diff --git a/.changeset/gorgeous-tomatoes-push.md b/.changeset/gorgeous-tomatoes-push.md new file mode 100644 index 00000000000..273949408a0 --- /dev/null +++ b/.changeset/gorgeous-tomatoes-push.md @@ -0,0 +1,5 @@ +--- +'@shopify/polaris': patch +--- + +Added support for ref to `Image` to handle image load with `EmptyState` diff --git a/polaris-react/src/components/EmptyState/EmptyState.tsx b/polaris-react/src/components/EmptyState/EmptyState.tsx index 6082727b7d6..ff5c71cb564 100644 --- a/polaris-react/src/components/EmptyState/EmptyState.tsx +++ b/polaris-react/src/components/EmptyState/EmptyState.tsx @@ -1,4 +1,4 @@ -import React, {useState, useCallback} from 'react'; +import React, {useState, useRef, useEffect} from 'react'; import {classNames} from '../../utilities/css'; import type {ComplexAction} from '../../types'; @@ -47,9 +47,10 @@ export function EmptyState({ footerContent, }: EmptyStateProps) { const [imageLoaded, setImageLoaded] = useState(false); + const imageRef = useRef(null); - const handleLoad = useCallback(() => { - setImageLoaded(true); + useEffect(() => { + if (imageRef.current?.complete) setImageLoaded(true); }, []); const imageClassNames = classNames( @@ -62,6 +63,7 @@ export function EmptyState({ setImageLoaded(true)} /> ) : ( setImageLoaded(true)} /> ); diff --git a/polaris-react/src/components/Image/Image.tsx b/polaris-react/src/components/Image/Image.tsx index 8b2657fbbfe..0261ff010aa 100644 --- a/polaris-react/src/components/Image/Image.tsx +++ b/polaris-react/src/components/Image/Image.tsx @@ -1,4 +1,4 @@ -import React, {useCallback} from 'react'; +import React, {useCallback, forwardRef} from 'react'; interface SourceSet { source: string; @@ -16,34 +16,33 @@ export interface ImageProps extends React.HTMLProps { onError?(): void; } -export function Image({ - alt, - sourceSet, - source, - crossOrigin, - onLoad, - className, - ...rest -}: ImageProps) { - const finalSourceSet = sourceSet - ? sourceSet - .map(({source: subSource, descriptor}) => `${subSource} ${descriptor}`) - .join(',') - : null; +export const Image = forwardRef( + ({alt, sourceSet, source, crossOrigin, onLoad, className, ...rest}, ref) => { + const finalSourceSet = sourceSet + ? sourceSet + .map( + ({source: subSource, descriptor}) => `${subSource} ${descriptor}`, + ) + .join(',') + : null; - const handleLoad = useCallback(() => { - if (onLoad) onLoad(); - }, [onLoad]); + const handleLoad = useCallback(() => { + if (onLoad) onLoad(); + }, [onLoad]); - return ( - {alt} - ); -} + return ( + {alt} + ); + }, +); + +Image.displayName = 'Image';