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

[dom2] Can't set CSS custom properties #39

Open
ComedyTomedy opened this issue May 12, 2023 · 4 comments
Open

[dom2] Can't set CSS custom properties #39

ComedyTomedy opened this issue May 12, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@ComedyTomedy
Copy link

dom2/style will only set properties that goog.style/setStyle recognises - anything else silently vanishes. This makes it impossible to use custom properties ("CSS variables"), and seems to affect browser-prefixed props too (I quickly tested -moz-transition, might be wrong about this).

Here are 2 attempts that don't work:

(dom/style {:--size (str x)})

(dom/props {:style (str "--size: " x)})

And one attempt that did:

(dom/set-attribute-ns dom/node
   nil "style" (str "--size: " x))

One possible fix might be for dom/set-property! to join style rules into a k: v; k2: v2 form and use set-attribute-ns to inject them as a string. It feels slightly hacky, and perhaps bypasses some clever internals of Google Closure (?), but it would solve the problem without styles overwriting oneanother.

(PS: I'm enjoying Electric v much, I'm not a dev, don't work in tech, and never used Clojure beyond toy examples, but Electric has revived my enthusiasm for a personal project that I abandoned last year. Thank you for everything!)

@s-ol
Copy link

s-ol commented Jun 12, 2023

the js API generally used to set CSS vars is element.style.setProperty(prop, val). This works for custom properties and browser properties equally, but I'm not sure what benefits goog.style has over using this browser API.

EDIT: having looked at the closure library, it doesn't seem to do anything useful and was probably added before setProperty was standardized (ca 2012).

@dustingetz dustingetz added the enhancement New feature or request label Oct 16, 2023
@dustingetz
Copy link
Member

Implemented for CSS vars. But not for all properties, e.g. -moz-transition. Need to confirm this:

Having looked at the closure library, it doesn’t seem to do anything useful and was probably added before setProperty was standardized (ca 2012).

@ggeoffrey
Copy link
Member

ggeoffrey commented Oct 16, 2023

goog.style.setStyle does two things that element.style.setProperty doesn’t:

  1. vendor autoprefixing: setStyle(element, {"orient": "vertical"}) will set -moz-orient to "vertical"
  2. direct access writes: setStyle will perform element.style.MozOrient = "vertical" instead of calling setProperty

I don’t know if 1. is useful.
I don’t know if 2. has any benefit over setProperty

If 1. is not useful, then I agree with @s-ol and I think we should call setProperty directly.

@ComedyTomedy
Copy link
Author

ComedyTomedy commented Oct 16, 2023

2. is referred to simply as alternative syntax in dev moz org

Personally I'd see 1. as undocumented magic -- the "rule of least surprise" would be that whatever property I enter gets added to the DOM and if it "doesn't work", that's my problem.

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

No branches or pull requests

4 participants