← Back to all snippets
JAVASCRIPT

Implementing Modals with Vue 3 Teleport

Master Vue 3's `Teleport` feature to render components like modals, notifications, or tooltips into a different part of the DOM tree, outside the parent component's hierarchy.

// components/MyModal.vue
<template>
  <teleport to="body">
    <div v-if="isOpen" class="modal-backdrop" @click="closeModal">
      <div class="modal-content" @click.stop>
        <h3><slot name="header">Default Header</slot></h3>
        <div class="modal-body">
          <slot>Default Body Content</slot>
        </div>
        <div class="modal-footer">
          <slot name="footer">
            <button @click="closeModal">Close</button>
          </slot>
        </div>
      </div>
    </div>
  </teleport>
</template>

<script setup>
import { defineProps, defineEmits } from 'vue';

const props = defineProps({
  isOpen: {
    type: Boolean,
    default: false,
  },
});

const emit = defineEmits(['close']);

function closeModal() {
  emit('close');
}
</script>

<style scoped>
.modal-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}
.modal-content {
  background-color: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  min-width: 300px;
  max-width: 80vw;
  z-index: 1001;
}
.modal-footer {
  margin-top: 15px;
  text-align: right;
}
</style>

// App.vue (Parent Component)
<template>
  <div>
    <h1>My Application</h1>
    <button @click="showModal = true">Open Modal</button>

    <MyModal :is-open="showModal" @close="showModal = false">
      <template #header>
        <h2>Welcome to the Modal!</h2>
      </template>
      <p>This content is rendered inside the modal. Teleport allows it to appear outside the component's normal DOM hierarchy.</p>
      <template #footer>
        <button @click="showModal = false" style="background-color: #007bff; color: white; border: none; padding: 8px 15px; border-radius: 4px; cursor: pointer;">Confirm</button>
        <button @click="showModal = false" style="background-color: #6c757d; color: white; border: none; padding: 8px 15px; border-radius: 4px; cursor: pointer; margin-left: 10px;">Cancel</button>
      </template>
    </MyModal>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import MyModal from './components/MyModal.vue';

const showModal = ref(false);
</script>
How it works: This snippet demonstrates Vue 3's `Teleport` feature by creating a modal component (`MyModal.vue`). `Teleport` allows the modal's content to be rendered directly into the `body` of the HTML document, regardless of where `MyModal` is used in the component tree. This is crucial for elements like modals or notifications that need to break out of parent component styling or `z-index` contexts.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs