-
Notifications
You must be signed in to change notification settings - Fork 711
css expression inheritance #2749
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
Comments
Also related https://bugs.chromium.org/p/chromium/issues/detail?id=850932 |
I think it's a bit confusing that some things like Maybe add a Would this make sense, syntactically? |
|
This is not currently possible with unregistered custom properties (the default). If you register the property with One of the plans for Variables 2 is to add a !-syntax for asking the browser to immediately evaluate a custom property as a given type, based on information from the declaring element, like @jonjohnjohnson That's a different issue entirely, related to letting you "build up" a variable value as you nest things. |
I'm not sure I follow, but I really would like to do:
would work like oop programming languages that support inheritance:
|
Yes, that's the "different" thing I was talking about. Plan is to address that in Variables 2, with a syntax like |
@tabatkins though I understand this "building up" a variable as you nest is a separate feature, I don't s'pose iteration like... .el { --index: 1; }
.el ~ .el { --index: calc(var(--index currentValue) + 1); } ...would ever be viable? In line with #1026? |
this is amazing! thank you tabatkins! |
It seems the discussion got derailed to reading the value of a variable from the parent element (#1962) or to force the specified value to be resolved immediately. That's useful for sure, but it doesn't seem to provide a way to avoid resolving @jonjohnjohnson I would prefer |
@Loirooriol I know this is off op topic, but my example |
Ah, I did indeed misread the issue. The mechanics of "allow a var() to resolve to a CSS-wide keyword" should be the same as the current "var() becomes inherit/initial if it's the guaranteed-invalid value". In other words, seems like it would be fine implementation-wise, it would just need a special syntax to trigger it. Late-resolving a var() (fetching the custom property value at time of var() resolution) has no mechanical problems either, but it does has some design issues. tokens() would be a "use once" thing - if you build up a variable from other variables, they'll be resolved at the middle stage, rather than at the ends. I guess you could wrap tokens() around itself multiple times, so each substitution stripped one layer off, but that requires a fragile prediction of how much you'll do, and might not even be possible if the build-up is based on depth. Maybe it's ok that it'll only survive a single substitution? Or maybe it survives all substitutions, and you have to explicitly un-raw it with another function when you want the substitution to happen. |
Could this potentially be addressed with an extension of the solution proposed in #2864? (cc @LeaVerou) Here's what I'm thinking, using the same example as OP: div {
--foo: 10px;
--border: calc(var(--foo) - 5px); /* --border must always be `--foo - 5px` */
}
div > p {
--foo: 100px;
border: inherit(--border) red solid; /* --border is inherited as `calc(var(--foo) - 5px)`, so it ends up being 95px */
} Or, maybe I'm misunderstanding how |
@benface And I don't like your alternatives, they would force browsers to keep the raw expressions of the parent element just in case some children references it. Seems better to just say that you want some expression to inherit as-is when you set it on the parent. |
I hadn't considered this. Alright, nevermind. 🫠 |
The design tokens spec, which is being used by tools such as Figma, require that references are preserved:
https://tr.designtokens.org/format/#aliases-references This means that, in Figma, if you change the value of a variable in a layer, all other variables that point to this also update. As above, this isn't the case with CSS variables. In the below example, repointing primary-light does not update button-bg-color: button {
background-color: var(--button-bg-color);
}
:root {
--primary-light: #0000FF;
--primary-dark: #6666FF;
--button-bg-color: var(--primary-light);
}
[data-mode="dark"] {
--button-bg-color: var(--primary-dark);
}
[data-color="red"] {
--primary-light: #FF0000;
--primary-dark: #FF6666;
} <body data-mode="dark">
<div data-color="red">
<button>Button</button>
</div>
</body> Unless I am missing something in the inherit() function spec, it seems that in order to accurately implement design tokens references using CSS variables, the ability to preserve references (e.g. with expression inheritance) will be needed? |
Hi w3c people! I have a proposal about
calc()
,var()
and similar functions.I'd like to be able to inherit the expression itself rather than the computed-once value they contain.
For example if I write
calc(1em + 20px)
I expect to get the current font-size added of 20px, rather than thefont-size
computed at the time of the very first evaluation of thecalc()
expression.A simple example could be the following:
http://jsfiddle.net/pd3zeg9v/
What do you think? Or maybe this is already possible and I don't know how?
Thank you in advance.
The text was updated successfully, but these errors were encountered: