JAVASCRIPT
Building Modals and Overlays with Vue 3 Teleport
Learn to use Vue 3's Teleport feature to render modal dialogs, notifications, or tooltips outside of the component's DOM tree, ensuring correct layering and styling.
<!-- public/index.html (or similar base HTML file) -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue Teleport Example</title>
</head>
<body>
<div id="app"></div>
<div id="modal-root"></div> <!-- Target element for teleported content -->
</body>
</html>
<!-- src/components/BaseModal.vue -->
<template>
<teleport to="#modal-root">
<div v-if="isOpen" class="modal-overlay" @click.self="closeModal">
<div class="modal-container">
<header class="modal-header">
<h3>{{ title }}</h3>
<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({
isOpen: Boolean,
title: {
type: String,
default: 'Modal Title'
}
});
const emit = defineEmits(['close']);
function closeModal() {
emit('close');
}
</script>
<style scoped>
.modal-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
z-index: 1000;
}
.modal-container {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
min-width: 300px;
max-width: 90%;
z-index: 1001;
}
.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;
}
.modal-header button {
background: none;
border: none;
font-size: 1.5em;
cursor: pointer;
}
.modal-body {
margin-bottom: 15px;
}
.modal-footer {
text-align: right;
}
</style>
<!-- src/App.vue (Usage Example) -->
<template>
<button @click="isModalOpen = true">Open Modal</button>
<BaseModal :is-open="isModalOpen" title="My Awesome Modal" @close="isModalOpen = false">
<p>This is the content of my modal!</p>
<template #footer>
<button @click="isModalOpen = false">Custom Close</button>
</template>
</BaseModal>
</template>
<script setup>
import { ref } from 'vue';
import BaseModal from './components/BaseModal.vue';
const isModalOpen = ref(false);
</script>
How it works: This snippet demonstrates Vue 3's `Teleport` feature by creating a reusable `BaseModal` component. By wrapping the modal's content with `<teleport to="#modal-root">`, the modal component's DOM structure is rendered directly into the `modal-root` element in `public/index.html`, independent of its parent component's DOM tree. This is ideal for managing modals, tooltips, or notifications, ensuring they are correctly positioned and layered without z-index conflicts from their parent components.