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
Some years ago, I was searching for a UI kit to use in one of my hobby react apps. I found some good-looking React UI kits like [Ant Design](http://ant.design), [BlueprintJS](https://blueprintjs.com) and [Evergreen](https://evergreen.segment.com) but sometimes the bloat becomes unbearable and customizability becomes a priority. [Material UI](https://material-ui.com) is said to be the most popular one, but, no thanks; not a fan of material UI. Anyway, the discussion on available react UI kits is a topic for a different post. Here what happened was that I tried to create my own UI kit with SASS and soon found out that there is a gap between my idea on how the components should look and my knowledge on how to use CSS properly.
3
+
Few years ago, I was searching for a UI kit to be used in one of my hobby react apps. I found some good-looking React UI kits like [Ant Design](http://ant.design), [BlueprintJS](https://blueprintjs.com) and [Evergreen](https://evergreen.segment.com) but sometimes the bloat becomes unbearable and customizability becomes a priority. [Material UI](https://material-ui.com) is said to be the most popular one, but, no thanks; not a fan of material UI. Anyway, the discussion on available react UI kits is a topic for a different post. Here what happened was that I tried to create my own UI kit with SASS and soon found out that there is a gap between my idea on how the components should look and my knowledge on how to use CSS properly.
4
4
5
5
# What is Tailwind CSS?
6
6
@@ -18,27 +18,27 @@ First I’ve created the `tailwind-test` folder and initialized the project with
18
18
First add parcel bundler; this takes care of how to load, process and bundle all the `.tsx`, .`css`, `.html` etc. you’re going to create.
19
19
20
20
```sh
21
-
yarn add --dev parcel-bundler
21
+
yarn add --dev parcel
22
22
```
23
23
24
24
Then add Tailwind CSS as stated in the documentation.
25
25
26
26
```sh
27
-
yarn add tailwindcss
27
+
yarn add tailwindcss postcss autoprefixer
28
28
```
29
29
30
30
Add the below `scripts` section to your `package.json` so that you can run, build and clean the project easily.
Create the `src` folder and create the `index.html` file with a basic HTML5 template. You can also use `html:5` snippet/emmet if you’re using [vsocde](https://code.visualstudio.com/).
41
-
Add `<div class="app"></div>` and `<script src="./main.tsx"></script>` inside body, so that React can mount your app there.
41
+
Add `<div class="app"></div>` and `<script src="./main.ts"></script>` inside body, so that React can mount your app there.
42
42
43
43
```html
44
44
<!DOCTYPE html>
@@ -51,28 +51,19 @@ Add `<div class="app"></div>` and `<script src="./main.tsx"></script>` inside bo
51
51
</head>
52
52
<body>
53
53
<divid="app"></div>
54
-
<scriptsrc="./main.tsx"></script>
54
+
<scripttype="module"src="./main.ts"></script>
55
55
</body>
56
56
</html>
57
57
```
58
58
59
-
Create the `main.tsx` and add your React app there. Note that we have added a [button](https://tailwindcss.com/components/buttons)which uses Tailwind styles with utility classes. Utility classes is [not the only way](https://tailwindcss.com/docs/extracting-components) you can add Tailwind styles. Since we’re trying Tailwind with React, utility classes is enough for us right now.
59
+
Create the `main.ts` and add your React app there. Note that we have added a custom card component which uses Tailwind styles with utility classes. `src/components/Card/index.tsx` and `src/views/App.tsx` are omitted for clarity. Utility classes is [not the only way](https://tailwindcss.com/docs/reusing-styles) you can add Tailwind styles. Since we’re trying Tailwind with React, utility classes is enough for us right now.
Create `main.css` file and add the below. These are tailwind directives. This is needed to inject tailwind [styles](https://tailwindcss.com/docs/preflight) and utility classes into your CSS.
@@ -85,135 +76,27 @@ Create `main.css` file and add the below. These are tailwind directives. This is
85
76
@tailwind utilities;
86
77
```
87
78
88
-
Add `postcss.config.js` file inside the project folder (i.e.: one level up from `src` folder). Tailwind CSS is a [PostCSS](https://postcss.org/) plugin where PostCSS handles all pre/post processing of CSS you write, such as adding [vendor prefixes](https://developer.mozilla.org/en-US/docs/Glossary/Vendor_Prefix)[automatically](https://github.com/postcss/autoprefixer). Parcel has built-in support for PostCSS, but doesn’t know yet about Tailwind, so we have to configure it with the below content. Make sure you include `tailwindcss` before `autoprefixer`.
89
-
90
-
```js
91
-
module.exports= {
92
-
plugins: [
93
-
require("tailwindcss"),
94
-
require("autoprefixer"),
95
-
require("postcss-nested"),
96
-
],
97
-
};
98
-
```
99
-
100
-
Now it’s show-time. Run `yarn start`.
101
-
At this moment, if you are curious why we didn’t add typescript or react, don’t worry; parcel will install them automatically — and yes, it knows that TypeScript is a dev dependency. And since you have passed the `--open` flag, it even opens the browser window for you.
Added an [active](https://tailwindcss.com/docs/pseudo-class-variants#active) background color using `active:bg-blue-900`. (This technique with colon is called Pseudo-class Variants in Tailwind CSS; we use pseudo-class names in `className` rather than in CSS.) Nothing changed. It still shows the hover color on press! What could have gone wrong? It is actually documented right below where the example for `active:` is. We have to add `active` variant to the Tailwind config file `tailwind.config.js`.
By default, [some of the variants are disabled](https://tailwindcss.com/docs/pseudo-class-variants#default-variants-reference) due to file-size considerations. Even with these disabled, Tailwind is quite [large](https://tailwindcss.com/docs/controlling-file-size). 😕
134
-
Anyway, now I configured variants as the above and ran `yarn clean` to clean temp files and started again. We clearly configured tailwind! Or… did we? There is an order how we should organize variants, because, [Tailwind is no magic](https://tailwindcss.com/docs/configuring-variants/#ordering-variants); it generates CSS. CSS precedence applies here too.
135
-
I enabled all the variants in the correct order for all the styles with the below. We don’t have file size considerations for this test so that is fine.
Gosh! It’s huge. This is not the size you want your production CSS bundle to be. We are using a lot of unwanted styles. But how do we reduce the bundle? [Manually selecting](https://tailwindcss.com/docs/controlling-file-size#removing-unused-theme-values) what is necessary using the configuration file is hard. We have to try [something else](https://tailwindcss.com/docs/controlling-file-size#setting-up-purgecss).
167
-
168
-
> Mozilla's [Firefox Send](https://send.firefox.com/) is built with Tailwind, yet somehow their CSS is only 13.1kb minified, and only 4.7kb gzipped! How?
169
-
> They're using [Purgecss](https://www.purgecss.com/),…
170
-
171
-
~~Let’s add `purgecss`. Run `yarn add @fullhuman/postcss-purgecss -D`. Unfortunately, parcel currently fails to auto install postcss plugins just by looking at its config. I don’t think it’s in parcel’s scope.~~
172
-
173
-
**Update: As of `tailwindcss` version `1.4.5`, we don't need to configure purgecss this way. So do not install `purgecss` and keep your `postcss.config.js` intact. (The old way would also work, anyway.)**
79
+
Add `.postcssrc` file inside the project folder (i.e.: one level up from `src` folder). Tailwind CSS is a [PostCSS](https://postcss.org/) plugin where PostCSS handles all pre/post processing of CSS you write, such as adding [vendor prefixes](https://developer.mozilla.org/en-US/docs/Glossary/Vendor_Prefix)[automatically](https://github.com/postcss/autoprefixer). Parcel has built-in support for PostCSS, but doesn’t know yet about Tailwind, so we have to configure it with the below content. Make sure you include `tailwindcss` before `autoprefixer`.
**Update: But edit the `tailwind.config.css` to look like the below (note the _purge_):**
197
-
198
-
```js
199
-
module.exports= {
200
-
purge: ["./src/**/*.html", "./src/**/*.{j,t}sx"],
201
-
theme: {
202
-
// ...
203
-
},
204
-
variants: [
205
-
//...
206
-
],
207
-
};
208
-
```
92
+
Now it’s show-time. Run `yarn` to install dependencies and `yarn start` to start. Since you have specified `--open` in `yarn start`, you’ll see the browser open with the `index.html` file.
209
93
210
-
Now, let’s check the file sizes…
94
+
You should see a card with a title and a description.
It’s down to ~~two~~ (**update: a little larger than that**) kilobytes. That’s because we only have a few classes used.
215
-
So what exactly does purgecss do? It basically traverses through all our `.tsx` files and finds the class names we have used. Then it removes selectors that match all unused class names from CSS (check that regex!). Ugly, but works. Of course, you have to be careful when dynamically generating CSS class names in react.
216
-
At this moment, you should’ve realized that although Tailwind can make our lives easier, it also has its own drawbacks. Working with Tailwind CSS is NOT a no-brainer.
98
+
Find my blogpost here
99
+
https://umstek.tk/posts/trying-out-tailwindcss-with-parcel/ which includes the original content written for tailwind 1.x and then updated for tailwind 2.x.
217
100
218
101
# The good, the bad, and the ugly
219
102
@@ -227,81 +110,29 @@ I can notice several good things about Tailwind CSS at a glance.
227
110
228
111
There isn’t much to complain about the library but,
229
112
230
-
- It can get unintuitive sometimes to configure.
231
-
- Gradients, icons, animations ~~and transitions~~ (**update: transitions and transforms are there after v1.2.0**) aren’t built-in. Adding them can be complicated.
113
+
- Fonts, Icons, animations aren’t built-in. Adding them can be complicated.
232
114
- Advanced controls such as switches, calendars, tables, floating notifications, modals etc. are not available.
233
-
- Generated CSS can get quite large if you’re using a lot of features. Using purgecss eliminates this but it’s kind of ugly because it does a string search; not proper parsing. But, again, it doesn’t know what template language/framework we’re using.
234
-
- Lack of IDE support (yet). But there are vscode extensions, ~~but none for react~~ (**Update: bradlc.vscode-tailwindcss worked for react**).
235
115
236
-
# Demo
116
+
(I had more points here for the old versions but looks like now tailwind supports pretty much everything you'll need.)
237
117
238
-
~~The demo below uses Tailwind plugins `tailwindcss-transforms` and `tailwindcss-transitions` because tailwind doesn’t support transforms and transitions out-of-the-box.~~
0 commit comments