Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Configuring Style Dictionary

Introduction

Design tokens are the visual design atoms of the product's UI, including colors, typography, spacing, etc. Style Dictionary is a build system that allows you to manage and transform design tokens into various formats. By configuring Style Dictionary, you can automate the generation of design tokens for use in your projects.

What is Style Dictionary?

Style Dictionary is a powerful tool created by Amazon that allows you to define design tokens in a single source and transform them into different formats (JSON, SCSS, etc.) for use in your applications.

Installation

Step 1: Install Style Dictionary

npm install style-dictionary --save-dev

Configuring Style Dictionary

To configure Style Dictionary, you need to create a configuration file that specifies how your tokens should be defined and transformed.

Step 1: Create a Configuration File

Create a file named config.json in your project root:

{
    "source": ["tokens/**/*.json"],
    "platforms": {
        "scss": {
            "transformGroup": "scss",
            "buildPath": "build/scss/",
            "files": [{
                "destination": "variables.scss",
                "format": "scssVariables"
            }]
        }
    }
}

Step 2: Define Your Design Tokens

Create a directory named tokens and add a file with your tokens in JSON format:

{
    "color": {
        "primary": {
            "value": "#FF5733"
        },
        "secondary": {
            "value": "#33FF57"
        }
    },
    "font": {
        "baseSize": {
            "value": "16px"
        }
    }
}

Step 3: Build Your Tokens

To generate the tokens in the desired format, run the following command:

npx style-dictionary build
Note: Ensure you have the appropriate permissions to write to the build directory.

Best Practices

  • Use meaningful names for your tokens to ensure clarity.
  • Group related tokens to maintain organization.
  • Regularly update and audit your tokens to prevent redundancy.

FAQ

What file formats can Style Dictionary generate?

Style Dictionary can generate formats such as JSON, SCSS, LESS, and many others depending on your configuration.

Can I use Style Dictionary with React?

Yes, you can integrate Style Dictionary with any framework, including React, by importing the generated token files.

Is Style Dictionary suitable for large projects?

Absolutely! Style Dictionary is designed to handle large-scale token management efficiently.

Flowchart: Configuring Style Dictionary

graph TD;
            A[Start] --> B[Install Style Dictionary];
            B --> C[Create Config File];
            C --> D[Define Design Tokens];
            D --> E[Run Build Command];
            E --> F[Tokens Generated];
            F --> G[Use in Your Project];