PortalVue for Vue 3

3.0.0 · active · verified Sun Apr 19

PortalVue is a Vue 3 component library that enables developers to render DOM elements outside of their natural component hierarchy, placing content anywhere in the document. The current stable version is 3.0.0, which targets Vue 3 and ships with full TypeScript types. While the project had a prolonged beta period, the 3.x line is now considered stable and actively maintained, receiving dependency updates and bug fixes as needed. Its primary differentiator is providing a robust, battle-tested solution for portal functionality directly within the Vue ecosystem, often used for modals, tooltips, and other overlay elements, without relying on imperative DOM manipulation. Vue 3 also offers its native `<Teleport>` component, which addresses many typical portal use cases, but PortalVue remains relevant for scenarios involving content movement between application components rather than simply moving to a specific DOM target.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a basic Vue 3 application using PortalVue to 'teleport' reactive content from one part of the component tree to a `PortalTarget` located elsewhere in the DOM.

import { createApp } from 'vue';
import PortalVue from 'portal-vue';

const App = {
  template: `
    <div>
      <h1>PortalVue Example</h1>
      <button @click="count++">Increment Count: {{ count }}</button>
      <Portal to="destination">
        <p>This slot content, including reactive data, will be rendered wherever the <PortalTarget> with name 'destination' is located.</p>
        <p>Current Portal Count: {{ count }}</p>
      </Portal>

      <div style="border: 2px dashed #007bff; padding: 20px; margin-top: 30px; background-color: #e6f7ff;">
        <h2>Outside the Component Tree (Portal Target)</h2>
        <PortalTarget name="destination">
          <!-- Content from the Portal above will appear here -->
        </PortalTarget>
      </div>
    </div>
  `,
  data() {
    return {
      count: 0
    };
  }
};

const app = createApp(App);
app.use(PortalVue); // Globally registers <Portal> and <PortalTarget> components
app.mount('#app');

view raw JSON →