use web_sys::HtmlStyleElement;
use yew::prelude::*;
use yew::virtual_dom::VNode;
use super::style_cache::StyleCache;
use crate::*;
#[derive(Properties, PartialEq)]
pub struct StyleProviderProps {
pub children: Children,
}
pub struct StyleProvider {
cache: StyleCache,
}
impl Component for StyleProvider {
type Message = ();
type Properties = StyleProviderProps;
fn create(_ctx: &Context<Self>) -> Self {
let cache = StyleCache::default();
Self { cache }
}
fn view(&self, ctx: &Context<Self>) -> Html {
let styles = self.cache.iter_styles();
html_template! {
{
for styles.map(|x| {
html! {
<StyleKeyed key={ x.0 } elem={ x.1 } />
}
})
}
<ContextProvider<StyleCache> context={ self.cache.clone() }>
{ for ctx.props().children.iter() }
</ContextProvider<StyleCache>>
}
}
}
#[derive(Properties, PartialEq)]
struct StyleKeyedProps {
elem: HtmlStyleElement,
}
#[function_component(StyleKeyed)]
fn style_renderer(props: &StyleKeyedProps) -> Html {
VNode::VRef(props.elem.clone().into())
}