Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix passing ref to Image #1268

Merged
merged 2 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tough-jars-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/hydrogen-react': patch
---

Fix passing `ref` to the `<Image>` component.
289 changes: 154 additions & 135 deletions packages/hydrogen-react/src/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -389,87 +389,102 @@ type FixedWidthImageProps = Omit<HydrogenImageProps, FixedImageExludedProps> & {
ref: React.Ref<HTMLImageElement>;
};

function FixedWidthImage({
aspectRatio,
crop,
decoding,
height,
imageWidths,
loader = shopifyLoader,
loading,
normalizedProps,
passthroughProps,
ref,
width,
}: FixedWidthImageProps) {
const fixed = React.useMemo(() => {
const intWidth: number | undefined = getNormalizedFixedUnit(width);
const intHeight: number | undefined = getNormalizedFixedUnit(height);
const FixedWidthImage = React.forwardRef<
HTMLImageElement,
FixedWidthImageProps
>(
(
{
aspectRatio,
crop,
decoding,
height,
imageWidths,
loader = shopifyLoader,
loading,
normalizedProps,
passthroughProps,
width,
},
ref,
) => {
const fixed = React.useMemo(() => {
const intWidth: number | undefined = getNormalizedFixedUnit(width);
const intHeight: number | undefined = getNormalizedFixedUnit(height);

/*
* The aspect ratio for fixed width images is taken from the explicitly
* set prop, but if that's not present, and both width and height are
* set, we calculate the aspect ratio from the width and height—as
* long as they share the same unit type (e.g. both are 'px').
*/
const fixedAspectRatio = aspectRatio
? aspectRatio
: unitsMatch(normalizedProps.width, normalizedProps.height)
? [intWidth, intHeight].join('/')
: normalizedProps.aspectRatio
? normalizedProps.aspectRatio
: undefined;

/*
* The aspect ratio for fixed width images is taken from the explicitly
* set prop, but if that's not present, and both width and height are
* set, we calculate the aspect ratio from the width and height—as
* long as they share the same unit type (e.g. both are 'px').
*/
const fixedAspectRatio = aspectRatio
? aspectRatio
: unitsMatch(normalizedProps.width, normalizedProps.height)
? [intWidth, intHeight].join('/')
: normalizedProps.aspectRatio
? normalizedProps.aspectRatio
: undefined;
/*
* The Sizes Array generates an array of all of the parts
* that make up the srcSet, including the width, height, and crop
*/
const sizesArray =
imageWidths === undefined
? undefined
: generateSizes(imageWidths, fixedAspectRatio, crop);

const fixedHeight = intHeight
? intHeight
: fixedAspectRatio && intWidth
? intWidth * (parseAspectRatio(fixedAspectRatio) ?? 1)
: undefined;

/*
* The Sizes Array generates an array of all of the parts
* that make up the srcSet, including the width, height, and crop
*/
const sizesArray =
imageWidths === undefined
? undefined
: generateSizes(imageWidths, fixedAspectRatio, crop);

const fixedHeight = intHeight
? intHeight
: fixedAspectRatio && intWidth
? intWidth * (parseAspectRatio(fixedAspectRatio) ?? 1)
: undefined;

const srcSet = generateSrcSet(normalizedProps.src, sizesArray, loader);
const src = loader({
src: normalizedProps.src,
width: intWidth,
height: fixedHeight,
crop: normalizedProps.height === 'auto' ? undefined : crop,
});
const srcSet = generateSrcSet(normalizedProps.src, sizesArray, loader);
const src = loader({
src: normalizedProps.src,
width: intWidth,
height: fixedHeight,
crop: normalizedProps.height === 'auto' ? undefined : crop,
});

return {
width: intWidth,
aspectRatio: fixedAspectRatio,
height: fixedHeight,
srcSet,
src,
};
}, [aspectRatio, crop, height, imageWidths, loader, normalizedProps, width]);
return {
width: intWidth,
aspectRatio: fixedAspectRatio,
height: fixedHeight,
srcSet,
src,
};
}, [
aspectRatio,
crop,
height,
imageWidths,
loader,
normalizedProps,
width,
]);

return (
<img
ref={ref}
alt={normalizedProps.alt}
decoding={decoding}
height={fixed.height}
loading={loading}
src={fixed.src}
srcSet={fixed.srcSet}
width={fixed.width}
style={{
aspectRatio: fixed.aspectRatio,
...passthroughProps.style,
}}
{...passthroughProps}
/>
);
}
return (
<img
ref={ref}
alt={normalizedProps.alt}
decoding={decoding}
height={fixed.height}
loading={loading}
src={fixed.src}
srcSet={fixed.srcSet}
width={fixed.width}
style={{
aspectRatio: fixed.aspectRatio,
...passthroughProps.style,
}}
{...passthroughProps}
/>
);
},
);

