Teleport

Intermediate · 5 min

Why modals break inside nested layouts, how <Teleport> renders a fragment elsewhere in the DOM while staying a logical child, and its to, disabled, defer props.

Before thisSlots and scoped slots

LearnVue · 5 min

Teleport

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by LearnVueReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A modal, toast or tooltip written inside a deeply nested component inherits that position in the DOM, so an ancestor's `transform`, `filter`, `overflow: hidden` or stacking context can clip it, misplace it, or bury it under other content.
  • `<Teleport to="body">` renders its slot content inside the target element instead of in place; `to` accepts a CSS selector or a real DOM node, and the target must already exist when the Teleport mounts.
  • Teleport only moves DOM. The teleported content is still a logical child of the component that wrote it, so props, emits, reactive state, provide/inject and the Vue Devtools tree all behave as if nothing moved.
  • Native DOM events from teleported content bubble through the real DOM (up to `body`), not through the component tree, which differs from React portals, whose synthetic events bubble through the React tree.
  • `:disabled` renders the content in place instead, which suits layouts that are an overlay on desktop but inline on mobile; multiple Teleports to the same target append in mount order.
  • Vue 3.5 added `defer`, which waits until the rest of the current render has mounted before resolving the target, so you can teleport into a container that Vue itself renders later in the same tree.

Where a component lives vs where its DOM should go

Components are organized by logic. The button that opens a “Delete account?” dialog and the dialog itself share one piece of state, open, so they belong in the same component:

<script setup>
import { ref } from 'vue'
const open = ref(false)
</script>

<template>
  <button @click="open = true">Delete account</button>

  <div v-if="open" class="modal">
    <p>This cannot be undone.</p>
    <button @click="open = false">Cancel</button>
  </div>
</template>

<style scoped>
.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
}
</style>

The trouble is that the dialog’s DOM now sits wherever this component happens to be rendered — maybe four levels deep inside a settings card inside a scrollable panel. CSS doesn’t care about your component tree, only the DOM tree, and several ordinary styles on ancestors break an overlay:

  • An ancestor with transform, filter or perspective becomes the containing block for position: fixed children, so “fixed to the viewport” quietly turns into “fixed to that card.”
  • An ancestor with overflow: hidden can clip a dropdown or tooltip that pokes outside it, and clips a fixed overlay too once a transform has made that ancestor its containing block.
  • z-index only competes within a stacking context. If an ancestor creates one with a lower z-index than a sibling, z-index: 1000 can’t lift the modal above that sibling.

Those rules are covered in the box model and the cascade. The usual workaround — move the modal markup up to App.vue and pass state down or through a store — fixes the CSS but scatters one feature across files.

<Teleport>: write it here, render it there

<Teleport> keeps the code where it belongs and puts the DOM somewhere else:

<template>
  <button @click="open = true">Delete account</button>

  <Teleport to="body">
    <div v-if="open" class="modal">
      <p>This cannot be undone.</p>
      <button @click="open = false">Cancel</button>
    </div>
  </Teleport>
</template>

The to prop takes a CSS selector string or an actual DOM element. Vue renders the slot content as children of that target — here, appended to body — so no ancestor’s transform or overflow applies any more.

Think of it like a restaurant kitchen printing an order ticket at the bar. The order still belongs to table 12 and the waiter still owns it; only where the ticket physically comes out has changed.

The target must already exist when the Teleport mounts. The safest target is an element outside the Vue app altogether, such as a dedicated <div id="modals"></div> next to <div id="app"> in index.html.

It moves DOM, not component relationships

This is the part that makes Teleport pleasant to use. In Vue’s component tree, the teleported content is still a child of the component that wrote the <Teleport>:

<!-- Parent.vue -->
<script setup>
import { ref, provide } from 'vue'
import ConfirmDialog from './ConfirmDialog.vue'

const open = ref(false)
provide('theme', 'danger')
</script>

<template>
  <button @click="open = true">Delete</button>
  <Teleport to="#modals">
    <ConfirmDialog v-if="open" title="Delete?" @close="open = false" />
  </Teleport>
</template>

ConfirmDialog receives its title prop, its close event reaches the parent, and inject('theme') inside it still finds the parent’s provide (provide and inject). Vue Devtools shows it nested under Parent, not under #modals. Scoped styles keep working too, because the scoping attribute is compiled onto the elements themselves.

