JAVASCRIPT
Managing Modals and Overlays with Vue 3's Teleport Feature
Utilize Vue 3's Teleport feature to render modals, tooltips, or notifications outside the component hierarchy, ensuring correct positioning and accessibility.
// Modal.vue
<template>
<Teleport to="body">
<div v-if="show" class="modal-backdrop" @click.self="closeModal">
<div class="modal-content">
<header class="modal-header">
<h3>{{ title }}</h3>
<button class="close-button" @click="closeModal">×</button>
</header>
<div class="modal-body">
<slot></slot>
</div>
<footer class="modal-footer">
<slot name="footer">
<button @click="closeModal">Close</button>
</slot>
</footer>
</div>
</div>
</Teleport>
</template>
<script setup>
import { defineProps, defineEmits } from 'vue';
const props = defineProps({
show: Boolean,
title: {
type: String,
default: 'Modal Title',
},
});
const emit = defineEmits(['close']);
function closeModal() {
emit('close');
}
</script>
<style scoped>
.modal-backdrop {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.6);
display: flex;
justify-content: center;
align-items: center;
z-index: 1000;
}
.modal-content {
background: white;
padding: 20px;
border-radius: 8px;
min-width: 300px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
position: relative;
max-height: 90vh;
overflow-y: auto;
}
.modal-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
border-bottom: 1px solid #eee;
padding-bottom: 10px;
}
.modal-header h3 {
margin: 0;
}
.close-button {
background: none;
border: none;
font-size: 1.5em;
cursor: pointer;
color: #aaa;
}
.modal-footer {
margin-top: 20px;
border-top: 1px solid #eee;
padding-top: 10px;
text-align: right;
}
</style>
// App.vue (Parent component usage)
<template>
<div>
<h1>My App Content</h1>
<button @click="showModal = true">Open Modal</button>
<Modal :show="showModal" title="Important Message" @close="showModal = false">
<p>This is the content of my custom modal. It's teleported!</p>
<p>Any content placed here will appear in the modal body.</p>
<template #footer>
<button @click="handleConfirm">Confirm</button>
<button @click="showModal = false">Cancel</button>
</template>
</Modal>
</div>
</template>
<script setup>
import { ref } from 'vue';
import Modal from './Modal.vue';
const showModal = ref(false);
function handleConfirm() {
alert('Confirmed!');
showModal.value = false;
}
</script>
How it works: Vue 3's `Teleport` component provides a way to render a part of your component's template into a DOM node that exists outside the hierarchy of that component. This is incredibly useful for modals, tooltips, and notifications, where you want the overlay to appear directly under `<body>` to avoid CSS stacking context issues (z-index) from parent components. In this example, `Modal.vue` uses `<Teleport to='body'>` to place its content directly into the document body, while still being controlled by its parent component (`App.vue`)'s reactive state. It also demonstrates slotting for flexible content and a named slot for the footer.