Repository navigation
Description
The whole proposal is here, I have included a summary below.
Pain points (summary)
The main pain point is aliasing. Currently, design systems are specified as a long series of variables, e.g. --color-green-100, --color-crimson-950 etc.
Aliasing them to other names (e.g. semantic names such as --color-primary or simply shorter/simpler names) is currently author hostile, as it requires manually aliasing every single one, i.e. hundreds of declarations. Not only is this painful to do for the whole page, it makes it very hard to theme areas on the page with a different color, or pass design tokens to components.
Additionally, tints and shades for each color are generated manually, even if some of them could be inferred via interpolation. Theoretically that can already be done via a series of color-mix() values, but it is very tedious and repetitive, especially if you want to tweak the endpoints by adding more manual values where the interpolated ones don't work well.
Other ideas explored
The following are useful in their own right, but I don’t think solve the pain points equally well.
- Custom functions
- CSS functions for tints and shades
- Dedicated syntax for design tokens with standardized names for the low level common things
The proposal (summary)
At its core (and as an MVP) this proposal allows authors to define groups of variables with the same prefix by using braces, and then pass the whole group around to other variables.
/* Author CSS */
:root {
--color-green: {
100: oklch(95% 13% 135);
200: oklch(95% 15% 135);
/* ... */
900: oklch(25% 20% 135);
};
}
some-component {
--color-primary: var(--color-green);
}/* some-component.css */
:host {
background: var(--color-primary-100);
border: var(--color-primary-400);
color: var(--color-primary-900);
}- Groups can be infinitely nested, to specify not only tints of a single color, but an entire color palette or even an entire design system and pass it around with a single CSS variable.
The granularity is completely progressive: you can go from defining--ds-color-greenas a group, to defining--ds-coloras a group to defining--dsas a group without changing anything about the places referencing them. - Individual properties can still be specified outside of the group and overridden via regular CSS cascade rules. This provides a migration path for porting existing design systems to groups without their code having to be updated.
- Groups (and their constituent properties) inherit normally, like any other value. Custom properties in descendants can shadow inherited properties with the same name, i.e. a local
--color-green-100will override the inherited one from--color-green(just like with shorthands). - There may be value in exposing a functional syntax for every CSS variable group, to facilitate selecting
Using groups on non-custom properties: The base property
Authors can optionally specify a base value, which will be output if the custom property is used in a context that does not support groups, such as a regular CSS property.
--color-green: {
base: oklch(65% 50% 135);
100: oklch(95% 13% 135);
200: oklch(95% 15% 135);
/* ... */
900: oklch(25% 20% 135);
};
.note {
/* Same as border: 1px solid oklch(65% 50% 135)); */
border: 1px solid var(--color-green);
}This can also be tweaked from outside the group (just like the group properties), by setting --color-green-base. This also facilitates the use case of having constituent properties that are dynamically computed from the base so changing the base tweaks all of them in unison.
Continuous tokens
(This is definitely beyond MVP and more speculative)
Authors can specify a catch-all expression to be used to resolve undefined keys via a default property:
--color-green: {
base: oklch(65% 50% 135);
100: oklch(95% 13% 135);
default: color-mix(in oklch, var(--color-green-100) calc((100 - arg / 10) * 1%), var(--color-green-900));
900: oklch(25% 20% 135);
};Together with something like color-scale() this could also facilitate piecewise interpolation.
Alternative decomposed design
We could decouple this into three separate features:
- A function to map CSS variables with a common prefix to a different prefix, e.g.
--color-primary: group(--color-green)or evenvar()itself, with a*to mark the prefix:--color-primary-*: var(--color-green-*);, which would require handling base values manually (but based on the comments, that seems to be seen as a feature for some). - A nesting syntax for setting multiple variables with the same prefix at once. This would look just like the one above, but instead of specifying a group, it is just syntactic sugar for setting many variables at once.
- Continuous variations would have to be specified as a separate feature, perhaps by setting
--color-green-*or using a syntax to mark the key part like--color-green-[tint](which would also allow for multiple arguments in the future). The syntax space here is likely quite restricted due to the restrictions we had to introduce to the grammar to make&-less nesting work.
I ran this by a couple design systems folks I know, and the response so far has been overwhelmingly "I NEED THIS YESTERDAY". While I’m pretty sure the design can use a lot of refinement and I’m not sure about the technical feasibility of some of these ideas, I’m really hoping we can prioritize solving this problem.
Note that beyond design systems, this would also address many (most?) of the use cases around maps that keep coming up (don't have time to track them down right now, but maybe someone else can).
Before commenting please take a look at the whole proposal in case the issue you’re about to point out has been addressed there!
Activity
MaxArt2501 commentedon Feb 24, 2024
This looks so nice! And yes, I need that not just yesterday, but 5 years ago.
A question I have is that it's not clear how to get the root property name from a specific one, i.e. from
--color-primary-dark-75to--color-primarywithdarkand75specifiers. (What that mean some kind of string manipulation in CSS?)I'm not sure if there could be much of a use case... Maybe if I want to use another variant with a different specifier, i.e. using
--color-primary-darker-75for another part of the component.Afif13 commentedon Feb 24, 2024
Will we have an implicit Group creation if we define variables that share the same prefixes?
If for example I do the following:
Does it mean that, automatically, we have the group variable
--color?GreLI commentedon Feb 24, 2024
Could be this proposal unified with
@propertyrule? So one can define not only properties but their configuration too. Maybe define groups with something like@property-groupsyntax to avoid ambiguity?@Afif13 I think property groups are here to handle custom properties managing complexity. Groups do not have any value for its own sake. Otherwise it would be mentioned in the proposal.
Afif13 commentedon Feb 24, 2024
They do based on the whole proposal as we can do something like
So if I define
--color-green-100and--color-green-900manually, does it mean we also have an implicit access to--color-greenGreLI commentedon Feb 24, 2024
That's a good point. I don't think however, that it should be propagated to auto-magically created groups. Could be a source of funny bugs.
romainmenke commentedon Feb 24, 2024
I like this and I wonder if it can also solve length values?
In design systems it is really common to also have a bunch of length values for margin, padding, font-sizes,...
Design tools only offer the ability to export everything as pixels or as rem.
But CSS authors need both. Sometimes
remis the right unit, sometimespx, to ensure that the design scales correctly for end users.So we end up writing this mess :
Maybe there is something to custom units which are also a kind of custom prop?
olets commentedon Feb 24, 2024
Interesting stuff!
Did you consider following compound nesting syntax?
Pros: no new pattern of an implicit separator, eliminates initial-digit question. Con: more verbose, could imply that
--color-green: { my-component: /* … */ }might mean something. IMO those pro > those cons.With that change, the essence of this proposal would be to extend nest syntax beyond selectors to custom properties. That extension feels natural to me, and the doors it opens might be valuable if the future is heavier in custom property use.
initialwould a CSS-y name choice, e.g.--color-green: { initial: oklch(65% 50% 135); /* styles --color-green */}But what are the contexts which would support this magic key but not groups?
I've built from designs that used "default" as a color level, and have been glad to have that word available.
This feels to me more radical (new "magic key" pattern) and restrictive (can't use that word in a single-word nests) than it's worth to make this
create
--color-greenrather than--color-green-base.Removing the magic eliminates the question "how do we override just the default value?":
would work to make core green a little yellower, with no special case needed.
Share more about the motivation?
In what way is the result different from removing the first three lines?
kizu commentedon Feb 24, 2024
I really like this, and the authors wanted to have an ability to have arrays, lists, and maps in CSS for years! This proposal sounds good enough to cover most of their use cases on the first glance.
If I understood the proposal correctly, the part that covers this is the functional syntax:
With it, we could potentially do
And get the value of
--color-primary-dark-75.I 100% sure we need some way to dynamically extract values, and, especially, use dynamic variables to do so, like
--color-primary(var(--theme), var(--tint))or something. This could unlock many doors, and could make the variable groups and any related CSS features like mixins and functions tremendously powerful.What I'm not sure about is the syntax: I am not sure that promoting the variable groups to functions/mixins is a good idea, as it makes it easy to clash with the regular functions, and makes the double-dashed functional syntax tightly coupled with the variables themselves.
If I proposed something, I'd try something like a
get()function, which will accept the group's name as the first argument, and the list of arguments to get as the following ones, so instead of--color-primary(dark, 75)we could doget(--color-primary, dark, 75). Bonus point will be if every argument could accept a CSS variable:get(var(--dict), var(--theme), var(--tint)).Some other quick unsorted notes after reading the proposal:
--list: { 0: foo; 1: bar; }we could do something like--list: [foo, bar]?defaultfallback property a name likefallbackcould work better, as it might be easy to confuse “default” with “base” (and it was already proposed as an alternative: “alternative names: default, value”)/I'll need to spend more time thinking about this proposal, but I just want to reiterate how excited I am about it.
brandonmcconnell commentedon Feb 24, 2024
I'm really digging this idea!
I had a few questions, and maybe some possible gotchas—
nesting clarification
By groups be infinitely nestable, does that mean that we could groups in groups in groups, etc., and essentially see
primary-100-200-400(all together like that) variants come into play (practicality aside)?property reference syntax
I know the idea of grouped properties has been tossed around before, usually using a separate function to access those values, e.g.,
groupVar(--color-primary, 100)(not trying to sell that name), with this general syntax:groupVar(<custom-property-name>, <nested-property-name>, <declaration-value>)Have you considered alternate syntax options like that? What pros and cons do you see, and what led you to your eventual conclusion? I'd love to understand the journey you went through to get to this final product.
Edit: I read @kizu's comment, and I think he did a better job expressing this point than I did
naming collisions
If I am in this situation:
:root { --color-primary-100: black; --color-green: { 100: oklch(95% 13% 135); 200: oklch(95% 15% 135); /* ... */ 900: oklch(25% 20% 135); }; } some-component { --color-primary: var(--color-green); }I'm not sure how clear it is that by—essentially—spreading those grouped values into the new
--color-primaryvariable, you are also overriding the value(s) of any variables that happen to be named the same name as that variable plus one of its properties/nested values ({custom-property-name}-{nested-property-name})Thanks! 🙂
jens-struct commentedon Feb 24, 2024
I would like to contribute a critical perspective to the whole design-system aliasing topic in css vars. Sorry if this is a bit off topic. In my experience this causes more problems than it solves, and may encourage dark patterns, at least at the moment. For example:
When you do the semantic aliasing through css vars, you can't mark the not semantic aliased css vars as private. So in a context of a widly distributed design system, you can't make sure that the design tokens are used in the correct way.
Another much bigger problem, at least from a performance perspective, is the size of the css files, if you do semantic aliasing through css vars. As part of a bigger performance optimisation strategy, we switched a few design systems partially back from css vars to a server side abstraction like sass, were possible. (Keeping the semantic color css vars for dark theme support, not the color aliasing). This resultet in significant smaller css files and a much faster first paint. In one project we reduced the size by more then 50%, by moving stuff (several hundred var definitions) you don't neccessarily need in the browser, back to the server.
I know each design system is different, and there maybe usecases, where it makes sense to do more in the browser. But i don't think the general advice to do everything in the browser now, should come without a disclaimer about the disadvantages and that server side css tooling will never go away completely, at least for large projects.
In this regard, this proposal seems like it will also help reducing the file size aliasing can contribute to. (But i would still advice anyone not to do it completely in the browser, if not neccessary) I think a good combination of both is they key here, depending on the usecase. So the proposals that focus on extending css with design system focussed capabilities, are very welcome (like this and css functions)! Don't get me wrong. Just trying to support with input, on what can go wrong.
ollicle commentedon Feb 24, 2024
My initial response to the green example of group assignment in isolation
--color-primary: var(--color-green);was discomfort that there is no immediate way to discern a group assignment is occurring. It wouldn’t be quite so beautifully seamless, butvars()in place ofvar()to do this magic would help express what’s happening.Of course there is always room for naming convention to suggest the “groupness” of a variable. For example, capitalising group names
--color-primary: var(--Color-green).I very much like how the standalone
defaultconcept levers the power offered by color-mix in your example. 👏adamwathan commentedon Feb 24, 2024
Excited by the idea of somehow handling this in CSS, thanks for pushing it forward!
Bunch of thoughts, broken up by some horizontal rules...
One thing I haven't seen mentioned yet (but maybe everyone just is aware/understands and sees no issue) is that the proposed syntax is already valid:
...which is good in some ways (no parsing changes) but also means this would be adding new behavior to any projects already storing JSON-y type stuff in a custom property, which as I understand was an intentional design goal of custom properties from the beginning, to allow storing things you might pull out using JS.
Any projects already doing anything like this would now implicitly have new variables defined in their project that they didn't have before. Maybe not an issue in practice, but thought it was worth making sure everyone is aware.
Personally I think the
default,min,max,min-key,max-key, andargstuff feels like too much scope to try and settle on for an initial version (as alluded to in the proposal).A lot of it feels very specific to solving problems just for colors, and the idea of custom property groups is not really color-specific, so I think these will be hard and slow questions to come to decisions on.
I think the
baseidea is interesting (we did a very similar thing in Tailwind but it was this all capsDEFAULTkeyword) but I don't like that it makesbasea reserved word that can't be used as part of one of these custom property names within a group now. To me it doesn't feel worth it — I'd rather just not have a way to create a no-suffix variable within a group, or come up with some syntax that is totally conflict free, for example:Challenge with that syntax of course is how do you override just the
--color-greenbase variable without overriding the whole list, but honestly maybe it's fine to just have a limitation that you can't use this syntax if you want to do that. People can always fall-back to defining the custom properties one by one.Another idea is to change the left hand side, for example:
Now the implicit
-separator is explicit and captured on the leftAgree with some others that the implicit
-separator feels a bit magical — I think some way of including the dash in the nested token names feels safer to me, like what was mentioned in the proposal:I think moving that to the left hand side with some sort of special character to imply the group would be nicer, like I mentioned above:
I like that you can disable a whole set of colors this way using
initial:This is a problem I've been trying to figure out for Tailwind CSS v4.0 because we are moving all of our color token definitions to CSS instead of the JS config file we've used historically.
This would also work with the
*syntax I suggested:If something like this makes it into CSS I would love a way to reference these groups in other potential future CSS features to sort of create dynamic selectors.
Tailwind for instance generates utility classes for every color automatically, but all of the tooling to do it is incredibly complex and relies on scanning all of your HTML files to find classes you're using and generate them.
It would be amazing to have some way to declaratively define a matching pattern in CSS that points to a group and makes these selectors "just work".
This is terribly under-thought pseudo-code but maybe it's enough to communicate the idea:
This would allow me to use classes like
bg-green-500in my HTML without actually defining every single class.This is obviously way out of scope for this proposal but it's the sort of interesting future use case that might be worth considering during the design. This sort of thing could be a Tailwind killer in a very good way 😄
We're prototyping this style of syntax for Tailwind CSS v4 as a way to create custom utilities that we ingest and process as a preprocessor, but it would be amazing to get power like this in the language in some way one day.
23 remaining items
jcubic commentedon Apr 1, 2024
This will break existing behavior. I've written an article how to use JSON inside CSS variable. This will break this functionality. There is a need for new syntax for the group (like a new keyword). Not just curly braces that right now is valid CSS custom property value.
This is perfectly valid CSS:
reference: Controlling Paint Worklet with JSON in CSS
adamwathan commentedon Apr 1, 2024
@jcubic Make sure to read the whole discussion, that's been brought up a few times already and is part of the motivation for the
--color-red-*: { ... }syntax 👍🏻jcubic commentedon Apr 1, 2024
@adamwathan there are a lot to digest here. There are also different proposals. I just wanted to be sure that this one will not be the part of the spec.
brandonmcconnell commentedon Apr 1, 2024
@jcubic Hey Jakub, nothing is set in stone at this point. As you mentioned, there are several proposals to unpack here and likely concerns to address with each of them, as has been the case for many new CSS features. CSS nesting, for example, went through like 4-5 rounds of revisions, if not more, before settling on the final syntax.
As a JS-in-CSS (vs. CSS-in-JS) package author, I leverage the approach you mentioned but could work around any breaking changes resulting from the syntax proposal you called out.
Do you have a specific library or package in mind that would be impacted by that syntax, specifically in a way that would affect consumer APIs? Adjusting library/framework APIs under the hood is more manageable and can often flow with API changes. When I started throwing JS into CSS variable values, I knew perfectly well that what I was doing was fringe at best and could be subject to change.
That said, as @adamwathan pointed out, this thread seems to be leaning toward the
-*syntax.jcubic commentedon Apr 1, 2024
@brandonmcconnell No, I don't have any specific library. Just bump into the issue in one of the newsletters (I'm way behind on reading them) and realized that the original proposal will break my demo and article.
brandonmcconnell commentedon Apr 1, 2024
@jcubic I'd love to hear more about your use case. I just DM'd you on X, so we can keep that discussion going offline.
LeaVerou commentedon Apr 1, 2024
If the content is valid JSON, properties need to be quoted, so that wouldn't be valid CSS syntax. So depending on how parsing is defined, even the original proposal may not actually break your demo.
That said, when we did research on this (for CSS Nesting), the fraction of custom properties whose value was JSON was tiny.
jcubic commentedon Apr 1, 2024
Sure, but now you can put anything into a CSS variable, including working JavaScript. See this article by Chris Coyer.
This is probably not very useful, but someone somewhere may rely on this feature and you may break that user code.
brandonmcconnell commentedon Apr 2, 2024
@jcubic Even with that syntax, it's very likely that the value received by JS for the parent property,
--pointer-optionsin your example, for instance, would remain the same. In that case, there would likely be a new Web API method introduced to theCSSStyleDeclarationinterface similar togetPropertyValue()to parse grouped variable values.brandonmcconnell commentedon May 15, 2024
Sub-property getter/setter syntax
Using a dot separator between levels of grouped properties vs. a hyphen would help disambiguate in cases of naming collisions and provide a clearer method (to developers and parsers) when a property ref is looking at a name vs. entering a group. @mirisuzanne briefly mentioned this syntax here as well in the related "Design of
@nestrule" issue, and I suggested it previously above.Disambiguation & naming collisions
Using a dot syntax would also help to disambiguate grouped property names from ones with hyphen characters:
In this example, with hyphen-delimiting,
var(--test-a-b)andvar(--test-b-a)would each hold two meanings, and it's unclear if the latter for each would directly override the former as is standard in traditional CSS (i.e. doa:{b:value}anda-b:valueset the same sub-property value?).With dot-delimiting, these would not be confusable, as each group level would be explicitly delimited by a
.and gettable or settable using the dot syntax without needing to implement a separate system to override a specific value in a nested property.var(--test.a.b),var(--test.b.a),var(--test.a-b), andvar(--test.b-a)would each be unique.Getter/setter syntax example
Setting a sub-property might look like this:
@LeaVerou Let me know if you want me to open a new ticket to discuss the getter/setter syntax.
LeaVerou commentedon May 17, 2024
I would strongly prefer not using an
@rulehere, for a number of reasons:*should be enough of a signifier that this is a group, and makes scoping more clear. Scoping is very unclear for nested@rules, since for many of them.foo { @rule {} }is rewritten to@rule { .foo {} }.I’m also not a fan of the dot syntax. One of the goals of this proposal was to pave the cowpaths and facilitate the patterns that are already in wide use, not introduce a new syntax pattern that needs to gain traction. Also, I think it’s a good design goal that it should not matter how a design token was defined (whether it was a group or an explicit variable declaration) at the point of usage. I.e. my component should neither need to know nor care how you defined your design tokens.
brandonmcconnell commentedon May 17, 2024
@LeaVerou I would also prefer not to introduce an extra @-rule like
@grouphere. I merely included that as I've seen that art floating around that included it.My main focus was the dot syntax, though I understand your feedback and concern there too.
Sub-groups/properties
For this example…
Without an alternative getter/setter syntax like the dot syntax…
Would
a:{b:{...}}anda-b:valueboth target the same sub-property value?a-bunder--testafter setting upa:bas a group overwrite its group value (naming collision), or would it set thebasevalue for that respective group, if one exists?Would sub-groups need to use the same
-*group syntax?-*syntax for sub-groups, would the object undera(a:b, nota-*:b-*) be seen as a plain string, as is currently standard practice in CSS?a-boverwrites the value ofa-*:b-*?Would setting the plain
a-bsub-property value before setting up thea-*:b-*group still preserve that value as thebasevalue, or would it be completely overridden when the new group is instantiated, if they would otherwise point to the same value?Getter / setter syntax
If the dot syntax is not needed for a getter syntax and names can be referenced using
-as the sole delimiter without risking naming collisions, would we need theget()function others have proposed, as that appears to serve the same purpose?How would one go about setting a specific sub-property, without affecting others?
--test-a-b-extra: 'xyz';effectively do that, rewrite the sub-property--test-*:a-*:b-*:extra, while leaving the rest of the group(s) intact?--test-a-brewrite the base value if--test-a-bis a group, whereas setting--test-a-b-*would rewrite the group itself?Are there any plans as of now to introduce a way to pass groups or sub-groups to other custom-properties? Something like one of these:
--custom-*: --test-a-b-*;--custom-*: var(--test-a-b-*);--custom-*: group(--test-a-b-*);--custom-*: --test-a-b;--custom-*: var(--test-a-b);--custom-*: group(--test-a-b);base/defaultsub-propertyTo allow developers to name sub-properties whatever standard idents they wish, could we consider a pseudo-property name for the
basekeyword rather than making that a reserved word? Perhaps:baseor:defaultto mirror theDEFAULTkeyword used by Tailwind CSS?Several others have voiced a similar concern—@kizu @adamwathan @Crissov @olets @tabatkins—which I think using a pseudo here might account for, though
:default: …isn't exactly common syntax either, as we have pseudo-classes and pseudo-elements, but not necessarily pseudo-properties, at least yet.Tab Atkins' idea of always requiring custom sub-properties to start with
--would negate this risk entirely, so only named properties could be used in groups, anddefaultcould be one of those named properties:sorvell commentedon Sep 23, 2024
I've played around some with stuff weird values into custom properties and these questions are related to that experience.
Currently, using a variable inside curly braces doesn't compute properly, e.g. a rule with
--foo: { background: var(--color); }computes to''but without thevarlike--foo: { background: red; }, it computes fine. I just wanted to make sure that there's not some difficult to change parsing behavior there that would cause problems for this proposal.You also noted that since groups define constituent properties, they compose with individual ones, but I'm wondering how this works if I want to override only part of a group (and sorry if this was covered somewhere)?
On
divare--p-1,--p-2, and--p-3all computed as set here? If so, great =).andrezero commentedon Mar 18, 2025
Just want to show my support this a million and add a DS typography use case.
All of these maps are hidden in plain sight in all our gazillion tokens
--style-body-small-sizeWould make all variants of
<p class="body small">or<Text variant="small" />so easy to implement in one clean swipe.I guess functions can take care of defaults using
var(, var())but map-like acess by key is the essential 🔑 to so many challenges.I guess
--text-styles[--family][--variant]reads better 😅 thoughFinally, the ability to return maps from functions would also be some kind of magic of course. Would avoid having to create different accessors to retrieve related tokens, resolving them all at once.
Don't know how well that plays out with "types". Would it require the ability to define dictionary types as well?
Again! Kudos 💯