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

feature: use Element.computedStyleMap() #2143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

BarryThePenguin
Copy link
Contributor

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 css height: 100px and JavaScript that updated it with an inline style of height: 85%, it would return the old height: 100px value

@@ -41,6 +41,7 @@
"devDependencies": {
"@arethetypeswrong/cli": "0.15.4",
"@types/qunit": "2.19.10",
"@typescript/lib-dom": "npm:@types/[email protected]",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines -32 to -34
type CSSStyleDeclarationProperty = keyof CSSStyleDeclaration;

type ActualCSSStyleDeclaration = Partial<Record<CSSStyleDeclarationProperty, unknown>>;
Copy link
Contributor Author

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>

Comment on lines +832 to +833
let computedStyle = getComputedStyle(element, selector);
let expectedProperties = Object.keys(expected);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

🗑️ the type assertion

Comment on lines -844 to +841
property =>
(computedStyle.getPropertyValue(property.toString()) || computedStyle[property]) ===
expected[property]
property => computedStyle[property] === expected[property]
Copy link
Contributor Author

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()

Comment on lines +4 to +6
if (!selector && 'computedStyleMap' in element) {
computedStyleMap = element.computedStyleMap();
}
Copy link
Contributor Author

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

Comment on lines +20 to +24
return (
value ||
computedStyle.getPropertyValue(property.toString()) ||
Reflect.get(computedStyle, property)
);
Copy link
Contributor Author

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]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant