Skip to content

window.getComputedStyle equivalent style() function in CSS #8968

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

Closed
jd-solanki opened this issue Jun 15, 2023 · 1 comment
Closed

window.getComputedStyle equivalent style() function in CSS #8968

jd-solanki opened this issue Jun 15, 2023 · 1 comment
Labels
Closed as Duplicate Closed as Question Answered Used when the issue is more of a question than a problem, and it's been answered.

Comments

@jd-solanki
Copy link

jd-solanki commented Jun 15, 2023

Note
I'm new to this repo & CSS spec so if I posted at wrong place or something wrong is at my side let me know. I just wanted to share this if this is possible via CSS.

Hi 👋🏻

I really love how CSS is evolving these days and really loved #CSSDay ❤️

I'm building UI framework Anu and needed some CSS magic at some point. These magic includes:

  • currentBackgroundColor
  • contrast-color
  • and more...

I wanted to share the idea got recently on CSS functions. We already have currentColor & there's already proposal for currentBackgroundColor. If we at these with bird view it comes down to JS version window.getComputedStyle. With JS's window.getComputedStyle we can easily fullfil requirement of currentColor & currentBackgroundColor

If we have similar function in CSS, let's say style() then we don't need keywords like currentColor & currentBackgroundColor at all 😱

Syntax

property: style(<property>, <?optional-selector>)

border-color: style(color) /* same as currentColor */
border-color: color-mix(style(backgroundColor), teal, 50%) /* same as Una shared in [this](https://github.com/w3c/csswg-drafts/issues/5292) issue */

We can take it to the next level by adding support for selector and not limiting to current element's context:

/* Assume we want to increase the size of icon if title is provided in alert */

font-size: style(font-size, '& + div > .alert-title') /* just a complex selector */

IDK if this is technically possible but just sharing my thoughts. If this is possible this unlocks great possibilities for UI design.
There's also question on what about invalid values or circular dependencies discussed in #5292 or assingning invalida values to property.

@Loirooriol
Copy link
Contributor

That's inherently circular. Basically a duplicate of #5729, see Tab's comment explaining the problem.
Note the circularity can be avoided by taking the styles from the parent element, that's #2864.
The selector idea also seems problematic: what if there are multiple elements matching the selector, what if they are descendants (circularity), etc.

@Loirooriol Loirooriol added Closed as Duplicate Closed as Question Answered Used when the issue is more of a question than a problem, and it's been answered. labels Jun 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Closed as Duplicate Closed as Question Answered Used when the issue is more of a question than a problem, and it's been answered.
Projects
None yet
Development

No branches or pull requests

2 participants