@csstools/postcss-design-tokens

中文文档
5.0.2发布于 2026/10/01MIT-0TypeScriptES Module近 30 天下载 9789

Use design tokens in your CSS

原始使用文档
文档目录11 节
  1. Usage
  2. Formats
  3. Options
  4. is
  5. unitsAndValues
  6. Customize function and at rule names
  7. Syntax
  8. Editor support
  9. @design-tokens rule
  10. design-token() function
  11. Further reading

PostCSS Design Tokens PostCSS Logo

npm install @csstools/postcss-design-tokens --save-dev

PostCSS Design Tokens lets you use design tokens in your CSS source files.

{
    "color": {
        "background": {
            "primary": { "value": "#fff" }
        }
    },
    "size": {
        "spacing": {
            "small": { "value": "16px" },
            "medium": { "value": "18px" },
            "medium-alias": { "value": "{size.spacing.medium}" }
        }
    },
    "viewport": {
        "medium": { "value": "35rem" }
    }
}
@design-tokens url('./tokens.json') format('style-dictionary3');

.foo {
    color: design-token('color.background.primary');
    padding-top: design-token('size.spacing.small');
    padding-left: design-token('size.spacing.small' to px);
    padding-bottom: design-token('size.spacing.small' to rem);
}

@media (min-width: design-token('viewport.medium')) {
    .foo {
        padding-bottom: design-token('size.spacing.medium-alias' to rem);
    }
}

/* becomes */

.foo {
    color: #fff;
    padding-top: 16px;
    padding-left: 16px;
    padding-bottom: 1rem;
}

@media (min-width: 35rem) {
    .foo {
        padding-bottom: 1.125rem;
    }
}

Usage

Add PostCSS Design Tokens to your project:

npm install postcss @csstools/postcss-design-tokens --save-dev

Use it as a PostCSS plugin:

const postcss = require('postcss');
const postcssDesignTokens = require('@csstools/postcss-design-tokens');

