Skip to content

Instantly share code, notes, and snippets.

@wildfrontend
Created June 5, 2020 15:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wildfrontend/a12bc826b3a26ae81c6657b56fb6449f to your computer and use it in GitHub Desktop.
Save wildfrontend/a12bc826b3a26ae81c6657b56fb6449f to your computer and use it in GitHub Desktop.
tailwind.css with css-in-jss , react setup

tailwind.css with css-in-jss

https://www.freecodecamp.org/news/how-to-style-your-react-apps-with-less-code-using-tailwind-css-and-styled-components/

Step

  1. install
yarn add -D tailwindcss twin.macro autoprefixer babel-plugin-macros styled-components
  1. add tailwind config
npx tailwind init src/tailwind.config.js
  1. add babel-plugin babel.plugin.js
module.exports = {
  tailwind: {
    plugins: ["macros"],
    config: "./src/tailwind.config.js",
    format: "auto",
  },
}
  1. style component file
import styled from "styled-components"
import tw from "twin.macro"

const StyledForm = styled.main.attrs({
  className: "flex flex-col h-screen justify-center items-center bg-gray-100",
})`
  & {
    form {
      ${tw`bg-white text-center rounded py-8 px-5 shadow max-w-xs`}
    }
    input {
      ${tw`border-gray-300 mb-4 w-full border-solid border rounded py-2 px-4`}
    }
    button {
      ${tw`bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 border border-blue-700 rounded`}
    }
  }
`
export default StyledForm

App.js

import React from "react"
import StyledForm from "./styles"

function App() {
  return (
    <StyledForm>
      <form>
        <input type="text" placeholder="Full name" />
        <input type="text" placeholder="Email" />
        <input type="text" placeholder="Password" />
        <button>Sign In</button>
      </form>
    </StyledForm>
  )
}

export default App

done1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment