perspective_viewer/ui/
style.rs1use wasm_bindgen::{JsCast, JsValue};
14use yew::prelude::*;
15
16#[derive(Properties, PartialEq)]
17pub struct StyleProviderProps {
18 pub root: web_sys::HtmlElement,
19 #[prop_or(true)]
20 pub is_shadow: bool,
21 pub sheet: web_sys::CssStyleSheet,
22 pub children: Children,
23}
24
25pub struct StyleProvider;
26
27impl Component for StyleProvider {
28 type Message = ();
29 type Properties = StyleProviderProps;
30
31 fn create(ctx: &Context<Self>) -> Self {
32 adopt_sheet(ctx.props());
33 Self
34 }
35
36 fn view(&self, ctx: &Context<Self>) -> Html {
37 html! { <>{ for ctx.props().children.iter() }</> }
38 }
39}
40
41fn adopt_sheet(props: &StyleProviderProps) {
42 let root: JsValue = if props.is_shadow {
43 props.root.shadow_root().unwrap().into()
44 } else {
45 web_sys::window().unwrap().document().unwrap().into()
46 };
47
48 let sheets = js_sys::Reflect::get(&root, &"adoptedStyleSheets".into())
49 .unwrap()
50 .unchecked_into::<js_sys::Array>();
51
52 let sheet_val: &JsValue = props.sheet.as_ref();
53 if sheets.index_of(sheet_val, 0) < 0 {
54 sheets.push(sheet_val);
55 }
56}