— A TypeScript implementation of the Design Tokens Format Module specification along with some utility functions
This packages aims to provide the most agnostic JavaScript/TypeScript definitions from the Design Tokens Format Module specification, for library developers and tooling creators.
Join the conversation on the W3C Design Tokens Community Group repository.
⚠️ Please note, the DTCG specification is NOT stable yet, breaking changes might occur in the future.
npm install design-tokens-format-module
yarn add design-tokens-format-module
pnpm add design-tokens-format-module
This module provides all the type definitions and the most basic helpers required to work with a JSON design token tree.
Constrain a JSON object that represents a design token tree.
import { JSONTokenTree } from 'design-tokens-format-module';
const tokenTree = {
color: {
primary: {
$type: 'color',
$value: '#000000',
},
},
spacing: {
small: {
$type: 'dimension',
$value: '8px',
},
},
} satisfies JSONTokenTree;
Each design token type is available as a TypeScript namespace.
import {
Color // Dimension, FontFamily...
} from 'design-tokens-format-module';
declare function parseColorToken(token: unknown): Color.Token;
declare function parseColorValue(tokens: unknown): Color.Value;
declare function matchIsColorTokenTypeName(
name: string,
): name is Color.TypeName;
All token type names are available as a constant.
import { tokenTypeNames } from 'design-tokens-format-module';
for(const tokenTypeName of tokenTypeNames) {
// color, dimension...
}
All token type signatures are available within a type union.
import { DesignToken } from 'design-tokens-format-module';
declare function parseDesignToken(token: unknown): DesignToken;
JSON values can be evaluated against the token signature
import { matchIsToken, matchIsTokenTypeName, matchIsAliasValue } from 'design-tokens-format-module';
function validateToken(token: unknown) {
if (matchIsToken(token)) {
const isValidType = matchIsTokenTypeName(token.$type ?? '');
if(matchIsAliasValue(token.$value)) {
// ...
}
}
// ...
}
Alias values can be validated and parsed.
import { captureAliasPath } from 'design-tokens-format-module';
const result = captureAliasPath('{path.to.token}');
if(result.status === 'ok') {
result.value // string[]
} else {
switch (result.error.tag) {
case 'TYPE_ERROR': {
result.error.message // Expected a string value. Got [x].
break;
}
case 'FORMAT_ERROR': {
result.error.message // Expected an alias value. Got [x].
break;
}
}
}
Some token types have a fixed set of values. These are available as constants.
import { fontWeightValues, strokeStyleStringValues, strokeStyleLineCapValues } from 'design-tokens-format-module';
The packages goal has shifted from being a generic parser — which requires way more feedback — to a reliable source of truth for the DTCG implementations in the JavaScript land.
The features and APIs available before version 0.9.0 has been relocated to a new location where they get revamped and improved.
If you find any typos, errors, or additional improvements, feel free to contribute to the project.
Install dependencies.
npm install
Run test in watch mode.
npm run test
Please add tests to cover the new functionality or bug fix you are working upon.
We expect the tests and the build to pass for a PR to be reviewed and merged.
npm run test --run
npm run build
type AliasValue = `{${string}}`;
namespace Json {
export type Value = JSONValue;
export type Object = JSONObject;
export type Array = JSONArray;
export type Primitive = string | number | boolean | null;
}
type JSONTokenTree = {
[key: string]: DesignToken | JSONTokenTree | GroupProperties;
} & GroupProperties;
namespace TokenTypeName {
export type TypeName = TypeName;
export type Value = Value;
export type Token = Token;
}
type DesignToken =
| ColorToken
| DimensionToken
// | ...
type TokenTypeName =
| 'color'
| 'dimension'
// | ...
const CAPTURE_ALIAS_PATH_ERRORS = {
TYPE_ERROR: 'Expected a string value.',
// ...
} as const;
type ValidationError = {
[k in keyof Writable<typeof CAPTURE_ALIAS_PATH_ERRORS>]?: {
message: string;
};
};
type Result<T, E> =
| {
status: 'ok';
value: T;
error?: undefined;
}
| {
status: 'error';
error: E;
value?: undefined;
};
declare function captureAliasPath(
value: unknown,
): Result<Array<string>, ValidationError>;
declare function captureAliasPath<AsString extends boolean | undefined>(
value: unknown,
asString: AsString,
): Result<AsString extends true ? string : Array<string>, ValidationError>;
Usage
const result = captureAliasPath('{path.to.token}');
if(result.status === 'ok') {
result.value // string[]
} else {
switch (result.error.tag) {
case 'TYPE_ERROR': {
result.error.message // Expected a string value. Got [x].
break;
}
case 'FORMAT_ERROR': {
result.error.message // Expected an alias value. Got [x].
break;
}
}
}
declare function matchIsAliasValue(candidate: unknown): candidate is AliasValue;
declare function matchIsGroup(candidate: unknown): candidate is JSONTokenTree;
declare function matchIsToken(candidate: unknown): candidate is DesignToken;
declare function matchIsTokenTypeName(candidate: unknown): candidate is TokenTypeName;
const ALIAS_PATH_SEPARATOR = '.';
const tokenTypeNames = [
'color',
'dimension',
// ...
] as const;
const fontWeightValues = [
100,
'thin',
'hairline',
// ...
] as const;
const strokeStyleStringValues = [
'solid',
'dashed',
// ...
] as const;
const strokeStyleLineCapValues = [
'round',
'butt',
// ...
] as const;