vue-shadow-dom is a JavaScript library providing robust Shadow DOM support for Vue 3 applications, currently stable at version 4.2.0. It allows developers to leverage the browser's native Shadow DOM capabilities directly within Vue components, enabling strict encapsulation of styles and markup. The library achieves this through the provision of a Vue plugin, a `v-shadow` directive, and dedicated `<shadow-root>` and `<shadow-style>` components. While there isn't a stated fixed release cadence, updates tend to align with major Vue ecosystem changes or feature enhancements. A key differentiator is its direct, declarative API for Shadow DOM usage, avoiding complex manual DOM manipulations, and its comprehensive support for various module environments including ESM, CommonJS, and UMD builds, catering to both bundler-driven and browser-only setups. Notably, versions 2.0 and above are specifically designed for Vue 3, while v1.x targets Vue 2.
npm install vue-shadow-domVerified import paths — ran on the pinned version, not inferred.
Demonstrates initializing the `vue-shadow-dom` plugin, using the `<shadow-root>` component with custom tag and abstract prop, applying encapsulated styles with `<shadow-style>`, and accessing the native ShadowRoot instance via a ref for direct DOM interaction.
For Vue 2 projects, you must install `vue-shadow-dom@1` using `npm i vue-shadow-dom@1`. Always verify your project's Vue version aligns with the compatible library version.
Carefully consider the implications of `abstract`. If you need sibling content to render alongside the shadow root or to be styled by global styles, avoid using `abstract`. The default behavior without `abstract` creates an intermediate `div` for the shadow root, allowing siblings to coexist.
Consult the 'Files' section in the `vue-shadow-dom` README. Ensure you are importing the correct build artifact for your specific environment: `shadow.esm-bundler.mjs` for packaging tools, `shadow.esm-browser.mjs` for browser ESM, `shadow.global.js` for UMD, and `shadow.cjs.cjs` or the main `vue-shadow-dom` package for Node.js CommonJS.
After creating your Vue application with `createApp()`, ensure you register the plugin by calling `app.use(shadow)` before mounting the application, e.g., `const app = createApp(MyRootComponent); app.use(shadow); app.mount('#app');`Confirm that your application entry point uses `createApp()` to initialize your Vue 3 application. If you are targeting Vue 2, you must use `vue-shadow-dom@1` and its corresponding initialization method for Vue 2 plugins.