Installation
A project needs two pieces: the build plugin, which turns each block into
classes, and the editor plugin, which tells the editor and vue-tsc
what classes holds. In Nuxt, the module sets up both.
Requirements
- Vue 3.3 or newer
- Nuxt 3.10 or newer, or Vite 5 or newer
- Tailwind CSS v3 or v4
- Vue - Official VS Code extension and
vue-tsc3.2 or newer, for editor types - ESLint 9 or 10 with flat config, for linting
Install
Add the package
pnpm add -D vue-tailwind-block
npm install -D vue-tailwind-block
yarn add -D vue-tailwind-block
bun add -D vue-tailwind-block
Register the module
export default defineNuxtConfig({
modules: ['vue-tailwind-block/nuxt'],
})
The module automatically configures the Vite build plugin and injects the
editor types into the tsconfig Nuxt generates.
Run npx nuxi prepare (or start the dev server), then reload your editor
window (Cmd/Ctrl + Shift + P → Developer: Reload Window) so the
language server loads the plugin.
Add the package
pnpm add -D vue-tailwind-block
npm install -D vue-tailwind-block
yarn add -D vue-tailwind-block
bun add -D vue-tailwind-block
Register the plugin
Register tailwindBlock() before vue(). Vite runs plugins in order;
the Vue plugin does not recognize <tailwind> blocks and must receive the
file after it has been transformed.
import tailwindcss from '@tailwindcss/vite'
import vue from '@vitejs/plugin-vue'
import { defineConfig } from 'vite'
import tailwindBlock from 'vue-tailwind-block/vite'
export default defineConfig({
// tailwindBlock() must come before vue()
plugins: [tailwindBlock(), vue(), tailwindcss()],
})
@tailwindcss/vite is used with Tailwind CSS v4. If your project uses
Tailwind CSS v3, omit @tailwindcss/vite and configure PostCSS via
postcss.config.js as usual.Add the editor types
Add the editor plugin to the tsconfig that includes your .vue files
(tsconfig.app.json in a create-vue project, or tsconfig.json):
{
"vueCompilerOptions": {
"plugins": [{ "name": "vue-tailwind-block/volar" }]
}
}
Restart the language server (Cmd/Ctrl + Shift + P → Vue: Restart Vue Language Server
or Developer: Reload Window).
Tailwind CSS IntelliSense
The Tailwind CSS IntelliSense
extension only inspects standard class attributes by default. Add this
setting to .vscode/settings.json so it scans <tailwind> blocks for
autocomplete, color swatches, and hovers:
{
"tailwindCSS.experimental.classRegex": [
["<tailwind[^>]*>([\\s\\S]*?)</tailwind>", "(?:^|\\n)[ \\t]*(?:-[ \\t]+|[^\\s:#\"'][^:\\n]*:[ \\t]+)[\"']?([^\\n#\"']+)"]
]
}
Renaming the variable (binding)
By default, the block compiles to a variable called classes. If a prop or
another identifier already uses that name, customize it with binding:
export default defineNuxtConfig({
modules: ['vue-tailwind-block/nuxt'],
tailwindBlock: {
binding: 'styles',
},
})
export default defineConfig({
plugins: [tailwindBlock({ binding: 'styles' }), vue()],
})
{
"vueCompilerOptions": {
"plugins": [{ "name": "vue-tailwind-block/volar", "binding": "styles" }]
}
}
binding is configured in both
vite.config.ts and your tsconfig. In Nuxt, setting it in nuxt.config.ts
configures both automatically.Verify
Add a block to any component and use classes in its template:
<template>
<div :class="classes.root">
<slot />
</div>
</template>
<tailwind lang="yaml">
root: rounded-lg border p-6 shadow-sm
</tailwind>
If the card renders with styling, the build plugin is active. If your editor
underlines classes as undefined:
- Verify the component uses
<script setup lang="ts">(in plain JavaScript,classesis typed asany). - Verify that your
tsconfigincludes the.vuefile and contains thevueCompilerOptions.pluginsentry. - In Nuxt, run
npx nuxi prepare. - Restart the language server (Vue: Restart Vue Language Server).