DEV Community

Cover image for Setup Reactjs + TypeScript with Tailwind CSS
0xkoji
0xkoji

Posted on • Updated on

Setup Reactjs + TypeScript with Tailwind CSS

In this post, I will show you how to set up Tailwind CSS with Reactjs(TypeScript).

What we will create is this.
https://hopeful-rosalind-29803f.netlify.app/

1. Create a reactjs project with create-react-app

$ yarn create react-app react-tailwind-ts --template typescript
Enter fullscreen mode Exit fullscreen mode

2. Install Tailwind CSS

$ yarn add -D tailwindcss@npm:@tailwindcss/postcss7-compat postcss@^7 autoprefixer@^9
Enter fullscreen mode Exit fullscreen mode

3. Install CRACO

$ yarn add @craco/craco
Enter fullscreen mode Exit fullscreen mode

4. Modify files

In this step, we will edit two files, package.json and craco.config.js(create).
package.json

"scripts": {
    "start": "craco start",
    "build": "craco build",
    "test": "craco test",
    "eject": "react-scripts eject"
},
Enter fullscreen mode Exit fullscreen mode

craco.config.js

module.exports = {
  style: {
    postcss: {
      plugins: [require("tailwindcss"), require("autoprefixer")],
    },
  },
};
Enter fullscreen mode Exit fullscreen mode

5. Generate tailwind.config.js

In this step, we will generate tailwind.config.js by the following command and edit purge.

$ yarn tailwindcss-cli@latest init
Enter fullscreen mode Exit fullscreen mode

tailwind.config.js

module.exports = {
  purge: ["./src/**/*.{js,jsx,ts,tsx}", "./public/index.html"],
  darkMode: false, // or 'media' or 'class'
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
};
Enter fullscreen mode Exit fullscreen mode

6. Add Tailwind

index.css

@tailwind base;
@tailwind components;
@tailwind utilities;
Enter fullscreen mode Exit fullscreen mode

index.tsx

import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";

ReactDOM.render(<App />, document.getElementById("root"));

reportWebVitals();
Enter fullscreen mode Exit fullscreen mode

7. Create a component for Gradient Text

components/GradientText.tsx

type Props = {
  text: string;
};

export const GradientText = ({ text }: Props) => {
  return (
    <div className="p-10 min-h-screen flex items-center justify-center bg-cool-gray-700">
      <h1 className="text-9xl font-black text-white text-center">
        <span className="bg-gradient-to-r text-transparent bg-clip-text from-green-400 to-purple-500">
          {text}
        </span>
      </h1>
    </div>
  );
};
Enter fullscreen mode Exit fullscreen mode

8. Run the app

$ yarn start
Enter fullscreen mode Exit fullscreen mode

If everything works well, you will see the following image.

Image description

repo

GitHub logo koji / react_with_TailwindCSS

reactjs with Tailwind CSS

Getting Started with Create React App

This project was bootstrapped with Create React App.

Available Scripts

In the project directory, you can run:

yarn start

Runs the app in the development mode.
Open http://localhost:3000 to view it in the browser.

The page will reload if you make edits.
You will also see any lint errors in the console.

yarn test

Launches the test runner in the interactive watch mode.
See the section about running tests for more information.

yarn build

Builds the app for production to the build folder.
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.
Your app is ready to be deployed!

See the section about deployment for more information.

yarn eject

Note: this is a one-way operation. Once you eject, you can’t go back!

If you aren’t satisfied…

Top comments (2)

Collapse
 
andreyscott profile image
Andreyscott

what is CARACO and why should we add it

Collapse
 
rahulbarwal profile image
Rahul Barwal

CARACO is used for changing what to do when you build your react app. When you want to add some additional things to the build process, it makes sense to use CARACO

For this scenario, I guess building tailwind again - when the base file changes(to rebuild all tailwind classes), it needs extra steps.