<Teleport> component allows you to render part of a component’s template in a DOM node that exists outside the component’s DOM hierarchy.
Basic Usage
<body> instead of nested within the component’s DOM structure.
Props
to
- Type:
string | Element | null | undefined - Required: Yes
String Selector
Must be a valid CSS selector:Element Reference
Can be an actual DOM element:disabled
- Type:
boolean - Default:
false
true, the content will remain in its original location instead of being teleported.
defer
- Type:
boolean - Default:
false
true, defers the teleport until after the component has mounted. This is useful when the target element is rendered by Vue in a later phase.
Common Use Cases
Modals and Dialogs
Tooltips and Popovers
Notifications
Multiple Teleports to Same Target
Multiple<Teleport> components can target the same container. The order will be a simple append - later mounts will be located after earlier ones within the target element.
Using with Components
<Teleport> works seamlessly with Vue components:
Using with Transitions
Custom Elements Support
When used inside a Custom Element, Vue tracks teleport targets to properly manage lifecycle and cleanup.Namespace Handling
<Teleport> automatically detects the namespace of the target element:
- If teleporting to an SVG element, content is rendered with SVG namespace
- If teleporting to a MathML element, content is rendered with MathML namespace
/packages/runtime-core/src/components/Teleport.ts:140-143
SSR Behavior
During server-side rendering:- Teleport content is rendered in place as placeholder comments
- The actual content is rendered in the target location in the SSR output
- During hydration, Vue matches the teleported content with its target location
Implementation Details
- Anchors: Teleport uses comment nodes (or text nodes in production) as anchors in both the original location and the target location
- Moving: When
disabledchanges or target changes, the content is moved between locations - Hydration: Special handling during SSR hydration to correctly match teleported content
Accessibility Considerations
When using
<Teleport> for modals and dialogs, remember to manage focus and ARIA attributes appropriately. The teleported content should have proper ARIA roles and focus management even though it’s rendered outside the component’s DOM hierarchy.Source Reference
- Package:
@vue/runtime-core - Implementation:
/packages/runtime-core/src/components/Teleport.ts:75-343 - Props Interface:
/packages/runtime-core/src/components/Teleport.ts:20-24