How to sync design tokens from Figma to CSS Custom Properties

fmerian

flo merian

Posted on August 1, 2022

How to sync design tokens from Figma to CSS Custom Properties

At Specify, we want to help you work better as a product team. As a developer, one of your job is to implement design tokens in your codebase. And let's face it, manually updating design data is cumbersome.

Specify helps you collect design data from Figma and distribute it in the right format, in the right project, at the right time, and to top it all: automatically.

Let's see how to sync design tokens and assets from Figma to a web project using CSS Custom Properties.


We'll pull design data from the following Specify repository:

The Specify repository we'll pull our design data from.

We'll generate the following files:

šŸ—‚ output
ā””ā”€ā”€ šŸ—‚ styles
ā”‚   ā””ā”€ā”€ colors.css
ā”‚   ā””ā”€ā”€ fonts.css
ā”‚   ā””ā”€ā”€ text-styles.css
ā”œā”€ā”€ šŸ—‚ fonts
ā””ā”€ā”€ šŸ—‚ vectors
Enter fullscreen mode Exit fullscreen mode

Prerequisites

Before anything else, please make sure you have:

  1. created a Specify account
  2. collected design data from Figma to a Specify repository

Specify is a Design API. You can pull design data from Specify through:

  • The Specify REST API
  • The Specify CLI
  • The GitHub integration

Using the CLI

The Specify CLI helps you get design tokens and assets from Specify right from the terminal or even in a CI/CD pipeline.

Here's a short video tutorial to help you use the CLI to generate CSS Custom Properties:

Installation

First of all, let's install the Specify CLI: yarn global add @specifyapp/cli.

Once the Specify CLI is installed, run the specify command. You should get the following menu:

Specify CLI's menu

Configuration

Specify is flexible. It's designed to adapt to your needs and not the other way around. However you must configure Specify to get design tokens and assets that fit your needs.

We want:

  • our design tokens to be pulled and transformed as CSS Custom Properties
  • our icons and our font files to be pulled, converted and optimized

First, let's create a .specifyrc.json Specify configuration inside our project folder.

Instead of writing our configuration manually, let's use our Configuration Template for CSS Custom Properties.

We could copy the suggested .specifyrc.json (CLI) template inside our config file. However, we could use this template right from the CLI thanks to the specify init command.

This commands help you setup a Specify configuration file right from the CLI:

  1. You either choose to use a dedicated configuration template or start from scratch
  2. You choose your template
  3. You choose your configuration format (.js or .json)

Initializing Specify

Finalizing the configuration

Our configuration is almost done but we still need to change a couple of things:

  1. We must set the Specify repository we're pulling design data from. In our case we're pulling design data from the all-design-data repository created in the @acme-inc organization.
  2. We must generate a new personal access token and set it in the personalAccessToken property.

Our final configuration is:

{
  "repository": "@acme-inc/all-design-data",
  "personalAccessToken": "1518676e8cb6a7d5f909acdaa59a00ae9d1f72263e49d1211a52e6303f7f2ece",
  "rules": [
    {
      "name": "Design Tokens / Colors",
      "path": "./output/styles/colors.css",
      "filter": {
        "types": [
          "color"
        ]
      },
      "parsers": [
        {
          "name": "sort-by",
          "options": {
            "keys": [
              "name"
            ]
          }
        },
        {
          "name": "to-css-custom-properties",
          "options": {
            "formatName": "kebabCase"
          }
        }
      ]
    },
    {
      "name": "Design Tokens / Text Styles",
      "path": "./output/styles/text-styles.css",
      "filter": {
        "types": [
          "textStyle"
        ]
      },
      "parsers": [
        {
          "name": "to-css-text-style",
          "options": {
            "exclude": [
              "color",
              "text-indent",
              "vertical-align",
              "text-align"
            ],
            "relativeLineHeight": true,
            "genericFamily": "serif"
          }
        }
      ]
    },
    {
      "name": "Design Tokens / Import font files in CSS",
      "path": "./output/styles/fonts.css",
      "filter": {
        "types": [
          "font"
        ]
      },
      "parsers": [
        {
          "name": "to-css-font-import",
          "options": {
            "formats": [
              "woff",
              "woff2"
            ],
            "fontsPath": "../fonts"
          }
        }
      ]
    },
    {
      "name": "Design Tokens / Import font files",
      "path": "./output/fonts",
      "filter": {
        "types": [
          "font"
        ]
      },
      "parsers": [
        {
          "name": "convert-font",
          "options": {
            "formats": [
              "woff",
              "woff2"
            ]
          }
        }
      ]
    },
    {
      "name": "Design Tokens / Vectors",
      "path": "./output/vectors",
      "filter": {
        "types": [
          "vector"
        ]
      },
      "parsers": [
        {
          "name": "svgo",
          "options": {
            "svgo": {
              "js2svg": {
                "pretty": true
              }
            }
          }
        }
      ]
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

This configuration is now ready to be executed.

Let's run it by executing: specify pull.

The pull command helps you pull design data from the Specify repository you set in the repository property of your configuration. It tells Specify to read and execute your configuration and so it can generate design tokens and assets in your project.


Using the GitHub app

The Specify GitHub app helps you distribute your design tokens and assets from your Specify to your GitHub repositories. Every changes detected in your Specify repository will create a Pull Request in your GitHub repositories.

How to do this:

  1. Head over to your Specify repository you want to pull design data from
  2. Go to your repository Destinations page
  3. Add a Destination
  4. Select GitHub app
  5. Select your GitHub account and your GitHub repository
  6. Select the "CSS Custom Properties" template
  7. Create the Pull Request
  8. Merge the Pull Request on GitHub
  9. A new Pull Request containing your design data is created šŸŽ‰

Let's sum things up

Thanks for reading!

In this post, we learned how to pull our design tokens tokens and assets from Figma in a web project with Specify and the CSS Custom Properties Configuration Template.

We learned how to do so within the Specify CLI and the GitHub app.

Did you find this tutorial helpful? Please do share! Don't see the Configuration Template you're looking for? Feel free to request a template.

šŸ’– šŸ’Ŗ šŸ™… šŸš©
fmerian
flo merian

Posted on August 1, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related