figma-tokens-cli
v1.0.2
Published
## Overview This tool allows a user to export a scss file containing all the font, gradient, and color variables for a given document. Functionality could be expanded to include standard button styles and general component styles.
Downloads
7
Readme
Figma style generator
Please note that this is currently a proof of concept and is not ready to be used. I hope this proves to be useful to anyone who is also playing around with Figma's API
Overview
This tool allows a user to export a scss file containing all the font, gradient, and color variables for a given document. Functionality could be expanded to include standard button styles and general component styles.
Usage
- Users download the style guide template file and create a style guide in figma.
- The template document has named artboards which needed by the tool to identify the components on the artboard and how to handle them.
- Users copy and paste the artboard ID into the text field on the tool
- Users click generate style
- The tool outputs a typography.scss file, colors.scss file and a variables.scss file
Specification
Colors
Flat Colors
- RGB values get stored into an object with the following properties:
name
: string (eg.'$blue'
)r
: int (0 - 255)g
: int (0 - 255)b
: int (0 - 255)a
: float (0.0 - 1.0)hex
: string (eg.'#e567a0'
)cssColor
: string (valid css color definition)reference
: string (eg.'$blue'
), reference to another color by color name, default (None
)
- HEX code is generated from RBG colors and saved as
hex
on the color object. - cssColor value is generated
- If
a !== 0
;- cssColor =
rgba(hex, a)
- cssColor =
- else;
- cssColor =
hex
- cssColor =
- If
- The color name is assigned based on the name of the object on the figma file.
- text is converted to lowercase.
- spaces get converted to hyphens.
$
is prepended to the name.
- The color object is pushed into the
flatColors
array. - SCSS is generated
- For each color object:
- a color variable is defined
- eg.
$blue: #0000ff;
- eg.
- a number of css selectors are created
.color-<color-name>
setscolor
.bg-<color-name>
setsbackground-color
- a color variable is defined
- For each color object:
Gradients
- Figma object on colors artboard is checked if it contains a gradient.
- Gradient objects get processed after all colors are processed
- A gradient object is created containing properties:
name
: string (eg'green-gradient'
)colors
: array of strings, references color name ( see flat colors )gradientHandles
: array of floatsangle
: float (0.0 - 360.0)
- For each gradient a series of new color objects are created with the names
<gradient-name>-position-<num>
- Each new color is pushed to the the
gradientColors
array - Each color in the gradient is checked with the existing colors in the
flatColors
array - if no color variable exists;
- the color object created in step 4 is populated as per the specification for flat colors
- if the color variable does exist;
- the color object is populated with a reference to the flat color variable name.
angle
is calculated trigonometrically by taking the start and end positions of the gradient handles- SCSS is generated
- For each gradient object:
- two gradient variables are defined
- normal: eg.
$blue-gradient: linear-gradient(0deg, $blue-gradient-1 0%, $blue-gradient-2 100%);
- reversed: eg.
$blue-gradient-reversed: linear-gradient(0deg, $blue-gradient-2 0%, $blue-gradient-1 100%);
- normal: eg.
- a number of css selectors are created
.bg-<gradient-name>
setsbackground-color
to gradient.bg-<gradient-name>-reversed
setsbackground-color
to reversed gradient
- two gradient variables are defined
- For each gradient object:
Typography
- Each text object on the figma fonts artboard is processed
- Desktop fonts should be suffixed with
-lg
on the figma file. - Mobile fonts should be suffixed with
-xs
on the figma file. - Tablet fonts do not need to be specified as they will be interpolated.
- standard
- Desktop fonts should be suffixed with
- for each font group (eg.
h1-lg
,h1-xs
) a font group object is created:name
: sting (group name eg.h1
)sizes
: object of typography objects {xs
,sm
,md
,lg
}classes
: array of strings (eg['h1', '.font-style-one']
)
- Data from the figma json file populates a typography object is created for
xs
andlg
containing the following properties:name
: string (generated based on figma names (eg.h1-lg
))color
: string, references color name ( see flat colors )font-family
: stringfont-weight
: intfontSize
: inttextAlignVertical
: stringletterSpacing
: int,lineHeightPercent
: int
- data for
sm
andmd
are generated through interpolation ofxs
andlg
sm = xs + (lg - xs)/3
md = xs + (2 * (lg - xs))/3
- SCSS is generated
- For each font group object a css selector is created
- joining array or classes on a
,
(h1, .font-style-one
) - this gives each of the heading tags automatic styling as well as the possibility of overriding the style using the
font-style
class.
- joining array or classes on a
- each size maps to a media query eg
@include('>md')
- For each font group object a css selector is created