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

feat(common): Add fill mode to NgOptimizedImage #47738

Closed
wants to merge 1 commit into from

Conversation

atcastle
Copy link
Contributor

This PR adds a new boolean attribute to NgOptimizedImage called fill which does the following:

  • Removes the requirement for height and width
  • Adds inline styling to cause the image to fill its containing element
  • Adds a default sizes value of 100vw which will cause the image to have a responsive srcset automatically generated

CC: @AndrewKushnir @pkozlowski-opensource @kara

@angular-robot angular-robot bot added the feature Issue that requests a new feature label Oct 11, 2022
Copy link
Contributor

@kara kara left a comment

Choose a reason for hiding this comment

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

LGTM


In cases where you want to have an image fill a containing element, you can use the `fill` attribute. This is often useful when you want to achieve a "background image" behavior, or when you don't know the exact width and height of your image.

When you add the `fill` attribute to your image, you do not need and should not include a `width` and `height`, as in this example:
Copy link
Contributor

Choose a reason for hiding this comment

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

(can be follow up PR): can we add something here about why width and height aren't necessary? e.g. that we usually require them to prevent layout shift but here it's unnecessary

@jessicajaniuk jessicajaniuk added the area: common Issues related to APIs in the @angular/common package label Oct 11, 2022
@ngbot ngbot bot added this to the Backlog milestone Oct 11, 2022
@atcastle atcastle force-pushed the fill-mode-2 branch 2 times, most recently from cf716ca to aab6ef8 Compare October 11, 2022 22:00
Add a new boolean attribute to NgOptimizedImage called `fill` which does the following:
* Removes the requirement for height and width
* Adds inline styling to cause the image to fill its containing element
* Adds a default `sizes` value of `100vw` which will cause the image to have a responsive srcset automatically generated
@jessicajaniuk jessicajaniuk added the target: major This PR is targeted for the next major release label Oct 11, 2022
Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

LGTM 👍

I've left some comments, but they can all be addressed in a followup PR.

@@ -330,6 +336,19 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {
}
private _disableOptimizedSrcset = false;

/**
* Sets the image to "fill mode," which eliminates the height/width requirement and adds
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: typo, let's fix it in a followup PR.

Suggested change
* Sets the image to "fill mode," which eliminates the height/width requirement and adds
* Sets the image to "fill mode", which eliminates the height/width requirement and adds

Comment on lines 407 to +416
// Must set width/height explicitly in case they are bound (in which case they will
// only be reflected and not found by the browser)
this.setHostAttribute('width', this.width!.toString());
this.setHostAttribute('height', this.height!.toString());
if (this.fill) {
if (!this.sizes) {
this.sizes = '100vw';
}
} else {
this.setHostAttribute('width', this.width!.toString());
this.setHostAttribute('height', this.height!.toString());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I think this section would require some extra comments (and updates to existing ones):

  • the width/height comment can be updated to better reflect the logic
  • it's be great to add a comment on why we define sizes as 100vw (related question: what happens when the sizes are defined differently by a user?)
  • we could also add a quick comment above the code that sets width/height and explain why we don't do it for "fill" mode

We could do it in a followup PR as well.

imgDirectiveDetails(
dir.ngSrc)} the attributes \`height\` and/or \`width\` are present ` +
`along with the \`fill\` attribute. Because \`fill\` mode causes an image to fill its containing ` +
`element, the size attributes have no effect and should be removed.`);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: the size might be confused with the sizes input, so we can rephrase this a bit:

Suggested change
`element, the size attributes have no effect and should be removed.`);
`element, the width/height attributes have no effect and should be removed.`);

Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

Reviewed-for: public-api

@AndrewKushnir AndrewKushnir added action: merge The PR is ready for merge by the caretaker merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note labels Oct 12, 2022
@AndrewKushnir
Copy link
Contributor

Merge-assistance (cc @jessicajaniuk):

  • The PR was reviewed and approved, the only missing item is one more pubic-api approval. Could you please take a quick look at the changes from the pubic-api perspective (the changes are pretty small, just an extra @Input)?
  • There is some minor feedback (mostly docs-related), which we'll take care of in a followup PR.

Overall status (after public-api approval) of this PR: ready for merge.

Copy link
Contributor

@jessicajaniuk jessicajaniuk left a comment

Choose a reason for hiding this comment

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

reviewed-for: public-api

@jessicajaniuk jessicajaniuk added merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note and removed merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note labels Oct 12, 2022
@jessicajaniuk
Copy link
Contributor

This PR was merged into the repository by commit 9483343.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Nov 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: common Issues related to APIs in the @angular/common package feature Issue that requests a new feature merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants