Those are great questions! In my personal experience, I have recently delved into utilizing React boilerplate projects.
To kick things off, open up your terminal and input the following commands to initiate a new project.
#using NPX
npx create-react-app tailwindreact-app
#using NPM
npm init react-app tailwindreact-app
#using yarn
yarn create react-app tailwindreact-app
Create-react-app
serves as the established React building tool for setting up new React endeavors.
Navigate to your app
directory
cd tailwindreact-app
Subsequently, install Tailwind CSS along with its prerequisites
#using npm
npm install -D tailwindcss@npm:@tailwindcss/postcss7-compat @tailwindcss/postcss7-compat postcss@^7 autoprefixer@^9
#using Yarn
yarn add tailwindcss@npm:@tailwindcss/postcss7-compat @tailwindcss/postcss7-compat postcss@^7 autoprefixer@^9 -D
As Create React App
does not yet support PostCSS 8, we must download the version of PostCSS7
that aligns with Tailwind CSS v2
Next on the agenda is setting up CRACO
to configure Tailwindcss
#using npm
npm install @craco/craco
#using Yarn
yarn add @craco/craco
Inaugurate a CRACO configuration file in your primary directory, either manually or through this command:
touch craco.config.js
Add tailwindcss
and autoprefixer
as PostCSS plugins to your CRACO configuration file:
// craco.config.js
module.exports = {
style: {
postcss: {
plugins: [
require('tailwindcss'),
require('autoprefixer'),
],
},
},
}
Adjust your app to employ craco
for executing development and build scripts.
Access your package.json
file and replace the contents within scripts
with:
"scripts": {
"start": "craco start",
"build": "craco build",
"test": "craco test",
"eject": "react-scripts eject"
}
Erect the default configurations scaffold:
npx tailwindcss init
This action generates a tailwind.config.js
in the foundational directory of your project. This file encapsulates all default configurations of Tailwind. By including an optional --full flag, you can generate a configuration file stocked with all the defaults bundled with Tailwind.
Integrating Tailwind in your CSS
Inside your src
folder, establish a subfolder named styles
. This will serve as the home for all your stylings.
Within that folder, craft a tailwind.css
alongside an index.css
document.
The index.css
file acts as the hub for importing tailwind’s fundamental styles and configurations while tailwind.css
houses the compiled output of index.css
.
Tailwind CSS components, utilities, and base styles
Append the subsequent content to your index.css
file.
//index.css
@tailwind base;
@tailwind components;
@tailwind utilities;
``@tailwind' functions as a tailwind directive implemented to infuse default base styles
, components
, utilities
, and bespoke configurations.
@tailwind base **injects Tailwind’s base styles, encompassing Normalize.css coupled with supplementary base styles.</p>
<p><code>@tailwind components
introduces any component—a concise reusable style such as buttons, form constituents, etc.—classes enlisted by plugins enumerated in your tailwind config file.
Beneath the component import resides where custom component classes can be appended—elements that should be loaded prior to the default utilities to give room for potential overrides by the utilities.
For instance:
.btn { ... }
.form-input { ... }
@tailwind utilities
embeds all of Tailwind’s utility classes (including both default and customized ones), which are assembled based on your config file.
Below the utilities import lies where additional unique utilities can be incorporated that diverge from the out-of-the-box provisions proffered by Tailwind.
Illustrative snippet:
.bg-pattern-graph-paper { ... }
.skew-45 { ... }
Tailwind swaps out all these directives during the construction phase and replaces them with the generated CSS.
Configuring your app to assemble your CSS file
When configuring your app to leverage CRACO
for fabricating your styles every time you execute the npm start
or yarn start
command, amend your package.json
file by reshaping the contents within scripts
:
"scripts": {
"build:style": "tailwind build src/styles/index.css -o src/styles/tailwind.css",
"start": "craco start",
"build": "craco build",
"test": "craco test",
"eject": "react-scripts eject"
},
To import your CSS into the application, navigate to your index.js
file and include your Tailwind styles:
import './styles/tailwind.css';
Eradicate the index.css
and app.css
files lying in your project's root directory and expunge their corresponding import statements in the Index.js
and App.js
documents, respectively.
Your index.js
file should resemble something akin to this:
// index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './styles/tailwind.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
That wraps it up.
I trust my insights will prove beneficial to others.