Prerequisites
Before starting the project make sure to install the following utilities:
Creating a new Nuxt application
Let's create a fresh Nuxt application so that we can go through all the steps together.
Step 1
Create new project.
npx nuxi init my-project
cd my-project
Step 2
Run the development server.
npm run dev
Installing and configuring Tailwind CSS and TW Elements
Step 1
Install Tailwind CSS
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init
File structure
If you have followed all instructions mentioned earlier, your file structure should look like this:
my-project/
├── .nuxt/
├── assets/ - needs to be created
│ └── css/
│ └── main.css
├── node_modules/
├── public/
├── ...
├── app.vue
├── nuxt.config.ts
├── package-lock.json
├── package.json
├── README.md
├── tailwind.config.js
└── tsconfig.json
Step 2
Include tailwindcss
and autoprefixer
in the
postcss.plugins
object within your
nuxt.config.ts
file. Additionally, make sure to attach the
css
object, specifying the localization of the main
.css
file.
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
devtools: { enabled: true },
css: ["~/assets/css/main.css"],
postcss: {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
},
});
Step 3
Add the paths to all of your template files in your
tailwind.config.js
file.
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./components/**/*.{js,vue,ts}",
"./layouts/**/*.vue",
"./pages/**/*.vue",
"./plugins/**/*.{js,ts}",
"./nuxt.config.{js,ts}",
"./app.vue",
"./node_modules/tw-elements/js/**/*.js",
],
theme: {
extend: {},
},
darkMode: "class",
plugins: [require("tw-elements/plugin.cjs")],
};
Step 4
Add the @tailwind
directives for each of Tailwind’s layers to
your ./assets/css/main.css
file.
@import url("https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,900&display=swap");
@tailwind base;
@layer base {
html {
@apply text-surface;
@apply bg-white;
}
html.dark {
@apply text-neutral-50;
@apply bg-body-dark;
}
}
@tailwind components;
@tailwind utilities;
p {
@apply leading-[1.6];
}
Step 5
Install TW Elements.
npm install tw-elements
Step 6
In the root directory of your project, create a
components
folder. Inside this folder, create a new file named
Tooltip.vue
. Import the TWE components you intend to use and
include the necessary function initTWE
. Initialize
initTWE
within a lifecycle method.
<template>
<div className="mt-16 flex justify-center">
<p className="text-lg">
Hover the link to see the
<a
href="#"
className="text-primary transition duration-150 ease-in-out hover:text-primary-600 focus:text-primary-600 active:text-primary-700 dark:text-primary-400 dark:hover:text-primary-500 dark:focus:text-primary-500 dark:active:text-primary-600"
data-twe-toggle="tooltip"
title="Hi! I'm tooltip">
tooltip
</a>
</p>
</div>
</template>
<script setup>
import { onMounted } from "vue";
import { Tooltip, initTWE } from "tw-elements";
onMounted(() => {
initTWE({ Tooltip });
});
</script>
Step 7
Import the newly created component, for instance in app.vue
,
and wrap it within a ClientOnly
element.
<template>
<ClientOnly>
<Tooltip />
</ClientOnly>
</template>
<script setup>
import Tooltip from "./components/Tooltip.vue";
</script>
Step 8
Start the app and see if everything's fine. Awesome! You're all set to dive into using TW Elements for your Nuxt project. Have fun!
npm run dev
Initializing via JS
By default all components have autoinit which means they are initialized by data attributes. But if you want to make init by JavaScript - there is also possibility to do that.
Step 1
Import components which are you intend to use and initialize them in lifecycle method.
<template>
<div className="mt-16 flex justify-center">
<p className="text-lg">
Hover the link to see the
<a
id="my-tooltip"
href="#"
className="text-primary transition duration-150 ease-in-out hover:text-primary-600 focus:text-primary-600 active:text-primary-700 dark:text-primary-400 dark:hover:text-primary-500 dark:focus:text-primary-500 dark:active:text-primary-600"
title="Hi! I'm tooltip">
tooltip
</a>
</p>
</div>
</template>
<script setup>
import { onMounted } from "vue";
import { Tooltip } from "tw-elements";
onMounted(() => {
const myTooltip = new Tooltip(document.getElementById("my-tooltip"));
});
</script>