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

fix(common): Warn on fill ngOptimizedImage without height #48036

Closed
wants to merge 2 commits into from

Conversation

atcastle
Copy link
Contributor

This PR includes one final fix for the ngOptimizedImage directive. Currently it does not warn if the image is in fill mode and the rendered height of the image is 0. This is a fairly common failure case when starting out with the image directive, which is caused by not properly setting the position style of the parent element.

The warning added in this PR will help new users diagnose why their fill image is not appearing on the page.

Note: I am currently unable to run (and therefor unable to author) e2e tests on my machine, and this feature can only be tested in an e2e context. @AndrewKushnir, @pkozlowski-opensource or @kara, could one of you help me get an e2e test added for this change? It would follow the basic pattern used in packages/core/test/bundling/image-directive/e2e/image-distortion/image-distortion.e2e-spec.ts

Add a warning if a fill-mode image is rendered without height. This is a common occurence if the user doesn't properly set the 'position' attribute of the parent element.
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

dir: NgOptimizedImage, img: HTMLImageElement, renderer: Renderer2) {
const removeListenerFn = renderer.listen(img, 'load', () => {
removeListenerFn();
const renderedHeight = parseFloat(img.clientHeight as any);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: can we replace any with a more specific type? e.g. string|null

Copy link
Member

Choose a reason for hiding this comment

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

more importantly: why do we need parseFloat ? Seems like the type of HTMLImageElement:clientHeight is a number already: https://www.typescriptlang.org/play?#code/MYewdgzgLgBAlgWwOYwLwzAVwDbZgQwgLAE8CiAJAFQFkAZASQXyQFMBRbVhVsKAbgBQg0JFgALNPGQA6YNji8oFVnCTiBwgA74AThFYAxbCHxQAFOICU-IA

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I think the parseFloat pattern got pasted around in a few places where it's not needed. I just added a cleanup task to the tracker to go back through and review typing and usages of parseFloat().

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the review Pawel! Since Alex added a cleanup item to the tracking doc, I'm moving this PR to the merge queue, so we include the fix into the v15 release.

@AndrewKushnir
Copy link
Contributor

Presubmit.

@AndrewKushnir AndrewKushnir added action: review The PR is still awaiting reviews from at least one requested reviewer area: common Issues related to APIs in the @angular/common package target: rc This PR is targeted for the next release-candidate common: image directive labels Nov 11, 2022
@ngbot ngbot bot modified the milestone: Backlog Nov 11, 2022
@ngbot ngbot bot modified the milestones: v15 final blockers, Backlog Nov 11, 2022
…edImage directive

This commit adds extra e2e tests for the fill mode checks in the NgOptimizedImage directive to make
sure a warning is logged in a console.
@pkozlowski-opensource pkozlowski-opensource added action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Nov 14, 2022
@pkozlowski-opensource
Copy link
Member

LGTM but left a cleanup comment.

@atcastle
Copy link
Contributor Author

LGTM! Thanks for your help with the e2e tests

@AndrewKushnir AndrewKushnir added action: merge The PR is ready for merge by the caretaker and removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Nov 14, 2022
@jessicajaniuk
Copy link
Contributor

This PR was merged into the repository by commit d6dc6b6.

jessicajaniuk pushed a commit that referenced this pull request Nov 14, 2022
…edImage directive (#48036)

This commit adds extra e2e tests for the fill mode checks in the NgOptimizedImage directive to make
sure a warning is logged in a console.

PR Close #48036
jessicajaniuk pushed a commit that referenced this pull request Nov 14, 2022
Add a warning if a fill-mode image is rendered without height. This is a common occurence if the user doesn't properly set the 'position' attribute of the parent element.

PR Close #48036
jessicajaniuk pushed a commit that referenced this pull request Nov 14, 2022
…edImage directive (#48036)

This commit adds extra e2e tests for the fill mode checks in the NgOptimizedImage directive to make
sure a warning is logged in a console.

PR Close #48036
@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 Dec 15, 2022
trekladyone pushed a commit to trekladyone/angular that referenced this pull request Feb 1, 2023
)

Add a warning if a fill-mode image is rendered without height. This is a common occurence if the user doesn't properly set the 'position' attribute of the parent element.

PR Close angular#48036
trekladyone pushed a commit to trekladyone/angular that referenced this pull request Feb 1, 2023
…edImage directive (angular#48036)

This commit adds extra e2e tests for the fill mode checks in the NgOptimizedImage directive to make
sure a warning is logged in a console.

PR Close angular#48036
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 common: image directive target: rc This PR is targeted for the next release-candidate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants