Skip to content

Conversation

@frehner
Copy link
Contributor

@frehner frehner commented May 13, 2022

Description

Closes #1223 and massively reworks the internal implementation of the Image component

Removes useImageUrl in favor of just calling the function shopifyImageLoader - it's actually probably faster / better, and it can also be run on the client and the server, unlike useImageUrl which can only be run on the client.

imageLoader now takes in an object instead of positional params, which makes it easy to only use what you care to use and skip the rest.

The TypeScript experience is improved; if you're using the data prop, you're using a SFAPI source and so the loader and loaderOptions will be typed for that. If you're using the src prop, we use a generic to infer the props you pass in to loaderOptions and apply that to the loader param. Also, when using src, width and height are required props.

Hopefully cleaned up some side effects in the Image tests that weren't being cleaned up before, but I'm still running into ts-jest issues that I can't figure out.

<Video/> props options turns into imagePreviewOptions to be more explicit what the options are for.

addImageSizeParametersToUrl function refactored to just use URL's .searchParams.append to make the logic much easier.

Despite the recent change to add the priority prop in #1245 , I decided it didn't make sense to essentially rename what is already a standardized HTML attribute, so I removed priority and changed the logic to make loading the source of truth.

Additional context

Todos before this is ready:

  • Update docs to match the above changes
  • Add a changelog and migration guide
  • Figure out why tf ts-jest is having issues parsing some tests

Before submitting the PR, please make sure you do the following:

  • Read the Contributing Guidelines
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123)
  • Update docs in this repository according to your change
  • Run yarn changeset add if this PR cause a version bump based on Keep a Changelog and adheres to Semantic Versioning

lib: ['ESNext', 'DOM'],
target: 'es6',
},
tsconfig: './packages/hydrogen/tsconfig.json',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the weird ts-jest issues but I don't know if it has other side effects or not??

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shall see!

@frehner frehner marked this pull request as ready for review May 13, 2022 22:30
@frehner frehner requested a review from a team May 13, 2022 22:31
Copy link
Contributor

@mcvinci mcvinci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for this, @frehner! I added a few suggestions throughout to align the code comments with the new descriptions in the reference doc.

| height | <code>height</code> | The integer value for the height of the image. This is a required prop when `src` is present. |
| loader? | <code>(props: ImageLoaderOptions): string</code> | A custom function that generates the image URL. Parameters passed into this function includes `src` and an `options` object that contains the provided `width`, `height` and `loaderOptions` values. |
| loaderOptions? | <code>ImageLoaderOptions['options']</code> | An object of `loader` function options. For example, if the `loader` function requires a `scale` option, then the value can be a property of the `loaderOptions` object (for example, `{scale: 2}`). |
| priority? | <code>boolean</code> | Whether the image will be immediately loaded. Defaults to `false`. This prop should be used only when the image is visible above the fold. For more information, refer to the [Image Embed element's loading attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-loading). |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should priority? be retained?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope it's gone

| `height` | A string of the pixel height (for example, `100px`) or `original` for the original height of the image. |
| `crop` | Valid values: `top`, `bottom`, `left`, `right`, or `center`. |
| `scale` | Valid values: 2 or 3. |
| `format` | Valid values: `jpg` or `pjpg`. |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just double-checking! Is this no longer an option?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct

Copy link
Contributor

@jplhomer jplhomer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Terrific work, Anthony!


it('renders an `img` element without `width` and `height` attributes when invalid dimensions are provided', () => {
// eslint-disable-next-line jest/no-focused-tests
it.only('renders an `img` element without `width` and `height` attributes when invalid dimensions are provided', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heads-up: looks like we need to remove it.only here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops nice catch!

width && newUrl.searchParams.append('width', width.toString());
height && newUrl.searchParams.append('height', height.toString());
crop && newUrl.searchParams.append('crop', crop);
scale && newUrl.searchParams.append('scale', scale.toString());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so nice!

Also cleaned up other tests from outputting to the console.

Updated migration docs a tiny bit
@frehner
Copy link
Contributor Author

frehner commented May 16, 2022

@mcvinci just did a small documentation update, you can more easily see the changes I made by looking at this specific commit

Fixed the tests, and also mocked out console.log / console.error stuff that was polluting our test output. Once tests pass, I'll merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

<Image> component improvements

3 participants