Skip to content

Commit

Permalink
Pass all Image.props to nested img element (#2628)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nenad Misic authored Jan 4, 2024
1 parent 6c6e3d8 commit 4189c5e
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions src/components/Image/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export type ImageProps = React.ImgHTMLAttributes<HTMLImageElement> & {
} & ({ href: string; title: string } | { href?: never; title?: never });

const Image = React.forwardRef<HTMLImageElement, ImageProps>(
({ className, ...props }, forwardRef) => {
(
{ alt = "", className, isSrcLoading, href, title, onError, ...props },
forwardRef
) => {
const ref = useRef<HTMLImageElement>(null);
const [isError, setError] = useState(false);

Expand All @@ -23,7 +26,7 @@ const Image = React.forwardRef<HTMLImageElement, ImageProps>(
() => ref.current
);

if ((!props.src && !props.isSrcLoading) || isError) {
if ((!props.src && !isSrcLoading) || isError) {
return <ImagePlaceholder className={className} />;
}

Expand All @@ -45,7 +48,8 @@ const Image = React.forwardRef<HTMLImageElement, ImageProps>(
* 5. Component `Image` flickers as it transitions from displaying `ContentLoader` to displaying `img`
*
*/
const isLoading = !ref.current?.complete && props.isSrcLoading;
const isLoading = !ref.current?.complete && isSrcLoading;
const commonClasses = `${className} ${isLoading ? "hidden" : ""}`;

return (
<>
Expand All @@ -62,20 +66,13 @@ const Image = React.forwardRef<HTMLImageElement, ImageProps>(
* wrapping the `img`.
*
*/}
<WithLink
className={`${className} ${isLoading ? "hidden" : ""}`}
href={props.href}
title={props.title}
>
<WithLink className={commonClasses} href={href} title={title}>
<img
ref={ref}
src={props.src}
className={`object-contain ${
isLoading ? "hidden" : ""
} ${className}`}
alt={props.alt || ""}
loading={props.loading}
onError={(e) => (props.onError ? props.onError(e) : setError(true))}
className={`object-contain ${commonClasses}`}
alt={alt}
onError={(e) => (onError ? onError(e) : setError(true))}
{...props}
/>
</WithLink>
</>
Expand Down

0 comments on commit 4189c5e

Please sign in to comment.