You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 03-bundling/06-vite/09-tailwindcss/README.md
+43-16Lines changed: 43 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,21 @@
1
-
# TailwindcCSS
1
+
# TailwindCSS
2
2
3
3
In this example, we are going add TailwindCSS integration.
4
4
5
5
📌 We start from sample `08-env-vars`.
6
6
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
+
7
19
# Steps to build it
8
20
9
21
## Prerequisites
@@ -20,18 +32,22 @@ Install [Node.js and npm](https://nodejs.org/en/) (20.19.0 || >=22.12.0) if they
20
32
npm install
21
33
```
22
34
23
-
- Let's add a TailwindCSS package:
35
+
- Let's add the main TailwindCSS package:
24
36
25
37
```bash
26
38
npm install tailwindcss
27
39
```
28
40
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
+
29
43
and also TailwindCSS's Vite plugin:
30
44
31
45
```bash
32
46
npm install @tailwindcss/vite --save-dev
33
47
```
34
48
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
+
35
51
- Let's modify `vite.config.ts` to include the plugin:
36
52
37
53
_vite.config.ts_
@@ -56,6 +72,8 @@ Install [Node.js and npm](https://nodejs.org/en/) (20.19.0 || >=22.12.0) if they
56
72
@import"tailwindcss";
57
73
```
58
74
75
+
> ℹ️ This CSS import will bring all base styles and utilities from TailwindCSS.
76
+
59
77
- And import it in our `index.tsx`;
60
78
61
79
_index.tsx_
@@ -106,6 +124,11 @@ Install [Node.js and npm](https://nodejs.org/en/) (20.19.0 || >=22.12.0) if they
106
124
+ @custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *));
107
125
```
108
126
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
+
109
132
- Update our `HelloComponent` to include styles for our new dark theme:
110
133
111
134
\__src/hello.tsx_
@@ -169,18 +192,22 @@ Install [Node.js and npm](https://nodejs.org/en/) (20.19.0 || >=22.12.0) if they
0 commit comments