One thing does follow the DOM: native events. Vue attaches real addEventListener listeners to elements, and a native click bubbles up the actual DOM tree. A click inside teleported content bubbles to #modals and body, not through the wrapper <div>s of the component that rendered it. So a <div @click="..."> around the component won’t see clicks from inside the modal. Component events sent with emit are unaffected.

That’s a real difference from React. React portals (createPortal) also move DOM while keeping the component relationship, but React’s synthetic events bubble through the React tree, so an onClick on a portal’s React ancestor does fire. In Vue, if a parent needs to know, emit an event.

disabled, multiple targets, and defer

disabled turns the teleport off and renders the content in place. It’s reactive, which suits responsive layouts:

<Teleport to="body" :disabled="isMobile">
  <FilterPanel />
</Teleport>

Several Teleports can share a target. Their content is appended in mount order, so a stack of toasts or nested modals layers naturally:

<Teleport to="#toasts"><Toast message="Saved" /></Teleport>
<Teleport to="#toasts"><Toast message="Synced" /></Teleport>

Vue 3.5 added defer. Normally the target is looked up the moment the Teleport mounts, so pointing at an element that Vue renders later in the same template fails. With defer, Vue waits until the rest of the current render has mounted:

<template>
  <Teleport defer to="#page-actions">
    <button>Export</button>
  </Teleport>

  <!-- rendered later in the same tree -->
  <header><div id="page-actions"></div></header>
</template>

The target still has to appear in the same mount or update tick; defer doesn’t wait for something that shows up a second later.

Modals still need accessibility work

Teleport solves a layout problem, not an accessibility one. A modal still needs focus moved into it on open, focus trapped while open, Escape to close, and focus returned to the trigger afterward — see accessible HTML and ARIA basics. It’s worth knowing that the native <dialog> element opened with showModal() renders in the browser’s top layer, which escapes ancestor stacking and clipping without any teleport, and handles Escape and inert background for you. Teleport remains the general tool for toasts, dropdown menus and popovers that aren’t dialogs. Pair it with <Transition> inside the Teleport to animate them.

With server-side rendering, teleported content has to be stitched into the right place in the HTML separately; SSR and hydration in Vue covers that.

What the video covers

LearnVue’s chapters follow this lesson closely: why Teleport is useful, how it works, building a modal with it, teleporting a component (which still behaves as a child), and sending multiple Teleports to one target. It predates Vue 3.5, so defer isn’t in it — the first link below covers that in under four minutes — and the event-bubbling difference above is the prose’s addition. The Vue School link builds an animated modal by combining Teleport with Transition.

Where this fits

The next built-ins deal with time rather than place: async components and Suspense handle components whose code or data isn’t ready yet.

Check yourself

  1. 01A modal uses `position: fixed; inset: 0` but appears offset and clipped inside a card that has `transform: scale(1)` applied for a hover effect. Why, and how does Teleport fix it?

    An ancestor with a `transform` (or `filter` or `perspective`) becomes the containing block for `position: fixed` descendants, so the modal is positioned relative to the card rather than the viewport, and the card's overflow can clip it. Wrapping the modal markup in `<Teleport to="body">` renders it as a child of `body`, outside the transformed ancestor, so `fixed` is relative to the viewport again.

  2. 02A teleported modal emits `close`, and its parent component listens with `@close`. Does the event still reach the parent even though the modal's DOM now lives under `body`?

    Yes. Teleport changes only where the DOM nodes are placed. The modal component is still a child of the component that rendered the `<Teleport>` in Vue's component tree, so component events, props, reactive state and injections all work exactly as they would without the Teleport.

  3. 03A parent `<div @click="track">` wraps a component that teleports a button to `body`. Clicking the teleported button does not call `track`. Why?

    Vue attaches native event listeners to real DOM elements, and native events bubble along the real DOM tree. The teleported button now sits under `body`, not inside the parent `<div>`, so the click never bubbles through that `<div>`. Component events emitted with `emit` are unaffected, because they travel through the component tree rather than the DOM.

  4. 04Your `<Teleport to="#sidebar-slot">` throws a 'failed to locate target' warning, and `#sidebar-slot` is rendered by a sibling component later in the same template. What are two ways to fix it?

    On Vue 3.5 or later, add the `defer` prop so the Teleport resolves its target after the rest of that render has mounted, which works as long as the target appears in the same mount or update tick. Otherwise, move the target outside the Vue app (for example a `<div id="sidebar-slot">` in `index.html`) or make sure the component that renders it mounts before the Teleport does.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark