Code Monkey home page Code Monkey logo

style-dictionary's Introduction

What's new in Style Dictionary 3.0!
What's coming in Style Dictionary 4.0?

Style Dictionary logo and mascot

npm version license PRs welcome GitHub Workflow Status downloads

Style Dictionary

Style once, use everywhere.

A Style Dictionary uses design tokens to define styles once and use those styles on any platform or language. It provides a single place to create and edit your styles, and exports these tokens to all the places you need - iOS, Android, CSS, JS, HTML, sketch files, style documentation, etc. It is available as a CLI through npm, but can also be used like any normal node module if you want to extend its functionality.

When you are managing user experiences, it can be quite challenging to keep styles consistent and synchronized across multiple development platforms and devices. At the same time, designers, developers, PMs and others must be able to have consistent and up-to-date style documentation to enable effective work and communication. Even then, mistakes inevitably happen and the design may not be implemented accurately. Style Dictionary solves this by automatically generating style definitions across all platforms from a single source - removing roadblocks, errors, and inefficiencies across your workflow.

For detailed usage head to https://amzn.github.io/style-dictionary

Watch the Demo on YouTube

Watch the video

Experiment in the playground

Try the browser-based Style Dictionary playground: https://www.style-dictionary-play.dev/, built by the folks at <div>RIOTS.

Contents

Installation

Note that you must have node (and npm) installed.

If you want to use the CLI, you can install it globally via npm:

$ npm install -g style-dictionary

Or you can install it like a normal npm dependency. This is a build tool and you are most likely going to want to save it as a dev dependency:

$ npm install -D style-dictionary

If you want to install it with yarn:

$ yarn add style-dictionary --dev

Usage

CLI

$ style-dictionary build

Call this in the root directory of your project. The only thing needed is a config.json file. There are also arguments:

Flag Short Flag Description
--config [path] -c Set the config file to use. Must be a .json file
--platform [platform] -p Only build a specific platform defined in the config file.
--help -h Display help content
--version -v Display the version

Node

You can also use the style dictionary build system in node if you want to extend the functionality or use it in another build system like Grunt or Gulp.

const StyleDictionary = require('style-dictionary').extend('config.json');

StyleDictionary.buildAllPlatforms();

The .extend() method is an overloaded method that can also take an object with the configuration in the same format as a config.json file.

const StyleDictionary = require('style-dictionary').extend({
  source: ['tokens/**/*.json'],
  platforms: {
    scss: {
      transformGroup: 'scss',
      buildPath: 'build/',
      files: [{
        destination: 'variables.scss',
        format: 'scss/variables'
      }]
    }
    // ...
  }
});

StyleDictionary.buildAllPlatforms();

Example

Take a look at some of our examples

A style dictionary is a collection of design tokens, key/value pairs that describe stylistic attributes like colors, sizes, icons, motion, etc. A style dictionary defines these design tokens in JSON or Javascript files, and can also include static assets like images and fonts. Here is a basic example of what the package structure can look like:

├── config.json
├── tokens/
│   ├── size/
│       ├── font.json
│   ├── color/
│       ├── font.json
│   ...
├── assets/
│   ├── fonts/
│   ├── images/

config.json

This tells the style dictionary build system how and what to build. The default file path is config.json or config.js in the root of the project, but you can name it whatever you want by passing in the --config flag to the CLI.

