### Initialize a new package with an example using the CLI Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/getting-started/examples.md Creates a folder, navigates into it, and runs the Style Dictionary CLI to generate a new package from an example. Replace [example] with 'basic' or 'complete'. ```bash mkdir MyFolder cd MyFolder style-dictionary init [example] ``` -------------------------------- ### buildPlatform usage example Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/api.mdx Example showing how to call buildPlatform for a single platform. The JavaScript example uses await, and the Bash command shows the CLI equivalent. ```javascript // Async, so you can do `await` or .then() if you // want to execute code after buildAllPlatforms has completed await sd.buildPlatform('web'); ``` ```bash style-dictionary build --platform web ``` -------------------------------- ### buildAllPlatforms usage example Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/api.mdx Example showing how to use buildAllPlatforms to build all platforms. The JavaScript example imports StyleDictionary, creates an instance, and calls buildAllPlatforms. The Bash command shows the CLI equivalent. ```javascript import StyleDictionary from 'style-dictionary'; const sd = new StyleDictionary('config.json'); // Async, so you can do `await` or .then() if you // want to execute code after buildAllPlatforms has completed await sd.buildAllPlatforms(); ``` ```bash style-dictionary build ``` -------------------------------- ### Install and run Storybook Source: https://github.com/style-dictionary/style-dictionary/blob/main/examples/advanced/storybook-token-docs/README.md Install dependencies and start Storybook. `npm run storybook` builds tokens and starts Storybook at http://localhost:6006. Use `npm run build-storybook` for a static build and `npm test` for contrast helper tests. ```sh npm install npm run storybook ``` -------------------------------- ### Initialize Basic Example Source: https://github.com/style-dictionary/style-dictionary/blob/main/README.md Run the style-dictionary init command to copy example files into the current directory. ```bash $ style-dictionary init basic ``` -------------------------------- ### Install Style Dictionary locally Source: https://github.com/style-dictionary/style-dictionary/blob/main/examples/basic/README.md Initializes a new npm project and installs Style Dictionary locally. Run this before the build command if Style Dictionary is not installed globally. ```bash npm init -y && npm install style-dictionary ``` -------------------------------- ### Install Style Dictionary with yarn Source: https://github.com/style-dictionary/style-dictionary/blob/main/README.md Install as a dev dependency using yarn. ```bash $ yarn add style-dictionary --dev ``` -------------------------------- ### json/flat format example Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Example output for the json/flat format, showing flat key-value pairs. ```json { "color-base-red": "#ff0000" } ``` -------------------------------- ### Live demo config with outputReferencesTransformed Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Utils/references.md Configuration for the live demo, using outputReferencesTransformed in the CSS platform. This is the same setup as the main example but without the tokens definition. ```javascript import { outputReferencesTransformed } from 'style-dictionary/utils'; import { formats, transformGroups } from 'style-dictionary/enums'; export default { platforms: { css: { transformGroup: transformGroups.css, transforms: ['ts/color/css/hexrgba'], files: [ { destination: 'vars.css', format: formats.cssVariables, options: { outputReferences: outputReferencesTransformed, }, }, ], }, }, }; ``` -------------------------------- ### Install Style Dictionary Source: https://github.com/style-dictionary/style-dictionary/blob/main/README.md Install the style-dictionary node module globally, create a directory, and change into it. ```bash $ npm i -g style-dictionary $ mkdir MyStyleDictionary $ cd MyStyleDictionary ``` -------------------------------- ### Example file header configuration Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/file-headers.md Complete configuration example using a file header with a custom message and applying it platform-wide. ```javascript import { transformGroups } from 'style-dictionary/enums'; export default { hooks: { fileHeaders: { 'my-file-header': (defaultMessages = []) => [ 'Ola, planet!', ...defaultMessages, 'Hello, World!', ], }, }, platforms: { css: { transformGroup: transformGroups.css, options: { fileHeader: 'my-file-header', }, files: [ { format: formats.cssVariables, destination: '_variables.css', }, ], }, }, }; ``` -------------------------------- ### Install and build the style dictionary Source: https://github.com/style-dictionary/style-dictionary/blob/main/examples/complete/README.md Install dependencies and run the build task to generate platform files. Run this command again after any changes to the style dictionary. ```bash $ npm install $ npm run build ``` -------------------------------- ### json/asset format example Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Example output for the json/asset format, showing an asset token with a value path. ```json { "asset": { "image": { "logo": { "value": "assets/logo.png" } } } } ``` -------------------------------- ### sketch/palette format example Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Example output for the sketch/palette format, showing a sketchpalette file with a list of colors. ```json { "compatibleVersion": "1.0", "pluginVersion": "1.1", "colors": ["#ffffff", "#ff0000", "#fcfcfc"] } ``` -------------------------------- ### css/variables example Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Creates a CSS file with variable definitions. The example shows the default output with a :root selector and token variables. ```css :root { --color-background-base: #f0f0f0; --color-background-alt: #eeeeee; } ``` -------------------------------- ### Build Style Dictionary with CLI Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/examples/basic.mdx Run the Style Dictionary build process from the command line. This is equivalent to the Node API used in the interactive demo. ```bash npx style-dictionary build ``` -------------------------------- ### android/colors resources.xml example Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Example XML output for the android/colors format, showing color resources. ```xml #fffaf3f2 #fff0cccc #ffe19d9c ``` -------------------------------- ### android/strings icons.xml example Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Example XML output for the android/strings format, showing string resources. ```xml ``` -------------------------------- ### Expand utility example with tokens and config Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/config.md Example tokens and configuration for the expand utility. The config includes an 'include' array to expand only the 'border' token, a custom typesMap mapping border style to 'borderStyle', and platform configurations for CSS and JS output. ```json { "border": { "type": "border", "value": { "width": "2px", "style": "solid", "color": "#000" } }, "typography": { "type": "typography", "value": { "fontWeight": "800", "fontSize": "16px", "fontFamily": "Arial Black" } } } ``` ```json { "expand": { "include": ["border"], "typesMap": { "border": { "style": "borderStyle" } } }, "platforms": { "css": { "transformGroup": "css", "files": [ { "destination": "vars.css", "format": "css/variables" } ], "expand": true }, "js": { "transformGroup": "js", "files": [ { "destination": "tokens.js", "format": "javascript/es6" } ] } } } ``` -------------------------------- ### Output CSS with custom file header Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/index.md Example output of a CSS file that starts with the custom file header. Shows the default message followed by the custom lines. ```css /** * Do not edit directly * Generated on Thu, 18 Mar 2021 21:30:47 GMT * hello? * is it me you're looking for? */ ``` -------------------------------- ### sketch/palette/v2 format example Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Example output for the sketch/palette/v2 format, showing a sketchpalette file with color objects. ```json { "compatibleVersion": "2.0", "pluginVersion": "2.2", "colors": [ { "name": "red", "r": 1.0, "g": 0.0, "b": 0.0, "a": 1.0 }, { "name": "green", "r": 0.0, "g": 1.0, "b": 0.0, "a": 1.0 }, { "name": "blue", "r": 0.0, "g": 0.0, "b": 1.0, "a": 1.0 } ] } ``` -------------------------------- ### Example output of android/resources format Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Example Android resources XML file generated by the android/resources format. It uses resource types like color and dimen based on token categories. The example is truncated in the source. ```xml #fffaf3f2 #fff0cccc 14sp ``` -------------------------------- ### json/nested format example Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Example output for the json/nested format, showing nested color values. ```json { "color": { "base": { "red": "#ff0000" } } } ``` -------------------------------- ### Example package structure Source: https://github.com/style-dictionary/style-dictionary/blob/main/README.md Shows the typical directory layout for a style dictionary project, including config.json, tokens, and assets folders. ```text ├── config.json ├── tokens/ │ ├── size/ │ ├── font.json │ ├── color/ │ ├── font.json │ ... ├── assets/ │ ├── fonts/ │ ├── images/ ``` -------------------------------- ### Example configuration (config.json) Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/config.md Example configuration file (config.json) that defines the source token files and two platforms (scss and android) with their transform groups, transforms, build paths, output files, and actions. The scss platform uses a transformGroup, prefix, and an action to copy assets; the android platform uses a list of transforms and outputs an XML colors file. ```json { "source": ["tokens/**/*.json"], "platforms": { "scss": { "transformGroup": "scss", "prefix": "sd", "buildPath": "build/scss/", "files": [ { "destination": "_variables.scss", "format": "scss/variables" } ], "actions": ["copy_assets"] }, "android": { "transforms": ["attribute/cti", "name/snake", "color/hex", "size/remToSp", "size/remToDp"], "buildPath": "build/android/src/main/res/values/", "files": [ { "destination": "style_dictionary_colors.xml", "format": "android/colors" } ] } } } ``` -------------------------------- ### Install Style Dictionary globally Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/getting-started/using_the_cli.md Installs Style Dictionary globally via npm so the CLI is available system-wide. ```bash npm install -g style-dictionary ``` -------------------------------- ### less/icons example output (vars.less) Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Example output of the less/icons format, showing a LESS variable for an icon content and a helper class that uses it. The file is named vars.less. ```less @content-icon-email: '\E001'; .icon.email:before { content: @content-icon-email; } ``` -------------------------------- ### android/integers integers.xml example Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Example XML output for the android/integers format, showing integer resources. ```xml 1000 2000 4000 ``` -------------------------------- ### Example output for ios-swift/any.swift Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Example Swift output generated by the ios-swift/any.swift format with the given configuration. It imports UIKit and AnotherModule, uses internal access control, and defines a struct named StyleDictionary with a static color constant. ```swift import UIKit import AnotherModule internal struct StyleDictionary { internal static let colorBackgroundDanger = UIColor(red: 1.000, green: 0.918, blue: 0.914, alpha: 1) } ``` -------------------------------- ### ios/macros Objective-C macros example Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Example output for the ios/macros format, generating an Objective-C header file with color and size macros. ```swift #import #import #define ColorFontLink [UIColor colorWithRed:0.00f green:0.47f blue:0.80f alpha:1.00f] #define SizeFontTiny 176.00f ``` -------------------------------- ### Example: tokens and config for filtering colors Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/filters.md An example showing a tokens JSON file and a configuration that uses a filter to exclude color tokens. The filter 'no-colors' returns true for tokens whose type is not 'color'. ```json { "colors": { "red": { "type": "color", "value": "#ff0000" }, "blue": { "type": "color", "value": "#0000ff" } }, "spacing": { "0": { "type": "dimension", "value": "0px" }, "1": { "type": "dimension", "value": "4px" }, "2": { "type": "dimension", "value": "8px" } } } ``` ```javascript import { formats, transformGroups } from 'style-dictionary/enums'; export default { hooks: { filters: { 'no-colors': (token, options) => { return token.type !== 'color'; }, }, }, platforms: { css: { transformGroup: transformGroups.css, files: [ { format: formats.cssVariables, destination: '_variables.css', filter: 'no-colors', }, ], }, }, }; ``` -------------------------------- ### compose/object Kotlin output example Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Example output for the compose/object format, generating a Kotlin file with a StyleDictionary object containing color values. ```kotlin package com.example.tokens; import androidx.compose.ui.graphics.Color object StyleDictionary { val colorBaseRed5 = Color(0xFFFAF3F2) } ``` -------------------------------- ### android/dimens dimens.xml example Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Example XML output for the android/dimens format, showing dimension resources. ```xml 5.00dp 10.00dp 15.00dp ``` -------------------------------- ### Install Style Dictionary as a dev dependency Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/getting-started/using_the_npm_module.md Install the style-dictionary package as a dev dependency using npm. ```bash npm install -D style-dictionary ``` -------------------------------- ### Swift class output example (colors.swift) Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Example output of the ios-swift/class.swift format, showing a Swift class named StyleDictionary with a static let for a color token. The default access control is public and the default import is UIKit. ```swift public class StyleDictionary { public static let colorBackgroundDanger = UIColor(red: 1.000, green: 0.918, blue: 0.914, alpha: 1) } ``` -------------------------------- ### less/variables example output (vars.less) Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Example output of the less/variables format, showing LESS variable definitions for color tokens. The file is named vars.less. ```less @color-background-base: #f0f0f0; @color-background-alt: #eeeeee; ``` -------------------------------- ### json format example tokens Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Example token input for the json format, showing a color token with value and type. ```json { "color": { "base": { "red": { "value": "#ff0000", "type": "color" } } } } ``` -------------------------------- ### Install Style Dictionary as dev dependency Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/getting-started/using_the_cli.md Installs Style Dictionary as a dev dependency, recommended because it is a build tool rather than a runtime tool. ```bash npm install --save-dev style-dictionary ``` -------------------------------- ### Live demo tokens and config for outputReferencesFilter Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Utils/references.md Live demo tokens and configuration for the outputReferencesFilter example. The tokens include a shadow that references a filtered-out color, and the config uses outputReferencesFilter to conditionally output references. ```json { "colors": { "black": { "value": "#000", "type": "color" }, "grey": { "value": "#ccc", "type": "color" } }, "spacing": { "2": { "value": "2px", "type": "dimension" } }, "border": { "value": "solid {spacing.2} {colors.black}" }, "shadow": { "value": "0 4px 2px {colors.grey}" } } ``` ```javascript import { outputReferencesFilter } from 'style-dictionary/utils'; import { formats, transformGroups } from 'style-dictionary/enums'; export default { platforms: { css: { transformGroup: transformGroups.css, files: [ { destination: 'vars.css', format: formats.cssVariables, filter: (token) => token.name !== 'colors-grey', options: { // returns false for the shadow token because it refs color-grey which is filtered out outputReferences: outputReferencesFilter, }, }, ], }, }, }; ``` -------------------------------- ### Demo tokens.json Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/index.md Example token file used in the interactive demo to illustrate sorting by value and then by name as a tie-breaker. ```json { "colors": { "a": { "value": "#111111" }, "c": { "value": "#000000" }, "b": { "value": "#000000" } } } ``` -------------------------------- ### Check Style Dictionary version Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/getting-started/using_the_cli.md Displays the installed version of Style Dictionary. ```bash style-dictionary --version ``` -------------------------------- ### new StyleDictionary() Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/api.mdx Documentation for the StyleDictionary constructor, including parameters and usage examples. ```APIDOC ## new StyleDictionary(config, options) ### Description Creates a new StyleDictionary instance. The constructor accepts a configuration object or a path to a config file, along with optional options for initialization, logging, and file system volume. ### Parameters - `config` (Config | string) - Required - Configuration options to build your style dictionary. If a string is passed, it is used as a path to a JSON or JavaScript ESM (default export) config file. TypeScript files are natively supported with Bun, Deno, or NodeJS >= 22.6.0 with `--experimental-strip-types` flag. Alternatively, an object with the configuration can be passed. - `options` (Object) - Optional - Options object when creating a new StyleDictionary instance. - `options.init` (boolean) - Optional - `true` by default but can be disabled to delay initializing the dictionary. You can then call `sdInstance.init()` yourself, e.g., for testing or error handling purposes. - `options.verbosity` ('silent' | 'default' | 'verbose') - Optional - Verbosity of logs, overrides `log.verbosity` set in SD config or platform config. An enum-like JS object `logVerbosityLevels` is available to import. - `options.warnings` ('error' | 'warn' | 'disabled') - Optional - Whether to throw warnings as errors, warn, or disable warnings, overrides `log.verbosity` set in SD config or platform config. An enum-like JS object `logWarningLevels` is available to import. - `options.volume` (import('memfs').IFs | typeof import('node:fs')) - Optional - FileSystem Volume to use as an alternative to the default FileSystem, handy for isolating or containerising StyleDictionary files. ### Example ```js import StyleDictionary from 'style-dictionary'; import { formats, transformGroups } from 'style-dictionary/enums'; const sd = new StyleDictionary('config.json'); const sdTwo = new StyleDictionary({ source: ['tokens/*.json'], platforms: { scss: { transformGroup: transformGroups.scss, buildPath: 'build/', files: [ { destination: 'variables.scss', format: formats.scssVariables, }, ], }, // ... }, }); ``` ### Example with volume option ```js import { Volume } from 'memfs'; // You will need a bundler like webpack/rollup for memfs in browser... // Or use as a prebundled fork: import memfs from '@bundled-es-modules/memfs'; import { formats, transformGroups } from 'style-dictionary/enums'; const { Volume } = memfs; const vol = new Volume(); const sd = new StyleDictionary( { tokens: { colors: { red: { value: '#FF0000', type: 'color', }, }, }, platforms: { css: { transformGroup: transformGroups.css, files: [ { destination: 'variables.css', format: formats.cssVariables, }, ], }, }, }, { volume: vol }, ); await sd.buildAllPlatforms(); vol.readFileSync('/variables.css'); /** * :root { * --colors-red: #FF0000; * } */ ``` ``` -------------------------------- ### Default design token metadata example Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/info/tokens.md Shows the configuration, input token, and resulting token with default metadata (name, path, original, filePath, isSource) added by Style Dictionary. ```json5 // config.json { source: ['tokens/**/*.json'], //... } ``` ```json5 // tokens/color/background.json { color: { background: { primary: { value: '#fff' }, }, }, } ``` ```json5 { color: { background: { primary: { name: 'primary', value: '#fff', path: ['color', 'background', 'primary'], original: { value: '#fff', }, filePath: 'tokens/color/background.json', isSource: true, }, }, }, } ``` -------------------------------- ### ios-swift/class.swift Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Documentation for the ios-swift/class.swift format hook, including its options and an example output. ```APIDOC ## ios-swift/class.swift ### Description Creates a Swift implementation file of a class with values. It adds default `class` object type, `public` access control and `UIKit` import. ### Parameters | Param | Type | Description | | --- | --- | --- | | `options` | `Object` | | | `options.accessControl` | `string` | Level of [access](https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html) of the generated swift object. Defaults to `public`. | | `options.import` | `string[] \| string` | Modules to import. Can be a string or array of strings. Defaults to `'UIKit'`. | | `options.className` | `string` | The name of the generated Swift object | | `options.showFileHeader` | `boolean` | Whether or not to include a comment that has the build date. Defaults to `true` | | `options.outputReferences` | `boolean \| OutputReferencesFunction` | Whether or not to keep [references](/reference/hooks/formats#references-in-output-files) (a -> b -> c) in the output. Defaults to `false`. Also allows passing a function to conditionally output references on a per token basis. | ### Example ```swift title="colors.swift" public class StyleDictionary { public static let colorBackgroundDanger = UIColor(red: 1.000, green: 0.918, blue: 0.914, alpha: 1) } ``` ``` -------------------------------- ### tailwind.config.js - Importing the Tailwind preset Source: https://github.com/style-dictionary/style-dictionary/blob/main/examples/advanced/tailwind-preset/README.md Example tailwind.config.js that imports the generated preset from the build directory. Run `npm run build-css` to watch the demo/index.html file and compile Tailwind classes into demo/output.css. ```javascript import tailwindPreset from './build/tailwind/preset.js'; /** @type {import('tailwindcss').Config} */ export default { theme: { extend: {}, }, presets: [tailwindPreset], content: ['./demo/**/*.{html,js}'], plugins: [], }; ``` -------------------------------- ### compose/object Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Documentation for the compose/object format, including its options and an example. ```APIDOC ## compose/object ### Description Creates a Kotlin file for Compose containing an object with a `val` for each property. ### Parameters | Param | Type | Description | | --- | --- | --- | | `options` | `Object` | | | `options.import` | `string[] \| string` | Modules to import. Can be a string or array of strings. Defaults to `['androidx.compose.ui.graphics.Color', 'androidx.compose.ui.unit.*']`. | | `options.showFileHeader` | `boolean` | Whether or not to include a comment that has the build date. Defaults to `true`. | | `options.outputReferences` | `boolean \| OutputReferencesFunction` | Whether or not to keep references (a -> b -> c) in the output. Defaults to `false`. Also allows passing a function to conditionally output references on a per token basis. | | `options.className` | `string` | The name of the generated Kotlin object. | | `options.packageName` | `string` | The package for the generated Kotlin object. | | `options.accessControl` | `string` | Level of access of the generated compose properties and object. Defaults to `''`. | | `options.objectType` | `string` | Type of Kotlin object. Defaults to `''` (regular object). | ### Example ```kotlin title="vars.kt" package com.example.tokens; import androidx.compose.ui.graphics.Color object StyleDictionary { val colorBaseRed5 = Color(0xFFFAF3F2) } ``` ``` -------------------------------- ### Using actions in config file Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/actions.md Configuration example showing how to use the copy_assets action in the android platform. The actions array lists the action names to run during the build. ```json { "source": ["tokens/**/*.json"], "platforms": { "android": { "transformGroup": "android", "files": [], "actions": ["copy_assets"] } } } ``` -------------------------------- ### scss/map-flat example Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Creates a SCSS file with a flat map. The map name can be set via the mapName option in the file configuration. ```scss $tokens: ( 'color-background-base': #f0f0f0; 'color-background-alt': #eeeeee;, ); ``` -------------------------------- ### Parser structure example (my-parser.js) Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/parsers.md Shows the basic structure of a custom parser: a name, a pattern (regular expression) to match file paths, and a parser function that receives an object with filePath and contents, returning the parsed data. This example parses JSON files. ```javascript const myParser = { name: 'json-parser', pattern: /\.json$/, parser: ({ filePath, contents }) => { return JSON.parse(contents); }, }; ``` -------------------------------- ### javascript/module-flat output example Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Output of the javascript/module-flat format. The style dictionary is flattened to a single level with token names as keys. ```js module.exports = { ColorBaseRed: '#ff0000', }; ``` -------------------------------- ### Generated Objective-C Output Source: https://github.com/style-dictionary/style-dictionary/blob/main/README.md Example output from the style dictionary build system, converting the design tokens to Objective-C constants. ```objc float const SizeFontSmall = 10.00f; float const SizeFontMedium = 16.00f; float const SizeFontLarge = 24.00f; float const SizeFontBase = 16.00f; ``` -------------------------------- ### Combining multiple outputReference utility functions Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Utils/references.md Example showing how to combine multiple outputReference utility functions, such as outputReferencesFilter and outputReferencesTransformed, in a format's options. ```APIDOC ## Combining multiple outputReference utility functions ### Description These utility functions can be quite easily combined together. ### Example ```javascript import StyleDictionary from 'style-dictionary'; import { outputReferencesFilter, outputReferencesTransformed } from 'style-dictionary/utils'; import { formats, transformGroups } from 'style-dictionary/enums'; const sd = new StyleDictionary({ platforms: { css: { transformGroup: transformGroups.css, files: [ { destination: 'vars.css', format: formats.cssVariables, options: { outputReferences: (token, options) => outputReferencesFilter(token, options) && outputReferencesTransformed(token, options), }, }, ], }, }, }); ``` ``` -------------------------------- ### Initialize a new Style Dictionary project Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/getting-started/installation.mdx Creates a new project directory, enters it, and runs `style-dictionary init basic` to copy starter files and run the initial build. ```bash mkdir MyStyleD cd MyStyleD style-dictionary init basic ``` -------------------------------- ### Generated SCSS Output Source: https://github.com/style-dictionary/style-dictionary/blob/main/README.md Example output from the style dictionary build system, converting the design tokens to SCSS variables. ```scss $size-font-small: 10px; $size-font-medium: 16px; $size-font-large: 24px; $size-font-base: 16px; ``` -------------------------------- ### Init command usage Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/getting-started/using_the_cli.md Generates a starter style dictionary. The example-type can be 'basic' or 'complete'. ```bash style-dictionary init ``` -------------------------------- ### Render three token-consuming components in App.js Source: https://github.com/style-dictionary/style-dictionary/blob/main/examples/advanced/create-react-app/README.md Shows the main app structure in App.js, rendering the three example components that consume the same design tokens. ```jsx
``` -------------------------------- ### CSS output with file header Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/file-headers.md Example output file with a custom file header comment at the top, followed by CSS custom properties. ```css /** * This is a file header! */ :root { --foo-bar: 4px; } ``` -------------------------------- ### Create StyleDictionary instance with config file or object Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/api.mdx Shows two ways to instantiate StyleDictionary: passing a config file path string or a configuration object. The object example uses transformGroups.scss and formats.scssVariables. ```javascript import StyleDictionary from 'style-dictionary'; import { formats, transformGroups } from 'style-dictionary/enums'; const sd = new StyleDictionary('config.json'); const sdTwo = new StyleDictionary({ source: ['tokens/*.json'], platforms: { scss: { transformGroup: transformGroups.scss, buildPath: 'build/', files: [ { destination: 'variables.scss', format: formats.scssVariables, }, ], }, // ... }, }); ``` -------------------------------- ### Storybook stories configuration Source: https://github.com/style-dictionary/style-dictionary/blob/main/examples/advanced/storybook-token-docs/README.md Configures Storybook to load stories from the build output. The `stories` glob points to the generated `tokens.stories.js` file, and the framework is set to `@storybook/html-vite`. ```js export default { stories: ['../build/storybook/*.stories.js'], framework: '@storybook/html-vite', }; ``` -------------------------------- ### Build with Style Dictionary CLI Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/getting-started/installation.mdx Run this command in the root directory of your project, which must include a configuration file. ```bash style-dictionary build ``` -------------------------------- ### File watcher with Chokidar using Style Dictionary CLI directly Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/examples/basic.mdx Alternative to the previous command when a wrapper build script is not needed. Watches 'tokens.json' and runs 'style-dictionary build' directly on changes. This requires the Style Dictionary CLI to be available in your environment. ```sh chokidar "tokens.json" -c "style-dictionary build" ``` -------------------------------- ### android/fontDimens font-dimens.xml example Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Example XML output for the android/fontDimens format, showing font dimension resources. ```xml 10.00sp 13.00sp 15.00sp ``` -------------------------------- ### Style Dictionary tokens configuration Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/examples/basic.mdx Defines color and font size tokens with references. The colors include base gray, red, green, and font colors that reference other tokens. Font sizes include small, medium, large, and base with comments. ```json { "colors": { "base": { "gray": { "light": { "value": "#CCCCCC", "type": "color" }, "medium": { "value": "#999999", "type": "color" }, "dark": { "value": "#111111", "type": "color" } }, "red": { "value": "#FF0000", "type": "color" }, "green": { "value": "#00FF00", "type": "color" } }, "font": { "base": { "value": "{colors.base.red}", "type": "color" }, "secondary": { "value": "{colors.base.green}", "type": "color" }, "tertiary": { "value": "{colors.base.gray.light}", "type": "color" } } }, "size": { "font": { "small": { "value": "0.75", "type": "fontSize", "comment": "the small size of the font" }, "medium": { "value": "1", "type": "fontSize", "comment": "the medium size of the font" }, "large": { "value": "2", "type": "fontSize", "comment": "the large size of the font" }, "base": { "value": "{size.font.medium}", "type": "fontSize", "comment": "the base size of the font" } } } } ``` -------------------------------- ### Output of style-dictionary init basic Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/getting-started/installation.mdx Expected output after running `style-dictionary init basic`, showing the generated build artifacts for each platform. ```text Copying starter files... Source style dictionary starter files created! Running `style-dictionary build` for the first time to generate build artifacts. css ✔︎ build/css/_variables.css android ✔︎ build/android/font_dimens.xml ✔︎ build/android/colors.xml compose ✔︎ build/compose/StyleDictionaryColor.kt ✔︎ build/compose/StyleDictionarySize.kt ios ✔︎ build/ios/StyleDictionaryColor.h ✔︎ build/ios/StyleDictionaryColor.m ✔︎ build/ios/StyleDictionarySize.h ✔︎ build/ios/StyleDictionarySize.m ios-swift ✔︎ build/ios-swift/StyleDictionary.swift ios-swift-separate-enums ✔︎ build/ios-swift/StyleDictionaryColor.swift ✔︎ build/ios-swift/StyleDictionarySize.swift ``` -------------------------------- ### buildAllPlatforms Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/api.mdx Uses `buildPlatform` under the hood for each platform. In the majority of cases, this is the method you'll use. ```APIDOC ## buildAllPlatforms ### Description Uses `buildPlatform` under the hood for each platform. In the majority of cases, this is the method you'll use. ### Method buildAllPlatforms ### Signature ```ts type buildAllPlatforms = (opts: { cache?: boolean }) => Promise; ``` ### Parameters - `opts` (object) - Required - Options object. - `cache` (boolean) - Optional - Whether to reuse cached tokens/config if already processed for this platform. Defaults to `false`. ### Returns Promise resolving to the SDInstance. ### Example ```js import StyleDictionary from 'style-dictionary'; const sd = new StyleDictionary('config.json'); await sd.buildAllPlatforms(); ``` ```bash style-dictionary build ``` ``` -------------------------------- ### Example output of typescript/es6-declarations Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Example TypeScript declaration file generated by the typescript/es6-declarations format. It declares exported constants for each token. ```typescript export const ColorBackgroundBase: string; export const ColorBackgroundAlt: string; ``` -------------------------------- ### init Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/api.mdx Documentation for the init instance method of Style Dictionary. ```APIDOC ## init ### Description Initializes the Style Dictionary instance. Called automatically when creating a new instance unless disabled, but can be called manually for testing or error handling. ### Method init ### Signature `type init = (config: Config) => Promise` ### Parameters - `initConfig` (Object) - Optional - Init configuration options. - `initConfig.verbosity` ('silent'|'default'|'verbose') - Optional - Verbosity of logs, overrides `log.verbosity` set in SD config or platform config. There is an enum-like JS object `logVerbosityLevels` available, that you can import. - `initConfig.warnings` ('error'|'warn'|'disabled') - Optional - Whether to throw warnings as errors, warn or disable warnings, overrides `log.verbosity` set in SD config or platform config. There is an enum-like JS object `logWarningLevels` available, that you can import. ### Returns - `Promise` - A promise that resolves to the Style Dictionary instance. ``` -------------------------------- ### Basic Style Dictionary build with config file Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/getting-started/using_the_npm_module.md Imports StyleDictionary, creates an instance with a JSON config file, and builds all platforms. ```javascript import StyleDictionary from 'style-dictionary'; const sd = new StyleDictionary('config.json'); await sd.buildAllPlatforms(); ``` -------------------------------- ### Use Style Dictionary as an npm module with extend and buildAllPlatforms Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/config.md Imports the Style Dictionary npm module, creates a new instance with a configuration, builds all platforms, and shows how to extend the instance with a new configuration and build again. ```javascript // build-tokens.js import StyleDictionary from 'style-dictionary'; const myStyleDictionary = new StyleDictionary({ // configuration }); await myStyleDictionary.buildAllPlatforms(); // You can also extend Style Dictionary multiple times: const myOtherStyleDictionary = await myStyleDictionary.extend({ // new configuration }); await myOtherStyleDictionary.buildAllPlatforms(); ``` -------------------------------- ### Loop over brands with different configurations Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/getting-started/using_the_npm_module.md Shows how to loop over an array of brands and apply different configurations to Style Dictionary, building all platforms for each. ```javascript import StyleDictionary from 'style-dictionary'; const brands = [`brand-1`, `brand-2`, `brand-3`]; await Promise.all( brands.map((brand) => { const sd = new StyleDictionary({ include: [`tokens/default/**/*.json`], source: [`tokens/${brand}/**/*.json`], // ... }); return sd.buildAllPlatforms(); }), ); ``` -------------------------------- ### Build command usage Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/getting-started/using_the_cli.md Builds a style dictionary package from the current directory. Options include -c/--config, -p/--platform, -s/--silent, -v/--verbose, and -n/--no-warn. ```bash style-dictionary build [options] ``` -------------------------------- ### Swift enum output example Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Example output of the ios-swift/enum.swift format. The generated Swift enum uses public access control and imports UIKit by default. ```swift public enum StyleDictionary { public static let colorBackgroundDanger = UIColor(red: 1.000, green: 0.918, blue: 0.914, alpha: 1) } ``` -------------------------------- ### stylus/variables output example Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Output of the stylus/variables format. Variable names are prefixed with $ and values are assigned without a colon. The example shows two color variables. ```stylus $color-background-base= #f0f0f0; $color-background-alt= #eeeeee; ``` -------------------------------- ### Example output of typescript/module-declarations Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Example TypeScript declaration file generated by the typescript/module-declarations format. It exports a default tokens object with a DesignToken interface. Note that this output is not 100% accurate. ```typescript export default tokens; declare interface DesignToken { value?: any; type?: string; name?: string; comment?: string; themeable?: boolean; attributes?: Record; [key: string]: any; } declare const tokens: { color: { red: DesignToken; }; }; ``` -------------------------------- ### File watcher with Chokidar to re-run Style Dictionary build Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/examples/basic.mdx Use this command to watch token files and automatically re-run the Style Dictionary build when changes are detected. The command watches 'tokens.json' and executes 'node build-tokens.mjs' on changes. This is useful for local development to avoid manual rebuilds. ```sh chokidar "tokens.json" -c "node build-tokens.mjs" ``` -------------------------------- ### scss/map-deep output example (vars.scss) Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Example output of the scss/map-deep format. The map is named by the `mapName` option; here it is `$tokens`. Token values are referenced as SCSS variables with `!default` flags. ```scss $color-background-base: #f0f0f0 !default; $color-background-alt: #eeeeee !default; $tokens: ( 'color': ( 'background': ( 'base': $color-background-base, 'alt': $color-background-alt, ), ), ); ``` -------------------------------- ### Import utilities from style-dictionary/utils Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Utils/index.md Import utility functions from the style-dictionary/utils entrypoint. ```js import { convertTokenData, resolveReferences, typeDtcgDelegate } from 'style-dictionary/utils'; ``` -------------------------------- ### Generated Storybook stories file Source: https://github.com/style-dictionary/style-dictionary/blob/main/examples/advanced/storybook-token-docs/README.md Example of the generated `tokens.stories.js` file. It defines stories for Colors, FontSizes, and Spacing, each rendering an HTML table. The actual content is generated by the custom format in `config/format.js`. ```js export default { title: 'Design tokens', parameters: { layout: 'padded' }, }; export const Colors = { render: () => '...
', }; export const FontSizes = { render: () => '...
', }; export const Spacing = { render: () => '...
', }; ``` -------------------------------- ### Use volume option with memfs Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/api.mdx Demonstrates using the volume option to write output to an in-memory filesystem. Requires a bundler for memfs in browser or use the prebundled fork. The example builds all platforms and reads the generated CSS file. ```javascript import { Volume } from 'memfs'; // You will need a bundler like webpack/rollup for memfs in browser... // Or use as a prebundled fork: import memfs from '@bundled-es-modules/memfs'; import { formats, transformGroups } from 'style-dictionary/enums'; const { Volume } = memfs; const vol = new Volume(); const sd = new StyleDictionary( { tokens: { colors: { red: { value: '#FF0000', type: 'color', }, }, }, platforms: { css: { transformGroup: transformGroups.css, files: [ { destination: 'variables.css', format: formats.cssVariables, }, ], }, }, }, { volume: vol }, ); await sd.buildAllPlatforms(); vol.readFileSync('/variables.css'); /** * :root { * --colors-red: #FF0000; * } */ ``` -------------------------------- ### Configure ios-swift/any.swift format Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/predefined.md Configuration example for the ios-swift/any.swift format, setting the format name, imports, object type, and access control. The options shown override defaults: objectType defaults to 'class', accessControl defaults to 'public', and import defaults to 'UIKit'. ```javascript format: 'ios-swift/any.swift', import: ['UIKit', 'AnotherModule'], objectType: 'struct', accessControl: 'internal', ``` -------------------------------- ### Import utils from style-dictionary/utils Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/getting-started/using_the_npm_module.md Shows how to import the convertTokenData utility from the utils entrypoint. ```javascript import { convertTokenData } from 'style-dictionary/utils'; ``` -------------------------------- ### Basic tokens.json with references Source: https://github.com/style-dictionary/style-dictionary/blob/main/docs/src/content/docs/reference/Hooks/Formats/index.md A basic set of design tokens with references, used to demonstrate outputReferences. ```json // tokens.json { "color": { "red": { "value": "#ff0000" }, "danger": { "value": "{color.red}" }, "error": { "value": "{color.danger}" } } } ```