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
Conversation
There was a problem hiding this 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: |
There was a problem hiding this comment.
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
cf716ca
to
aab6ef8
Compare
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
There was a problem hiding this 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 |
There was a problem hiding this comment.
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.
* 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 |
// 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()); | ||
} |
There was a problem hiding this comment.
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
as100vw
(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.`); |
There was a problem hiding this comment.
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:
`element, the size attributes have no effect and should be removed.`); | |
`element, the width/height attributes have no effect and should be removed.`); |
There was a problem hiding this 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
Merge-assistance (cc @jessicajaniuk):
Overall status (after |
There was a problem hiding this 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
This PR was merged into the repository by commit 9483343. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
This PR adds a new boolean attribute to NgOptimizedImage called
fill
which does the following:sizes
value of100vw
which will cause the image to have a responsive srcset automatically generatedCC: @AndrewKushnir @pkozlowski-opensource @kara