"readme": "# PostCSS Design Tokens [<img src=\"https://postcss.github.io/postcss/logo.svg\" alt=\"PostCSS Logo\" width=\"90\" height=\"90\" align=\"right\">][PostCSS]\n\n`npm install @csstools/postcss-design-tokens --save-dev`\n\n[PostCSS Design Tokens] lets you use design tokens in your CSS source files.\n\n```json\n{\n\t\"color\": {\n\t\t\"background\": {\n\t\t\t\"primary\": { \"value\": \"#fff\" }\n\t\t}\n\t},\n\t\"size\": {\n\t\t\"spacing\": {\n\t\t\t\"small\": { \"value\": \"16px\" },\n\t\t\t\"medium\": { \"value\": \"18px\" },\n\t\t\t\"medium-alias\": { \"value\": \"{size.spacing.medium}\" }\n\t\t}\n\t},\n\t\"viewport\": {\n\t\t\"medium\": { \"value\": \"35rem\" }\n\t}\n}\n```\n\n```css\n@design-tokens url('./tokens.json') format('style-dictionary3');\n\n.foo {\n\tcolor: design-token('color.background.primary');\n\tpadding-top: design-token('size.spacing.small');\n\tpadding-left: design-token('size.spacing.small' to px);\n\tpadding-bottom: design-token('size.spacing.small' to rem);\n}\n\n@media (min-width: design-token('viewport.medium')) {\n\t.foo {\n\t\tpadding-bottom: design-token('size.spacing.medium-alias' to rem);\n\t}\n}\n\n/* becomes */\n\n.foo {\n\tcolor: #fff;\n\tpadding-top: 16px;\n\tpadding-left: 16px;\n\tpadding-bottom: 1rem;\n}\n\n@media (min-width: 35rem) {\n\t.foo {\n\t\tpadding-bottom: 1.125rem;\n\t}\n}\n```\n\n## Usage\n\nAdd [PostCSS Design Tokens] to your project:\n\n```bash\nnpm install postcss @csstools/postcss-design-tokens --save-dev\n```\n\nUse it as a [PostCSS] plugin:\n\n```js\nconst postcss = require('postcss');\nconst postcssDesignTokens = require('@csstools/postcss-design-tokens');\n\npostcss([\n\tpostcssDesignTokens(/* pluginOptions */)\n]).process(YOUR_CSS /*, processOptions */);\n```\n\n\n\n## Formats\n\nAt this time there is no standardized format for design tokens.\nAlthough there is an ongoing effort to create this, we feel it is still too early to adopt this.\n\nFor the moment we only support [Style Dictionary](https://amzn.github.io/style-dictionary/#/).\nUse `style-dictionary3` in `@design-tokens` rules to pick this format.\n\n## Options\n\n### is\n\nThe `is` option determines which design tokens are used.<br>\nThis allows you to generate multiple themed stylesheets<br>by running PostCSS multiple times with different configurations.\n\nBy default only `@design-tokens` without any `when('foo')` conditions are used.\n\n_This plugin itself does not produce multiple outputs, it only provides an API to change the output._\n\n#### Example usage\n\n**For these two token files :**\n\n```json\n{\n\t\"color\": {\n\t\t\"background\": {\n\t\t\t\"primary\": { \"value\": \"#0ff\" }\n\t\t}\n\t}\n}\n```\n\n```json\n{\n\t\"color\": {\n\t\t\"background\": {\n\t\t\t\"primary\": { \"value\": \"#f0f\" }\n\t\t}\n\t}\n}\n```\n\n**And this CSS :**\n\n```css\n@design-tokens url('./tokens-brand-1.json') format('style-dictionary3');\n@design-tokens url('./tokens-brand-2.json') when('brand-2') format('style-dictionary3');\n\n.foo {\n\tcolor: design-token('color.background.primary');\n}\n```\n\n**You can configure :**\n\n##### No `is` option.\n\n```js\npostcssDesignTokens()\n```\n\n```css\n@design-tokens url('./tokens-brand-1.json') format('style-dictionary3');\n@design-tokens url('./tokens-brand-2.json') when('brand-2') format('style-dictionary3');\n\n.foo {\n\tcolor: design-token('color.background.primary');\n}\n\n/* becomes */\n\n.foo {\n\tcolor: #0ff;\n}\n```\n\n##### `is` option set to 'brand-2'.\n\n```js\npostcssDesignTokens({ is: ['brand-2'] })\n```\n\n```css\n@design-tokens url('./tokens-brand-1.json') format('style-dictionary3');\n@design-tokens url('./tokens-brand-2.json') when('brand-2') format('style-dictionary3');\n\n.foo {\n\tcolor: design-token('color.background.primary');\n}\n\n/* becomes */\n\n.foo {\n\tcolor: #f0f;\n}\n```\n\n### unitsAndValues\n\nThe `unitsAndValues` option allows you to control some aspects of how design values are converted to CSS.\n`rem` <-> `px` for example can only be calculated when we know the root font size.\n\n#### rootFontSize\n\ndefaults to `16`\n\n```js\npostcssDesignTokens({\n\tunitsAndValues: {\n\t\trootFontSize: 20,\n\t},\n})\n```\n\n```css\n@design-tokens url('./tokens.json') format('style-dictionary3');\n\n.foo {\n\tcolor: design-token('color.background.primary');\n\tpadding-top: design-token('size.spacing.small');\n\tpadding-left: design-token('size.spacing.small' to px);\n\tpadding-bottom: design-token('size.spacing.small' to rem);\n}\n\n@media (min-width: design-token('viewport.medium')) {\n\t.foo {\n\t\tpadding-bottom: design-token('size.spacing.medium-alias' to rem);\n\t}\n}\n\n/* becomes */\n\n.foo {\n\tcolor: #fff;\n\tpadding-top: 16px;\n\tpadding-left: 16px;\n\tpadding-bottom: 0.8rem;\n}\n\n@media (min-width: 35rem) {\n\t.foo {\n\t\tpadding-bottom: 0.9rem;\n\t}\n}\n```\n\n### Customize function and at rule names\n\n#### importAtRuleName\n\nThe `importAtRuleName` option allows you to set a custom alias for `@design-tokens`.\n\n```js\npostcssDesignTokens({ importAtRuleName: 'tokens' })\n```\n\n```css\n@tokens url('./tokens.json') format('style-dictionary3');\n\n.foo {\n\tcolor: design-token('color.background.primary');\n\tpadding-top: design-token('size.spacing.small');\n\tpadding-left: design-token('size.spacing.small' to px);\n\tpadding-bottom: design-token('size.spacing.small' to rem);\n}\n\n/* becomes */\n\n.foo {\n\tcolor: #fff;\n\tpadding-top: 16px;\n\tpadding-left: 16px;\n\tpadding-bottom: 1rem;\n}\n```\n\n#### valueFunctionName\n\nThe `valueFunctionName` option allows you to set a custom alias for `design-token`.\n\n```js\npostcssDesignTokens({ valueFunctionName: 'token' })\n```\n\n```css\n@design-tokens url('./tokens.json') format('style-dictionary3');\n\n.foo {\n\tcolor: token('color.background.primary');\n\tpadding-top: token('size.spacing.small');\n\tpadding-left: token('size.spacing.small' to px);\n\tpadding-bottom: token('size.spacing.small' to rem);\n}\n\n/* becomes */\n\n.foo {\n\tcolor: #fff;\n\tpadding-top: 16px;\n\tpadding-left: 16px;\n\tpadding-bottom: 1rem;\n}\n```\n\n## Syntax\n\n[PostCSS Design Tokens] is non-standard and is not part of any official CSS Specification.\n\n### Editor support\n\nThis is all very new and we hope that one day design tokens will become first class citizens in editors and other tools.\nUntil then we will do our best to provide extensions.\nThese will have rough edges but should illustrate were we want to go.\n\n| editor | plugin |\n| --- | --- |\n| VSCode | [CSSTools Design Tokens](https://marketplace.visualstudio.com/items?itemName=RomainMenke.csstools-design-tokens) |\n\n### `@design-tokens` rule\n\nThe `@design-tokens` rule is used to import design tokens from a JSON file into your CSS.\n\n```css\n@design-tokens url('./tokens.json') format('style-dictionary3');\n```\n\n```css\n@design-tokens url('./tokens.json') format('style-dictionary3');\n@design-tokens url('./tokens-dark-mode.json') format('style-dictionary3') when('dark');\n```\n\nYou can also import tokens from an `npm` package:\n\n```css\n@design-tokens url('node_modules:my-npm-package/tokens.json') format('style-dictionary3');\n@design-tokens url('node_modules:my-npm-package/tokens-dark-mode.json') format('style-dictionary3') when('dark');\n```\n\n```\n@design-tokens [ <url> | <string> ]\n [ when(<theme-condition>*) ]?\n format(<format-name>);\n\n<theme-condition> = <string>\n\n<format-name> = [ 'style-dictionary3' ]\n```\n\nAll `@design-tokens` rules in a document are evaluated in order of appearance.\nIf a token with the same path and name already exists it will be overridden.\n\nAll `@design-tokens` rules are evaluated before any `design-token()` functions.\n\n`@design-tokens` rules can be conditional through `when` conditions. Multiple values can be specified in `when`.<br>\nMultiple conditions always have an `AND` relationship.\n\n> ```css\n> /* only evaluated when tooling receives 'blue' and 'muted' as arguments */\n> @design-tokens url('./tokens.json') format('style-dictionary3') when('blue' 'muted');\n> ```\n\n`@design-tokens` rules can never be made conditional through `@supports`, `@media` or other conditional rules.\n\n> ```css\n> @media (min-width: 500px) {\n> @design-tokens url('./tokens.json') format('style-dictionary3'); /* always evaluated */\n> }\n> ```\n\nAny form of nesting is meaningless, `@design-tokens` will always be evaluated as if they were declared at the top level.\n\n\n### `design-token()` function\n\nThe `design-token()` function takes a token path and returns the token value.\n\n```css\n.foo {\n\tcolor: design-token('color.background.primary');\n}\n```\n\n```\ndesign-token() = design-token( <token-path> [ to <unit> ]? )\n\n<token-path> = <string>\n<unit> = [ px | rem | ... ]\n```\n\nThe plugin can convert `px` to `rem` and `rem` to `px` via the [`unitsandvalues`](#unitsandvalues) plugin options.\nWhen a design token is unit-less any `unit` can be assigned with `to`.\n\n#### [Stylelint](https://stylelint.io/user-guide/rules/declaration-property-value-no-unknown/#propertiessyntax--property-syntax-)\n\nStylelint is able to check for unknown property values.\nSetting the correct configuration for this rule makes it possible to check even non-standard syntax.\n\n```js\n\t// Disallow unknown values for properties within declarations.\n\t'declaration-property-value-no-unknown': [\n\t\ttrue,\n\t\t{\n\t\t\tpropertiesSyntax: {\n\t\t\t\tcolor: '| <design-token()>',\n\t\t\t\t// ... more properties ...\n\t\t\t},\n\t\t\ttypesSyntax: {\n\t\t\t\t'<design-token()>': 'design-token( <string> [ to <ident> ]? )',\n\t\t\t},\n\t\t},\n\t],\n```\n\n## Further reading\n\n- [Why we think PostCSS Design Tokens is needed]\n- [About Design Tokens (Adobe Spectrum)]\n\n[cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test\n\n[discord]: https://discord.gg/bUadyRwkJS\n[npm-url]: https://www.npmjs.com/package/@csstools/postcss-design-tokens\n\n[PostCSS]: https://github.com/postcss/postcss\n[PostCSS Design Tokens]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-design-tokens\n[Why we think PostCSS Design Tokens is needed]: https://github.com/csstools/postcss-plugins/wiki/Why-we-think-PostCSS-Design-Tokens-is-needed\n[About Design Tokens (Adobe Spectrum)]: https://spectrum.adobe.com/page/design-tokens/\n",
0 commit comments