1use stardom_macros::create_tagged_macros;
2
3#[macro_export]
4macro_rules! text_node {
5 ($content:expr $(;* $parent:expr)?) => {{
6 let text = stardom_nodes::Node::text();
7
8 let t = ::std::clone::Clone::clone(&text);
9 stardom_reactive::effect(move || {
10 let content = ::std::string::ToString::to_string(&$content);
11 stardom_nodes::Node::set_text(&t, &content);
12 });
13
14 $(stardom_nodes::Node::insert($parent, &text, None);)?
15 text
16 }};
17}
18
19#[macro_export]
20macro_rules! raw_node {
21 ($content:expr $(;* $parent:expr)?) => {{
22 let raw = stardom_nodes::Node::raw();
23
24 let r = ::std::clone::Clone::clone(&raw);
25 stardom_reactive::effect(move || {
26 let content = ::std::string::ToString::to_string(&$content);
27 stardom_nodes::Node::set_text(&r, &content);
28 });
29
30 $(stardom_nodes::Node::insert($parent, &raw, None);)?
31 raw
32 }}
33}
34
35#[macro_export]
36macro_rules! element {
37 ($name:expr, $($body:tt)*) => {{
38 let name = ::std::string::ToString::to_string(&$name);
39 let element = stardom_nodes::Node::element(None, &name);
40
41 let parent = stardom_macros::node_body!(&element, $($body)*);
42 if let Some(parent) = parent {
43 stardom_nodes::Node::insert(parent, &element, None);
44 }
45
46 element
47 }};
48}
49
50#[macro_export]
51macro_rules! fragment {
52 ($($body:tt)*) => {{
53 let fragment = stardom_nodes::Node::fragment();
54
55 let parent = stardom_macros::node_body!(&fragment, $($body)*);
56 if let Some(parent) = parent {
57 stardom_nodes::Node::insert(parent, &fragment, None);
58 }
59
60 fragment
61 }};
62}
63
64create_tagged_macros!(stardom_nodes::element);
65
66#[macro_export]
67macro_rules! show {
68 ($condition:expr, { $($body:tt)* } ;* $parent:expr) => {{
69 let fragment = stardom_nodes::Node::fragment();
70 let child = stardom_nodes::Node::fragment();
71
72 let _: Option<()> = stardom_macros::node_body!(&child, $($body)*);
73
74 let f = ::std::clone::Clone::clone(&fragment);
75 let last = ::std::cell::Cell::new(false);
76 stardom_reactive::effect(move || {
77 let now = $condition;
78 if now == last.get() {
79 return;
80 } else {
81 last.set(now);
82 }
83
84 if $condition {
85 stardom_nodes::Node::insert(&f, &child, None);
86 } else {
87 stardom_nodes::Node::remove(&f, &child);
88 }
89 });
90
91 stardom_nodes::Node::insert($parent, &fragment, None);
92 }};
93}