raw_html.rs (1165B)
1 use web_sys::Element; 2 use yew::{html, Component, ComponentLink, Html, NodeRef, Properties, ShouldRender}; 3 4 #[derive(Debug, Clone, Eq, PartialEq, Properties)] 5 pub struct RawHTMLProps { 6 pub inner_html: String, 7 } 8 9 pub struct RawHTML { 10 props: RawHTMLProps, 11 node_ref: NodeRef, 12 } 13 14 impl Component for RawHTML { 15 type Message = (); 16 type Properties = RawHTMLProps; 17 18 fn create(props: Self::Properties, _: ComponentLink<Self>) -> Self { 19 Self { 20 props, 21 node_ref: NodeRef::default(), 22 } 23 } 24 25 fn update(&mut self, _: Self::Message) -> ShouldRender { 26 true 27 } 28 29 fn change(&mut self, props: Self::Properties) -> ShouldRender { 30 if self.props != props { 31 self.props = props; 32 true 33 } else { 34 false 35 } 36 } 37 38 fn view(&self) -> Html { 39 // create the parent element and store a reference to it 40 html! { 41 <div ref=self.node_ref.clone()/> 42 } 43 } 44 45 fn rendered(&mut self, _first_render: bool) { 46 let el = self.node_ref.cast::<Element>().unwrap(); 47 el.set_inner_html(&self.props.inner_html); 48 } 49 }