References
These utilities have to do with token references/aliases.
usesReferences
Whether or not a token value contains references
resolveReferences
Takes a token value string value and resolves any reference inside it
You can pass a third options
argument where you can pass some configuration options for how references are resolved:
usesDtcg
boolean, if set to true, theresolveReferences
utility will assume DTCG syntax ($value
props)warnImmediately
boolean,true
by default. You should only set this tofalse
if you know that this utility is used used inside of the Transform lifecycle hook of Style Dictionary, allowing the errors to be grouped and only thrown at the end of the transform step (end of exportPlatform method).
getReferences
Whether or not a token value contains references
You can pass a third options
argument where you can pass some configuration options for how references are resolved:
usesDtcg
boolean, if set to true, theresolveReferences
utility will assume DTCG syntax ($value
props)unfilteredTokens
, assuming the secondtokens
argument is your filteredtokens
object where filters have already done its work, you’ll likely want to pass the unfiltered set in case the reference you’re trying to find no longer exist in the filtered set, but you still want to get the reference values. This is useful when you’re writing your own custom format with anoutputReferences
feature and you want to prevent outputting refs that no longer exist in the filtered set.warnImmediately
boolean,true
by default. You should only set this tofalse
if you know that this utility is used inside of the Format lifecycle hook of Style Dictionary, allowing the errors to be grouped and only thrown at the end of the format step.
Complicated example
You can use the getReferences
utility to create your own custom formats that have outputReferences
capability.
outputReferencesFilter
An OutputReferences
function that filters for tokens containing references to other tokens that are filtered out in the output.
Usually Style Dictionary will throw a warning when you’re using outputReferences: true
and are about to have a broken reference in your output because the token you’re referencing is filtered out.
What that means is that you usually have to either adjust your filter or disable outputReferences
altogether, but supplying a function instead allows you to conditionally output references on a per token basis.
Output:
Note that --colors-grey
was filtered out and therefore the shadow does not contain a CSS custom property (reference) but rather the resolved value.
Live Demo:
outputReferencesTransformed
An outputReferences
function that checks for each token whether
the value has changed through a transitive transform compared to the original value where references are resolved.
Output:
Note that --referred
is using the resolved value that is a transformed version of --base
instead of rgba(var(--base), 12%)
which would be invalid CSS.
This can be verified by setting outputReferences
to true
in the demo below.
Live Demo:
Combining multiple outputReference utility functions
These utility functions can be quite easily combined together: