1
0

readme_expanded.rs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. use dioxus::prelude::*;
  2. fn main() {
  3. dioxus_desktop::launch(app);
  4. }
  5. fn app(cx: Scope) -> Element {
  6. let mut count = use_state(cx, || 0);
  7. cx.render(
  8. // rsx expands to LazyNodes::new
  9. ::dioxus::core::LazyNodes::new(
  10. move |__cx: &::dioxus::core::ScopeState| -> ::dioxus::core::VNode {
  11. // The template is every static part of the rsx
  12. static TEMPLATE: ::dioxus::core::Template = ::dioxus::core::Template {
  13. // This is the source location of the rsx that generated this template. This is used to make hot rsx reloading work. Hot rsx reloading just replaces the template with a new one generated from the rsx by the CLI.
  14. name: "examples\\readme.rs:14:15:250",
  15. // The root nodes are the top level nodes of the rsx
  16. roots: &[
  17. // The h1 node
  18. ::dioxus::core::TemplateNode::Element {
  19. // Find the built in h1 tag in the dioxus_elements crate exported by the dioxus html crate
  20. tag: dioxus_elements::h1::TAG_NAME,
  21. namespace: dioxus_elements::h1::NAME_SPACE,
  22. attrs: &[],
  23. // The children of the h1 node
  24. children: &[
  25. // The dynamic count text node
  26. // Any nodes that are dynamic have a dynamic placeholder with a unique index
  27. ::dioxus::core::TemplateNode::DynamicText {
  28. // This index is used to find what element in `dynamic_nodes` to use instead of the placeholder
  29. id: 0usize,
  30. },
  31. ],
  32. },
  33. // The up high button node
  34. ::dioxus::core::TemplateNode::Element {
  35. tag: dioxus_elements::button::TAG_NAME,
  36. namespace: dioxus_elements::button::NAME_SPACE,
  37. attrs: &[
  38. // The dynamic onclick listener attribute
  39. // Any attributes that are dynamic have a dynamic placeholder with a unique index.
  40. ::dioxus::core::TemplateAttribute::Dynamic {
  41. // Similar to dynamic nodes, dynamic attributes have a unique index used to find the attribute in `dynamic_attrs` to use instead of the placeholder
  42. id: 0usize,
  43. },
  44. ],
  45. children: &[::dioxus::core::TemplateNode::Text { text: "Up high!" }],
  46. },
  47. // The down low button node
  48. ::dioxus::core::TemplateNode::Element {
  49. tag: dioxus_elements::button::TAG_NAME,
  50. namespace: dioxus_elements::button::NAME_SPACE,
  51. attrs: &[
  52. // The dynamic onclick listener attribute
  53. ::dioxus::core::TemplateAttribute::Dynamic { id: 1usize },
  54. ],
  55. children: &[::dioxus::core::TemplateNode::Text { text: "Down low!" }],
  56. },
  57. ],
  58. // Node paths is a list of paths to every dynamic node in the rsx
  59. node_paths: &[
  60. // The first node path is the path to the dynamic node with an id of 0 (the count text node)
  61. &[
  62. // Go to the index 0 root node
  63. 0u8,
  64. //
  65. // Go to the first child of the root node
  66. 0u8,
  67. ],
  68. ],
  69. // Attr paths is a list of paths to every dynamic attribute in the rsx
  70. attr_paths: &[
  71. // The first attr path is the path to the dynamic attribute with an id of 0 (the up high button onclick listener)
  72. &[
  73. // Go to the index 1 root node
  74. 1u8,
  75. ],
  76. // The second attr path is the path to the dynamic attribute with an id of 1 (the down low button onclick listener)
  77. &[
  78. // Go to the index 2 root node
  79. 2u8,
  80. ],
  81. ],
  82. };
  83. // The VNode is a reference to the template with the dynamic parts of the rsx
  84. ::dioxus::core::VNode {
  85. parent: None,
  86. key: None,
  87. // The static template this node will use. The template is stored in a Cell so it can be replaced with a new template when hot rsx reloading is enabled
  88. template: std::cell::Cell::new(TEMPLATE),
  89. root_ids: Default::default(),
  90. dynamic_nodes: __cx.bump().alloc([
  91. // The dynamic count text node (dynamic node id 0)
  92. __cx.text_node(format_args!("High-Five counter: {0}", count)),
  93. ]),
  94. dynamic_attrs: __cx.bump().alloc([
  95. // The dynamic up high button onclick listener (dynamic attribute id 0)
  96. dioxus_elements::events::onclick(__cx, move |_| count += 1),
  97. // The dynamic down low button onclick listener (dynamic attribute id 1)
  98. dioxus_elements::events::onclick(__cx, move |_| count -= 1),
  99. ]),
  100. }
  101. },
  102. ),
  103. )
  104. }