Skip to content

Commit

Permalink
update best-practices-for-react-development-with-tailwind-css
Browse files Browse the repository at this point in the history
  • Loading branch information
gautamankoji committed Jul 1, 2024
1 parent d13e8e6 commit cfc4a15
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ author: Pentakota Avilash
lastmod: 2024-05-16T13:12:12-07:00
tags:
- react
- tailwind
- tailwind-css
draft: false
---

**Authors**:

- [Kulsoom Nisha](/authors/kulsoom-nisha/)
- [Pentakota Avilash](/authors/pentakota-avilash/)
- [Geeth Sowri](/authors/geeth-sowri/)

---

Expand Down Expand Up @@ -82,68 +83,69 @@ React and Tailwind CSS have become highly popular in the web development communi
- **PurgeCSS**: Enable PurgeCSS in your Tailwind configuration to remove unused CSS in production, reducing file size.
- **JIT Mode**: Use Just-in-Time (JIT) mode for faster build times and on-demand generation of styles.

### Integrating React with Tailwind CSS
## Integrating React with Tailwind CSS

#### Setup
### Setup

1. **Install Tailwind CSS** using npm or yarn.
1. Install Tailwind CSS using npm or yarn.

```bash
npm install tailwindcss
npx tailwindcss init
```
```sh
npm install tailwindcss
npx tailwindcss init
```

2. **Configuration**:
Configure `tailwind.config.js` and `postcss.config.js` to include Tailwind’s directives. Import Tailwind styles in your main CSS file.
2. Configuration:
Configure tailwind.config.js and postcss.config.js to include Tailwind’s directives.
Import Tailwind styles in your main CSS file.

```css
@tailwind base;
@tailwind components;
@tailwind utilities;
```

#### Usage in Components
```css
@tailwind base;
@tailwind components;
@tailwind utilities;
```

3. Usage in Components:
Apply Tailwind’s utility classes directly in React components.

```javascript
import React from 'react';
```javascript
const Button = () => (
<button className="bg-blue-500 text-white font-bold py-2 px-4 rounded">
Click Me
</button>
);
import React from 'react';
export default Button;
```
const Button = () => (
<button className="bg-blue-500 text-white font-bold py-2 px-4 rounded">
Click Me
</button>
);
#### Custom Components
export default Button;
```

## Custom Components

Create reusable components with Tailwind CSS.

```javascript
const Card = ({ title, description }) => (
<div className="max-w-sm rounded overflow-hidden shadow-lg">
<div className="px-6 py-4">
<div className="font-bold text-xl mb-2">{title}</div>
<p className="text-gray-700 text-base">
{description}
</p>
<p className="text-gray-700 text-base">{description}</p>
</div>
</div>
);
export default Card;
```

#### More Examples of React Components Styled with Tailwind CSS
Here are more examples of React components styled with Tailwind CSS. These examples cover various common UI elements like buttons, forms, modals, and navigation bars.

##### Example 1: Button Component
1. Example 1: Button Component

A simple button component with different styles for primary and secondary buttons.

```javascript
import React from 'react';
const Button = ({ type = 'primary', children, onClick }) => {
Expand All @@ -163,11 +165,12 @@ const Button = ({ type = 'primary', children, onClick }) => {
export default Button;
```

##### Example 2: Form Component
2. Example 2: Form Component

A form component with input fields styled using Tailwind CSS.

```javascript
import React, { useState } from 'react';
const Form = () => {
Expand Down Expand Up @@ -220,7 +223,7 @@ const Form = () => {
export default Form;
```
##### Example 3: Modal Component
3. Example 3: Modal Component
A modal component that can be toggled open and closed.
Expand Down Expand Up @@ -264,11 +267,12 @@ const App = () => {
export default App;
```
##### Example 4: Navigation Bar Component
4. Example 4: Navigation Bar Component
A responsive navigation bar component.
```javascript
import React from 'react';
const NavBar = () => {
Expand All @@ -277,18 +281,10 @@ const NavBar = () => {
<div className="container mx-auto flex justify-between items-center">
<div className="text-white text-lg font-semibold">MyApp</div>
<div className="hidden md:flex space-x-4">
<a href="#home" className="text-gray-300 hover:text-white">
Home
</a>
<a href="#about" className="text-gray-300 hover:text-white">
About
</a>
<a href="#contact" className="text-gray-300 hover:text-white">
Contact
</a>
</div>
<a href="#home" className="text-gray-300 hover:text-white">Home</a>
<a href="#about" className="text-gray-300 hover:text-white">About</a>
<a href="#contact" className="text-gray-300 hover:text-white">Contact</a>
</div>
</div>
</nav>
);
Expand All @@ -297,11 +293,12 @@ const NavBar = () => {
export default NavBar;
```
##### Example 5: Card Component
1. Example 5: Card Component
A card component for displaying content in a structured format.
```javascript
import React from 'react';
const Card = ({ title, description }) => {
Expand All @@ -314,8 +311,6 @@ const Card = ({ title, description }) => {
</div>
);
};
export default Card;
```
### Component-Driven Development
Expand Down
2 changes: 1 addition & 1 deletion public

0 comments on commit cfc4a15

Please sign in to comment.