style-dictionary: I can't generate variables css to a specific class
Hello!
We are use the Style Dictionary to generate our tokens, but we have a problem here.
The tokens are building to different products (themes) and we like use variables css, but in the generated files, we got:
// theme-x.css
:root {
// variables
}
// theme-y.css
:root {
// variables
}
How change the destiny of variables css to custom class, like a:
// theme-x.css
.theme-x {
// variables
}
// theme-y.css
.theme-y {
// variables
}
🍻
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 4
- Comments: 15 (4 by maintainers)
Unfortunately the built-in css/variables format does not allow this. But that would be a good addition! Here is the code: https://github.com/amzn/style-dictionary/blob/master/lib/common/formats.js#L106
You could write a custom format that does this too if you can’t wait for that change to be made into the core library:
Note: this is untested code, but should probably work and doesn’t include full working code. Also it won’t exactly match the functionality of ‘css/variables’ format, but if you want to you could copy the functions in https://github.com/amzn/style-dictionary/blob/main/lib/common/formats.js to make it work exactly the same.
Let me know if this helps!
Hi!
I think is worth pointing out that you can also use Custom format helpers. To make use of the functions already defined in the library without having to re-write the formatter, if you only need to change/add a selector/class!
@dbanksdesign Thank you so mutch for your help! ❤️
We understand about the registerFormat method and got a great result. Just a observation to your script. The method
.map
return a new array with,
. We use thejoin()
method to solve this and get a right css on finish.Script:
@sarah-martinellibenedetti Sorry, us project don’t is open source 😢
But, I go try explain to you:
folders
brands/brand1.json
plataforms/web/font.json
@sarah-martinellibenedetti It looks like @gabrielmedina followed this example: https://github.com/amzn/style-dictionary/tree/main/examples/advanced/multi-brand-multi-platform hopefully that helps!
@uptonking you could actually use
outputReferences
in a custom format. Take a look at this example:https://github.com/amzn/style-dictionary/blob/3.0/examples/advanced/variables-in-outputs/sd.config.js
The custom format doesn’t use the
outputReferences
option, which is my bad, it will always output references. But you could change the formatter code to this to check if theoutputReferences
option is true:Man, you helped us so much. The
.join()
is a detail 😉Thank you @dbanksdesign! You’ve saved the day! 😁👏👏👏👏👏