postcss([
    postcssDesignTokens(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */);

Formats

At this time there is no standardized format for design tokens. Although there is an ongoing effort to create this, we feel it is still too early to adopt this.

For the moment we only support Style Dictionary. Use style-dictionary3 in @design-tokens rules to pick this format.

Options

is

The is option determines which design tokens are used.
This allows you to generate multiple themed stylesheets
by running PostCSS multiple times with different configurations.

By default only @design-tokens without any when('foo') conditions are used.

This plugin itself does not produce multiple outputs, it only provides an API to change the output.

Example usage

For these two token files :

{
    "color": {
        "background": {
            "primary": { "value": "#0ff" }
        }
    }
}
{
    "color": {
        "background": {
            "primary": { "value": "#f0f" }
        }
    }
}

And this CSS :

@design-tokens url('./tokens-brand-1.json') format('style-dictionary3');
@design-tokens url('./tokens-brand-2.json') when('brand-2') format('style-dictionary3');

.foo {
    color: design-token('color.background.primary');
}

You can configure :

No is option.
postcssDesignTokens()
@design-tokens url('./tokens-brand-1.json') format('style-dictionary3');
@design-tokens url('./tokens-brand-2.json') when('brand-2') format('style-dictionary3');

.foo {
    color: design-token('color.background.primary');
}

/* becomes */

.foo {
    color: #0ff;
}
is option set to 'brand-2'.
postcssDesignTokens({ is: ['brand-2'] })
@design-tokens url('./tokens-brand-1.json') format('style-dictionary3');
@design-tokens url('./tokens-brand-2.json') when('brand-2') format('style-dictionary3');

.foo {
    color: design-token('color.background.primary');
}

/* becomes */

.foo {
    color: #f0f;
}

unitsAndValues

The unitsAndValues option allows you to control some aspects of how design values are converted to CSS. rem <-> px for example can only be calculated when we know the root font size.

rootFontSize

defaults to 16

postcssDesignTokens({
    unitsAndValues: {
        rootFontSize: 20,
    },
})
@design-tokens url('./tokens.json') format('style-dictionary3');

.foo {
    color: design-token('color.background.primary');
    padding-top: design-token('size.spacing.small');
    padding-left: design-token('size.spacing.small' to px);
    padding-bottom: design-token('size.spacing.small' to rem);
}

@media (min-width: design-token('viewport.medium')) {
    .foo {
        padding-bottom: design-token('size.spacing.medium-alias' to rem);
    }
}

/* becomes */

.foo {
    color: #fff;
    padding-top: 16px;
    padding-left: 16px;
    padding-bottom: 0.8rem;
}

@media (min-width: 35rem) {
    .foo {
        padding-bottom: 0.9rem;
    }
}

Customize function and at rule names

importAtRuleName

The importAtRuleName option allows you to set a custom alias for @design-tokens.

postcssDesignTokens({ importAtRuleName: 'tokens' })
@tokens url('./tokens.json') format('style-dictionary3');

.foo {
    color: design-token('color.background.primary');
    padding-top: design-token('size.spacing.small');
    padding-left: design-token('size.spacing.small' to px);
    padding-bottom: design-token('size.spacing.small' to rem);
}

/* becomes */

.foo {
    color: #fff;
    padding-top: 16px;
    padding-left: 16px;
    padding-bottom: 1rem;
}

valueFunctionName

The valueFunctionName option allows you to set a custom alias for design-token.

postcssDesignTokens({ valueFunctionName: 'token' })
@design-tokens url('./tokens.json') format('style-dictionary3');

.foo {
    color: token('color.background.primary');
    padding-top: token('size.spacing.small');
    padding-left: token('size.spacing.small' to px);
    padding-bottom: token('size.spacing.small' to rem);
}

/* becomes */

.foo {
    color: #fff;
    padding-top: 16px;
    padding-left: 16px;
    padding-bottom: 1rem;
}

Syntax

PostCSS Design Tokens is non-standard and is not part of any official CSS Specification.

Editor support

This is all very new and we hope that one day design tokens will become first class citizens in editors and other tools. Until then we will do our best to provide extensions. These will have rough edges but should illustrate were we want to go.

editorplugin
VSCodeCSSTools Design Tokens

@design-tokens rule

The @design-tokens rule is used to import design tokens from a JSON file into your CSS.

@design-tokens url('./tokens.json') format('style-dictionary3');
@design-tokens url('./tokens.json') format('style-dictionary3');
@design-tokens url('./tokens-dark-mode.json') format('style-dictionary3') when('dark');

You can also import tokens from an npm package:

@design-tokens url('node_modules:my-npm-package/tokens.json') format('style-dictionary3');
@design-tokens url('node_modules:my-npm-package/tokens-dark-mode.json') format('style-dictionary3') when('dark');
@design-tokens [ <url> | <string> ]
               [ when(<theme-condition>*) ]?
               format(<format-name>);

<theme-condition> = <string>

<format-name> = [ 'style-dictionary3' ]

All @design-tokens rules in a document are evaluated in order of appearance. If a token with the same path and name already exists it will be overridden.

All @design-tokens rules are evaluated before any design-token() functions.

@design-tokens rules can be conditional through when conditions. Multiple values can be specified in when.
Multiple conditions always have an AND relationship.

/* only evaluated when tooling receives 'blue' and 'muted' as arguments */
@design-tokens url('./tokens.json') format('style-dictionary3') when('blue' 'muted');

@design-tokens rules can never be made conditional through @supports, @media or other conditional rules.

@media (min-width: 500px) {
  @design-tokens url('./tokens.json') format('style-dictionary3'); /* always evaluated */
}

Any form of nesting is meaningless, @design-tokens will always be evaluated as if they were declared at the top level.

design-token() function

The design-token() function takes a token path and returns the token value.

.foo {
    color: design-token('color.background.primary');
}
design-token() = design-token( <token-path> [ to <unit> ]? )

<token-path> = <string>
<unit> = [ px | rem | ... ]

The plugin can convert px to rem and rem to px via the unitsandvalues plugin options. When a design token is unit-less any unit can be assigned with to.

Stylelint

Stylelint is able to check for unknown property values. Setting the correct configuration for this rule makes it possible to check even non-standard syntax.

    // Disallow unknown values for properties within declarations.
    'declaration-property-value-no-unknown': [
        true,
        {
            propertiesSyntax: {
                color: '| <design-token()>',
                // ... more properties ...
            },
            typesSyntax: {
                '<design-token()>': 'design-token( <string> [ to <ident> ]? )',
            },
        },
    ],

Further reading

版本与发布记录

@csstools/postcss-design-tokens 共发布 30 个版本,首次发布于 2022/05/23;过去 12 个月发布了 3 个版本。

版本发布日期距今
5.0.2latest2026/10/011 个月前
5.0.12026/09/251 个月前
5.0.02026/01/149 个月前
4.0.52025/05/271.5 年前
4.0.42024/11/022 年前
4.0.32024/10/242 年前

关于 @csstools/postcss-design-tokens 的常见问题

@csstools/postcss-design-tokens 是什么?

Use design tokens in your CSS

如何安装 @csstools/postcss-design-tokens?

在项目目录运行 npm install @csstools/postcss-design-tokens,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:postcss(^8.4)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@csstools/postcss-design-tokens 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@csstools/postcss-design-tokens 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@csstools/postcss-design-tokens 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.19.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@csstools/postcss-design-tokens 可以商用吗?

@csstools/postcss-design-tokens 使用 MIT-0 许可。声明的许可为 MIT-0,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@csstools/postcss-design-tokens 的下载量有多少?

根据 npm 官方下载统计,@csstools/postcss-design-tokens 在 2026/10/11 – 2026/11/09 这 30 天内被下载 9,789 次,日均约 326 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@csstools/postcss-design-tokens 还在维护吗?

最新版本 5.0.2 发布于 2026/10/01(1 个月前);至今共发布 30 个版本,其中过去 12 个月发布了 3 个;首次发布于 2022/05/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。