Skip to content
GitHub

Vue custom elements

Tailwind, scoped styles and nested components inside the shadow DOM.

Vue's own defineCustomElement leaves out Tailwind. The plugin wraps it with a thin subclass that adopts Tailwind and a few base styles into the shadow root. Everything else (props, events, slots, provide/inject, useHost(), HMR) is Vue's own implementation.

Utility classes, scoped styles and nested components all work inside the shadow root.
Live preview · Tailwind inside a Vue custom element Open in new tab ↗
templates/demos/vue/tailwind-card.idle.ce.vue
<docs lang="ts">
/**
 * Tailwind utilities inside the shadow DOM of a Vue custom element.
 *
 * @element demo-tailwind-card
 *
 * @attr {string} heading - Card heading.
 * @slot - Card body.
 */
</docs>

<script setup lang="ts">
defineProps<{
  heading?: string
}>()

const count = ref(0)
</script>

<template>
  <div class="rounded-xl bg-indigo-600 p-6 text-white shadow-lg">
    <div class="flex items-center justify-between gap-4">
      <h2 class="m-0 text-xl font-bold">
        {{ heading ?? 'Tailwind in shadow DOM' }}
      </h2>
      <!-- Local "*.ce.vue" components are auto-imported, no import statement. -->
      <DemoBadge label="nested component" />
    </div>
    <p class="mt-3 text-indigo-100">
      <slot />
    </p>
    <button class="mt-4 cursor-pointer rounded-md bg-white px-4 py-2 font-semibold text-indigo-700 hover:bg-indigo-50" @click="count++">
      Clicked {{ count }} times
    </button>
  </div>
</template>

Styles in the shadow root

Each Vue custom element adopts three stylesheets into its shadow root:

  1. Base styles: the layer order, :host { display: block }, box-sizing: border-box and [hidden].
  2. Tailwind: the rules of the page's Tailwind stylesheet, the <link data-uebertool-tailwind> from the generated library. It is parsed once and shared by all elements as a constructed stylesheet. If the stylesheet has not loaded yet, it is filled in on its load event.
  3. Component styles: the <style> blocks of the element, inserted by Vue.

Nested components

*.ce.vue files without a loading strategy are not registered as elements. They are regular components that can be used by name, without an import (here <DemoBadge>). Vue injects a nested *.ce.vue component's <style> into the shadow root of the element it renders in. The plugin also injects a useStyles() call into every *.ce.vue file. It only does something when such a component renders in a shadow root outside a Vue custom element.

templates/demos/vue/DemoBadge.ce.vue
<script setup lang="ts">
defineProps<{
  label: string
}>()
</script>

<template>
  <span class="badge">{{ label }}</span>
</template>

<style>
/* Styles of nested components are adopted into the parent's shadow root. */
.badge {
  border-radius: 9999px;
  background: #fde68a;
  color: #78350f;
  font-size: 0.75rem;
  padding: 0.125rem 0.5rem;
}
</style>

Tailwind HMR

In dev mode a useHMR() call is injected as well: when the element updates, it re-reads the Tailwind stylesheet into the shared sheet, which updates all elements at once. Newly used utility classes show up inside the shadow DOM without a reload.

Good to know

  • Any tag containing a dash is treated as a custom element by the Vue template compiler.
  • The Options API is disabled in builds. Use <script setup>.
  • A <docs> block is removed from the bundle and only used for editor support.