This package is an Otter Framework Module.
This package exposes a set of Hooks and Modules for the Style Dictionary to enhance the capabilities of Design Tokens.
Set up your Style Dictionary in your project thanks to the command:
Example :ng add @o3r/style-dictionary
By default, the command will do the following updates:
generate:theme
and generate:metadata
scripts in the project package.json.You will then be able to customize this setup with your project's specifications:
getTargetFiles
helper.The Design Tokens format allows to provide the $extensions
property to enhance the Token it is applied to.
The property can be applied to the Token directly as follows:
// colors.token.json
{
"colors": {
"primary": {
"$value": "#000000",
"$extensions": {
"o3rPrivate": true
}
}
}
}
or in a dedicated .extensions.json
file (when the o3r/json-parser/extensions parser is loaded):
// color.token.json
{
"colors": {
"primary": {
"$value": "#000000"
}
}
}
// custom.extensions.json
{
"colors.primary": {
"$description": "Primary private color",
"$extensions": {
"o3rPrivate": true
}
}
}
Extensions | Type | Description | Required hooks |
---|---|---|---|
o3rPrivate | boolean |
Determine if the token is flagged as private | pre-processor: o3r/pre-processor/extensions formatter: o3r/css/variable |
o3rImportant | boolean |
Determine if the token should be flagged as important when generated | pre-processor: o3r/pre-processor/extensions formatter: o3r/css/variable |
o3rScope | string |
Scope to apply to the generated variable | pre-processor: o3r/pre-processor/extensions formatter: o3r/css/variable |
o3rMetadata | CMS Metadata | Additional information to provide to the metadata if generated | pre-processor: o3r/pre-processor/extensions formatter: o3r/json/metadata |
o3rUnit | string |
Convert a numeric value from the specified unit to the new unit. It will add a unit to the tokens of type "number" for which the unit is not specified. In the case of complex types (such as shadow, transition, etc...), the unit will be applied to all numeric types they contain. |
pre-processor: o3r/pre-processor/extensions transforms: o3r/transform/unit |
o3rRatio | number |
Ratio to apply to the previous value. The ratio will only be applied to tokens of type "number" or to the first numbers determined in "string" like types. In the case of complex types (such as shadow, transition, etc...), the ratio will be applied to all numeric types they contain. |
pre-processor: o3r/pre-processor/extensions transforms: o3r/transform/ratio |
o3rExpectOverride | boolean |
Indicate that the token is expected to be overridden by external rules | pre-processor: o3r/pre-processor/extensions |
[!WARNING] The required hooks need to be registered to the Style Dictionary configuration to fully support the extension.
Name | Matching files | Description |
---|---|---|
o3r/json-parser/one-line-token | **/*.json |
Allow dot notation Token in JSON file |
o3r/json-parser/extensions | **/*.extensions.json |
Parse file containing $extensions instructions to apply on top of Design Token |
Name | Description |
---|---|
o3r/pre-processor/extensions | Pre-processor to add the support of the $extensions instructions in the Token (or dedicated extensions.json file). This pre-processor is mandatory for any Otter hooks. |
Name | Type | Criteria | Description |
---|---|---|---|
o3r/transform/ratio | value |
o3rRatio extension is provided |
Apply the given o3rRatio to the numeric values of the Token(s) it refers to. |
o3r/transform/unit | value |
o3rUnit extension is provided |
Replace the unit of the values of the Token(s) it refers to, by the provided o3rUnit . |
Name | Description |
---|---|
o3r/css/recommended | Extend the official CSS transform groups by adding o3r/transform/ratio and o3r/transform/unit |
Name | Options | Description |
---|---|---|
o3r/css/variable | See css/variables options | Render CSS variable block (based on the built-in format) supporting additional Otter Extensions features. |
o3r/json/metadata | See css/variables options (applied to defaultValue )- keepPrivate: include private variables |
Render CMS style metadata. |
The different hooks/modules can be registered individually via the different register...() functions from StyleDictionary
or all at once with the register
function as follows:
import { register, baseConfig } from '@o3r/style-disctionary';
const sd = new StyleDictionary({
...baseConfig,
// custom configs ...
});
register(sd); // Register all Otter modules
[!IMPORTANT] To be able to use any of the listed modules/hooks, they have to be registered to the
StyleDefinition
instance.
[!TIP] The helper
baseConfig
will provide the minimal configuration for Otter extension support.
To enhance and facilitate the configuration of the StyleDictionary
instance, the @o3r/style-dictionary
exposes a set of helpers.
The getTargetFiles
function is used to parameterize the generated file based on Design Token (following the same logic as $extensions
):
// Example to generate the `color-primary-**` variables in `my-color-primary.css`
import { getTargetFiles } from '@o3r/style-disctionary';
const rule = {
'color.primary': 'my-color-primary.css'
};
// or `const rule = {color: {primary: 'my-color-primary.css'}}`
// or `const rule = {'color.primary': {$extensions: {o3rTargetFile: 'my-color-primary.css'}}}`
// or `const rule = {'color: {primary': {$extensions: {o3rTargetFile: 'my-color-primary.css'}}}}`
const sd = new StyleDictionary({
//...
plateforms: {
css: {
files: [
...getTargetFiles({ rule, { format: 'css', defaultFile: 'default-file.css' } })
]
}
}
});
register(sd); // Register all Otter modules
[!NOTE] The
format
option will be applied to all the files provided to thegetTargetFiles
function (includingdefaultFile
).defaultFile
defines the default file where variables not matching any rule will be generated.
// style-builder.mjs
import { register, getTargetFiles, baseConfiguration } from '@o3r/style-dictionary';
import StyleDictionary from 'style-dictionary';
// Rules to generate different CSS files according to Token name
const fileRules = {
colors: 'style/colors.tokens.css',
'components.panel': 'components/panel/panel.tokens.css'
}
const sd = new StyleDictionary({
...baseConfiguration, // Use basic Otter configuration setup
usesDtcg: true, // Use Design Token Standard format
source: ['tokens/*.tokens.json'], // Design Token files
include: ['token.extensions.json'], // Custom extensions
platforms: {
// Generate CSS Files
css: {
options: {
outputReferences: true // to output `--var: var(--other-var)` instead of the value of `--other-var`
},
transformGroup: 'o3r/css/recommended',
files: [
...getTargetFiles(fileRules, { format: 'css' }),
// default CSS file where generate variables
{ destination: 'style/default.tokens.css', format: 'css' }
]
},
cms: {
options: {
outputReferences: true
},
transformGroup: 'o3r/css/recommended',
files: [
{ destination: 'style.metadata.json', format: 'o3r/json/metadata' }
]
}
}
});
// Register otter hooks/modules
register(sd);
if (process.env.CSS_ONLY){
sd.buildPlatform('css');
} else {
sd.buildAllPlatforms();
}
Can be run with the following command:
Example :node ./style-builder.mjs