feat(common): Add fill mode to NgOptimizedImage#47738
feat(common): Add fill mode to NgOptimizedImage#47738atcastle wants to merge 1 commit intoangular:mainfrom
Conversation
aio/content/guide/image-directive.md
Outdated
There was a problem hiding this comment.
(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
AndrewKushnir
left a comment
There was a problem hiding this comment.
LGTM 👍
I've left some comments, but they can all be addressed in a followup PR.
| private _disableOptimizedSrcset = false; | ||
|
|
||
| /** | ||
| * Sets the image to "fill mode," which eliminates the height/width requirement and adds |
There was a problem hiding this comment.
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.
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
sizesas100vw(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.
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.`); |
AndrewKushnir
left a comment
There was a problem hiding this comment.
Reviewed-for: public-api
|
Merge-assistance (cc @jessicajaniuk):
Overall status (after |
jessicajaniuk
left a comment
There was a problem hiding this comment.
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
fillwhich does the following:sizesvalue of100vwwhich will cause the image to have a responsive srcset automatically generatedCC: @AndrewKushnir @pkozlowski-opensource @kara