-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
report(html): replace Typed OM getComputedStyle() with CSSOM equivalent #5984
Conversation
@@ -130,17 +130,21 @@ class ReportUIFeatures { | |||
|
|||
_setupHeaderAnimation() { | |||
const scoresWrapper = this._dom.find('.lh-scores-wrapper', this._document); | |||
this.headerOverlap = /** @type {number} */ | |||
(Number(window.getComputedStyle(scoresWrapper).marginTop.replace('px', ''))); | |||
const computedMarginTop = scoresWrapper.style.marginTop ? |
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.
computedStyle should get you the value even if an inline style is present so i'm not sure why this is necessary. whatcha thinkin?
const computedMarginTop = scoresWrapper.style.marginTop ? | ||
window.getComputedStyle(scoresWrapper).marginTop : '0px'; | ||
this.headerOverlap = computedMarginTop ? | ||
/** @type {number} */ (Number(computedMarginTop.replace('px', ''))) : 0; |
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 know you're copying what was here but parseFloat(computedMarginTop)
should do the same. (unless there's some tsc thing i'm not thinking of).
could even do parseFloat(computedMarginTop|| 0)
to drop your ternary?
this.headerHeight = /** @type {number} */ | ||
(Number(window.getComputedStyle(this.headerBackground).height.replace('px', ''))); | ||
const computedHeight = this.headerBackground.style.height ? | ||
window.getComputedStyle(this.headerBackground).height : '0px'; |
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.
2 above comments apply here too.
I appreciate your feedback @paulirish |
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.
lg! thx
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.
Last feedback :)
(scoresWrapper.computedStyleMap().get('margin-top').value); | ||
|
||
const computedMarginTop = window.getComputedStyle(scoresWrapper).marginTop; | ||
this.headerOverlap = /** @type {number} */ (parseFloat(computedMarginTop || '0')); |
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.
parseFloat returns a number, so no longer need to cast
// @ts-ignore - TODO: move off CSSOM to support other browsers | ||
this.headerHeight = this.headerBackground.computedStyleMap().get('height').value; | ||
const computedHeight = window.getComputedStyle(this.headerBackground).height; | ||
this.headerHeight = /** @type {number} */ (parseFloat(computedHeight || '0')); |
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.
Same
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.
Looks great!
Summary
Replace Typed OM's
element.getComputedStyle()
with CSSOMwindow.getComputedStyle()
Typed OM is not widely supported yet. Our delivered JS should work properly in all major browsers
https://developers.google.com/web/updates/2018/03/cssom#computed
Related Issues/PRs
fixes #5966
Yeah, this replacement looks a bit hacky. But I've not found a more elegant way