-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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
[draft] Add 'fill mode' to NgOptimizedImage #47727
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -268,6 +268,12 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy { | |
*/ | ||
@Input() sizes?: string; | ||
|
||
/** | ||
* The base `sizes` attribute passed through to the `<img>` element. | ||
* Providing sizes causes the image to create an automatic responsive srcset. | ||
*/ | ||
@Input() style?: string; | ||
|
||
/** | ||
* The intrinsic width of the image in pixels. | ||
*/ | ||
|
@@ -326,6 +332,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 | ||
* styles such that the image fills its containing element. | ||
*/ | ||
@Input() | ||
set fill(value: string|boolean|undefined) { | ||
this._fill = inputToBoolean(value); | ||
} | ||
get fill(): boolean { | ||
return this._fill; | ||
} | ||
private _fill = false; | ||
|
||
/** | ||
* Value of the `src` attribute if set on the host `<img>` element. | ||
* This input is exclusively read to assert that `src` is not set in conflict | ||
|
@@ -352,7 +371,11 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy { | |
} | ||
assertNotBase64Image(this); | ||
assertNotBlobUrl(this); | ||
assertNonEmptyWidthAndHeight(this); | ||
if (this.fill) { | ||
assertEmptyWidthAndHeight(this); | ||
} else { | ||
assertNonEmptyWidthAndHeight(this); | ||
} | ||
assertValidLoadingInput(this); | ||
assertNoImageDistortion(this, this.imgElement, this.renderer); | ||
if (!this.ngSrcset) { | ||
|
@@ -379,8 +402,15 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy { | |
private setHostAttributes() { | ||
// 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) { | ||
this.setHostAttribute('style', this.getFillStyle()) | ||
} else { | ||
this.setHostAttribute('width', this.width!.toString()); | ||
this.setHostAttribute('height', this.height!.toString()); | ||
if (this.style) { | ||
this.setHostAttribute('style', this.style) | ||
} | ||
} | ||
|
||
this.setHostAttribute('loading', this.getLoadingBehavior()); | ||
this.setHostAttribute('fetchpriority', this.getFetchPriority()); | ||
|
@@ -397,6 +427,27 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy { | |
} | ||
} | ||
|
||
private getFillStyle(): string { | ||
const fillStyle = { | ||
position: 'absolute', | ||
width: '100%', | ||
height: '100%', | ||
left: '0', | ||
top: '0', | ||
right: '0', | ||
bottom: '0' | ||
} let providedStyles: {[key: string]: string|number} = {} if (this.style) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like some semicolons are missing, thus the formatting is broken. |
||
this.style.split(';').forEach(styleKVP => { | ||
const [key, value] = styleKVP.split(':') | ||
if (key && value) { | ||
providedStyles[key.trim()] = value.trim(); | ||
} | ||
}) | ||
} | ||
const combinedStyles = Object.assign(providedStyles, fillStyle); | ||
return Object.entries(combinedStyles).map(kvp => `${kvp[0]}: ${kvp[1]}`).join('; '); | ||
} | ||
|
||
ngOnChanges(changes: SimpleChanges) { | ||
if (ngDevMode) { | ||
assertNoPostInitInputChange( | ||
|
@@ -787,6 +838,22 @@ function assertNonEmptyWidthAndHeight(dir: NgOptimizedImage) { | |
} | ||
} | ||
|
||
/** | ||
* Verifies that width and height are not set. Used in fill mode, where those attributes don't make | ||
* sense. | ||
*/ | ||
function assertEmptyWidthAndHeight(dir: NgOptimizedImage) { | ||
if (dir.width || dir.height) { | ||
throw new RuntimeError( | ||
RuntimeErrorCode.INVALID_INPUT, | ||
`${ | ||
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.`); | ||
} | ||
} | ||
|
||
/** | ||
* Verifies that the `loading` attribute is set to a valid input & | ||
* is not used on priority images. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think it'd be great to avoid custom parsing of styles and writing them directly to the DOM via renderer.
The "style" attribute values are handled by the framework and it takes into account various use-cases which might be broken because of writing to the DOM directly.
Instead, we can try to lean on the framework functionality and set extra styles via host binding, similar to this example:
https://github.com/angular/components/blob/f9583184d6ca487970ee70a1c05cec6e36f3e18c/src/material/legacy-progress-spinner/progress-spinner.ts#L100-L101
This would delegate the styling processing, overrides and handing of the "style" attribute bindings to the framework. WDYT?
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.
(I think this is actually a necessity, otherwise this call would clobber user-applied
style
bindings).