type FluidImageExcludedProps =
| 'data'
Expand All @@ -488,66 +503,70 @@ type FluidImageProps = Omit<HydrogenImageProps, FluidImageExcludedProps> & {
ref: React.Ref<HTMLImageElement>;
};

function FluidImage({
crop,
decoding,
imageWidths,
loader = shopifyLoader,
loading,
normalizedProps,
passthroughProps,
placeholderWidth,
ref,
sizes,
}: FluidImageProps) {
const fluid = React.useMemo(() => {
const sizesArray =
imageWidths === undefined
? undefined
: generateSizes(imageWidths, normalizedProps.aspectRatio, crop);

const placeholderHeight =
normalizedProps.aspectRatio && placeholderWidth
? placeholderWidth *
(parseAspectRatio(normalizedProps.aspectRatio) ?? 1)
: undefined;

const srcSet = generateSrcSet(normalizedProps.src, sizesArray, loader);

const src = loader({
src: normalizedProps.src,
width: placeholderWidth,
height: placeholderHeight,
const FluidImage = React.forwardRef<HTMLImageElement, FluidImageProps>(
(
{
crop,
});

return {
placeholderHeight,
srcSet,
src,
};
}, [crop, imageWidths, loader, normalizedProps, placeholderWidth]);
decoding,
imageWidths,
loader = shopifyLoader,
loading,
normalizedProps,
passthroughProps,
placeholderWidth,
sizes,
},
ref,
) => {
const fluid = React.useMemo(() => {
const sizesArray =
imageWidths === undefined
? undefined
: generateSizes(imageWidths, normalizedProps.aspectRatio, crop);

const placeholderHeight =
normalizedProps.aspectRatio && placeholderWidth
? placeholderWidth *
(parseAspectRatio(normalizedProps.aspectRatio) ?? 1)
: undefined;

const srcSet = generateSrcSet(normalizedProps.src, sizesArray, loader);

const src = loader({
src: normalizedProps.src,
width: placeholderWidth,
height: placeholderHeight,
crop,
});

return (
<img
ref={ref}
alt={normalizedProps.alt}
decoding={decoding}
height={fluid.placeholderHeight}
loading={loading}
sizes={sizes}
src={fluid.src}
srcSet={fluid.srcSet}
width={placeholderWidth}
{...passthroughProps}
style={{
width: normalizedProps.width,
aspectRatio: normalizedProps.aspectRatio,
...passthroughProps.style,
}}
/>
);
}
return {
placeholderHeight,
srcSet,
src,
};
}, [crop, imageWidths, loader, normalizedProps, placeholderWidth]);

return (
<img
ref={ref}
alt={normalizedProps.alt}
decoding={decoding}
height={fluid.placeholderHeight}
loading={loading}
sizes={sizes}
src={fluid.src}
srcSet={fluid.srcSet}
width={placeholderWidth}
{...passthroughProps}
style={{
width: normalizedProps.width,
aspectRatio: normalizedProps.aspectRatio,
...passthroughProps.style,
}}
/>
);
},
);

/**
* The shopifyLoader function is a simple utility function that takes a src, width,
Expand Down