Vue / Advanced: architecture, hydration, and production evidence / Lesson 21
Teleport targets and rendering boundaries
Move Vue-rendered content to a deliberate DOM target while preserving ownership, accessibility, and hydration contracts.
On this page
Lesson goals
- Explain the difference between DOM placement and component ownership.
- Provide and verify a Teleport target.
- Account for server rendering and accessible interaction.
- Design Teleport target, timing and ownership contracts for client and SSR rendering.
- Verify accessible interaction and cleanup independently of DOM placement.
Choose a placement boundary
Teleport renders part of a component’s template into another DOM location while retaining its logical Vue component relationship. It can help place a status region outside a clipped container, but it does not automatically implement a modal, change ownership or fix accessibility. Choose a stable target and decide whether the content should be inline or relocated before adding the feature.
Render a status elsewhere
This component toggles a nonmodal status message in a target outside its own DOM position. The controlling state remains in the component. The example illustrates placement rather than claiming that a real save occurred; an actual application should show the message only after its owned operation succeeds. A role alone does not guarantee announcement in every browser and assistive technology combination.
<script setup>
import { ref } from "vue";
const visible = ref(false);
</script>
<template>
<button @click="visible = !visible">Toggle status</button>
<Teleport to="#status-area">
<p v-if="visible" role="status">Draft saved locally.</p>
</Teleport>
</template>
Activating Toggle status displays Draft saved locally. in the status area.
The button changes local state while Teleport places the conditional status in the target.
Provide the target
The host document includes a dedicated target before the application mounts. The example also includes a conventional module entry; a real project must supply that entry and its Vue build configuration. A selector that matches no element cannot receive the teleported nodes. Inspect the actual document and application mount timing rather than assuming a component-local element exists elsewhere.
<!doctype html>
<html lang="en">
<head><meta charset="UTF-8"><title>Vue host</title></head>
<body>
<div id="app"></div>
<div id="status-area"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>
The host contains separate app and status-area elements.
The status-area element is available as the Teleport destination before the application entry runs.
Account for server output and dialogs
Server rendering requires the framework’s documented handling of teleported output and a stable target that can be hydrated. Avoid placing private request data in a process-wide shared container. If the teleported content is a modal dialog, placement alone is insufficient: manage initial and restored focus, modal keyboard behavior and inert background using an appropriate accessible implementation. The status example is not a modal.
Verify placement and ownership
Toggle the status with keyboard and pointer, inspect the target and confirm the message disappears when the owner unmounts. Test a missing target in a controlled environment and a direct server-rendered load when supported. Check that state and events still belong to the original component. Verify actual announcement with assistive technology; DOM placement and a status role do not establish a complete accessibility result.
Separate DOM placement from Vue ownership
Teleport moves part of a component’s rendered DOM to a chosen target while preserving its logical Vue component relationship. A status message can appear outside a clipped panel while its state, props, emitted events and lifecycle remain associated with the original component tree. The target does not become the state owner simply because it contains the nodes. Choose this boundary when DOM placement solves a real layout or integration problem; it is not a substitute for designing the interaction.
The host must contain a suitable target at the required time. A selector that matches nothing cannot receive the content. Use a dedicated, stable element with an unambiguous id and verify the actual host document and mount sequence. Vue 3.5 supports deferred target resolution within the documented mount or update timing, but it is not an unlimited wait for an element created later by an unrelated asynchronous task. These fixtures use Vue 3.5.44 and are not claimed browser executions.
Define target, timing and disabled behavior
A normal Teleport resolves its destination when it mounts. In Vue 3.5, the defer option can postpone target resolution until other parts of the same application update have mounted, which is useful when a target is rendered later in that same cycle. It does not solve a target that appears seconds later after a network response. Test the exact host and timing rather than relying on a selector string alone. A missing target is a configuration failure to diagnose, not a signal that the content safely appeared elsewhere.
The disabled prop lets a Teleport render its content at its original location instead of the external target. If that behavior depends on viewport or application state, test both branches and transitions. Multiple Teleports can target the same element; their mount order affects the appended content, so do not assume that visual ordering is a durable priority queue. Use stable ownership and explicit ordering when the product requires it.
Build the interaction, not just its placement
Teleport does not implement a modal dialog. A modal needs an appropriate accessible name, initial focus, keyboard navigation contained within the dialog when required, a dismissal policy, focus restoration and background interaction management. Use an established accessible dialog implementation where suitable, and verify its behavior in the actual browser. Moving a panel above a clipped container may fix layout while leaving all those interaction requirements unresolved.
A nonmodal status message has a different contract from a modal: it should report a real outcome, remain understandable and be announced appropriately without stealing focus unnecessarily. A role such as status is a semantic cue, not a guarantee of announcement in every browser and assistive-technology combination. Test keyboard and pointer behavior, the timing of the status update and the user’s ability to continue the task. Do not announce a save before its actual operation succeeds.
Integrate teleported output during server rendering
In SSR, teleported content is not simply included in the main rendered application string at its source position. Vue exposes teleported output in the server rendering context, and the server integration must place it into the intended dedicated target in the final HTML. Avoid targeting body for SSR teleports when other server-rendered content makes the correct hydration starting point ambiguous. Follow the documented integration for the selected framework rather than assuming a client-only host arrangement is sufficient.
Create request-owned application and rendering context state. A module-level mutable container can leak one user’s teleported content into another request. Serialize only authorized data, use safe output handling and verify cache scope for personalized responses. The server and first client render must agree on the target and initial content; moving DOM alone does not make random, time-dependent or browser-only initial state deterministic. Inspect the generated HTML and test actual hydration separately.
Verify target, owner and recovery together
An isolated component test can toggle a teleported status and inspect the designated target while checking that events and state still belong to the source component. Remove the owner and verify its teleported nodes and owned effects are cleaned up. Test a missing target and the disabled branch in a controlled host. A test that only checks the source component’s local DOM can miss content that Vue placed elsewhere.
For a modal, add browser checks for initial focus, contained keyboard navigation, dismissal, restoration and background behavior. For SSR, request the route directly, inspect the dedicated target in the response and then verify hydration and interaction in a browser. Include two synthetic requests when personalized content is involved. Record what actually ran; a DOM placement assertion alone does not prove screen-reader announcement, server isolation or production hosting behavior.
20 worked examples
1. Dedicated target
Problem: Place a status message outside its source component DOM.
- The selector names the host element with id status-area.
- The paragraph remains part of the source component’s logical Vue tree.
<template><Teleport to="#status-area"><p role="status">Ready</p></Teleport></template>
<!-- Host: <div id="status-area"></div> -->
This contextual example is not executed; a configured Vue host and browser are required where applicable.
Expected result: In a configured host, the intended paragraph is rendered in the status-area target.
Why this result follows: Teleport changes the rendered DOM location while preserving component ownership.
2. Missing target
Problem: Diagnose a selector that matches no host element.
- The selector requires an element with id missing.
- The stated host has no such element when the Teleport mounts.
<template><Teleport to="#missing"><p>Ready</p></Teleport></template>
<!-- Host contains no element with id missing. -->
This contextual example is not executed; a configured Vue host and browser are required where applicable.
Expected result: The destination cannot be resolved at mount; provide a valid target and inspect the Vue diagnostic.
Why this result follows: A Teleport needs a resolvable destination at the required time.
3. Owner state stays local
Problem: Toggle content that is rendered elsewhere.
- The button updates state declared by the source component.
- Teleport relocates the paragraph’s DOM but does not transfer that state.
<script setup>
import { ref } from "vue"
const visible = ref(false)
</script>
<template><button @click="visible = !visible">Toggle</button><Teleport to="#status-area"><p v-if="visible">Ready</p></Teleport></template>
This contextual example is not executed; a configured Vue host and browser are required where applicable.
Expected result: After activation, the source component’s visible ref makes Ready appear in the target.
Why this result follows: Logical ownership remains with the component that declared the teleported content.
4. Disabled teleport
Problem: Render the content at its original location.
- The disabled prop selects the nonteleported placement.
- The external target is not used for this content in that state.
<template><Teleport to="#status-area" :disabled="true"><p>Inline notice</p></Teleport></template>
This contextual example is not executed; a configured Vue host and browser are required where applicable.
Expected result: With disabled true, the intended paragraph renders inline at the Teleport’s original location.
Why this result follows: Disabling Teleport changes placement without changing the content’s owner.
5. Modal needs more than placement
Problem: Assess a panel moved into an overlay target.
- Moving DOM does not establish initial or restored focus.
- Keyboard containment, dismissal and background behavior require deliberate implementation.
Teleport target: #overlay
Panel: looks like a modal
Focus management and background behavior: not implemented
This contextual example is not executed; a configured Vue host and browser are required where applicable.
Expected result: The placement alone does not provide an accessible modal; implement and test the dialog interaction contract.
Why this result follows: A modal is an interaction pattern rather than only a DOM location.
6. Deferred same-cycle target
Problem: Resolve a target rendered later in the same update.
- The target is part of the same application render cycle.
- defer delays target resolution until the relevant other content has mounted.
<template>
<Teleport defer to="#late-target"><p>Ready</p></Teleport>
<div id="late-target"></div>
</template>
This contextual example is not executed; a configured Vue host and browser are required where applicable.
Expected result: In Vue 3.5, defer permits the target rendered later in the same mount cycle to be resolved.
Why this result follows: Deferred resolution addresses this specific same-cycle ordering.
7. Deferred is not indefinite
Problem: Distinguish later asynchronous target creation.
- The target is absent from the same mount or update cycle.
- The documented deferred resolution is tied to that cycle.
Teleport defer mounts now
Target is created by an unrelated network response several seconds later
This contextual example is not executed; a configured Vue host and browser are required where applicable.
Expected result: The deferred option does not provide an unlimited wait for that later target; coordinate rendering or create the target earlier.
Why this result follows: A bounded resolution delay is not a general asynchronous target waiter.
8. Multiple teleports
Problem: Predict content order for two mounts to one target.
- Both Teleports resolve the same destination.
- The later mounted content is appended after the earlier content.
<template>
<Teleport to="#notices"><p>First</p></Teleport>
<Teleport to="#notices"><p>Second</p></Teleport>
</template>
This contextual example is not executed; a configured Vue host and browser are required where applicable.
Expected result: When mounted in this order, the target receives First followed by Second.
Why this result follows: Mount order determines the appended order in this simple fixture.
9. Unmount source owner
Problem: Remove teleported content when its declaring component ends.
- The teleported nodes remain owned by the source component.
- Ending that component ends its rendered content.
Source component mounts -> target contains Ready
Source component unmounts
This contextual example is not executed; a configured Vue host and browser are required where applicable.
Expected result: The source component’s teleported content is removed as part of its unmount.
Why this result follows: DOM relocation does not detach content from its logical component lifetime.
10. Child event still reaches owner
Problem: Trace an event from a teleported child.
- The listener is declared in the logical component tree.
- Teleport changes DOM placement rather than the child-parent relationship.
<template><Teleport to="#overlay"><NoteEditor @save="handleSave" /></Teleport></template>
This contextual example is not executed; a configured Vue host and browser are required where applicable.
Expected result: A save event from NoteEditor is handled by the source component’s handleSave listener.
Why this result follows: Component event wiring follows logical ownership rather than physical DOM ancestry.
11. Props remain logical inputs
Problem: Pass a title to a teleported child.
- The child is declared with a bound title input.
- Its physical DOM destination does not replace the declaring component’s bindings.
<template><Teleport to="#overlay"><NoteEditor :title="title" /></Teleport></template>
This contextual example is not executed; a configured Vue host and browser are required where applicable.
Expected result: NoteEditor receives the source component’s title prop despite rendering under the overlay target.
Why this result follows: Teleport does not alter the component’s prop contract.
12. Switch disabled placement
Problem: Move between inline and target placement deliberately.
- The disabled binding reads the current inline state.
- The same logical content is rendered under the selected placement mode.
<template><Teleport to="#overlay" :disabled="inline"><p>Help</p></Teleport></template>
This contextual example is not executed; a configured Vue host and browser are required where applicable.
Expected result: When inline is true the paragraph is inline; when false it is placed in the overlay target.
Why this result follows: The disabled prop controls placement rather than changing state ownership.
13. Dedicated SSR target
Problem: Integrate server teleported output into a stable container.
- Vue exposes teleported output through the server rendering context.
- The final document must include it at the destination expected by the client.
Server context teleports: #overlay -> rendered dialog markup
Final HTML: <div id="overlay">[that markup]</div>
This contextual example is not executed; a configured Vue host and browser are required where applicable.
Expected result: The server integration places the teleported output into the matching dedicated target for client hydration.
Why this result follows: A consistent target connects server output to the client hydration boundary.
14. Avoid ambiguous body target in SSR
Problem: Choose a target with a clear hydration location.
- The body contains multiple unrelated nodes in the fixture.
- A dedicated container identifies the intended teleport region.
Proposed target: body
Body also contains unrelated server-rendered elements
Alternative: dedicated #overlay container
This contextual example is not executed; a configured Vue host and browser are required where applicable.
Expected result: Use a dedicated target so the teleported region has a clear location in the server document.
Why this result follows: A specific target reduces ambiguity when integrating and hydrating server output.
15. Request-specific teleport data
Problem: Keep personalized notices isolated on the server.
- The notices belong to different users.
- A shared mutable context can mix output across concurrent requests.
Request A notice: "Hello Ada"
Request B notice: "Hello Basil"
Risk: one shared mutable render context
This contextual example is not executed; a configured Vue host and browser are required where applicable.
Expected result: Create separate request-associated render contexts and verify each response contains only its authorized notice.
Why this result follows: Request ownership applies to teleported output as well as the main application tree.
16. Status is not save confirmation
Problem: Avoid announcing a success before the operation finishes.
- A click starts a task but does not establish its result.
- The status message should reflect the actual operation outcome.
User clicks Save
Request: pending
Proposed status: "Saved" immediately
This contextual example is not executed; a configured Vue host and browser are required where applicable.
Expected result: Show a pending state first and announce Saved only after the owned operation confirms success.
Why this result follows: Teleport placement does not change the truthfulness required of feedback.
17. Focus into a dialog
Problem: Plan an initial focus target for a teleported modal.
- The user’s previous focus remains at the trigger unless managed.
- The dialog needs a meaningful initial point for keyboard interaction.
Trigger: Open settings button
Dialog appears in #overlay
Policy: focus the dialog heading or first appropriate control
This contextual example is not executed; a configured Vue host and browser are required where applicable.
Expected result: After the dialog is rendered, move focus according to the dialog’s accessible interaction policy and verify it in a browser.
Why this result follows: DOM placement alone does not move focus to the new task.
18. Restore focus on dismissal
Problem: Return to a meaningful control after closing a modal.
- The focused dialog control disappears on dismissal.
- The opener is a known surviving control associated with the action.
Before open: focus on Open settings
Dialog closes and its content is removed
Opener still exists
This contextual example is not executed; a configured Vue host and browser are required where applicable.
Expected result: Restore focus to Open settings under the stated policy and test keyboard continuation.
Why this result follows: Explicit restoration preserves the user’s location in the workflow.
19. Target CSS inheritance
Problem: Notice a style change after moving DOM.
- CSS inheritance follows physical DOM ancestry.
- Teleport places the rendered nodes outside the source ancestor.
Source ancestor defines CSS custom property --accent
Teleport target is outside that ancestor
Teleported content uses var(--accent)
This contextual example is not executed; a configured Vue host and browser are required where applicable.
Expected result: The content may no longer inherit the source ancestor’s custom property; provide styling at an appropriate DOM ancestor.
Why this result follows: Logical Vue ownership does not preserve physical CSS inheritance.
20. Inspect the correct test target
Problem: Find content that is not under the source wrapper’s DOM.
- Teleport renders the paragraph in the external target.
- A source-only DOM query can miss the relocated node.
Mounted source wrapper: button only
Document #status-area: teleported paragraph "Ready"
This contextual example is not executed; a configured Vue host and browser are required where applicable.
Expected result: A test should inspect the designated target as well as the source wrapper.
Why this result follows: Test queries must follow the actual physical placement being asserted.
Common errors and fixes
The teleported content is missing.
- Why it happens
- The target selector does not resolve at the required time.
- How to fix it
- Create the intended target before mounting or use the documented deferred option where supported.
- Prevent it next time
- Test the host document and mount timing.
A modal looks correct but keyboard focus escapes.
- Why it happens
- Teleport changes placement but does not implement modal behavior.
- How to fix it
- Use an accessible dialog implementation with focus management.
- Prevent it next time
- Test keyboard, focus restoration and background behavior.
Server and client content disagree.
- Why it happens
- Teleported server output is not integrated into the intended target.
- How to fix it
- Follow the framework’s server rendering integration for teleports.
- Prevent it next time
- Test direct hydration with a dedicated target.
20 practice exercises
1. Does moving the DOM node move its Vue state to the target’s component?
Show hint
Compare physical DOM ancestry with the logical Vue component tree.
Show worked solution
No. Teleport changes DOM placement while the content stays in the original logical component tree. Test its event and lifecycle at the owner.
Teleport changes where nodes are rendered, but their state, props, events and lifecycle remain associated with the declaring component tree. Test the owner’s event and remove the owner to verify cleanup at that boundary.
Check your solution
- The source component retains state ownership.
- Unmount removes the teleported content.
2. What extra work does a teleported modal require?
Show hint
A modal is a complete interaction pattern, not just an overlay position.
Show worked solution
It needs an accessible dialog contract, including suitable focus placement, contained keyboard navigation, dismissal and focus restoration. Teleport by itself supplies none of those behaviors.
A teleported modal needs an accessible name, suitable initial focus, appropriate contained keyboard navigation, dismissal, focus restoration and background interaction management. Verify these behaviors in the target browser and assistive technology; Teleport supplies placement only.
Check your solution
- Focus enters and returns appropriately.
- Keyboard and background behavior are tested.
3. A status must appear outside a clipped panel. Choose a Teleport target contract.
Show hint
The host needs a stable destination.
Show worked solution
Create a dedicated target in the host before the Teleport resolves and point the component to its selector. Test the target’s existence and the status placement in a configured Vue host.
Check your solution
- The target id is unique and present.
- The status appears in that target.
4. Teleported content is under a different DOM element. Who owns its Vue state?
Show hint
Physical placement and logical tree differ.
Show worked solution
The component that declares the content retains its state, props, events and lifecycle relationship. Test its control and remove the owner to verify the content follows that lifetime.
Check your solution
- The source component controls the state.
- Unmount removes its teleported content.
5. A Teleport points to #overlay but the host has no matching element. Diagnose it.
Show hint
Resolve the selector at mount time.
Show worked solution
Provide the intended target at the required time and inspect the Vue diagnostic in a controlled host. Do not assume the missing content was rendered safely in another location.
Check your solution
- The target exists when required.
- The diagnostic is resolved.
6. A target is rendered later in the same Vue 3.5 mount cycle. Which option can help?
Show hint
Use the documented bounded delay.
Show worked solution
Use defer when the target is rendered later in the same mount or update cycle, then test the actual component tree. It does not wait indefinitely for an unrelated asynchronous target.
Check your solution
- The target appears in the same cycle.
- Deferred resolution finds the intended element.
7. A target arrives from a network response seconds after Teleport mounts. Is defer alone enough?
Show hint
Its resolution delay is bounded.
Show worked solution
No. Create the target earlier or conditionally render the Teleport after the target is available, and test the actual timing. Do not rely on defer as a general asynchronous waiter.
Check your solution
- The target exists before resolution.
- The network timing is tested.
8. A Teleport uses :disabled="inline". Predict placement for true and false.
Show hint
Disabled controls DOM location.
Show worked solution
When inline is true the content remains at its original location; when false it is placed in the target. Verify both transitions while preserving the same logical owner and state.
Check your solution
- True renders inline.
- False renders in the target.
9. Two Teleports mount to the same target in order First then Second. Predict their simple order.
Show hint
Later content is appended.
Show worked solution
The target receives First followed by Second in this mount sequence. If the product needs a durable priority order, manage that ordering explicitly and test remounts.
Check your solution
- Both use the same target.
- The expected mount order is observed.
10. A teleported child emits save. Which component should handle the event?
Show hint
Follow the logical component tree.
Show worked solution
The listener declared by the source component handles the child event despite the external DOM target. Test the emitted payload and the owner’s response separately from physical placement.
Check your solution
- The source listener receives the event.
- The payload remains correct.
11. A teleported modal looks centered. What interaction work remains?
Show hint
Placement is not a dialog implementation.
Show worked solution
Provide an accessible name, initial focus, appropriate keyboard containment and dismissal, focus restoration and background interaction management. Verify the complete dialog behavior in a browser.
Check your solution
- Focus enters and returns appropriately.
- Keyboard and background behavior are tested.
12. A nonmodal status says Saved immediately after a click while the request is pending. Repair it.
Show hint
A click is not confirmation.
Show worked solution
Show pending feedback and announce success only after the owned operation confirms it. Handle failure or uncertainty separately and test the announcement with appropriate assistive technology.
Check your solution
- Pending and confirmed states differ.
- Success follows the actual outcome.
13. A dialog closes while its close button has focus. Where should focus go?
Show hint
The focused node will be removed.
Show worked solution
Restore focus to the opener when it still exists, or use another meaningful destination defined by the interaction. Test keyboard continuation after dismissal in a real browser.
Check your solution
- Focus leaves the removed dialog.
- The destination is meaningful and reachable.
14. A teleported node loses an inherited CSS custom property. Why can that happen?
Show hint
CSS inheritance follows physical DOM ancestry.
Show worked solution
The target may lie outside the ancestor that defined the property. Provide the styling at a suitable DOM ancestor or explicitly on the target, then verify the rendered appearance.
Check your solution
- The target receives the needed style value.
- The visual result is checked in a browser.
15. SSR renders Teleport content for #overlay. Where must the server integration place it?
Show hint
The main application string is not the entire output.
Show worked solution
Use the server rendering context’s teleported output and insert it into the matching dedicated target in the final HTML according to the framework integration. Test the response and client hydration.
Check your solution
- The target contains the server teleport output.
- The client hydrates the matching destination.
16. A server-rendered Teleport targets body containing unrelated nodes. Choose a clearer target.
Show hint
Hydration needs an identifiable region.
Show worked solution
Use a dedicated container such as #overlay and integrate the server output there. Verify the final HTML and direct browser hydration rather than relying on an ambiguous body position.
Check your solution
- The target has a dedicated container.
- Server and client agree on its content.
17. Two personalized SSR notices use one module-level mutable render context. Identify the risk.
Show hint
The process can serve multiple users.
Show worked solution
Concurrent requests can mix one user’s notice into another response. Create request-associated state and context, minimize serialized data and test two synthetic users plus an anonymous request.
Check your solution
- Each request has an isolated context.
- Responses contain only authorized notices.
18. A source wrapper query cannot find the teleported paragraph. What should the test inspect?
Show hint
The paragraph is physically elsewhere.
Show worked solution
Inspect the designated target in the test document as well as the source wrapper. Verify the owner’s state and event behavior, then unmount and check that the target is cleaned up.
Check your solution
- The target contains the expected paragraph.
- Unmount removes the owner’s teleported nodes.
19. A Teleport target has role status. Does that prove every assistive technology announces it?
Show hint
Markup and actual announcement are different evidence.
Show worked solution
No. The role supplies a semantic cue, but timing and announcement behavior must be checked with appropriate browser and assistive technology combinations. Ensure the message reflects a real outcome.
Check your solution
- The status text is meaningful.
- Announcement is verified in the intended environment.
20. A direct SSR response looks correct. What client evidence remains?
Show hint
Server delivery and hydration differ.
Show worked solution
Load the direct route in a browser, inspect hydration diagnostics and exercise the teleported interaction. Test target availability and focus behavior separately from the server HTML assertion.
Check your solution
- Hydration completes without unexplained mismatch.
- The interaction works in the browser.
Key takeaways
- Teleport changes DOM placement, not logical ownership.
- Supply a stable target and handle server integration.
- Accessibility requires interaction behavior beyond placement.
- Teleport changes DOM placement while retaining logical component ownership.
- A target must exist at the documented resolution time.
- A teleported modal still needs a complete accessible interaction contract.