consecuit_html/misc_components/
text_node.rs1use web_sys::{window, Text};
2
3use consecuit::prelude::*;
4
5pub fn text_node(cc: ComponentBuilder, value: impl AsRef<str>) -> impl ComponentReturn {
6 let cc = cc.init();
7 let parent = cc.get_parent_node();
8 let (cc, store): (_, Reference<Option<Text>>) = cc.hook(use_ref, ());
9 let text = store
10 .visit_mut_with(|opt| {
11 let text = opt.get_or_insert_with(|| {
12 let document = window().unwrap().document().unwrap();
13 let text = document.create_text_node(value.as_ref());
14 parent.append_child(&text).unwrap();
15 text
16 });
17 text.clone()
18 })
19 .unwrap();
20 text.set_node_value(Some(value.as_ref()));
21 cc.bare_leaf_node()
22}