Skip to content
Closed
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
a52f736
New Image PR
benjaminsehl Mar 6, 2023
c0cfbe0
Make existing tests pass
cartogram Mar 7, 2023
b1110fa
Remove missing sizes warning if fixed width
benjaminsehl Mar 7, 2023
e33681f
Add tests for warnings and fix bug when getting normalizedWidth
cartogram Mar 7, 2023
ca6fa6f
Change multiline log style
cartogram Mar 7, 2023
694cc48
test missing src value
cartogram Mar 8, 2023
e49968e
Enable restrict template literal rule
cartogram Mar 9, 2023
c3f102a
More tests
cartogram Mar 9, 2023
e6f21f9
more tests
cartogram Mar 9, 2023
2039367
Pass custom loader to srcSet
cartogram Mar 13, 2023
daa0b59
Adds width and height prop to the image
benjaminsehl Mar 13, 2023
9bf619d
Improve default width and height props on rendered HTML
benjaminsehl Mar 13, 2023
0f34b1d
Only console warn in dev
benjaminsehl Mar 13, 2023
3c5cb24
Intellisense improvements
benjaminsehl Mar 13, 2023
bd82a80
Comment out console warning tests
benjaminsehl Mar 13, 2023
981ba48
Updates Demo Store images
benjaminsehl Mar 13, 2023
010be7d
Adds docs and examples, updates demo store implementation
benjaminsehl Mar 14, 2023
21cb8ae
Update generated docs data
benjaminsehl Mar 14, 2023
2108d73
Export IMAGE_FRAGMENT from Image component
benjaminsehl Mar 14, 2023
9fcc242
Merge branch '2023-01' into new-image
benjaminsehl Mar 14, 2023
e5e8b84
Update Demo Store
benjaminsehl Mar 14, 2023
a754e2b
Fix image sizing demo store
benjaminsehl Mar 14, 2023
5c22196
Update product card size
benjaminsehl Mar 14, 2023
a812593
Update demo store, improve image srcset for fixed width
benjaminsehl Mar 14, 2023
133433b
Updates tests and starting config
benjaminsehl Mar 14, 2023
0975081
Add crop to placeholder image
benjaminsehl Mar 14, 2023
c0b539d
remove sizes from fixed width image
benjaminsehl Mar 14, 2023
c73adeb
adds changesets
benjaminsehl Mar 14, 2023
90c4de4
fix development env only tests
cartogram Mar 14, 2023
68a09c5
Fixes __HYDROGEN_DEV__
benjaminsehl Mar 15, 2023
1aef9e6
Use Component notation instead of React.createElement
benjaminsehl Mar 15, 2023
d06c228
Tries to fix TS stuff
benjaminsehl Mar 15, 2023
1ec83dd
Tries to fix TS stuff, part 2
benjaminsehl Mar 16, 2023
8172bf1
Refactor
benjaminsehl Mar 17, 2023
0ccc4dc
Fix checks
benjaminsehl Mar 17, 2023
3956df7
Merge branch '2023-04-POC' into new-image
benjaminsehl Apr 6, 2023
566ef57
Delete ImageLegacy and remove old references
benjaminsehl Apr 6, 2023
4227e72
Remove test utils
benjaminsehl Apr 12, 2023
d001ad9
Fixes errors with test-utils
benjaminsehl Apr 12, 2023
4df5c29
Static ProcessEnv type, and adds details to changeset
benjaminsehl Apr 12, 2023
8f8dec7
Removes node types from devDependencies
benjaminsehl Apr 12, 2023
7170955
Remove test utils
benjaminsehl Apr 12, 2023
d11da0a
Removes unneeded prop, updates package lock file
benjaminsehl Apr 12, 2023
e83d11a
Change to work off 2023-04
benjaminsehl Apr 12, 2023
3b32aa8
Removes width and height as specified inline styles
benjaminsehl Apr 12, 2023
6fc38a0
Updates changeset
benjaminsehl Apr 12, 2023
eada9a3
Update image size on demo store cart line item
benjaminsehl Apr 12, 2023
a648c50
PDP css fix
benjaminsehl Apr 12, 2023
0a051e3
Merge branch '2023-04-POC' into new-image
lordofthecactus Apr 13, 2023
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/five-chefs-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/hydrogen-react': patch
---

