Giter Club home page Giter Club logo

style-dictionary-theme-transformer's Introduction

Style Dictionary Tailwind CSS Transformer

Release Test Release

Style Dictionary to Tailwind CSS

This is a plugin to generate the config of Tailwind CSS using Style Dictionary.

Install

$ npm install sd-tailwindcss-transformer
# or with yarn
$ yarn add sd-tailwindcss-transformer

Usage

Creating configuration file

Generate tailwind.config.js by setting type to all.
See Creating each theme file if you wish to customize the configuration file with plugin functions, etc.

const StyleDictionaryModule = require('style-dictionary')
const { makeSdTailwindConfig } = require('sd-tailwindcss-transformer')

const StyleDictionary = StyleDictionaryModule.extend(
  makeSdTailwindConfig({ type: 'all' })
)

StyleDictionary.buildAllPlatforms()

Output:

// tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  mode: "jit",
  content: ["./src/**/*.{ts,tsx}"],
  darkMode: "class",
  theme: {
    extend: {
      colors: {
        base: {
          gray: "#111111",
          red: "#FF0000",
          ...
        }
      },
      fontSize: {
        small: "0.75rem",
        medium: "1rem",
        ...
      }
    },
  }
}

Creating each theme file

Create an object for each theme, assuming that various customizations will be made in the configuration file.
Import and use the created files in tailwind.config.js.

const StyleDictionaryModule = require('style-dictionary')
const { makeSdTailwindConfig } = require('sd-tailwindcss-transformer')

const types = ['colors', 'fontSize']

types.map((type) => {
  const StyleDictionary = StyleDictionaryModule.extend(
    makeSdTailwindConfig({ type })
  )

  StyleDictionary.buildAllPlatforms()
})

Output:

/// colors.tailwind.js
module.exports = {
  base: {
    gray: "#111111",
    red: "#FF0000",
    ...
  }
}
/// fontSize.tailwind.js
module.exports = {
  small: "0.75rem",
  medium: "1rem",
  ...
}

Using CSS custom variables

CSS custom variables can be used by setting isVariables to true.
In this case, a CSS file must also be generated.

const StyleDictionaryModule = require('style-dictionary')
const { makeSdTailwindConfig } = require('sd-tailwindcss-transformer')

const sdConfig = makeSdTailwindConfig({
  type: 'all',
  isVariables: true,
})

sdConfig.platforms['css'] = {
  transformGroup: 'css',
  buildPath: './styles/',
  files: [
    {
      destination: 'tailwind.css',
      format: 'css/variables',
      options: {
        outputReferences: true
      }
    }
  ]
}

const StyleDictionary = StyleDictionaryModule.extend(sdConfig)
StyleDictionary.buildAllPlatforms()

Output:

/* tailwind.css */
/**
 * Do not edit directly
 * Generated on ○○○○
 */

:root {
  --font-size-medium: 1rem;
  --font-size-small: 0.75rem;
  --colors-base-red: #FF0000;
  --colors-base-gray: #111111;
  ...
}
// tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  mode: "jit",
  content: ["./src/**/*.{ts,tsx}"],
  darkMode: "class",
  theme: {
    extend: {
      colors: {
        base: {
          gray: "var(--colors-base-gray)",
          red: "var(--colors-base-red)",
          ...
        }
      },
      fontSize: {
        small: "var(--font-size-small)",
        medium: "var(--font-size-medium)",
        ...
      }
    },
  }
}

Please see Example for details.

Options

Optional except for type.

Attribute Description Type
type Set the name of each theme (colors, fontSize, etc.) for 'all' or tailwind. 'all' or string
isVariables Set when using CSS custom variables.
Default value: false
boolean
source source attribute of style-dictionary.
Default value: ['tokens/**/*.json']
Array of strings
transforms platform.transforms attribute of style-dictionary.
Default value: ['attribute/cti','name/cti/kebab']
Array of strings
buildPath platform.buildPath attribute of style-dictionary.
Default value: 'build/web/'
string
tailwind.content Content attribute of Tailwind CSS. Set if necessary when 'all' is set in type.
Default value: ['./src/**/*.{ts,tsx}']
Array of strings
tailwind.darkMode Dark Mode attribute of Tailwind CSS. Set if necessary when 'all' is set in type.
Default value: 'class'
'media' 'class'
tailwind.plugin Tailwind CSS official plugins. Set if necessary when 'all' is set in type. Array of 'typography' 'forms' 'aspect-ratio' 'line-clamp' 'container-queries'

License

Apache 2.0

style-dictionary-theme-transformer's People

Contributors

renovate[bot] avatar nado1001 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.