An escape hatch for DOM Elements in Vue.js components.

Overview

vue-dom-portal

npm vue2

An escape hatch for DOM Elements in Vue.js components.

The directive v-dom-portal will move DOM Nodes from their current place in a Vue component to a target DOM Node via appendChild. Similar to vue-transfer-dom, but updated for [email protected].

Setup

npm install vue-dom-portal --save
import DomPortal from 'vue-dom-portal'
Vue.use(DomPortal)

Usage

The target is the DOM Node that the element will be appened to, it defaults to document.body. Otherwise we can manually set this, or even pass a Boolean to dynamically move it around.

target can be any the following types:

  • string - this will be passed to document.querySelector to find the target
  • Node - A DOM Node or Element if you really want to specify it explitly..
  • Boolean - will either appened to document.body if true, or return to where it came from if false
  • undefined - will behave as true, appending to document.body
<!--will be appended to document.body-->
<div v-dom-portal></div>
<div v-dom-portal="true"></div>

<!--nothing will happen, left in-place-->
<div v-dom-portal="false"></div>

<!--will be appended to document.querySelector('#app') -->
<div v-dom-portal="'#app'"></div>

If you can't make up your mind on where you want the DOM Node to go, you can toss it around the page at will with a variable.

const vm = new Vue({
  template: `
    <div>
      <div v-dom-portal="target"></div>
    </div>
  `,
  data: {
    target: 'body'
  }
})

setTimeout(() => {
  vm.target = '#app'
}, 500)

setTimeout(() => {
  vm.target = false
}, 1000)

setTimeout(() => {
  vm.target = '#another-id'
}, 1500)

Since it's just a simple directive, it still works with transitions and directives.

const vm = new Vue({
  template: `
    <div>
      <transition name="fade">
        <div v-dom-portal="target" v-show="isShown" class="overlay">
          <img src="image.png" alt="Look, Ma, no z-index problems!">
        </div>
      </transition>
    </div>
  `,
  data: {
    target: 'body',
    isShown: true
  }
})

Use at your own risk! No tests have been written, but it seems to be working.

If you find any problems please write up an issue.

©️ License

MIT

You might also like...
Vue.js 2.x directive to set a default value for editable elements with no affect to the model state

vue-default-value A Vue.js Directive to set a default value for editable elements with no affect to the model state. Installation Via NPM Install the

Vue plugin to match the height of elements

vue-match-heights Quick and easy method of setting element heights to be the same. You can provide an array of selectors that will be calculated in se

Vue Visibility 👀 Hides or shows elements depending of a criteria

Vue plugin that hides or shows elements depending of a criteria. 👀

A Vue.js directive helps track visible elements.

vue-visibility-track A Vue.js directive helps track elements's visibility. Installing Using npm: npm install --save vue-visibility-track Using yarn: y

A component to render defined aspect ratio elements
A component to render defined aspect ratio elements

vue-aspect-ratio A component to render a certain aspect ratio element without javascript computations. It exposes a default slot that will have the de

🖱 An animated custom cursor effects for interactive elements like navigation - w/ VueJS - SSR Compatible
🖱 An animated custom cursor effects for interactive elements like navigation - w/ VueJS - SSR Compatible

🎉 Vue Cursor Fx An animated custom cursor effects for interactive elements like navigation - w/ VueJS - SSR Compatible Installation This package is a

💍 Pick out specific elements from the component slot
💍 Pick out specific elements from the component slot

💍 Pick out specific elements from the component slot

Directive to be able to attach events through other events on HTML elements in VueJS

vue-attach-event Directive to be able to attach events through other events on HTML elements in VueJS Install NPM: npm i --save vue-attach-event Requi

Composable components for rendering fake (progressive) content like facebook in vue
Composable components for rendering fake (progressive) content like facebook in vue

vue-content-placeholders Vue addon for rendering fake content while data is fetching to provide better UX and lower bounce rate. 💿 Installation via n

Comments
  • moved element not destroyed when original parent gets destroyed

    moved element not destroyed when original parent gets destroyed

    I am trying to use this for a special overlay element, but once the element is moved to the body it is not being removed when the parent component is destroyed. The beforeDestroy() method is being called, but the element stays in the body rather than been removed as I would expect it.

    opened by theDevelopper 0
  • vue 2.3.x, v-if do not append element before element moved away

    vue 2.3.x, v-if do not append element before element moved away

    <template>
        <div>
            <a @click="showElement = true">Show Element</a>
            <div v-if="showElement" class=“element”>element</div>
            <div v-dom-portal></div>
        </div>
    </template>
    <script>
        export default {
            data: () => ({ showElement: false })
        }
    </script>
    

    v-if do not append element before element which had been moved away, Tested in vue 2.2.6 represent correct, but not in vue 2.3.0, even 2.3.2

    opened by harvey-woo 3
Releases(0.1.4)
Owner
Caleb Roseland
Web App Developer
Caleb Roseland
A Vue.js 2 component to clone DOM elements

vue-cloneya A vue component to clone DOM elements Live demo here Install yarn add vue-cloneya # or npm i vue-cloneya Import import Vue from 'vue'

Rida Amirini 15 Aug 27, 2022
BEM is a great way to namespace DOM elements

Vue BEM Plugin BEM is a great way to namespace DOM elements. If you're like me, however, you can't stand manually writing __ and -- over and over agai

Mike Schutte 0 Apr 6, 2019
A directive-supported plugin for transfering DOM to another location in Vue.js components

A directive-supported plugin for transfering DOM to another location in Vue.js components

Emil Zhai 1 Jul 13, 2019
A vue directive to keep dom ratio

vue-keep-ratio [中文项目介绍] As there comes a new css attribute: aspect-ratio, I picked up my old work vue-keep-ratio, use a more adaptive (but hack) way t

Aiello 6 Sep 13, 2021
Vue component to trap the focus within a DOM element

focus-trap-vue Vue component to trap the focus within a DOM element Installation For Vue 2 npm install focus-trap focus-trap-vue For Vue 3 npm install

Eduardo San Martin Morote 169 Jan 1, 2023
Enable tap, swipe, touch, hold, mouse down, mouse up events on any HTML DOM Element in vue.js 3.x.

vue3-touch-events Enable tap, swipe, touch, hold, mouse down, mouse up events on any HTML DOM Element in vue.js 3.x. The easiest way to make your inte

Robin Rodricks 108 Dec 27, 2022
Detect DOM element resizing based on Vue3.x

Detect DOM element resizing based on Vue3.x

null 0 Jul 22, 2021
Convert HTML DOM Element to PDF

Convert HTML DOM Element to PDF. You can save a specified element as PDF File, print it directly, or preview as PDF.

Emily Wang 0 Jul 29, 2019
MKTemplate is Free and Open Source. It features multiple HTML and VueJS elements and it comes with dynamic components for VueJS

MKTemplate A beautiful UI . Start your Web Site with this bootstrap and VueJS UI . MKTemplate is Free and Open Source. It features multiple HTML and V

Moutawakkil Mohammed 10 May 31, 2022
A Vue directive that parses child elements for internally linking anchor tags and binds their click events to use Vue Router's push().

vue-routing-anchor-parser A Vue directive that parses child elements for internally linking anchor tags and binds their click events to use Vue Router

Bukwild 3 Nov 23, 2021