|
| 1 | +--- |
| 2 | +title: Use daisyUI with Stimulus |
| 3 | +desc: How to install and use daisyUI with Stimulus |
| 4 | +--- |
| 5 | + |
| 6 | +<script> |
| 7 | + import Translate from "$components/Translate.svelte" |
| 8 | +</script> |
| 9 | + |
| 10 | +> :INFO: |
| 11 | +> |
| 12 | +> Stimulus does not handle CSS, so daisyUI is installed the same way as in any other project. |
| 13 | +> If you use Rails, follow the [Rails install guide](/docs/install/rails/) instead, because Stimulus already comes with Rails. |
| 14 | +
|
| 15 | +### 1. Install |
| 16 | + |
| 17 | +Initialize a new Node project in the current directory using `npm init -y` if it's not a Node project already. |
| 18 | + |
| 19 | +Install Tailwind CSS CLI and daisyUI |
| 20 | + |
| 21 | +```sh:Terminal |
| 22 | +npm install tailwindcss@latest @tailwindcss/cli@latest daisyui@latest |
| 23 | +``` |
| 24 | + |
| 25 | +### 2. Add Tailwind CSS and daisyUI |
| 26 | + |
| 27 | +Add Tailwind CSS and daisyUI to your CSS file. |
| 28 | + |
| 29 | +```postcss:app.css |
| 30 | +@import "tailwindcss"; |
| 31 | +@plugin "daisyui"; |
| 32 | +``` |
| 33 | + |
| 34 | +### 3. Build CSS |
| 35 | + |
| 36 | +Add a script to your package.json to build the CSS. |
| 37 | + |
| 38 | +```json:package.json |
| 39 | +{ |
| 40 | + "scripts": { |
| 41 | + "build:css": "npx @tailwindcss/cli -i app.css -o public/output.css" |
| 42 | + }, |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +Run the script to build the CSS file |
| 47 | + |
| 48 | +```sh:Terminal |
| 49 | +npm run build:css |
| 50 | +``` |
| 51 | + |
| 52 | +This command creates a `public/output.css` file with the compiled CSS. You can link this file to your HTML file. |
| 53 | + |
| 54 | +```html:public/index.html |
| 55 | +<link href="./output.css" rel="stylesheet"> |
| 56 | +``` |
| 57 | + |
| 58 | +### 4. Add Stimulus |
| 59 | + |
| 60 | +Load Stimulus from a CDN and start the application. |
| 61 | + |
| 62 | +```html:public/index.html |
| 63 | +<script type="module"> |
| 64 | + import { Application } from "https://unpkg.com/@hotwired/stimulus/dist/stimulus.js" |
| 65 | + window.Stimulus = Application.start() |
| 66 | +</script> |
| 67 | +``` |
| 68 | + |
| 69 | +If you use a bundler, install `@hotwired/stimulus` with npm and import it from the package name instead. |
| 70 | + |
| 71 | +Now you can use daisyUI class names! |
| 72 | + |
| 73 | +```html:public/index.html |
| 74 | +<button class="btn btn-primary">Hello daisyUI!</button> |
| 75 | +``` |
0 commit comments