Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: enhance style injection logic to support custom root elements#5027

Merged
zyyv merged 1 commit into
unocss:mainfrom
octavio1243:feat/inject-styles-to-root-element
Dec 29, 2025
Merged

feat: enhance style injection logic to support custom root elements#5027
zyyv merged 1 commit into
unocss:mainfrom
octavio1243:feat/inject-styles-to-root-element

Conversation

@octavio1243

@octavio1243 octavio1243 commented Dec 29, 2025

Copy link
Copy Markdown
Contributor

Problem
Currently, when using rootElement option in the runtime, styles are still injected into the element instead of the specified root element. This makes it impossible to use the runtime with Shadow DOM or other encapsulated contexts, as the generated styles are not accessible inside the shadow tree.

Solution
This PR modifies the inject function to check if a rootElement is specified. When present, styles are injected directly into that element instead of the document's .

Example: Using with Vue 3 and Shadow DOM

<script setup lang="ts">
import presetWind3 from '@unocss/preset-wind3'
import initUnocssRuntime from '@unocss/runtime'
import ShadowContent from './components/ShadowContent.vue'

const shadowHost = ref<HTMLElement>()

onMounted(() => {
  const shadow = shadowHost.value!.attachShadow({ mode: 'open' })
  const appContainer = document.createElement('div')
  shadow.appendChild(appContainer)
  createApp(ShadowContent).mount(appContainer)

  initUnocssRuntime({
    defaults: { presets: [presetWind3()] },
    rootElement: () => appContainer, // Styles will be injected here
  })
})
</script>

<template>
  <div id="app" class="font-sans">
    <div ref="shadowHost" />
    <p class="text-red-500 text-2xl font-bold">
      Styles should NOT apply here (outside shadow)
    </p>
  </div>
</template>

ShadowContent.vue

<template>
  <p class="text-red-500 text-2xl font-bold">
    Styles WILL apply here (inside shadow)
  </p>
</template>

Benefits

  • Enables proper style encapsulation with Shadow DOM
  • No breaking changes: falls back to default behavior when rootElement is not specified
  • Allows using UnoCSS runtime in micro-frontend architectures or Web Components

Screenshot
image

Conclusion
This PR solve this problem:
#4961

@netlify

netlify Bot commented Dec 29, 2025

Copy link
Copy Markdown

Deploy Preview for unocss ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 08e8a89
🔍 Latest deploy log https://app.netlify.com/projects/unocss/deploys/6951e5b546d2930008ffb34e
😎 Deploy Preview https://deploy-preview-5027--unocss.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@zyyv zyyv enabled auto-merge December 29, 2025 06:42
@pkg-pr-new

pkg-pr-new Bot commented Dec 29, 2025

Copy link
Copy Markdown

Open in StackBlitz

commit: 08e8a89

@zyyv zyyv added this pull request to the merge queue Dec 29, 2025
Merged via the queue into unocss:main with commit 70f1bc6 Dec 29, 2025
11 checks passed
This was referenced Feb 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants