JAVASCRIPT

Implement a Modal Dialog with Vue 3 Teleport

Create flexible and accessible modal dialogs in Vue 3 using the `Teleport` component to render content outside the current component's DOM tree.

// components/Modal.vue
<template>
  <teleport to="body">
    <div v-if="isOpen" class="modal-backdrop" @click.self="closeModal">
      <div class="modal-content">
        <header class="modal-header">
          <slot name="header"><h3>Default Header</h3></slot>
          <button @click="closeModal" class="close-button">&times;</button>
        </header>
        <main class="modal-body">
          <slot>Default body content</slot>
        </main>
        <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: {
    type: Boolean,
    default: false
  }
});

const emit = defineEmits(['update:isOpen', 'close']);

const closeModal = () => {
  emit('update:isOpen', false);
  emit('close');
};
</script>

<style scoped>
.modal-backdrop {
  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-content {
  background: white;
  padding: 20px;
  border-radius: 8px;
  min-width: 300px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}
.close-button {
  background: none;
  border: none;
  font-size: 1.5em;
  cursor: pointer;
}
.modal-footer {
  text-align: right;
  margin-top: 20px;
}
</style>

// App.vue (Example Usage)
<template>
  <div>
    <button @click="showModal = true">Open Modal</button>
    <Modal v-model:isOpen="showModal" @close="handleModalClose">
      <template #header><h2>My Custom Modal Title</h2></template>
      <p>This is the content of my modal dialog.</p>
      <template #footer>
        <button @click="showModal = false">Got It!</button>
      </template>
    </Modal>
  </div>
</template>

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

const showModal = ref(false);

const handleModalClose = () => {
  console.log('Modal was closed!');
  // Perform any actions needed after modal close
};
</script>
How it works: This snippet demonstrates creating a reusable modal component using Vue 3's `Teleport` feature. `Teleport` allows the modal's content to be rendered directly into the `body` element of the HTML document, preventing z-index issues and ensuring proper accessibility, while still being controlled by its parent component. It includes slots for flexible content and `v-model` for easy state management, making it a robust solution for modals.

Need help integrating this into your project?

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

Hire DigitalCodeLabs