npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, πŸ‘‹, I’m Ryan HefnerΒ  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you πŸ™

Β© 2024 – Pkg Stats / Ryan Hefner

@crowdstrike/design-tokens

v2.0.1

Published

πŸŽ‰ **Create CSS themes using Figma Variables.** πŸŽ‰

Downloads

47

Readme

Design Tokens

πŸŽ‰ Create CSS themes using Figma Variables. πŸŽ‰

  • βœ… Export variables from Figma
  • βœ… Build Style Dictionary compatible tokens
  • βœ… Create CSS custom properties from tokens

Access to Figma's Variables REST API

Per Figma's developer documentation to use Figma's Variables REST API you must have a full seat in an Enterprise org, guests cannot use the API.

Install

pnpm install -D @crowdstrike/design-tokens

Exporting Variables from Figma

To export variables you will need a a personal access token and a file (or branch) id.

export FIGMA_TOKEN=<personalAccessToken>

pnpm dt export-variables <FILE_ID>

If it's more convenient, or you'd rather not have to pass in the file id to the command line everytime, the file id could instead be commited to a tokens.config.js file. For example:

// tokens.config.js (default export must be an object)

export default {
  fileId: <FILE_ID>
}

// then run
pnpm dt export-variables;

The tokens.config.js file should be placed in the same directory as package.json.

The Figma variables will be written to variables.json in the same directory as package.json.

Building Tokens

To build tokens, run:

pnpm dt build-tokens

This will parse the variable's json and write a style-dictionary compatible tokens.json file alongside the previously created variables.json.

Exclude collections or variables

To exclude collections or variables from the tokens build, add a tokens.config.js file alongside your package.json. For example:

// tokens.config.js (default export must be an object)

export default {
  excludeCollections(collectionName) {
    const collections = ['System', 'Theme'];

    // Return true to exclude a collection. You could also
    // return false if it's more practical to define the
    // collections you want to keep. In this example only
    // the collections named System and Theme will be included.
    return !collections.includes(collectionName);
  },
  excludeVariables(variableName) {
    const variables = ['color', 'component', 'data-viz'];

    for (const variable of variables) {
      if (variableName.includes(variable)) {
        return true;
      }
    }
  },
};

The excludeCollections and excludeVariables methods must return either true or false.

Creating Custom Properties, Stylesheets, and Themes

Let's walk through the files needed to create stylesheets. Create a tokens.config.js file in your project root alongside tokens.json, with the following content:

{
  themes: [
    {
      colorScheme: 'dark',
      name: 'dark',
      tokenKey: 'dark',
    },
    {
      colorScheme: 'light',
      name: 'dark',
      tokenKey: 'light',
    },
    {
      colorScheme: 'normal',
      name: 'system',
      tokenKey: 'system',
    },
  ];
}

Assuming our tokens.json only consisted of:

{
  "dark": {
    "background/lightest": {
      "value": "#2c2c30",
      "type": "color"
    }
  },
  "light": {
    "background/lightest": {
      "value": "#ffffff",
      "type": "color"
    }
  },
  "system": {
    "space/x0": {
      "value": 0,
      "type": "float"
    }
  }
}

and we run:

pnpm dt build-styles

a styles directory would be created with 3 stylesheets:

/* dark.css */
:host,
.theme-dark {
  color-scheme: dark;
  --background-lightest: #2c2c30;
}

/* light.css */
:root,
:host,
.theme-light {
  color-scheme: light;
  --background-lightest: #ffffff;
}

/* system.css */
:root,
:host {
  color-scheme: normal;
  --space-x0: 0;
}

A quick study of the created stylesheets shows how the tokens and theme config were used to create the stylesheets:

  • colorScheme defined the value for the color-scheme property in the stylesheet.
  • name defined the stylesheet file name and also the theme's CSS selector concatenated with .theme-.
  • tokenKey referenced an object in the tokens json.

Note also the selectors at the top of each rule set. A colorScheme of light is scoped to :root, meaning this theme will be the default theme. A color scheme of normal means the styles will also be scoped to :root but no .theme-<name> selector will be added as it's assumed this is a global stylesheet that will be loaded regardless of theme.

If you'd prefer the stylesheets were output to a directory other than styles, add outputDirectoryForStyles to the tokens.config.js, e.g:

export default {
  outputDirectoryForStyles: 'src/css',
}

The stylesheets will now be written in the src/css directory.

CSS custom properties prefix

By default there is no prefix added to the CSS variable names. If you wish to add a prefix define cssPrefix in tokens.config.js e.g:

export default {
  cssPrefix: 'cs',
}

All variables created will now have the cs prefix, e.g a variable named --foo-bar will now be --cs-foo-bar.