{
  "source": ["tokens/**/*.json"],
  "platforms": {
    "scss": {
      "transformGroup": "scss",
      "buildPath": "build/",
      "files": [{
        "destination": "scss/_variables.scss",
        "format": "scss/variables"
      }]
    },
    "android": {
      "transformGroup": "android",
      "buildPath": "build/android/",
      "files": [{
        "destination": "font_dimens.xml",
        "format": "android/fontDimens"
      }]
    }
  }
}
Attribute Type Description
source Array An array of file path globs to design token files. Style Dictionary will do a deep merge of all of the token files, allowing you to organize your files files however you want.
include Array An array of file path globs to design token files that contain default styles. The Style Dictionary uses this as a base collection of tokens. The tokens found using the "source" attribute will overwrite tokens found using include.
platforms Object Sets of platform files to be built.
platform.transformGroup String (optional) Apply a group of transforms to the tokens, must either define this or transforms.
platform.transforms Array (optional) Transforms to apply sequentially to all tokens. Can be a built-in one or you can create your own.
platform.buildPath String (optional) Base path to build the files, must end with a trailing slash.
platform.files Array (optional) Files to be generated for this platform.
platform.file.destination String (optional) Location to build the file, will be appended to the buildPath.
platform.file.format String (optional) Format used to generate the file. Can be a built-in one or you can create your own. More on formats
platform.file.options Object (optional) A set of extra options associated with the file.
platform.file.options.showFileHeader Boolean If the generated file should have a "Do not edit + Timestamp" header (where the format supports it). By default is "true".

Design Tokens

{
  "size": {
    "font": {
      "small" : { "value": "10px" },
      "medium": { "value": "16px" },
      "large" : { "value": "24px" },
      "base"  : { "value": "{size.font.medium.value}" }
    }
  }
}

Here we are creating some basic font size design tokens. The style definition size.font.small.value is "10px" for example. The style definition size.font.base.value is automatically aliased to the value found in size.font.medium.value and both of those resolve to "16px".

Now what the style dictionary build system will do with this information is convert it to different formats, enabling these values to be used in any type of codebase. From this one file you can generate any number of files like:

$size-font-small: 10px;
$size-font-medium: 16px;
$size-font-large: 24px;
$size-font-base: 16px;
<dimen name="font-small">10sp</dimen>
<dimen name="font-medium">16sp</dimen>
<dimen name="font-large">24sp</dimen>
<dimen name="font-base">16sp</dimen>
float const SizeFontSmall = 10.00f;
float const SizeFontMedium = 16.00f;
float const SizeFontLarge = 24.00f;
float const SizeFontBase = 16.00f;

Quick Start

The style dictionary framework comes with some example code to get you started. Install the node module globally, create a directory and cd into it.

$ npm i -g style-dictionary
$ mkdir MyStyleDictionary
$ cd MyStyleDictionary

Now run:

$ style-dictionary init basic

This command will copy over the example files found in example in this repo. Now you have an example project set up. You can make changes to the style dictionary and rebuild the project by running:

$ style-dictionary build

Take a look at the documentation for the example code.

Design Tokens

A design token is an attribute to describe something visually. It is atomic (it cannot be broken down further). Design tokens have a name, a value, and optional attributes or metadata. The name of a token can be anything, but we have a proposed naming structure that we find works really well in the next section.

Category/Type/Item Structure

While not exactly necessary, we feel this classification structure of design tokens makes the most sense semantically. Design tokens can be organized into a hierarchical tree structure with the top level, category, defining the primitive nature of the token. For example, we have the color category and every token underneath is always a color. As you proceed down the tree to type, item, sub-item, and state, you get more specific about what that color is. Is it a background color, a text color, or a border color? What kind of text color is it? You get the point. Now you can structure your token json files like simple objects:

{
  "size": {
    "font": {
      "base":  { "value": "16" },
      "large": { "value": "20" }
    }
  }
}

The CTI is implicit in the structure, the category and type is 'size' and 'font', and there are 2 tokens 'base' and 'large'.

Structuring design tokens in this manner gives us consistent naming and accessing of these tokens. You don't need to remember if it is button_color_error or error_button_color, it is color_background_button_error!

You can organize and name your design tokens however you want, there are no restrictions. But we have a good amount of helpers if you do use this structure, like the 'attribute/cti' transform which adds attributes to the token of its CTI based on the path in the object. There are a lot of name transforms as well for when you want a flat structure like for Sass variables.

