1.1.3 • Published 11 months ago

figma-tokens-to-tailwind-variables v1.1.3

Weekly downloads
-
License
ISC
Repository
-
Last release
11 months ago

Usage

Use this package to convert exported Figma tokens JSON into valuable TailwindCSS data.

Install

yarn add figma-tokens-to-tailwind-variables

or

npm install figma-tokens-to-tailwind-variables

Usage

In your tailwind.config.js file.

/*
 * Change "figma-tokens.json" to point to
 * the location of your exported tokens file.
 */
import tokens from "figma-tokens.json" assert { "type": "json" };
import FigmaTokensToTailwindVariables from "figma-tokens-to-tailwind-variables";

const {
  Colors,
  FontSizes,
  FontFamilies,
  FontWeights,
  LineHeights,
  BoxShadows,
} = FigmaTokensToTailwindVariables(tokens);

export default {
  // ... Your other TailwindCSS configuration
  theme: {
    /*
     * NOTE: You can use color or font-family
     * variables inside of extend, but
     * I prefer to have my colors only.
     */
    colors: {
      ...Colors,
    },
    fontFamily: {
      ...FontFamilies,
    },

    extend: {
      fontSize: {
        ...FontSizes,
      },
      fontWeight: {
        ...FontWeights,
      },
      lineHeight: {
        ...LineHeights,
      },
      boxShadow: {
        ...BoxShadows,
      }
    }
  }
};

Other helpful snippets

Color variables

export default = {
  // ... Your other TailwindCSS configuration
  theme: {
    /*
     * NOTE: I use this in tandem with
     * @mertasan/tailwindcss-variables
     * to export CSS variables.
     * Variables are exported as
     * --color-NAME-NUMBER into the CSS :root
     * (eg --color-green-100: #E5F2EB)
     */
    variables: {
      DEFAULT: {
        color: {
          ...Colors,
        },
      },
    },
  }
  plugins: [
    require("@mertasan/tailwindcss-variables"),
  ],
};

Require vs import.

This can alternately be called in via require.

const tokens = require("figma-tokens.json");
const { default: FigmaTokensToTailwindVariables } = require("figma-tokens-to-tailwind-variables");
const { Colors } = FigmaTokensToTailwindVariables(tokens);

Options

The function FigmaTokensToTailwindVariables allows a secondary parameter with options:

import tokens from "figma-tokens.json" assert { "type": "json" };
import FigmaTokensToTailwindVariables from "figma-tokens-to-tailwind-variables";

const {
  ...
} = FigmaTokensToTailwindVariables(tokens, {
  // Place your options here
});
Option nameDefaultUsageType
fontFamilyNamespacetypography/font-familyDefines the string-based search pattern that the processor searches for define font family names. Eg. a token with a variable name typography/font-family/base will return FontFamily { 'base': value }String
fontSizeNamespacetypography/sizeDefines the string-based search pattern that the processor searches for define font sizes. Eg. a token with a variable name typography/size/h1-mobile will return FontSize { 'h1-mobile': VALUE }String
fontWeightNamespacetypography/weightsDefines the string-based search pattern that the processor searches for define font weights. Eg. a token with a variable name typography/weights/base/bold will return FontWeights { base: { bold: VALUE } }String
lineHeightNamespacetypography/line-heightDefines the string-based search pattern that the processor searches for define line-heights. Eg. a token with a variable name typography/line-height/heading will return LineHeights { heading: VALUE }String

Figma naming recommendations

  • Store groups of variables in collections and folders that can be easily found by their namespace (eg. all font size variables should fall under Typography/Font size/H1 or, better, typography/font-size/h1).
  • While the tokens processor sanitizes the value placed beyond the final slash (eg. tokens stored as Typography/Font Size/H1 Mobile will return as h1-mobile), we recommend adopting a lowercase formatting and using hyphens (-) instead of spaces.
1.1.3

11 months ago

1.1.2

11 months ago

1.1.1

12 months ago

1.0.2

12 months ago

1.1.0

12 months ago

1.0.9

12 months ago

1.0.8

12 months ago

1.0.7

12 months ago

1.0.6

12 months ago

1.0.5

12 months ago

1.0.4

12 months ago

1.0.10

12 months ago

1.0.15

12 months ago

1.0.14

12 months ago

1.0.13

12 months ago

1.0.12

12 months ago

1.0.1

12 months ago

1.0.0

12 months ago