Skip to content

Commit 80dacff

Browse files
committed
review tailwind readme
1 parent 4a2b0a4 commit 80dacff

File tree

1 file changed

+43
-16
lines changed

1 file changed

+43
-16
lines changed

03-bundling/06-vite/09-tailwindcss/README.md

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
# TailwindcCSS
1+
# TailwindCSS
22

33
In this example, we are going add TailwindCSS integration.
44

55
📌 We start from sample `08-env-vars`.
66

7+
## Introduction
8+
9+
ℹ️ Before jumping into the sample, let's briefly introduce TailwindCSS.
10+
11+
TailwindCSS is a utility-first framework that provides a set of predefined CSS classes — often referred to as _primitives_. These classes allow you to style elements directly in your markup, instead of writing custom CSS from scratch.
12+
13+
Each class typically maps to a single, simple CSS rule (for example: `h-full` translates to `height: 100%`). This modular approach makes styling faster and more predictable.
14+
15+
While most classes are low-level utilities, TailwindCSS also includes more advanced tools out-of-the-box — like animations, transitions, and responsive variants.
16+
17+
Think of it as a well-organized CSS toolbox: consistent, fast, and highly extensible, making UI development smoother and less prone to complexity than managing large custom stylesheets.
18+
719
# Steps to build it
820

921
## Prerequisites
@@ -20,18 +32,22 @@ Install [Node.js and npm](https://nodejs.org/en/) (20.19.0 || >=22.12.0) if they
2032
npm install
2133
```
2234

23-
- Let's add a TailwindCSS package:
35+
- Let's add the main TailwindCSS package:
2436

2537
```bash
2638
npm install tailwindcss
2739
```
2840

41+
> ℹ️ `tailwindcss` package is the core of TailwindCSS. It ships the engine that interprets classes (like `bg-red-500` or `h-full`) and generates the proper CSS. It uses `PostCSS` under the hood to compile and optimize styles.
42+
2943
and also TailwindCSS's Vite plugin:
3044

3145
```bash
3246
npm install @tailwindcss/vite --save-dev
3347
```
3448

49+
> ℹ️ Although not mandatory, this plugin will help us to integrate TailwindCSS compiler with Vite in a more efficient way. Otherwise, we would have to manually setup PostCSS in Vite configuration.
50+
3551
- Let's modify `vite.config.ts` to include the plugin:
3652

3753
_vite.config.ts_
@@ -56,6 +72,8 @@ Install [Node.js and npm](https://nodejs.org/en/) (20.19.0 || >=22.12.0) if they
5672
@import "tailwindcss";
5773
```
5874

75+
> ℹ️ This CSS import will bring all base styles and utilities from TailwindCSS.
76+
5977
- And import it in our `index.tsx`;
6078

6179
_index.tsx_
@@ -106,6 +124,11 @@ Install [Node.js and npm](https://nodejs.org/en/) (20.19.0 || >=22.12.0) if they
106124
+ @custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *));
107125
```
108126

127+
> ℹ️ This line is doing the following:
128+
>
129+
> - The directive `@custom-variant` is creating a variant called `dark` that TailwindCSS will use to apply conditional sytles ... but ¿when?
130+
> - The rest, is an advanced selector that is telling TailwindCSS when to apply the `dark:` variant: whe the current element (`&`) is placed within an HTML tree tree that contains `data-theme="dark"` attribute.
131+
109132
- Update our `HelloComponent` to include styles for our new dark theme:
110133

111134
\__src/hello.tsx_
@@ -169,18 +192,22 @@ Install [Node.js and npm](https://nodejs.org/en/) (20.19.0 || >=22.12.0) if they
169192
\__src/hello.tsx_
170193

171194
```diff
172-
return (
173-
<>
174-
+ <button
175-
+ className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
176-
+ onClick={() => {
177-
+ const html = document.documentElement;
178-
+ const isDark = html.getAttribute("data-theme") === "dark";
179-
+ if (isDark) html.setAttribute("data-theme", "");
180-
+ else html.setAttribute("data-theme", "dark");
181-
+ }}
182-
+ >
183-
+ Toggle theme
184-
+ </button>
185-
<h2>Hello from React</h2>
195+
};
196+
197+
+ const toggleTheme = () => {
198+
+ const html = document.documentElement;
199+
+ const isDark = html.getAttribute("data-theme") === "dark";
200+
+ if (isDark) html.removeAttribute("data-theme");
201+
+ else html.setAttribute("data-theme", "dark");
202+
+ };
203+
204+
return (
205+
<>
206+
+ <button
207+
+ className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
208+
+ onClick={toggleTheme}
209+
+ >
210+
+ Toggle theme 🎨
211+
+ </button>
212+
<h2>Hello from React</h2>
186213
```

0 commit comments

Comments
 (0)