Filters
Filters is a hook that provides a way to filter your tokens prior to formatting them to the final output.
Common use cases for filtering are:
- Not outputting your primitive/option tokens such as a color palette
- Splitting component tokens into separate files for each component
Filter structure
A filter is an object with two props:
name
: the name of the filterfilter
: a callback function that receives thetoken
as argument and returns a boolean,true
to include the token,false
to exclude/filter it out. Can also be an async function. Also has a second argument with the Style Dictionary options, which also contains thetokens
object,usesDTCG
option, etc.
const myFilter = { name: 'my-filter', // async is optional filter: async (token, options) => { return !token.filePath.endsWith('core.json'); },};
Using filters
First you will need to tell Style Dictionary about your filter. You can do this in two ways:
- Using the
.registerFilter
method - Inline in the configuration
hooks.filters
property
.registerFilter
import StyleDictionary from 'style-dictionary';
StyleDictionary.registerFilter(myFilter);
Inline
export default { hooks: { filters: { 'my-filter': myFilter, }, }, // ... the rest of the configuration};
Applying it in config
{ "source": ["**/*.tokens.json"], "platforms": { "css": { "transformGroup": "css", "files": [ { "format": "css/variables", "destination": "_variables.css", "filter": "my-filter" } ] } }}