Building Vue 3 custom elements with Tailwind shadow DOM style injection
This approach imports Tailwind CSS as a compiled string and assigns it to the styles array of each defineCustomElement component, ensuring Tailwind styles are included inside each shadow root for framework-agnostic usage (e.g., inside React). The example shows Vite and PostCSS config, Tailwind setup, a sample button custom element using styles, and how to register elements for library build. React integration simply imports the built script and uses the custom elements with styles encapsulated in shadow DOM.
Below is a minimal, working example of how to:
- Author Vue 3 components.
- Export them as native Custom Elements via
defineCustomElement.
- Bundle Tailwind and inject its styles into each component’s shadow root at runtime (so they carry their own styles when dropped into a React app or any other host).
The key parts are:
- Use the
styles option on the custom-element component to inject the compiled Tailwind CSS (imported with ?inline).
- Build in library mode with Vite and Vue 3.
- Register the resulting custom elements in
main.ce.ts.
You can expand this pattern to any number of components.
Project structure
your-lib/
├─ package.json
├─ vite.config.ts
├─ postcss.config.cjs
├─ tailwind.config.cjs
├─ src/
│ ├─ index.css # Tailwind entry
│ ├─ main.ce.ts # Registers Custom Elements
│ └─ components/
│ └─ MyButton.ce.vue # Example CE using Tailwind
└─ ...
package.json
{
"name": "vue-tailwind-custom-elements",
"version": "1.0.0",
"scripts": {
"dev": "vite",
"build": "vite build --config vite.config.ts",
"preview": "vite preview"
},
"dependencies": {
"vue": "^3.3.4"
},
"devDependencies": {
"@vitejs/plugin-vue": "^4.2.3",
"autoprefixer": "^10.4.14",
"postcss": "^8.4.23",
"tailwindcss": "^3.3.2",
"typescript": "^5.1.3",
"vite": "^4.3.9"
}
}
tailwind.config.cjs
module.exports = {
content: [
"./index.html",
"./src/**/*.{vue,ts,js}"
],
theme: {
extend: {},
},
plugins: [],
};
postcss.config.cjs
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {}
}
};
src/index.css
This is the Tailwind entry; Vite will process it and, with ?inline, we can import the compiled CSS as a string to inject into each shadow root.
@tailwind base;
@tailwind components;
@tailwind utilities;
/* Add any global Tailwind-based utilities/components here */
vite.config.ts
We configure Vite to build a library of custom elements. Note build.lib.entry points to src/main.ce.ts. We also import the Vue plugin.
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import { resolve } from 'path';
export default defineConfig({
plugins: [vue()],
build: {
lib: {
entry: resolve(__dirname, 'src/main.ce.ts'),
name: 'MyVueCustomElements',
fileName: (format) => `my-vue-custom-elements.${format}.js`,
formats: ['es'] // modern builds for React/modern browsers
},
// bundle CSS together; you can keep cssCodeSplit: true if you prefer
cssCodeSplit: false,
target: 'esnext'
}
});
src/components/MyButton.ce.vue
Here’s a simple custom element using Tailwind classes. The important part is styles: [tailwindStyles], which injects the compiled Tailwind CSS into the component’s shadow root.
<template>
<button
class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white font-semibold rounded transition"
@click="$emit('clicked')"
>
<slot>Click me</slot>
</button>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
// Import the processed Tailwind as a string
import tailwindStyles from '../index.css?inline';
export default defineComponent({
name: 'MyButton',
// This injects Tailwind into the shadow root of the custom element
styles: [tailwindStyles],
});
</script>
Note: styles: [tailwindStyles] is a Vue Custom Element feature. Each Custom Element will get its own <style> inside its shadow root with the content of tailwindStyles.
src/main.ce.ts
Register the Custom Elements. You can expose multiple components here.
import { defineCustomElement } from 'vue';
import MyButton from './components/MyButton.ce.vue';
// Define custom elements
const MyButtonElement = defineCustomElement(MyButton);
customElements.define('my-button', MyButtonElement);
// If you have more elements, define them similarly:
// customElements.define('my-card', defineCustomElement(MyCard));
Build
npm install
npm run build
This will produce dist/my-vue-custom-elements.es.js. You can publish this file or copy it into your React app.
Using in a React app
- Copy or install the built library (e.g., via npm/yarn or a local file).
- Import it once in your React entry (so the custom elements get defined):
// src/index.tsx or App.tsx
import 'my-vue-custom-elements/dist/my-vue-custom-elements.es.js';
export default function App() {
return (
<div>
<h1>React + Vue Custom Element</h1>
{/* Use the custom element like any other DOM tag */}
<my-button onClicked={() => alert('Clicked from CE!')}>
Press me
</my-button>
</div>
);
}
- No global Tailwind stylesheet is needed in React for the CE to look correct; the Tailwind rules are injected into the shadow root of each
<my-button>.
How the Tailwind injection works
src/index.css contains Tailwind directives. Vite + PostCSS compiles them to real CSS.
- Importing
../index.css?inline returns that compiled CSS as a string at build time.
- Setting
styles: [tailwindStyles] on the SFC (used with defineCustomElement) tells Vue to inject that CSS string into the shadow root of each instance of the custom element.
- The component thus carries its own Tailwind styling when used anywhere (React, plain HTML, etc.).
If you want to reduce bundle size and share Tailwind across many CEs, you can alternatively:
- Load a single global Tailwind
<style> or <link> in the host page.
- At runtime, clone that stylesheet text into each shadow root (e.g., via a helper function in
mounted()).
But the above inline approach is the simplest and most robust for standalone embedding.