-
-
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.
* initial poc implementation of portals * add new_before * add portals example * add shadow dom example * add english website documentation
- Loading branch information
1 parent
a5c343d
commit aa9b9b2
Showing
14 changed files
with
411 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[package] | ||
name = "portals" | ||
version = "0.1.0" | ||
authors = ["Martin Molzer <[email protected]>"] | ||
edition = "2018" | ||
license = "MIT OR Apache-2.0" | ||
|
||
[dependencies] | ||
yew = { path = "../../packages/yew" } | ||
gloo-utils = "0.1" | ||
wasm-bindgen = "0.2" | ||
|
||
[dependencies.web-sys] | ||
version = "0.3" | ||
features = [ | ||
"Document", | ||
"Element", | ||
"Node", | ||
"HtmlHeadElement", | ||
"ShadowRoot", | ||
"ShadowRootInit", | ||
"ShadowRootMode", | ||
] |
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,10 @@ | ||
# Portals Example | ||
|
||
[![Demo](https://img.shields.io/website?label=demo&url=https%3A%2F%2Fexamples.yew.rs%2Fportals)](https://examples.yew.rs/portals) | ||
|
||
This example renders elements into out-of-tree nodes with the help of portals. | ||
|
||
## Concepts | ||
|
||
- Manually creating `Html` without the `html!` macro. | ||
- Using `web-sys` to manipulate the DOM. |
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,9 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Yew • Portals</title> | ||
</head> | ||
|
||
<body></body> | ||
</html> |
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,106 @@ | ||
use wasm_bindgen::JsCast; | ||
use web_sys::{Element, ShadowRootInit, ShadowRootMode}; | ||
use yew::{create_portal, html, Children, Component, Context, Html, NodeRef, Properties}; | ||
|
||
#[derive(Properties, PartialEq)] | ||
pub struct ShadowDOMProps { | ||
#[prop_or_default] | ||
pub children: Children, | ||
} | ||
|
||
pub struct ShadowDOMHost { | ||
host_ref: NodeRef, | ||
inner_host: Option<Element>, | ||
} | ||
|
||
impl Component for ShadowDOMHost { | ||
type Message = (); | ||
type Properties = ShadowDOMProps; | ||
|
||
fn create(_: &Context<Self>) -> Self { | ||
Self { | ||
host_ref: NodeRef::default(), | ||
inner_host: None, | ||
} | ||
} | ||
|
||
fn rendered(&mut self, ctx: &Context<Self>, first_render: bool) { | ||
if first_render { | ||
let shadow_root = self | ||
.host_ref | ||
.get() | ||
.expect("rendered host") | ||
.unchecked_into::<Element>() | ||
.attach_shadow(&ShadowRootInit::new(ShadowRootMode::Closed)) | ||
.expect("installing shadow root succeeds"); | ||
let inner_host = gloo_utils::document() | ||
.create_element("div") | ||
.expect("can create inner wrapper"); | ||
shadow_root | ||
.append_child(&inner_host) | ||
.expect("can attach inner host"); | ||
self.inner_host = Some(inner_host); | ||
ctx.link().send_message(()); | ||
} | ||
} | ||
|
||
fn update(&mut self, _: &Context<Self>, _: Self::Message) -> bool { | ||
true | ||
} | ||
|
||
fn view(&self, ctx: &Context<Self>) -> Html { | ||
let contents = if let Some(ref inner_host) = self.inner_host { | ||
create_portal( | ||
html! { | ||
{for ctx.props().children.iter()} | ||
}, | ||
inner_host.clone(), | ||
) | ||
} else { | ||
html! { <></> } | ||
}; | ||
html! { | ||
<div ref={self.host_ref.clone()}> | ||
{contents} | ||
</div> | ||
} | ||
} | ||
} | ||
|
||
pub struct Model { | ||
pub style_html: Html, | ||
} | ||
|
||
impl Component for Model { | ||
type Message = (); | ||
type Properties = (); | ||
|
||
fn create(_ctx: &Context<Self>) -> Self { | ||
let document_head = gloo_utils::document() | ||
.head() | ||
.expect("head element to be present"); | ||
let style_html = create_portal( | ||
html! { | ||
<style>{"p { color: red; }"}</style> | ||
}, | ||
document_head.into(), | ||
); | ||
Self { style_html } | ||
} | ||
|
||
fn view(&self, _ctx: &Context<Self>) -> Html { | ||
html! { | ||
<> | ||
{self.style_html.clone()} | ||
<p>{"This paragraph is colored red, and its style is mounted into "}<pre>{"document.head"}</pre>{" with a portal"}</p> | ||
<ShadowDOMHost> | ||
<p>{"This paragraph is rendered in a shadow dom and thus not affected by the surrounding styling context"}</p> | ||
</ShadowDOMHost> | ||
</> | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
yew::start_app::<Model>(); | ||
} |
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
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
Oops, something went wrong.