style-dictionary-utils

style-dictionary-utils

Style Dictionary 的设计令牌处理工具集

style-dictionary-utils 是一个为 Style Dictionary 开发的工具集,用于处理 w3c 设计令牌。它包含解析器、过滤器、转换器和格式化器,支持颜色转换、尺寸单位转换和字体样式处理等功能。该工具兼容 ESM 和 CommonJS 模块系统,并能根据媒体查询和选择器生成高级 CSS 变量。它简化了设计系统开发流程,提高了开发效率。

Style Dictionary设计令牌JSON解析器CSS格式化JavaScript格式化Github开源项目

Style dictionary utils

npm GitHub release (release name instead of tag name)

style-dictionary-utils is a collection of parsers, filters, transformers and formats for Style Dictionary that make working with w3c design tokens a lot easier.

Installation

Install the style-dictionary-utils as well as style-dictionary.

npm i -D style-dictionary-utils style-dictionary

If you are using .json5 files to define your design tokens install json5 as well.

npm i -D json5

How to use style dictionary version 3?

If you are not ready to upgrade to style dictinary version 3 you can continue using style-dictionary-utils by locking to v2 currently v2.4.1 version.

Getting started

The easiest way to use style-dictionary-utils is to import the prepared StyleDictionary object into your build file:

// build.ts import StyleDictionary from 'style-dictionary-utils' const myStyleDictionary = StyleDictionary.extend({ "platforms": { "ts": { "transforms": ['color/hexAlpha', 'shadow/css'], "files": [{ "filter": "isSource", "destination": "tokens.ts", "format": "javascript/esm", }] } } }); myStyleDictionary.buildAllPlatforms();

Now all the included utilities<sup>*</sup> are available to you via the keys mentioned in the docs below.

<sup>*</sup> You only need to register the w3cTokenJson5Parser if you want to use json5.

Extending style dictionary

You can still extend style dictionary with your own transformers and formats like before. The only difference is that you must use the StyleDictionary object that you import from style-dictionary-utils.

// build.ts import StyleDictionary from 'style-dictionary-utils' StyleDictionary.registerTransform({ name: 'transform/pxToRem', type: `value`, transitive: true, transformer: () => // ... })

Look at the tests to get an idea how it works.

Included utilities

📠 Parsers

w3cTokenJsonParser

This parser parses .json with w3c design tokens.

This means the following files can be used with this parser.

{ "token": { "value": "#223344", "type": "color", "description": "token description" }, "w3cToken": { "$value": "#223344", "$type": "color", "$description": "token description" } }

The parser will keep most propertys as is and only change $value to value and $description or description to comment. This required for Style Dictionary.

To register the parsers add the following code to your build file.

import StyleDictionary from 'style-dictionary-utils' import { w3cTokenJsonParser } from 'style-dictionary-utils/dist/parser/w3c-token-json-parser' StyleDictionary.registerParser(w3cTokenJsonParser)

w3cTokenJson5Parser (not autoloaded)

If you are using .json5 or .jsonc files to define your design tokens you need to use the w3cTokenJson5Parser. This is NOT enabled by default as it requires an additonal package, json5, to work.

This parser is exactly the same as the w3cTokenJsonParser with the only difference that it can parse .json5 or .jsonc.

To register the parsers add the following code to your build file.

import StyleDictionary from 'style-dictionary-utils' import { w3cTokenJson5Parser } from 'style-dictionary-utils/dist/parser/w3c-token-json5-parser' StyleDictionary.registerParser(w3cTokenJson5Parser)

Make sure to install json5 by running npm i -D json5.

If you’re using Prettier, be aware that the default configuration removes quote props, which are needed in $type and $value props in order to parse the tokens.

Here’s an example of a prettier config that overrides the default:

semi: false singleQuote: true overrides: - files: '*.json[c|5]' options: quoteProps: preserve singleQuote: false

📑 Formats

javascript/esm

The javascript/esm format exports a token dictionary as an es6 export statement.

