question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I can't generate variables css to a specific class

See original GitHub issue

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
}

🍻

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

13reactions
dbanksdesigncommented, Jul 20, 2020

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:

StyleDictionary.registerFormat({
  name: 'css/variables',
  formatter: function(dictionary, config) {
    return `${this.selector} {
${dictionary.allProperties.map(prop => `  --${prop.name}: ${prop.value};`)}
    }`
  }
});

StyleDictionary.extend({
  source: [],
  platforms: {
    css: {
      files: [{
        destination: 'theme-x.css',
        format: 'css/variables',
        selector: '.theme-x'
      }]
    }
  }
})

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!

4reactions
mdueniascommented, Feb 10, 2022

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!

const { fileHeader, formattedVariables } = StyleDictionary.formatHelpers;

StyleDictionary.registerFormat({
  name: 'css/variables-themed',
  formatter: function({dictionary, file, options}) {
    const { outputReferences, theme } = options;
    return fileHeader({file}) +
      `:root .${theme} {\n` +
      formattedVariables({format: 'css', dictionary, outputReferences}) +
      '\n}\n';
  }
});

StyleDictionary.extend({
  source: ['src/**/*base.json', 'src/**/*dark.json'],
  platforms: {
    scss: {
      transformGroup: 'css',
      buildPath: 'build/css/',
      files: [{
        destination: 'dark.css',
        format: 'css/variables-themed',
        options: {
          outputReferences: true,
          theme: "dark"
        }
      }]
    }
  }
}).buildAllPlatforms();
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use CSS variables like a pro - LogRocket Blog
To declare a variable in CSS, come up with a name for the variable, then append two hyphens (–) as the prefix. element...
Read more >
Using CSS custom properties (variables) - MDN Web Docs
Custom properties (sometimes referred to as CSS variables or cascading variables) are entities defined by CSS authors that contain specific ...
Read more >
Overwriting css root variables by adding a class to element
So to fix the issue, you can add the class, you called it .night , to the root element itself. Here's a quick...
Read more >
Everything you need to know about CSS Variables
I'll show how easy CSS variables make creating site-wide theme styles. ... Now when the .red class is used, the browser notes the...
Read more >
A Complete Guide To CSS Variables [With Examples]
The CSS variables are case-sensitive. Therefore, the variables my_var and My_var are both different. CSS Variable Cannot Be A Property Name. The ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found