Using Figma Tokens Plugin

Here we show the flow in a 2-minute video, showing the power of design tokens using Figma Tokens Plugin and Backlight.

These docs will cover GitHub, but you can use GitLab & GitLab Runners to accomplish the same workflow.

Workflow Summary

  • A designer changes some tokens in Figma Tokens Plugin and pushes the changes to GitHub.
  • A pull request is created automatically inside GitHub
  • Style-Dictionary is executed and results are added to this PR automatically
  • A Backlight preview link will appear in the PR
  • Designers (& optionally developers) can see visual changes and review + approve
  • Changes are merged into main branch

Note how 0 developers are needed here!

Code & design, will be in sync 🎉 and designers will have more autonomy.

Full Video

Below, Joren shows how he created this starter kit by walking you through connecting Figma Tokens Plugin, Backlight and GitHub to your Figma Design system. This enables the flow shown in the video above.

Alternatively, follow the steps below to set up Figma Tokens Plugin with your Backlight project.

In these examples, we show how we set up a project which is equivalent to our Figma Tokens starter kit, which you can also duplicate as a quick start.

You'll still need to hook it up to your GitHub repository, Figma Library, and Figma Tokens Plugin.

Connect to GitHub

In your Backlight Project, make sure to connect to GitHub first if you haven't already.

Figma Tokens Plugin

As a first step, create a Figma file and start with some Design Tokens.

Install the Figma Tokens Plugin by Jan Six and open it in your Figma file.

Tokens

Now let's start creating some design tokens in this plugin.

If you've duplicated the starter kit, you can connect this to your own GitHub project, meaning you will already have Figma Tokens JSON in your repository which you can pull from in the Figma Tokens Plugin. You will also have a Style-Dictionary config and GitHub Action workflows setup. In this case, skip to the GitHub Sync section.

We create a color palette consisting of a primary color with a 50-900 lightness range, as well as a greyscale using the same technique. In the example, we went for emerald green. We put these colors in a core tokenset.

Figma core tokens

After this, we create a button tokenset where we will start defining the colors for our simple button design.

Figma button tokens

The Figma file is public. You can also view the tokens JSON inside GitHub

We will synchronize these tokens to GitHub in the last step.

Style-Dictionary

We will soon have some style-dictionary input files generated automatically from our Figma tokens. This will enable us to start exporting our design tokens to a platform of our choice.

In this example, we will take CSS Custom Properties (also known as CSS variables).

Inside Backlight, create a sd.config.cjs if you don't have one yet. Here you can choose how you'd like to export your tokens, for example:

module.exports = {
  source: ['sd-input/**/*.json'],
  fileHeader: {
    autoGeneratedFileHeader: () => {
      return [`Do not edit directly, this file was auto-generated`];
    },
  },
  platforms: {
    css: {
      transformGroup: 'css',
      buildPath: 'tokens/',
      prefix: 'figma',
      files: [
        {
          destination: 'variables.css',
          format: 'css/variables',
          options: {
            fileHeader: 'autoGeneratedFileHeader',
          },
        },
      ],
    },
  },
};

Note that we added a custom fileHeader called autoGeneratedFileHeader. The default one from Style-Dictionary will put a comment with the current date and time of when the tokens were generated. This can lead to merge conflicts on this line happening quite often when multiple diverging branches end up running style-dictionary but without actual token changes. It gets quite tedious to have to solve those one-line merge conflicts every time, so in our custom header we leave that date/time out.

Push these changes to the default branch through a pull request, you can do this using the Backlight interface.

GitHub Action

To automate everything, we need to take one more step. When you change tokens in Figma, then sync to GitHub, this should create a pull request. In this pull request, we will get the new sd-input tokens, but we need to run a token-transformer and Style-Dictionary as well to get the output variables.css. We can automate that with GitHub Actions.

Start by locally cloning the GitHub repository and opening it in your editor.

Push workflow

Create a workflow:

.github/workflows/figma-tokens-push.yaml:

name: Create Figma Tokens PR

# match the branch you put in your Figma Tokens Sync Credentials in the next step
on:
  push:
    branches:
      - figma-tokens

jobs:
  pull-request:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Create Pull Request
        uses: repo-sync/pull-request@v2
        with:
          destination_branch: 'main' # match your default branch
          pr_title: 'Figma Tokens Update'
          pr_body: '🍥 Design Tokens were updated in Figma! This PR was created to update the code.'
          github_token: ${{ secrets.GH_PAT }}

For this workflow to run, you will have to add a GitHub Actions secret called GH_PAT. We must use a personal access token here because this workflow must trigger another workflow through a pull request event, and with the default GitHub Action token this will not happen, as protection against unintended trigger loops between workflows.

Create a Personal Access Token in your GitHub developer settings.

Then add this to your secrets:

GitHub secrets

Make sure to store the token somewhere, as we will reuse it in the next step.

Pull request workflow

Create a workflow:

.github/workflows/figma-tokens-pr.yaml:

name: Process Figma Tokens PR

on: pull_request

jobs:
  run_if: # only if base branch is "figma-tokens"
    if: ${{ github.head_ref == 'figma-tokens' }}
    name: Process Figma Tokens
    runs-on: ubuntu-latest
    timeout-minutes: 5
    steps:
      # checkout to the head of this PR
      - uses: actions/checkout@master
        with:
          repository: ${{ github.event.pull_request.head.repo.full_name }}
          ref: ${{ github.event.pull_request.head.ref }}

      - name: Install Dependencies
        run: npm install

      - name: Run token-transformer
        run: npx token-transformer sd-input/src/figma-tokens-output.json sd-input/src/sd-input.json --resolveReferences false

      - name: Run style-dictionary
        run: npx style-dictionary build --config sd.config.cjs

      - name: Commit changes
        uses: EndBug/add-and-commit@v9
        with:
          author_name: Example
          author_email: example@gmail.com
          message: 'chore: style-dictionary output'
          add: '.'

Make sure to change the author_name and author_email to use your own information.

Now push your changes to the default branch (either directly or through a pull request).

GitHub Sync

This is the last step, setting up the GitHub synchronization in your Figma Tokens Plugin and pushing the tokens.

In the Figma Tokens Plugin Settings, go to the Token Storage -> GitHub tab.

Click on Add new credentials and fill out the information to synchronize your tokens to GitHub.

Creating a Personal Access Token can be done in your GitHub Developer Settings.

Figma credentials

For our workflow, make sure to pick a default branch here that is not the GitHub repo's default branch. This workflow will automatically create pull requests to the default main branch when pushing our tokens from Figma.

For file path, we pick sd-input as our package folder which will act as the input for Style Dictionary.

After saving, it should prompt you to push your tokens. Push your tokens. Make sure you don't click "Create Pull Request", GitHub Actions will do this automatically for us!

Any time you want to push token updates, you can click the sync push button:

Push-button

What's more awesome is that due to Backlight's GitHub integration, your PR will get a comment with a link to a visual diff view so you can see your changes visually in the coded components.

Visual diff link GitHub commentVisual diff in Backlight