Render children into a DOM node that exists outside the DOM hierarchy of the parent component

Related tags

Miscellaneous npm vue
Overview

Portals

Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.

Example

import Portal from '../src/Portal';
import { cloneVNode } from './VueHelper';
 
export default {
  name: 'PortalExample',
  props: {
    showPortal: {
      type: Boolean,
      default: false
    }
  },
 
  methods: {
    renderPortal(h) {
      if(!this.showPortal) {
        return null;
      }
      
      return cloneVNode(this.$slots.default, h);
    }
  },
  
  beforeMount() {
    // Portal will be attach to div#portal-root
    // If no node is found, Portal will create div#portal-root
    // at the end of document.body
    this.portal = new Portal('portal-root', this.renderPortal);

    // Or you may pass an element 
    /* 
      const root = document.createElement('div')
      root.setAttribute('id', 'fresh-root');
      document.body.append(root); // You may bind it to something else than body
      this.portal = new Portal(root, this.renderPortal);
    */
      
    // this.portal = new Portal(document.querySelector('.other-portal-root'), this.renderPortal);
  },
 
  destroyed() {
    // It will destroy the portal if no other components is using it
    this.portal.unmount();
  },
  
  render(h) {
    // Trigger the portal renderer
    this.portal.update();
 
    return h('div', 'I am rendering with the normal flow without portal');
  }
}
You might also like...
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

Detect DOM element resizing based on Vue3.x

Detect DOM element resizing based on Vue3.x

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.

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

Places component is based on places.js for Vue 2.x. Turn any input into an address autocomplete.

vue-places Places component is based on places.js for Vue 2.x. Turn any into an address autocomplete Installation # yarn $ yarn add vue-places # npm

一款自带工作流引擎的Node.js全栈框架。A Node.js full-stack framework with workflow engine, based on koa + egg + vue + framework7
一款自带工作流引擎的Node.js全栈框架。A Node.js full-stack framework with workflow engine, based on koa + egg + vue + framework7

简体中文 | English CabloyJS 一款自带工作流引擎的Node.js全栈框架, 基于koa + egg + vue + framework7 目标、历史、信念 关于CabloyJS 特性 - 第一部分: 基础功能 Bean & AOP 几乎所有事物都是Bean Bean支持AOP AO

A require hook for loading single-file vue components in node

A require hook for loading single-file vue components in node. Useful for testing without having to spin up web browsers!

Webpack/Rollup/Vite plugin to add multiple root-node support to Vue 2 SFCs

vue-frag-plugin Webpack/Rollup/Vite plugin to use multiple root nodes in Vue 2 Single-file Components (SFCs). Powered by vue-frag. template !--

Multiplayer UNO game with Node.js and Vue
Multiplayer UNO game with Node.js and Vue

Multiplayer UNO game with Node.js and Vue Check out YouTube video Set up development environment Install docker, docker-compose, Node.js/npm Clone rep

Owner
Nourse
Encore du travail ! Oui Monseigneur !
Nourse
Vue V2 directive to react on clicks outside an element.

v-click-outside-x Vue V2 directive to react on clicks outside an element. Install $ npm install --save v-click-outside-x $ yarn add v-click-outside-x

Sergio Crisostomo 4 Nov 18, 2022
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

Alberto De Agostini 37 Nov 24, 2022
Tiny component to render Vue.js vNodes in the template.

Tiny component to render Vue.js vNodes in the template

hiroki osame 6 Jul 2, 2022
This project is based on Codepen and use vuejs to render a live view html/js/css of your code

Homemade Codepen This project is based on Codepen, and use vuejs to render a live view html/js/css of your code. Features Live preview of your code Ed

Erwan Martin 2 May 6, 2022
LogLive - Render changelog as a website on the fly.

This is inspired by headwayapp.co but as a single script you can add to an HTML file. Install yarn add loglive Table of Contents Usage Changel

EGOIST 149 Dec 23, 2022
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
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
An escape hatch for DOM Elements in Vue.js components.

vue-dom-portal 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

Caleb Roseland 202 Nov 2, 2022
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
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