-
Notifications
You must be signed in to change notification settings - Fork 123
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
feature: use Element.computedStyleMap()
#2143
base: master
Are you sure you want to change the base?
feature: use Element.computedStyleMap()
#2143
Conversation
@@ -41,6 +41,7 @@ | |||
"devDependencies": { | |||
"@arethetypeswrong/cli": "0.15.4", | |||
"@types/qunit": "2.19.10", | |||
"@typescript/lib-dom": "npm:@types/[email protected]", |
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.
https://www.npmjs.com/package/@types/web for the new Web APIs
type CSSStyleDeclarationProperty = keyof CSSStyleDeclaration; | ||
|
||
type ActualCSSStyleDeclaration = Partial<Record<CSSStyleDeclarationProperty, unknown>>; |
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 don't think these types provide much value as they are only used internally, with a type assertion..
I think these types are better represented as a plain record, eg; Record<string, string>
let computedStyle = getComputedStyle(element, selector); | ||
let expectedProperties = Object.keys(expected); |
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.
🗑️ the type assertion
property => | ||
(computedStyle.getPropertyValue(property.toString()) || computedStyle[property]) === | ||
expected[property] | ||
property => computedStyle[property] === expected[property] |
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.
camelCase
and kebab-case
properties are handled by getComputedStyle()
if (!selector && 'computedStyleMap' in element) { | ||
computedStyleMap = element.computedStyleMap(); | ||
} |
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.
Element.computedStyleMap()
doesn't work with with pseudo-elements, so it should fallback to window.getComputedStyle()
in those cases
return ( | ||
value || | ||
computedStyle.getPropertyValue(property.toString()) || | ||
Reflect.get(computedStyle, property) | ||
); |
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.
Maintains the previous logic of
computedStyle.getPropertyValue(property.toString()) || computedStyle[property]
Uses
Element.computedStyleMap()
to evaluate element styles when it is available. Firefox is the only browser that hasn't implemented CSS Typed OM https://ishoudinireadyyet.com/I was running into issues where
hasStyle()
was returning outdated values. Specifically when an element had a cssheight: 100px
and JavaScript that updated it with an inline style ofheight: 85%
, it would return the oldheight: 100px
value