Skip to content

Commit

Permalink
Uses width if present for resourceWidth
Browse files Browse the repository at this point in the history
  • Loading branch information
supernova-at committed Nov 6, 2019
1 parent fa254e2 commit 4e798f7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
21 changes: 20 additions & 1 deletion packages/peregrine/lib/talons/Image/__tests__/useImage.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ test('it returns the proper shape', () => {
});

describe('resourceWidth', () => {
test('uses width if present', () => {
// Arrange.
const myProps = {
...props,
width: 75
};

// Act.
createTestInstance(<Component {...myProps} />);

// Assert.
expect(log).toHaveBeenCalledWith(
expect.objectContaining({
resourceWidth: myProps.width
})
);
});

test('falls back to the default entry in widths', () => {
// Act.
createTestInstance(<Component {...props} />);
Expand All @@ -49,10 +67,11 @@ describe('resourceWidth', () => {
);
});

test('returns undefined if widths is not present', () => {
test('returns undefined if width and widths are not present', () => {
// Arrange.
const myProps = {
...props,
width: undefined,
widths: undefined
};

Expand Down
11 changes: 9 additions & 2 deletions packages/peregrine/lib/talons/Image/useImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import { useCallback, useMemo, useState } from 'react';
* @param {function} props.onError callback for error of loading image
* @param {function} props.onLoad callback for load of image
* @param {string} props.unconstrainedSizeKey the key in props.widths for the unconstrained / default width.
* @param {number} props.width the intrinsic width of the image & the width to request for the fallback image for browsers that don't support srcset / sizes.
* @param {Map} props.widths a map of breakpoints to possible widths used to create the img's sizes attribute.
*/
export const useImage = props => {
const { onError, onLoad, unconstrainedSizeKey, widths } = props;
const { onError, onLoad, unconstrainedSizeKey, width, widths } = props;
const [isLoaded, setIsLoaded] = useState(false);
const [hasError, setHasError] = useState(false);

Expand All @@ -31,12 +32,18 @@ export const useImage = props => {

// Use the unconstrained / default entry in widths.
const resourceWidth = useMemo(() => {
if (width) {
return width;
}

// We don't have an explicit width.
// Attempt to use the unconstrained entry in widths.
if (!widths) {
return undefined;
}

return widths.get(unconstrainedSizeKey);
}, [unconstrainedSizeKey, widths]);
}, [unconstrainedSizeKey, width, widths]);

return {
handleError,
Expand Down
4 changes: 4 additions & 0 deletions packages/venia-ui/lib/components/Image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const UNCONSTRAINED_SIZE_KEY = 'default';
* @param {string} props.resource the Magento path to the image ex: /v/d/vd12-rn_main_2.jpg
* @param {string} props.src the source of the image, ready to use in an img element
* @param {string} props.type the Magento image type ("image-category" / "image-product"). Used to build the resource URL.
* @param {number} props.width the intrinsic width of the image & the width to request for the fallback image for browsers that don't support srcset / sizes.
* @param {Map} props.widths a map of breakpoints to possible widths used to create the img's sizes attribute.
*/
const Image = props => {
Expand All @@ -44,6 +45,7 @@ const Image = props => {
resource,
src,
type,
width,
widths,
...rest
} = props;
Expand All @@ -52,6 +54,7 @@ const Image = props => {
onError,
onLoad,
unconstrainedSizeKey: UNCONSTRAINED_SIZE_KEY,
width,
widths
});

Expand Down Expand Up @@ -148,6 +151,7 @@ Image.propTypes = {
resource: conditionallyRequiredString,
src: conditionallyRequiredString,
type: string,
width: oneOfType([number, string]),
widths: instanceOf(Map)
};

Expand Down

0 comments on commit 4e798f7

Please sign in to comment.