Vue Teleport

Vue Teleport
It is especially useful for modals, dialogs, tooltips, dropdowns, and notifications.
What is Vue Teleport?
Teleport lets you “teleport” HTML markup to another place in the DOM, such as <body>.
- Logical position (Vue component tree)
- Visual position (DOM tree) can be different
Why Teleport is Needed
Without Teleport, UI elements like modals can break due to:
overflow: hiddenz-indexissuesNested layouts
Teleport solves these problems cleanly.
Basic Syntax
to→ CSS selector where content should be rendered
Basic Example
- Modal renders inside
<body> - State & events still work normally
Teleport Target
You can teleport to:
- Helps organize multiple teleported elements
Teleport Does NOT Break Reactivity
Even though the DOM is moved:
datapropscomputedmethods$emit()
All work exactly the same
Conditional Teleport
- Element is created only when needed
Disable Teleport (Advanced)
You can turn teleport on/off dynamically.
true→ renders normally (no teleport)false→ teleports
Teleport vs Dynamic Components
| Feature | Teleport | Dynamic Component |
|---|---|---|
| Moves DOM | Yes | No |
| Switch components | No | Yes |
| Use case | Modals, overlays | Tabs, dashboards |
Common Use Cases
Modals & dialogs
Toast notifications
Tooltips
Dropdown menus
Context menus
Side panels
Common Mistakes
- Forgetting teleport target exists
- Using teleport unnecessarily
- Styling issues without global CSS
- Expecting teleport to manage state
Best Practices
- Use Teleport for overlay UI
- Keep teleport targets organized
- Combine with
v-iffor performance - Style teleported content globally or scoped wisely
Summary Table
| Feature | Description |
|---|---|
| Purpose | Move DOM outside hierarchy |
| Syntax | <Teleport to="..."> |
| Reactivity | Preserved |
| Common target | body |
| Typical use | Modals, popups |
Conclusion
Vue Teleport cleanly separates UI placement from component logic.
It is a must-know feature for building professional modals and overlays in Vue 3.