Adds an all-new responsive Image component
Comment thread
benjaminsehl marked this conversation as resolved.
Outdated
427 changes: 305 additions & 122 deletions packages/hydrogen-react/docs/generated/generated_docs_data.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/hydrogen-react/src/Image.doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const data: ReferenceEntityTemplateSchema = {
},
],
description:
"The `Image` component renders an image for the Storefront API's\n[Image object](https://shopify.dev/api/storefront/reference/common-objects/image) by using the `data` prop. You can [customize this component](https://shopify.dev/api/hydrogen/components#customizing-hydrogen-components) using passthrough props.\n\nAn image's width and height are determined using the following priority list:\n1. The width and height values for the `loaderOptions` prop\n2. The width and height values for bare props\n3. The width and height values for the `data` prop\n\nIf only one of `width` or `height` are defined, then the other will attempt to be calculated based on the image's aspect ratio,\nprovided that both `data.width` and `data.height` are available. If `data.width` and `data.height` aren't available, then the aspect ratio cannot be determined and the missing value will remain as `null`",
"The `Image` component renders an image for the Storefront API's\n[Image object](https://shopify.dev/api/storefront/reference/common-objects/image) by using the `data` prop. You can [customize this component](https://shopify.dev/api/hydrogen/components#customizing-hydrogen-components) using passthrough props.\n\nImages default to being responsive automativally (`width: 100%, height: auto`), and expect an `aspectRatio` prop, which ensures your image doesn't create any layout shift. For fixed-size images, you can set `width` to an exact value, and a `srcSet` with 1x, 2x, and 3x DPI variants will automatically be generated for you.",
type: 'component',
defaultExample: {
description: 'I am the default example',
Expand Down
22 changes: 20 additions & 2 deletions packages/hydrogen-react/src/Image.example.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
import {Image} from '@shopify/hydrogen-react';
import {Image, IMAGE_FRAGMENT} from '@shopify/hydrogen-react';

// An example query that includes the image fragment
const IMAGE_QUERY = `#graphql
${IMAGE_FRAGMENT}
query {
product {
featuredImage {
...Image
}
}
}
`;

export default function ProductImage({product}) {
const image = product.featuredImage;
Expand All @@ -7,5 +19,11 @@ export default function ProductImage({product}) {
return null;
}

return <Image data={image} />;
return (
<Image
data={image}
sizes="(min-width: 45em) 50vw, 100vw"
aspectRatio="4/5"
/>
);
}
23 changes: 21 additions & 2 deletions packages/hydrogen-react/src/Image.example.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
import {Image} from '@shopify/hydrogen-react';
import React from 'react';
import {Image, IMAGE_FRAGMENT} from '@shopify/hydrogen-react';
import type {Product} from '@shopify/hydrogen-react/storefront-api-types';

// An example query that includes the image fragment
const IMAGE_QUERY = `#graphql
${IMAGE_FRAGMENT}
query {
product {
featuredImage {
...Image
}
}
}
`;

export default function ProductImage({product}: {product: Product}) {
const image = product.featuredImage;

if (!image) {
return null;
}

return <Image data={image} />;
return (
<Image
data={image}
sizes="(min-width: 45em) 50vw, 100vw"
aspectRatio="4/5"
/>
);
}
105 changes: 71 additions & 34 deletions packages/hydrogen-react/src/Image.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,82 @@
import * as React from 'react';
import type {Story} from '@ladle/react';
import {Image, type ShopifyImageProps} from './Image.js';
import {IMG_SRC_SET_SIZES} from './image-size.js';
import {Image, ShopifyLoaderOptions, LoaderParams} from './Image.js';
import type {PartialDeep} from 'type-fest';
import type {Image as ImageType} from './storefront-api-types.js';

type Crop = 'center' | 'top' | 'bottom' | 'left' | 'right';

type ImageConfig = {
intervals: number;
startingWidth: number;
incrementSize: number;
placeholderWidth: number;
};

type HtmlImageProps = React.ImgHTMLAttributes<HTMLImageElement>;

const Template: Story<{
'data.url': ShopifyImageProps['data']['url'];
'data.width': ShopifyImageProps['data']['width'];
'data.height': ShopifyImageProps['data']['height'];
width: ShopifyImageProps['width'];
height: ShopifyImageProps['height'];
widths: ShopifyImageProps['widths'];
loaderOptions: ShopifyImageProps['loaderOptions'];
as?: 'img' | 'source';
data?: PartialDeep<ImageType, {recurseIntoArrays: true}>;
loader?: (params: LoaderParams) => string;
src: string;
width?: string | number;
height?: string | number;
crop?: Crop;
sizes?: string;
aspectRatio?: string;
config?: ImageConfig;
alt?: string;
loading?: 'lazy' | 'eager';
loaderOptions?: ShopifyLoaderOptions;
widths?: (HtmlImageProps['width'] | ImageType['width'])[];
}> = (props) => {
const finalProps: ShopifyImageProps = {
data: {
url: props['data.url'],
width: props['data.width'],
height: props['data.height'],
id: 'testing',
},
width: props.width,
height: props.height,
widths: props.widths,
loaderOptions: props.loaderOptions,
};
return <Image {...finalProps} />;
return (
<>
{/* Standard Usage */}
<Image
{...props}
width="100%"
loading="eager"
aspectRatio="1/1"
sizes="100vw"
/>
{/* */}
<Image
{...props}
src="https://cdn.shopify.com/s/files/1/0551/4566/0472/products/Main.jpg"
width="100%"
aspectRatio="1/1"
sizes="100vw"
/>
<Image {...props} sizes="50vw" width="50vw" />
<Image {...props} aspectRatio="4/3" width="50vw" sizes="50vw" />
<Image {...props} width="30vw" sizes="30vw" />
<Image {...props} width={100} height={200} />
<Image {...props} width="5rem" />
<Image {...props} widths={[100, 200, 300]} />
<Image
{...props}
loaderOptions={{
crop: 'center',
}}
widths={[200, 300]}
/>
<Image
sizes="100vw"
src="https://cdn.shopify.com/s/files/1/0551/4566/0472/products/Main.jpg"
widths={[100, 200, 300]}
/>
</>
);
};

export const Default = Template.bind({});
Default.args = {
'data.url':
'https://cdn.shopify.com/s/files/1/0551/4566/0472/products/Main.jpg',
'data.width': 100,
'data.height': 100,
width: 500,
height: 500,
widths: IMG_SRC_SET_SIZES,
loaderOptions: {
crop: 'center',
scale: 2,
width: 500,
height: 500,
data: {
url: 'https://cdn.shopify.com/s/files/1/0551/4566/0472/products/Main.jpg',
altText: 'alt text',
width: 3908,
height: 3908,
},
};
Loading