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

Improved Image API #1956

Merged
merged 13 commits into from
Nov 12, 2019
Merged

Improved Image API #1956

merged 13 commits into from
Nov 12, 2019

Conversation

supernova-at
Copy link
Contributor

@supernova-at supernova-at commented Nov 4, 2019

Description

This PR improves the Image component API. Specifically, it:

  • No longer restricts breakpoints / image widths to three ("small", "medium", "large") sizes only
  • Renames resourceSizes / resourceSizeBreakpoints to widths and makes it a Map
  • Removes resourceWidth in favor of width (or the default entry in widths, if not present)
  • Removes resourceHeight in favor of height

Related Issue

Closes JIRA-90.

Acceptance

Verification Stakeholders

@zetlen @jimbo

Specification

Verification Steps

📝 This is just a code refactor. There should be no impact on the UI.

  1. Ensure images work correctly throughout the application

Screenshots / Screen Captures (if appropriate)

Checklist

  • I have updated the documentation accordingly, if necessary.
  • I have added tests to cover my changes, if necessary.

@PWAStudioBot
Copy link
Contributor

PWAStudioBot commented Nov 4, 2019

Messages
📖

Access a deployed version of this PR here. Make sure to wait for the "pwa-pull-request-deploy" job to complete.

📖 DangerCI Failures related to missing labels/description/linked issues/etc will persist until the next push or next nightly build run (assuming they are fixed).
📖

Associated JIRA tickets: JIRA-90.

Generated by 🚫 dangerJS against 533dfca

@revanth0212
Copy link
Contributor

@supernova-at there are prettier fails. Check on that once. I am not sure how did it miss the git commit hook when you pushed them.

if (propResourceWidth) {
return propResourceWidth;
if (!widths) {
return undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this not be any default value returned if widths is not provided?

Copy link
Contributor

Choose a reason for hiding this comment

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

It is odd that this would be undefined. I would assume that the components that care about this require it, right? We definitely don't want resourceImage throwing when it attempts to get widths[index] here.

Maybe instead of having the useImage talon do this work you could just pass the widths array to the components that use it and have them do this work. We're already talking about potentially splitting them anyways so that components can directly import resource or simple image rather than Image.

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 is a little shortcut if the caller doesn't pass a width but does pass widths.

If they don't pass either one, we literally don't have any information to go off of - undefined seemed the best choice here. This will result in the img element rendered to the DOM to not have a width attribute and the network request to fetch this image won't include resize parameters.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I realized it wasn't preferring the passed-in width so I updated that in 4e798f7.

packages/peregrine/lib/talons/Image/useResourceImage.js Outdated Show resolved Hide resolved
@supernova-at supernova-at added the version: Major This changeset includes incompatible API changes and its release necessitates a Major version bump. label Nov 4, 2019
Copy link
Contributor

@sirugh sirugh left a comment

Choose a reason for hiding this comment

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

Renaming is good 👍 for that.

However, I don't think an array is the right structure for maintaining the links between widths and breakpoints. I think a map fits this use case perfectly and would really simplify the logic in the sizes memo in useResourceImage.

const sizes = new Map();
sizes.set('default', 150)
sizes.set(100, 150);
sizes.set(200, 250);

const widths = [];
for (const [breakpoint, size] of sizes) {
  if (breakpoint !== 'default') {
    widths.push(`(max-width: ${breakpoint}px) ${size}px`);
  }
}

widths.push(`${sizes.get('default')}px`)
console.log(widths.join(', '))
// (max-width: 100px) 150px, (max-width: 200px) 250px, 150px

@jimbo
Copy link
Contributor

jimbo commented Nov 5, 2019

However, I don't think an array is the right structure for maintaining the links between widths and breakpoints. I think a map fits this use case perfectly and would really simplify the logic in the sizes memo in useResourceImage.

Good suggestion. 👍

@supernova-at
Copy link
Contributor Author

On it! 🖖

@supernova-at
Copy link
Contributor Author

PR updated with new Map structure.

I named the prop widths instead of sizes to avoid confusion with the actual sizes attribute of an image (which we generate here but might be passed in by a caller).

Copy link
Contributor

@jimbo jimbo left a comment

Choose a reason for hiding this comment

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

Good changes. 👍

@@ -5,16 +5,12 @@ 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 {Map} props.resourceSizes image sizes used by the browser to select the image source. Supported keys are 'small', 'medium', and 'large'.
* @param {number} props.resourceWidth the intrinsic width of the image & the width to request for the fallback image for browsers that don't support srcset / sizes.
* @param {string} props.unconstrainedSizeKey the key in props.widths for the unconstrained / default width.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is pretty weird. Is there a reason why we can't just let "default" be the key here?

Copy link
Contributor Author

@supernova-at supernova-at Nov 7, 2019

Choose a reason for hiding this comment

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

It is always "default", I just didn't want every file to have to know that.

I agree it is weird though. I'm not married to the idea.

Copy link
Contributor Author

@supernova-at supernova-at Nov 7, 2019

Choose a reason for hiding this comment

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

The other way I thought of doing it was importing the key but then I ran into circular dependencies and Peregrine talons needing to import Venia UI files.

Maybe it could live on the useImage talon, I'll try that on for size.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, way better: 14fca61.

Copy link
Contributor

Choose a reason for hiding this comment

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

The other way I thought of doing it was importing the key but then I ran into circular dependencies and Peregrine talons needing to import Venia UI files.

Right, the dependency should flow in one direction; we shouldn't have any situations where peregrine imports from venia-ui.

@supernova-at
Copy link
Contributor Author

supernova-at commented Nov 7, 2019

PR Updated:

  • No longer threads the UNCONSTRAINED_SIZE_KEY
  • Simplifies most common use case: have a width but no widths. sizes will just use the given width as the unconstrained size. Ex: width=100 => sizes="100px".

@jimbo
Copy link
Contributor

jimbo commented Nov 7, 2019

PR Updated:

* No longer threads the `UNCONSTRAINED_SIZE_KEY`

* Simplifies most common use case: have a `width` but no `widths`. `sizes` will just use the given `width` as the unconstrained size. Ex: `width=100 => sizes="100px"`.

Excellent. 💯

Copy link
Contributor

@jimbo jimbo left a comment

Choose a reason for hiding this comment

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

Approved from my end. Will wait for @sirugh to approve as well.

@dpatil-magento dpatil-magento merged commit 872ab78 into develop Nov 12, 2019
@dpatil-magento dpatil-magento deleted the supernova/90_improved_image_api branch November 12, 2019 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg:peregrine pkg:venia-ui version: Major This changeset includes incompatible API changes and its release necessitates a Major version bump.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants