perspective_viewer/components/style/
style_provider.rs

1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
5// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13use web_sys::HtmlStyleElement;
14use yew::prelude::*;
15use yew::virtual_dom::VNode;
16
17use super::style_cache::StyleCache;
18
19#[derive(Properties, PartialEq)]
20pub struct StyleProviderProps {
21    pub root: web_sys::HtmlElement,
22    #[prop_or(true)]
23    pub is_shadow: bool,
24    pub children: Children,
25}
26
27/// A context which injects any CSS snippet registered within its tree, doing
28/// so only once for each unqiue snippet name.
29///
30/// CSS can be registered within sub-components via the `<LocalStyle>` component
31/// and `css!()` resource inlining macro.
32pub struct StyleProvider {
33    cache: StyleCache,
34}
35
36impl Component for StyleProvider {
37    type Message = ();
38    type Properties = StyleProviderProps;
39
40    fn create(ctx: &Context<Self>) -> Self {
41        let cache = StyleCache::new(ctx.props().is_shadow, &ctx.props().root);
42        Self { cache }
43    }
44
45    fn view(&self, ctx: &Context<Self>) -> Html {
46        html! {
47            <ContextProvider<StyleCache> context={self.cache.clone()}>
48                { for ctx.props().children.iter() }
49            </ContextProvider<StyleCache>>
50        }
51    }
52}
53
54#[derive(Properties, PartialEq)]
55struct StyleKeyedProps {
56    elem: HtmlStyleElement,
57}
58
59/// Necessary only to attach `key` to individual `HtmlStylElement` children,
60/// as `yew` does not calculate list updates correctly for sequences of these
61/// without keys.
62#[function_component(StyleKeyed)]
63fn style_renderer(props: &StyleKeyedProps) -> Html {
64    VNode::VRef(props.elem.clone().into())
65}