You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been trying to figure this out for a while now with no luck. Below, I have my JSON for my typography token, and when I run my config, the tokens are created as separate lines. However, since it's typography, I'm trying to make it easier to use in the code. Ideally, it should generate a reusable text style that includes all the necessary properties.
I've tried several different ways to create a custom formatter, but nothing has worked. I'm using Style Dictionary v4.
Fair warning, I've tried to do this before, and it's pretty difficult to get it working in a reliable manner.
My advice would be to use an attribute transform to add attributes to each token that you want to include inside of a CSS class, so a fontFamily token for example would end up looking like this:
{value: 'Stolzl',type: 'fontFamily',attributes: {cssProp: 'font-family',// have your token.path[token.path.length - 1] (last item) map "fontFamily" to CSS property "font-family"cssClass: 'typography-desktop-body-regular-xl',// your token.path.slice(0, token.path.length - 1) (excludes last item) kebabcased},// this below is from the `name/kebab` transformname: 'typography-desktop-body-regular-xl-font-family'}
And then in your format, you can format as you would normally for CSS but you'll add another custom format that specifically handles the tokens which should be grouped as CSS classes, so basically your tokens with cssProp/cssClass attributes:
Group these tokens together by their cssClass e.g. using a JavaScript keyed Map()
For each group, render the CSS text style class:
// wrap below also in a loop if you have more than 1 class`.${cssClass} {${props.map(prop=>`${prop.cssProp}: var(--${name})${prop.fallback ? `, ${prop.fallback}` : ''};`).join('\n ')}}`
which then renders to CSS string (assuming we put a sans-serif fallback for fontFamily props):
Hi
I've been trying to figure this out for a while now with no luck. Below, I have my JSON for my typography token, and when I run my config, the tokens are created as separate lines. However, since it's typography, I'm trying to make it easier to use in the code. Ideally, it should generate a reusable text style that includes all the necessary properties.
I've tried several different ways to create a custom formatter, but nothing has worked. I'm using Style Dictionary v4.
End Goal
variables.css file
My token.json file
The text was updated successfully, but these errors were encountered: