Token Types
Gradient Color

Gradient Color Tokens

You can define Color Token values as gradients by writing a css-like syntax. This includes:

  • Angle of the gradient in degrees (deg for short).
  • Two or more colors.
  • The position of each color in percentage.
    • 0% is the start
    • 100% is the end

Limited gradient support
Today, the plugin only supports linear gradients when exporting to Figma.

Here's an example of how to write the value of a linear gradient with two stops that appear to change colors diagonally (on a 45 degree angle):

linear-gradient(45deg, #87CEEB 0%, #008000 100%)

To add additional color stops, add them between the round brackets, being sure to define the color value and the position as a percentage.

Here is the same example with a third color stop added exactly at 50%:

linear-gradient(45deg, #0000FF 0%, #87CEEB 50%, #008000 100%)

You can also Import Color Styles of gradients you've created in Figma to see how they are written in the plugin.

Read the Importing Styles Guide

Gradients can reference another Color Token

Like Color Tokens with a single value, you can reference another Token in your gradients by adding curly brackets around the Token name you want to reference.

Here's an example of a linear gradient with two stops, with colors that appear to change horizontally from light to dark gray:

linear-gradient(180deg, {colors.gray.50} 0%, {colors.gray.900} 100%)

Reduced opacity (alpha values)

Like Color Tokens with a single value, you can write the value of your gradients to have colors with reduced opacity by defining each color stop in RGBA.

RGBA colors use a unitless number to define the alpha value, converted into a percentage in Figma. For example:

  • 1 = 100% opacity
  • 0.4 = 40% opacity
  • 0.06 = 6% opacity

Here's an example of a linear gradient with two stops, with colors that appear to change vertically from light with 40% opacity 0.40 to dark gray with 100% opacity 1:

linear-gradient(270deg, rgba({colors.gray.50}, 0.4) 0%, rgba({colors.gray.900}, 1) 100%)

This also works when your colors are defined as hard-coded values instead of referencing another Token.

Color Styles with Variable references

Like Color Tokens with a single value, your gradients can be Exported to Figma as Color Styles that reference Color Variables.

Before you export your gradient Color Tokens to Figma as styles, ensure your:

  • Gradients have values that reference Color Tokens.
  • Color variables have been created and are attached to the Referenced Color Tokens.

When you Export to Figma as Color Styles, ensure:

  • The option for Color styles is selected.
  • The option for Create styles with variable references is selected.
  • Themes and Token sets where the gradients are located are active.
  • Themes and Token sets where the variables are attached are reference only.

Note that if the Tokens referenced include modified colors that haven't been resolved, the connection to the Variable will not work as expected.

  • For example, a Token included in the gradient written as rgba({colors.gray.900, 0.5) will not be connected to the colors.gray.900 variable because the plugin has not yet resolved the value, written as a 50% opacity in RGBA.
  • You could create a new Token, for example, gradient-stop with a value of rgba({colors.gray.900, 0.5), then reference the {gradient-stop} Token in the gradient value instead.

Read the Export to Figma Styles with Variable References for more details

Limitations of gradient color Tokens

The plugin will support creating Tokens of any CSS Gradients. However, we are not yet able to create all types of gradients in Figma:

  • Linear - Supported
  • Radial - Requested
  • Conic - Requested

Positioning a gradient on a layer

In code, gradients are"a special type of image", but Figma doesn't treat them that way.

As design Tokens are intended to be used in code, we are not able to control the position of a gradient on a layer within the plugin in the same way you can in Figma.

  • The plugin is only able to store the degree of the gradient.
  • Without any translation information, the positioning of the gradient on a layer cannot be accurately determined.
  • By default, the plugin applies the gradient to the "edges" of the design element you select when applying the Token.

W3C DTCG Token Format

gradient is an official Token type in the in the W3C Design Token Community Group specifications.(9.6 Gradient)

However, Tokens Studio has yet to support Gradients as an independent Token Type. It is on our radar as we work towards more closely aligning with the DTCG spec.

Transforming Tokens

Engineers typically transform Tokens used in code with Style Dictionary, which is tool-agnostic. Tokens coming from Tokens Studio require an additional step: @Tokens-studio/sd-transforms, an npm package that prepares Tokens for Style Dictionary.

When transforming Color Tokens with gradient values, there are specific configurations to be aware of.

The SD-Transforms generic package will convert color Token values with Figma's "hex code RGBA" into actual rgba() format for CSS.

SD-Transforms Read-Me Doc, ts/color/css/hexrgba

Color Tokens with gradient values are technically a composite Token as it contains multiple values. Composite Tokens require the SD-Transforms option to expand composite Tokens into multiple Tokens.

  • Make sure you look at the generic SD-Transforms package to include this option, which allows you to further customize this transformation further using Style Dictionary.

SD-Transforms Read-Me Doc, Using the preprocessor
SD-Transforms Read-Me Doc, Using Expand

"object, object"
When you transform your Color Tokens, and they show "object, object" it means your SD-Transforms configuration needs to be adjusted to include "expand".

Resources

Mentioned in this doc:

Figma resources:

Gradient resources

CSS resources:

Community resources:

  • None yet!

💡 Something to share? Submit it here!

Known issues and bugs

Tokens Studio Plugin GitHub - Open issues for Token Type Gradients

  • Linear-Gradient Token Does Not Work With Other Color-spaces (Also When Creating Styles/Variables) #2718
    • Gradients with values in the HSL and P3 color spaces aren't working as expected.

🐞 If you are experiencing an issue not listed here, please reach out to us on the Troubleshooting channel of our community Slack, or submit it on our feedback tool.

Requests, roadmap and changelog

💌 Visit https://feedback.Tokens.studio/ to contribute or subscribe to updates.

Last updated on September 20, 2024