Date posted: 2022-08-21
Category: Web Development
Tags: Vue,Vite,DaisyUI,tailwindcss,JavaScript
I've decided to document how to set-up and use DaisyUI, with Vue and Tailwind, having used it recently in a client project. I added DaisyUI to ensure there was some consistency in the application I was building. It was initially being developed by myself but then it quickly grew to being developed by developers in multiple teams. Features and components were built without any wire-frames or base styleguides to follow. Some groundwork had to be introduced before the UI became too fragmented.
We'll be using Vite as our development server. It's now the standard, over Webpack, for developing JS applications. I highly recommend moving your existing web app builds over to Vite. The speed increase is worth the migration process.
Pop open your terminal of choice and run the following commands:
npm init vite projectName
, where projectName
will be the name of a new folder for your application
vue
using the arrow keys and press enter to selectvue
againcd
into your new project directory
npm install
to install the base dependenciesnpm run dev
if you want to see the base Vue app. You can also experience how fast Vite is at getting your dev server runningnpm install -D tailwindcss postcss autoprefixer
to get tailwind, postcss and autoprefixer installed as Dev dependencies
npx tailwindcss init -p
to generate config files for Tailwind and PostCsstailwind.config.js
we need to update content: []
with content: ["./index.html", "./src/**/*.{vue,js,ts,jsx,tsx}",]
to tell Tailwind to look at our index.html
and our Vue.js
files where we'll be using Tailwind classes./src/
we need to create a new CSS file. It can be called anything you like. I call it index.css
. Within this file we need to add the following to import Tailwind's directives for each of its layers:
@tailwind base;
@tailwind components;
@tailwind utilities;
./src/main.js
add the following line import './index.css'
, under import App from './App.vue'
to import the new stylesheet. You should have:
import { createApp } from 'vue'
import App from './App.vue'
import './index.css'
createApp(App).mount('#app')
./src/App.vue
and add the below with Tailwind's classes:
<template>
<h1 class="text-3xl font-bold underline mb-10">Hello Vue!</h1>
</template>
npm run dev
and go to the URL Vite has provided, e.g: http://localhost:3000
Hello vue!
in large bold text with an underline:npm i daisyui
tailwind.config.js
file we need to add require("daisyui")
within the plugins
array. You should have the following in your tailwind.config.js
file:/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./index.html",
"./src/**/*.{vue,js,ts,jsx,tsx}",
],
theme: {
extend: {},
},
plugins: [require("daisyui")],
}
./src/App.vue
file we can start adding some DaisyUI classes, e.g:
<template>
<h1 class="text-3xl font-bold underline mb-10">Hello Vue!</h1>
<button class="btn btn-primary">Hello Daisy button</button>
</template>
npm run dev
. You should now see an updated page with a theme and styled button:
I feel like the following steps are important to create a template for any new applications you feel like building:
./src/components/
./src/assets/
That's it, You can now commit this to a repo and use it when you're starting new projects. I've created my version here for you to clone if you'd like.