Socket
Book a DemoInstallSign in
Socket

@ama-styling/style-dictionary

Package Overview
Dependencies
Maintainers
1
Versions
126
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ama-styling/style-dictionary

Package exposing modules and hooks for Style Dictionary

latest
Source
npmnpm
Version
13.1.0
Version published
Weekly downloads
1.6K
71.55%
Maintainers
1
Weekly downloads
 
Created
Source

Otter Style Dictionary

Super cute Otter!

This package is an Otter Framework Module.

Description

Stable Version Bundle Size

This package exposes a set of Hooks and Modules for the Style Dictionary to enhance the capabilities of Design Tokens.

Get Started

Set up your Style Dictionary in your project thanks to the command:

ng add @ama-styling/style-dictionary

By default, the command will do the following updates:

  • Add a dev dependency to the Style Dictionary package.
  • Create a minimal config.mjs configuration file with the required setup for CSS and Metadata generation.
  • Add the 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:

Enhancement

Design Token Extensions

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
    }
  }
}

Available Otter Extensions

ExtensionsTypeDescriptionRequired hooks
o3rPrivatebooleanDetermine if the token is flagged as privatepre-processor: o3r/pre-processor/extensions
formatter: o3r/css/variable
o3rImportantbooleanDetermine if the token should be flagged as important when generatedpre-processor: o3r/pre-processor/extensions
formatter: o3r/css/variable
o3rScopestringScope to apply to the generated variablepre-processor: o3r/pre-processor/extensions
formatter: o3r/css/variable
o3rMetadataCMS MetadataAdditional information to provide to the metadata if generatedpre-processor: o3r/pre-processor/extensions
formatter: o3r/json/metadata
o3rUnitstringConvert 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
o3rRationumberRatio 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
o3rExpectOverridebooleanIndicate that the token is expected to be overridden by external rulespre-processor: o3r/pre-processor/extensions

[!WARNING] The required hooks need to be registered to the Style Dictionary configuration to fully support the extension.

Modules

Parsers

NameMatching filesDescription
o3r/json-parser/one-line-token**/*.jsonAllow dot notation Token in JSON file
o3r/json-parser/extensions**/*.extensions.jsonParse file containing $extensions instructions to apply on top of Design Token

Pre-processor

NameDescription
o3r/pre-processor/extensionsPre-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.

Transforms

NameTypeCriteriaDescription
o3r/transform/ratiovalueo3rRatio extension is providedApply the given o3rRatio to the numeric values of the Token(s) it refers to.
o3r/transform/unitvalueo3rUnit extension is providedReplace the unit of the values of the Token(s) it refers to, by the provided o3rUnit.

Transform-groups

NameDescription
o3r/css/recommendedExtend the official CSS transform groups by adding o3r/transform/ratio and o3r/transform/unit

Formats

NameOptionsDescription
o3r/css/variableSee css/variables optionsRender CSS variable block (based on the built-in format) supporting additional Otter Extensions features.
o3r/json/metadataSee css/variables options (applied to defaultValue)
- keepPrivate: include private variables
Render CMS style metadata.

Registration process

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 '@ama-styling/style-dictionary';

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.

Helpers

To enhance and facilitate the configuration of the StyleDictionary instance, the @ama-styling/style-dictionary exposes a set of helpers.

getTargetFiles

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 '@ama-styling/style-dictionary';

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 the getTargetFiles function (including defaultFile). defaultFile defines the default file where variables not matching any rule will be generated.

The helper function getTargetFiles is registering the filter into StyleDictionary with a unique ID.
This IDs can be retrieved as follows:

import { getTargetFiles } from '@ama-styling/style-dictionary';

const sd = new StyleDictionary();
const files = getTargetFiles({ rules, { styleDictionary: sd } });

/**
 * Filter IDs registered in {@link sd} Style Dictionary instance
 * @type {string[]}
 **/
const filterIds = files.map(({ filter }) => filter);

Advanced

Basic Node Configuration Example

// style-builder.mjs

import { register, getTargetFiles, baseConfiguration } from '@ama-styling/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:

node ./style-builder.mjs

Keywords

design

FAQs

Package last updated on 22 Sep 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts