-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Teleport component guide + API reference #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2221ea8
feat: finished teleport guide
NataliaTepluhina 1dae759
feat: added Teleport API
NataliaTepluhina 93c7528
Update src/api/built-in-components.md
NataliaTepluhina ee6cce1
Update src/api/built-in-components.md
NataliaTepluhina 5543a16
Update src/guide/teleport.md
NataliaTepluhina 68e13a3
Update src/guide/teleport.md
NataliaTepluhina ef34095
Update src/guide/teleport.md
NataliaTepluhina 4850af1
Update src/api/built-in-components.md
NataliaTepluhina 39b163c
fix: moved h1 to 'wrong' section
NataliaTepluhina d4f0f10
fix: typo
NataliaTepluhina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# Teleport | ||
|
||
Vue encourages us to build our UIs by encapsulating UI and related behavior into components. We can nest them inside one another to build a tree that makes up an application UI. | ||
|
||
However, sometimes a part of a component's template belongs into this component logically, while from a technical point of view, it would be preferable to move this part of the template somewhere else in the DOM, outside of Vue app. For example, due to styling requirements, we want to move `<p id="content">` from it's deeply nested position to the `<div>` with `id="endofbody"` | ||
|
||
```html | ||
<body> | ||
<div id="app" class="demo"> | ||
<h3>Move the #content with the portal component</h3> | ||
<div> | ||
<p id="content"> | ||
This should be moved to #endofbody. | ||
</p> | ||
<span>This content should be nested</span> | ||
</div> | ||
</div> | ||
<div id="endofbody"></div> | ||
</body> | ||
``` | ||
|
||
To do so, we can use Vue's built-in `<teleport>` component: | ||
|
||
```html | ||
<body> | ||
<div id="app" class="demo"> | ||
<h3>Move the #content with the portal component</h3> | ||
<div> | ||
<teleport to="#endofbody"> | ||
<p id="content"> | ||
This should be moved to #endofbody. | ||
</p> | ||
</teleport> | ||
<span>This content should be nested</span> | ||
</div> | ||
</div> | ||
<div id="endofbody"></div> | ||
</body> | ||
``` | ||
|
||
As a result, we will have `teleport` content moved in the rendered DOM tree: | ||
|
||
<p class="codepen" data-height="300" data-theme-id="39028" data-default-tab="html,result" data-user="Vue" data-slug-hash="WNrXYXd" data-preview="true" data-editable="true" style="height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;" data-pen-title="Teleport"> | ||
<span>See the Pen <a href="https://codepen.io/team/Vue/pen/WNrXYXd"> | ||
Teleport</a> by Vue (<a href="https://codepen.io/Vue">@Vue</a>) | ||
on <a href="https://codepen.io">CodePen</a>.</span> | ||
</p> | ||
<script async src="https://static.codepen.io/assets/embed/ei.js"></script> | ||
|
||
As you can see, all of the children of `<teleport>` will be appended to `<div id="endofbody">`. | ||
|
||
## Using with Vue components | ||
|
||
If `<teleport>` contains a Vue component, it will remain a logical child component of the `<teleport>`'s parent: | ||
|
||
```js | ||
const app = Vue.createApp({ | ||
template: ` | ||
<h1>Root instance</h1> | ||
<parent-component /> | ||
` | ||
}) | ||
|
||
app.component('parent-component', { | ||
template: ` | ||
<h2>This is a parent component</h2> | ||
<teleport to="#endofbody"> | ||
<child-component name="John" /> | ||
</teleport> | ||
` | ||
}) | ||
|
||
app.component('child-component', { | ||
props: ['name'], | ||
template: ` | ||
<div>Hello, {{ name }}</div> | ||
` | ||
}) | ||
``` | ||
|
||
In this case, even when `child-component` is rendered in the different place, it will remain a child of `parent-component` and will receive a `name` prop from it. | ||
NataliaTepluhina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
This also means that injections from a parent component work as expected, and that the child component will be nested below the parent component in the Vue Devtools, instead of being placed where the actual content moved to. | ||
|
||
## Using multiple teleports on the same target | ||
|
||
A common use case scenario would be a reusable `<Modal>` component of which there might be multiple instances active at the same time. For this kind of scenario, multiple `<teleport>` components can mount their content to the same target element. The order will be a simple append - later mounts will be located after earlier ones within the target element. | ||
|
||
```html | ||
<teleport to="#modals"> | ||
<div>A</div> | ||
</teleport> | ||
<teleport to="#modals"> | ||
<div>B</div> | ||
</teleport> | ||
|
||
<!-- result--> | ||
<div id="modals"> | ||
<div>A</div> | ||
<div>B</div> | ||
</div> | ||
``` | ||
|
||
You can check `<teleport>` component options in the [API reference](../api/built-in-components.html#teleport) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.