Vue.js & Composition API (50 Qs)•Part 3: Advanced Internals, Scale & Production Architecture (Senior / Lead)
Vue 3 `<Teleport>`: Escaping Stacking Contexts for Modals and Popovers.
senior LevelCode ExampleMemory ManagementSystem DesignVue & Nuxt
In Simple Words:
<Teleport> renders a component's template DOM node into a different part of the document body (e.g. to="body" or to="#modal-target") while preserving Vue component reactivity, props, and emit hierarchies.
vue
<script setup lang="ts">
import { ref } from 'vue';
const isOpen = ref(false);
</script>
<template>
<button @click="isOpen = true">Open Dialog</button>
<!-- Content escapes overflow:hidden and z-index traps of parent container -->
<Teleport to="body" :disabled="!isOpen">
<div v-if="isOpen" class="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
<div class="bg-white dark:bg-slate-900 p-6 rounded-2xl shadow-2xl">
<h3>Portal Modal</h3>
<button @click="isOpen = false">Close</button>
</div>
</div>
</Teleport>
</template>