Also, the CTI structure provides a good mechanism to target transforms for specific kinds of tokens. All of the transforms provided by the framework use the CTI of a token to know if it should be applied. For instance, the 'color/hex' transform only applies to tokens of the category 'color'.

You can also add a comment to a design token:

{
  "size": {
    "font": {
      "base":  {
        "value": "16",
        "comment": "the base size of the font"
      },
      "large": {
        "value": "20",
        "comment": "the large size of the font"
      }
    }
  }
}

The comment will appear in the output files, where relevant or the output format supports comments.

Extending

The style dictionary build system is made to be extended. We don't know exactly how everyone will want to use style dictionaries in their project, which is why you can create custom transforms and formats.

const StyleDictionary = require('style-dictionary').extend('config.json');

StyleDictionary.registerTransform({
  name: 'time/seconds',
  type: 'value',
  matcher: function(token) {
    return token.attributes.category === 'time';
  },
  transformer: function(token) {
    return (parseInt(token.original.value) / 1000).toString() + 's';
  }
});

StyleDictionary.buildAllPlatforms();

For more information on creating your own transforms and formats, take a look at our docs.

Version 4

In May 2021, we started an issue / RFC, "What would you like to see in Style-Dictionary 4.0?" to gather feedback on what the community would like to see. Fortunately, in August 2023, the folks at Tokens Studio contacted us about co-maintaining this project, and leading the v4 release (and beyond)!

We have started working on the biggest and most important changes, like migrating to ESM, making the library browser-compatible out of the box, and supporting asynchronicity in Style-Dictionary's various APIs. There will be multiple prereleases to battle-test these changes before a final v4.0 is released.

You can follow this roadmap board to keep an eye on the developments for v4.0, we will also keep adding to this board when we encounter changes we'd like to see in v4.0 that would entail a breaking change. Absence of something in this roadmap does not mean we don't see value in it, but rather that it could also be added in a (non-breaking) minor release within v4.x.x.

From the folks at Tokens Studio

Hi everyone! I'm Joren from Tokens Studio, a big fan of this project (see Style-Dictionary-Play, Token Configurator, sd-transforms) and the main pusher behind leading a 4.0 release of this project, I think it would be good to explain from our perspective why we've made the move to collaborate with Amazon on this.

At Tokens Studio, we're a huge fan of Design Tokens and the workflows they enable. We believe exporting design tokens to various platforms is a key ingredient in ensuring that the journey from design to implementation code is smooth. In our minds, Style-Dictionary has been the most popular and most flexible library for reaching that goal, and so we want to build on top of that. Rather than starting our own spinoff tool, we much prefer bringing Style-Dictionary further, together with its vibrant community of contributors, which is why we reached out to Danny Banks.

I think it's important to stress that it is our shared vision to keep Style-Dictionary as an agnostic (so not "Tokens Studio"-specific) and flexible tool. As Tokens Studio, while we are highly incentivized to see this project progress further to strengthen our product journey, we value the open source community highly and want to make sure this library remains the go-to tool for exporting Design Tokens, whether you use Tokens Studio or not.

We are very open to feedback and collaboration, feel free to reach out to us in our Slack -> style-dictionary-v4 channel!

Mascot

The mascot for Style Dictionary is "Pascal" the chameleon (seen below). You can also find them blending in as the logo throughout the documentation.

Style Dictionary logo and mascot

Contributing

Please help make this framework better. For more information take a look at CONTRIBUTING.md

License

Apache 2.0

style-dictionary's People

Contributors

7studio avatar chazzmoney avatar coliff avatar davixyz avatar dbanksdesign avatar dependabot[bot] avatar didoo avatar dru89 avatar iglvzx avatar jakobe avatar jbarreiros avatar jorenbroekema avatar josephyi avatar jreichenberg avatar kharrop avatar kyh avatar kylegach avatar lukasoppermann avatar markacianfrani avatar mattleff avatar mdemetrio avatar nhoizey avatar owensgit avatar pnkapadia64 avatar rickycpadilla avatar silversonicaxel avatar tenga avatar tiamanti avatar tonyjwalt avatar yoniholmes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

