|
| 1 | +# TailwindcCSS |
| 2 | + |
| 3 | +In this example, we are going add TailwindCSS integration. |
| 4 | + |
| 5 | +📌 We start from sample `08-env-vars`. |
| 6 | + |
| 7 | +# Steps to build it |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +Install [Node.js and npm](https://nodejs.org/en/) (20.19.0 || >=22.12.0) if they are not already installed on your computer. |
| 12 | + |
| 13 | +> ⚠ Verify that you are running at least latest Node LTS version and npm. You can check your current version by running `node -v` and `npm -v` in a terminal/console window. Older versions may produce errors. |
| 14 | +
|
| 15 | +## Steps |
| 16 | + |
| 17 | +- We start from `08-env-vars`. Just copy the project and install: |
| 18 | + |
| 19 | + ```bash |
| 20 | + npm install |
| 21 | + ``` |
| 22 | + |
| 23 | +- Let's add a TailwindCSS and TailwindCSS's Vite plugin: |
| 24 | + |
| 25 | + _.env_ |
| 26 | + |
| 27 | + ```bash |
| 28 | + npm install tailwindcss @tailwindcss/vite --save-dev |
| 29 | + ``` |
| 30 | + |
| 31 | +- Let's modify `vite.config.ts` to include the plugin: |
| 32 | + |
| 33 | + _vite.config.ts_ |
| 34 | + |
| 35 | + ```diff |
| 36 | + import { defineConfig } from "vite"; |
| 37 | + import checker from "vite-plugin-checker"; |
| 38 | + import react from "@vitejs/plugin-react"; |
| 39 | + + import tailwindcss from "@tailwindcss/vite"; |
| 40 | + |
| 41 | + export default defineConfig({ |
| 42 | + - plugins: [checker({ typescript: true }), react()], |
| 43 | + + plugins: [checker({ typescript: true }), tailwindcss(), react()], |
| 44 | + }); |
| 45 | + ``` |
| 46 | + |
| 47 | +- Create `src/styless.css` that will contain TailwindCSS entrypoint: |
| 48 | + |
| 49 | + _src/styless.css_ |
| 50 | + |
| 51 | + ```css |
| 52 | + @import "tailwindcss"; |
| 53 | + ``` |
| 54 | + |
| 55 | +- And import it in our `index.tsx`; |
| 56 | + |
| 57 | + _index.html_ |
| 58 | + |
| 59 | + ```diff |
| 60 | + import { createRoot } from "react-dom/client"; |
| 61 | + import { HelloComponent } from "./hello"; |
| 62 | + + import "./styles.css"; |
| 63 | + |
| 64 | + const root = createRoot(document.getElementById("root")); |
| 65 | + ``` |
| 66 | + |
| 67 | +- Now let's update `HelloComponent` to include new code with TailwindCSS classes: |
| 68 | + |
| 69 | + _src/hello.tsx_ |
| 70 | + |
| 71 | + ```diff |
| 72 | + <p>Feature A is {config.IS_FEATURE_A_ENABLED ? "enabled" : "disabled"}</p> |
| 73 | + <p>Counter state: {counter}</p> |
| 74 | + + <a |
| 75 | + + href="#" |
| 76 | + + className="m-2 block max-w-sm p-6 bg-white border border-gray-200 rounded-lg shadow-sm hover:bg-gray-100" |
| 77 | + + > |
| 78 | + + <h5 className="mb-2 text-2xl font-bold tracking-tight text-gray-900"> |
| 79 | + + Card title |
| 80 | + + </h5> |
| 81 | + + <p className="font-normal text-gray-700"> |
| 82 | + + Some quick example text to build on the card title and make up the |
| 83 | + + bulk of the card's content. |
| 84 | + + </p> |
| 85 | + + </a> |
| 86 | + ``` |
| 87 | + |
| 88 | +- Let's run the project: |
| 89 | + |
| 90 | + ```bash |
| 91 | + npm start |
| 92 | + ``` |
| 93 | + |
| 94 | + 🔎 Navigate to [http://localhost:5173](http://localhost:5173) and you'll see the card with style. |
| 95 | + |
| 96 | +- Let's do one more update. We'll add a dark mode. Modify `src/styles.css` to add new variant: |
| 97 | + |
| 98 | + _src/styles.css_ |
| 99 | + |
| 100 | + ```diff |
| 101 | + @import "tailwindcss"; |
| 102 | + + @custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *)); |
| 103 | + ``` |
| 104 | + |
| 105 | +- Update our `HelloComponent` to include styles for our new dark theme: |
| 106 | + |
| 107 | + \__src/hello.tsx_ |
| 108 | + |
| 109 | + ```diff |
| 110 | + - <a |
| 111 | + - href="#" |
| 112 | + - className="m-2 block max-w-sm p-6 bg-white border border-gray-200 rounded-lg shadow-sm hover:bg-gray-100" |
| 113 | + - > |
| 114 | + - <h5 className="mb-2 text-2xl font-bold tracking-tight text-gray-900"> |
| 115 | + - Card title |
| 116 | + - </h5> |
| 117 | + - <p className="font-normal text-gray-700 dark:text-gray-600"> |
| 118 | + - Some quick example text to build on the card title and make up the |
| 119 | + - bulk of the card's content. |
| 120 | + - </p> |
| 121 | + - </a> |
| 122 | + + <a |
| 123 | + + href="#" |
| 124 | + + className="m-2 block max-w-sm p-6 bg-white border border-gray-200 rounded-lg shadow-sm hover:bg-gray-100 dark:bg-gray-800 dark:border-gray-700 dark:hover:bg-gray-700" |
| 125 | + + > |
| 126 | + + <h5 className="mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white"> |
| 127 | + + Card title |
| 128 | + + </h5> |
| 129 | + + <p className="font-normal text-gray-700 dark:text-gray-400"> |
| 130 | + + Some quick example text to build on the card title and make up the |
| 131 | + + bulk of the card's content. |
| 132 | + + </p> |
| 133 | + + </a> |
| 134 | + ``` |
| 135 | + |
| 136 | +- Finally to see our style in action update `index.html`: |
| 137 | + |
| 138 | + _index.html_ |
| 139 | + |
| 140 | + ```diff |
| 141 | + <!DOCTYPE html> |
| 142 | + - <html lang="en"> |
| 143 | + + <html lang="en" data-theme="dark"> |
| 144 | + <head> |
| 145 | + ``` |
0 commit comments