-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
21ce197
commit 1d53284
Showing
4 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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,35 @@ | ||
--- | ||
title: "Portals" | ||
description: "Rendering into out-of-tree DOM nodes" | ||
--- | ||
|
||
## How to think about portals? | ||
|
||
Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component. | ||
`create_portal(child, host)` returns a `Html` value that renders `child` not hierarchically under its parent component, | ||
but as a child of the `host` element. | ||
|
||
## Usage | ||
|
||
Typical uses of portals can include modal dialogs and hovercards, as well as more technical applications such as controlling the contents of an element's [`shadowRoot`](https://developer.mozilla.org/en-US/docs/Web/API/Element/shadowRoot), appending stylesheets to the surrounding document's `<head>` and collecting referenced elements inside a central `<defs>` element of an `<svg>`. | ||
|
||
```rust | ||
use yew::{html, create_portal, function_component}; | ||
|
||
#[function_component(Modal)] | ||
fn modal() -> Html { | ||
let modal_host = gloo_utils::document() | ||
.get_element_by_id("modal_host") | ||
.expect("a #modal_host element"); | ||
|
||
create_portal( | ||
html! { | ||
<div>{"Modal dialog mounted into another element"}</div> | ||
}, | ||
modal_host.into(), | ||
) | ||
} | ||
``` | ||
|
||
## Further reading | ||
- [Portals example](https://github.com/yewstack/yew/tree/master/examples/portals) |
This file contains 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