style-dictionary's Issues

Create readme

Potential items to include:

  • What it is
  • Why Danny (Amazon?) made it
  • Link to demo video
  • Link to tutorial
  • Link to explanation of structure of style dictionary JSON
  • Link to list of options in using the style dictionary
  • What is the customer problem we are solving?

Add verbose option to CLI

Maybe turn it on by default too? It would be nice to see what files are being generated in the stdout.

Inconsistent/unexpected behavior of formats

Some of the formats (e.g. scss/variables javascript/es6) output flat dictionaries keyed by each properties name (as specified by a name transform like name/cti/camel). Other formats output the entire dictionary (e.g. javascript/umd or json) along with all the internal metadata. This behavior was surprising to me (despite the fact that I added javascript/umd myself heh). Here are a couple things I would expect from formatters:

  • They respect any name transforms I supply to them
  • They output only transformed property values, not internal dictionary metadata etc.

I don't know if these are reasonable expectations or, if so, what the best solution is. I mostly wanted to get the conversation started because it feels like the current state is inconsistent and makes certain usages impossible (e.g. getting a umd module with nice camelCaseProperties).

One potential solution: Any format that supports nested data structures (e.g. json, js) should use those structures IFF a name transform is not supplied. If a name transform is supplied then they should output flat data structures using the names that result from the name transform. The data under each key should always be just the transformed property value and should not include any dictionary metadata. If the user needs a raw dump of the dictionary they could use the actions API.

What are your thoughts? Am I just misunderstanding the API here?

Create sketch plugin?

Create a sketch plugin that allows an export to a style-dictionary collection of JSON files.

Error when parsing value with space...

Works

{
  "color": {
    "font": {
      "base": {
        "value": "{color.black.value}"
      }
    }
  }
}

Doesn't work:

{
  "color": {
    "font": {
      "base": {
        "value": "{ color.black.value }"
      }
    }
  }
}

Error Message

Error: Reference doesn't exist:  color.black.value

Support for nested transformGroups

Would help with extensibility to allow transformGroups to declare other transformGroups. For example:

StyleDictionary.registerTransformGroup({
  name: 'my-group',
  transforms: [
    'custom/transform',
    'scss'
  ]
});

That way, custom transforms don't need to redeclare the contents of an existing transform group when adding net-new transforms.

Reference modifiers

Would it be useful to add in support to modify property references in a style dictionary?

Some examples:

  • Having a base font size, and modify by multipliers
  • Lighten or darken colors, although maybe not recommended because the developer would not have explicit control over exactly what the color will be. Multiply font sizes is easy to understand (16 * 1.5 = 24)
  • Something else?

Update dependencies and clean up package/yarn files

  • Update dependencies if necessary
  • Make sure package-lock.json, yarn.lock, and package.json files are in sync so that fresh installs are deterministic and don't change those files
  • Update contribution docs to reflect the proper way to checkout the repo and install dependencies

Filter support across the board

Summary

While doing some custom implementation where I need to filter very granularly certain properties for specific platform I noticed that the filtering system seems to be applied on some templates only.
In iOS Templates we have:

<% var props = _.filter(allProperties, this.filter); %>

Which basically takes whatever is coming from the configuration object as filter.

In Android, it's slightly different, color for instance:

