Giter Club home page Giter Club logo

tw-tag's Introduction

tw-tag

tw-tag is a library for describing Tailwind CSS as a kind of DSL.

Usage

import { tw } from "tw-tag"

const tailwindClassName = tw`
  flex
  pt-4
  text-center
  rotate-90
`

console.log(tailwindClassName) // 'flex pt-4 text-center rotate-90'

function SomeComponent() {
  return (
    <>
      <div className={tailwindClassName} />
      {/* inline */}
      <div className={tw`pt-4 mx-auto`} />
    </>
  )
}

Use Babel

The tw function converts consecutive spaces into a single space. You can do this at build time by using the Babel plugin.

// babel.config.js
module.exports = {
  plugins: [
    'tw-tag/babel-plugin',
  ],
}
// before
import { tw } from 'tw-tag'

const tailwindClassName = tw`
  flex
  pt-4
  text-center
  rotate-90
`

// after
const tailwindClassName = `flex pt-4 text-center rotate-90`

in TypeScript

If you write tw(`...`), the type will be inferred based on your input.

// type: string
const a = tw`
  flex
  pt-4
  text-center
  rotate-90
`

// type: 'flex pt-4 text-center rotate-90'
const b = tw(`
  flex
  pt-4
  text-center
  rotate-90
`)

The tw`...` format will be supported once TemplateStringsArray inference is improved.

ref: microsoft/TypeScript#49552

Babel options

// babel.config.js
module.exports = {
  plugins: [
    ['tw-tag/babel-plugin', {
      devLabel: false,
    }],
  ],
}

devLabel

type: boolean (default value: process.env.NODE_ENV !== 'production')

Add className for debugging. format: DEV-[fileName]-[localName]

// some-file.js
const variableName = tw`p4`

// build when develop
const variableName = `DEV-someFile-variableName p4`

tw support for VSCode

To enable Tailwind CSS IntelliSense completion, add the setting below.

// settings.json
{
  // ...
  "tailwindCSS.experimental.classRegex": [
    "tw\\(?`([^`]*)"
  ],
}

tw-tag's People

Contributors

imasanari 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.