export default { colors: { primary: '#0D70E6' } }
Usage:
const myStyleDictionary = StyleDictionary.extend({ "platforms": { "ts": { "transforms": //..., "files": [{ // ... "format": "javascript/esm", }] } } });

javascript/commonJs

The javascript/commonJs format exports a token dictionary as an commonJs module.

exports.default = { colors: { primary: '#0D70E6' } }
Usage:
const myStyleDictionary = StyleDictionary.extend({ "platforms": { "js": { "transforms": //..., "files": [{ // ... "format": "javascript/commonJs", }] } } });

css/advanced

The css/advanced format exports a token dictionary as a css file with css variables. It allows you to define media queries that can wrap specific parts of your css variables. If nothing is defined the entire file will be wrapped in a :root selector.

You can change the selector by defining it in file.options.selector.

You can define rules on a file level using file.options.rules. If one or more rules are defined, only tokens within any of the rules will be output. You can define as many rule objects within file.options.rules as you want. Tokens can be part of one or multiple rules.

A rule object may have any or all of the three properties atRule, selector and matcher.

  • selector is a string that is wrapped around your css. If the selector is undefined, the default selector or one define at file.options.selector will be used. If you don't want a selector, set it to false.
  • atRule can be a string or array of strings, that are wrapped around the css and selector with the first being the outer layer.
  • matcher is a filter function that returns true for tokens that should be included in the query. If you want to match all tokens, just return true from the matcher.
body[theme="dark"] { --color-background-primary: #ff0000; --color-background-secondary: #0000ff; } @media (min-width: 768px) { body[theme="dark"] { --color-button-primary: #c1c1c1; --color-button-secondary: #007D79; } }
Usage:
const myStyleDictionary = StyleDictionary.extend({ "platforms": { "css": { "transforms": //..., "files": [{ // ... "format": "css/advanced", "options": { selector: `body[theme="dark"]`, // defaults to :root; set to false to disable rules: [ { atRule: '@media (min-width: 768px)', selector: `body[size="medium"]` // this will be used instead of body[theme="dark"]` matcher: (token: StyleDictionary.TransformedToken) => token.filePath.includes('tablet'), // tokens that match this filter will be added inside the media query }] } }] } } });

🤖 Transformers

Transforms change the value or name of a token. You can use transforms by refering the name in the array value of the transforms property of a platform.

Transform group

If you want to use the same transformers multiple times you can create a transform group for easy reference.

StyleDictionary.registerTransformGroup({ name: 'webHex', transforms: [ 'color/hexAlpha', 'dimension/pixelToRem', 'font/css' ] });

css/extended transform group

This packages ships a predefined transform group, called css/extended. It includes all transforms from the original css transform group as well as the following transforms: color/rgbAlpha, shadow/css, font/css, fontFamily/css, fontWeight/number, name/pathToDotNotation, cubicBezier/css, border/css.

You can use it like any other transform Group:

const myStyleDictionary = StyleDictionary.extend({ "platforms": { "css": { "transformGroup": 'css/extended', "files": [{ // ... }] } } });

name/pathToDotNotation

This name transformer replaces the token name with the entire path of the token in dot.notation. This is especially useful for flat .js or .json files.

To use it simply add name/pathToDotNotation to the transforms array.

const myStyleDictionary = StyleDictionary.extend({ "platforms": { "ts": { "transforms": ['name/pathToDotNotation'], "files": [{ // ... }] } } });
Before transformation
{ colors: { red: { 100: { // ... } } } }
After transformation
{ "colors.red.100": { // ... } }

name/pathToCamelCase

This name transformer replaces the token name with the entire path of the token in camelCase notation.

To use it simply add name/pathToCamelCase to the transforms array.

const myStyleDictionary = StyleDictionary.extend({ "platforms": { "ts": { "transforms": ['name/pathToCamelCase'], "files": [{ // ... }] } } });
Before transformation
{ colors: { bg: { default: { // ... } } } }
After transformation
{ "colorsBgDefault": { // ... } }

color/rgbAlpha

This value transformer replaces the value of a token with a $type or type of color with an rgba string. If the token has an alpha value, it will be used as the alpha of the rgba string.

Note: If your initial color value has an alpha value (e.g. hex8) AND you add an alpha property, the alpha property will simply replace the previous alpha value.

const myStyleDictionary = StyleDictionary.extend({ "platforms": { "ts": { "transforms": ['color/rgbAlpha'], "files": [{ // ... }] } } });
Before transformation
{ colors: { blue: { 500: { value: "#0D70E6", $type: "color", alpha: 0.4 } } } }
After transformation
{ colors: { blue: { 500: { value: "rgba(13, 112, 230, 0.4)", $type: "color", alpha: 0.4 } } } }

color/hexAlpha

This value transformer replaces the value of a token with a $type or type of color with a hex string. If the token has an alpha value, it will be used as the alpha of the hex8 string.

Note: If your initial color value has an alpha value (e.g. rgba) AND you add an alpha property, the alpha property will simply replace the previous alpha value.

const myStyleDictionary = StyleDictionary.extend({ "platforms": { "ts": { "transforms": ['color/hexAlpha'], "files": [{ // ... }] } } });
Before transformation
{ colors: { blue: { 500: { value: "rgba(13, 112, 230, 0.4)", $type: "color", alpha: 0.2 } } } }
After transformation
{ colors: { blue: { 500: { value: "#0D70E633", // prev alpha value is replaced with 0.2 from alpha property $type: "color", alpha: 0.2 } } } }

color/hex

This value transformer replaces the value of a token with a $type or type of color with a hex string.

const myStyleDictionary = StyleDictionary.extend({ "platforms": { "ts": { "transforms": ['color/hex'], "files": [{ // ... }] } } });
Before transformation
{ colors: { blue: { 500: { value: "rgba(13, 112, 230, 0.4)", $type: "color" } } } }
After transformation
{ colors: { blue: { 500: { value: "#0D70E666", $type: "color" } } } }

color/rgba

This value transformer replaces the value of a token with a $type or type of color with an rgba string.

const myStyleDictionary = StyleDictionary.extend({ "platforms": { "ts": { "transforms": ['color/rgba'], "files": [{ // ... }] } } });
Before transformation
{ colors: { blue: { 500: { value: "#0D70E666", $type: "color" } } } }
After

编辑推荐精选

TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
蛙蛙写作

蛙蛙写作

AI小说写作助手,一站式润色、改写、扩写

蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。

AI辅助写作AI工具蛙蛙写作AI写作工具学术助手办公助手营销助手AI助手
问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

热门AI助手AI对话AI工具聊天机器人
Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

AI办公办公工具AI工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图热门
讯飞星火

讯飞星火

深度推理能力全新升级,全面对标OpenAI o1

科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。

热门AI开发模型训练AI工具讯飞星火大模型智能问答内容创作多语种支持智慧生活
Spark-TTS

Spark-TTS

一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型

Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。

咔片PPT

咔片PPT

AI助力,做PPT更简单!

咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。

讯飞绘文

讯飞绘文

选题、配图、成文,一站式创作,让内容运营更高效

讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。

热门AI辅助写作AI工具讯飞绘文内容运营AI创作个性化文章多平台分发AI助手
材料星

材料星

专业的AI公文写作平台,公文写作神器

AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。

下拉加载更多