var props = _.filter(allProperties, function(prop) {

It will only filter colors... but if we want to add a custom filter, we can't.

Proposals

  1. Standardize filters within templates/formats to use whatever is in config and extend a certain configuration.
Example in Android:

var props = _.filter(allProperties, function(prop) {

Changes
var defaultFilter = {
  attributes: {
    category: "color"
  }
};
var props = _.filter(allProperties, Object.extend({}, this.filter, defaultFilter));
  1. Add this filtering pre-processing to any transform; So we don't need to pre-filter at template/format level but the library does it automatically for everything.

Create Tutorial

Create tutorials to show how to use the framework.

  1. A simple version that uses our example code and show changes making their way to iOS, Android, and web.
  2. We should also have a step-by-step tutorial from scratch that goes more in-depth.
  3. We should also have migration tutorials that show how to integrate the framework into existing codebases.

Create demo video

Quick timeline (2 minutes max):

  • "Hi, I'm Danny, let me show you what style dictionary does for you"
  • "here we can see the app I've built. here it is in iOS. And Android. And here is our style documentation."
  • "Okay, now you've seen the app. Say that I've now decided that I want to change the default text to 16 point for readability." (make the change)
  • "we've also decided to go with an orange theme instead of blue." (make the change)
  • "I'm also adding a new font type for validation errors in forms" (add it)
  • "Now I save the style dictionary. Everything rebuilds."
  • "Lets go look at our app. here is iOS. Here is android. And here is our style documentation."

Create a GUI interface

  • should be able to edit JSON files by hand
  • should be able to see what this looks like (html/css example?)
  • should be able to modify configuration
  • should be able to specify integration points with projects
  • should be able to click a button and create / upload / save output files wherever

Original property with reference being overridden

When we resolve property references in the JSON like "{color.font.base}", we resolve ALL references including the 'original' object which is meant to be an immutable copy of the original property. We do not want to resolve that reference.

Need to add unit tests to catch this.

Add Custom Parser Support

Acceptance Criteria

User should be able to supply a custom parser to the style dictionary to be used when reading property files. It should be done in a generic way so that any parser (yaml, hjson, and any other) could conceivably be used. A parser should be a function that takes a file (as a path string) as input, and outputs a JS object. Any function that follows this could be used as a parser.

How does this benefit the user?

Some do not like writing JSON, it can be verbose and not as simple as something like yaml. This will allow users to use any data formatting syntax to describe their style dictionaries. This fits in with the spirit of style dictionary in being flexible to accommodate any project/platform/framework. At its core, a style dictionary is a data structure for defining styles; the filetype the data structure is written in is irrelevant.

Add icons to static style guide

Once the css/fonts format PR is included and the font faces can be generated, make sure we show the icons in the static style guide.

Add support for non-string values

Currently, when style dictionary tries to transform a value it does this:

if (transform.type === 'value' && property.value.indexOf('{') < 0)

lib/transform/property.js

However if you put a number as a value, which should make sense for unit-less numbers like line-height or milliseconds. I think we just need to add a check in there to make sure property.value is a string before testing for '{'

Promises and Async for actions

Some actions we might want to run asynchronously to speed up build times, others we might want to run synchronously in case there are later actions that depend on the output of previous actions. This will be a placeholder for thoughts on asynchronous v. synchronous actions.

Create Swift Example App

Objective-C is cool and all, but we should really have a swift example app as that is the future.

Clean action

There should be a better way to clean build artifacts. Because you can generate files in any place, if you change paths around there might be zombie artifacts littered around.

How to handle colors with transparency?

I've been trying to figure it out how to add a transparent variant of a color.
something like this:

{
  "color": {
    "base": {
      "transparent": { "value": "#DDFF3300"}
  }
}

Normally in CSS you would use the word transparent but I know that's not cross platform compatible, so I was thinking maybe of using the hex8 value in here, so a total transparent could be #00000000.
When I run this with

    scss: {
      transformGroup: 'scss',
      buildPath: 'build/',
      files: [{
        destination: 'scss/variables.scss',
        format: 'scss/variables',
      }],

I'm just getting the first 6 values due to this group only handling the transforms as hex. Is there a way to specify a custom transform at property level? or how would I go about this... searched a lot in the docs and I ended up a little bit more confused on how to handle this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.