@quartzds/style-dictionary-extensions v2.0.0
SPDX-FileCopyrightText: Ā© 2025 Schneider Electric
SPDX-License-Identifier: Apache-2.0
Style Dictionary extensions for the Quartz design tokens
š” Overview
This package provides extensions and helper functions to transform Quartz design tokens into resources for developers using Style Dictionary. Those design tokens are edited using the Tokens Studio Figma plugin.
Prerequisites
- A hierarchy of JSON files produced by the Tokens Studio Figma plugin in the "legacy" format (do not enable the DTCG format in the Figma plugin).
- node
^18.0.0 || >=20.0.0
Token transformation rules
Translating design tokens into platform-specific assets requires rules. Quartz uses specific rules based on its own conventions. The goal of this library is to package all those transformation rules into a convenient API for Style Dictionary.
Supported platforms
- CSS
Experimental support:
- Android
- Swift
- XAML
Coming later:
- JavaScript
- TypeScript
šæ Installation
npm install @quartzds/style-dictionary-extensions
š Usage
Create a build script that will configure and run Style Dictionary to output tokens to several formats, using the helpers provided by this package. See the Transformation rules page for the list of transformations provided for each target format.
The general algorithm is as follows:
- Create a base
StyleDictionary
object, pre-configured with Quartz's custom transforms. - For every token set you want to export (desktop, tablet, mobile, dark, light,
etc.):
- create a Style Dictionary
Config
object using the helpers from this package - call the
extend(config)
method of the baseStyleDictionary
object to get a new instance from a specific configuration - let that Style Dictionary instance generate the files using the
buildAllPlatforms()
function
- create a Style Dictionary
Get a base StyleDictionary
object
The getPresetStyleDictionary()
function registers Quartz extensions and
returns a fresh Style Dictionary instance to use as the base to export to
one or more formats:
import { getPresetStyleDictionary } from '@quartzds/style-dictionary-extensions'
const baseSD = await getPresetStyleDictionary()
Create Style Dictionary configurations
This package provides transform groups for supported target formats, as well as helper functions to help you set up Style Dictionary configuration objects.
Use the get<Format>PlatformConfig(sourceType, destination, options?)
functions
to get ready-made PlatformConfig
objects to use in your Style Dictionary
Config
object:
import {
getAndroidPlatformConfig,
getCSSPlatformConfig,
getSwiftPlatformConfig,
} from '@quartzds/style-dictionary-extensions'
const sdConfig = {
platforms: {
android: getAndroidPlatformConfig(...),
css: getCSSPlatformConfig(...),
swift: getSwiftPlatformConfig(...),
},
...other config properties...
},
Run Style Dictionary generation
Finally, let Style Dictionary build all the platforms for your configuration objects:
var sd = baseSD.extend(desktopConfig)
sd.buildAllPlatforms()
sd = baseSD.extend(lightThemeConfig)
sd.buildAllPlatforms()
// etc.
š Simple example
This example assumes the following structure of design tokens files generated by the Tokens Studio Figma plugin:
src/
āāā tokens
āāā private.json <-- don't export these tokens (internal bindings)
āāā platform
ā āāā desktop.json
ā āāā mobile.json
āāā theme
āāā dark.json
āāā light.json
We want to transform these tokens into the following platform-specific assets:
dist/
āāā platform
ā āāā desktop.css
ā āāā desktop.swift
ā āāā desktop.xml
ā āāā mobile.css
ā āāā mobile.swift
ā āāā mobile.xml
āāā theme
āāā dark.css
āāā dark.swift
āāā dark.xml
āāā light.css
āāā light.swift
āāā light.xml
Here is the script to achieve that:
import {
getAndroidPlatformConfig,
getCSSPlatformConfig,
getPresetStyleDictionary,
getSwiftPlatformConfig,
SourceType,
} from '@quartzds/style-dictionary-extensions'
import type { Config, FileHeader } from 'style-dictionary/types'
const baseSD = await getPresetStyleDictionary()
const copyrightFileHeader: FileHeader = (defaultMessage, options) => (...)
const lightThemeConfig: Config = {
platforms: {
android: getAndroidPlatformConfig(
SourceType.theme,
`dist/theme/light.xml`,
{
fileHeader: copyrightFileHeader,
},
),
css: getCSSPlatformConfig(
SourceType.theme,
`dist/theme/light.css`,
{
fileHeader: copyrightFileHeader,
selector: '.qds-theme-light',
},
),
swift: getSwiftPlatformConfig(
SourceType.theme,
`dist/theme/light.swift`,
{
fileHeader: copyrightFileHeader,
className: 'Light',
},
),
},
include: [`./tokens/private.json`],
source: [`./tokens/theme/light.json`],
}
const darkThemeConfig: Config = {
/* ... */
}
const desktopConfig: Config = {
/* ... */
}
const mobileConfig: Config = {
/* ... */
}
const allConfigs = [
lightThemeConfig,
darkThemeConfig,
desktopConfig,
mobileConfig,
]
for (const config of allConfigs) {
const sd = StyleDictionary.extend(config)
sd.cleanAllPlatforms() // Optional
sd.buildAllPlatforms()
}
āļø License
See the LICENSE file for license rights and limitations.
7 months ago
7 months ago
7 months ago
7 months ago
6 months ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago