HTML
Vue 3 Teleport Component for Modals
Render Vue 3 component templates outside their current DOM hierarchy using the built-in Teleport feature for modals.
<template>
<div class="relative">
<button @click="isOpen = true">Open Modal</button>
<teleport to="body">
<div v-if="isOpen" class="modal">
<p>This is teleported to the body!</p>
<button @click="isOpen = false">Close</button>
</div>
</teleport>
</div>
</template>
<script setup>
import { ref } from 'vue';
const isOpen = ref(false);
</script>
How it works: The Vue 3 <teleport> component allows clean architecture by letting you render a piece of template logic into a DOM node that exists outside the DOM hierarchy of the component itself, which is ideal for overlays and modals.