rsx_overview.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #![allow(non_snake_case)]
  2. use dioxus::prelude::*;
  3. fn main() {
  4. dioxus_desktop::launch(App);
  5. }
  6. pub fn App(cx: Scope) -> Element {
  7. cx.render(rsx!(
  8. Empty {},
  9. Children {},
  10. Fragments {},
  11. Attributes {},
  12. VariableAttributes {},
  13. CustomAttributes {},
  14. Formatting {},
  15. Expression {},
  16. ))
  17. }
  18. #[rustfmt::skip]
  19. pub fn Empty(cx: Scope) -> Element {
  20. // ANCHOR: empty
  21. cx.render(rsx!(div {
  22. // attributes / listeners
  23. // children
  24. }))
  25. // ANCHOR_END: empty
  26. }
  27. #[rustfmt::skip]
  28. pub fn Children(cx: Scope) -> Element {
  29. // ANCHOR: children
  30. cx.render(rsx!(ol {
  31. li {"First Item"}
  32. li {"Second Item"}
  33. li {"Third Item"}
  34. }))
  35. // ANCHOR_END: children
  36. }
  37. #[rustfmt::skip]
  38. pub fn Fragments(cx: Scope) -> Element {
  39. // ANCHOR: fragments
  40. cx.render(rsx!(
  41. p {"First Item"},
  42. p {"Second Item"},
  43. Fragment {
  44. span { "a group" },
  45. span { "of three" },
  46. span { "items" },
  47. }
  48. ))
  49. // ANCHOR_END: fragments
  50. }
  51. #[rustfmt::skip]
  52. pub fn ManyRoots(cx: Scope) -> Element {
  53. // ANCHOR: manyroots
  54. cx.render(rsx!(
  55. p {"First Item"},
  56. p {"Second Item"},
  57. ))
  58. // ANCHOR_END: manyroots
  59. }
  60. #[rustfmt::skip]
  61. pub fn Attributes(cx: Scope) -> Element {
  62. // ANCHOR: attributes
  63. cx.render(rsx!(a {
  64. href: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
  65. class: "primary_button",
  66. color: "red",
  67. }))
  68. // ANCHOR_END: attributes
  69. }
  70. #[rustfmt::skip]
  71. pub fn VariableAttributes(cx: Scope) -> Element {
  72. // ANCHOR: variable_attributes
  73. let written_in_rust = true;
  74. let button_type = "button";
  75. cx.render(rsx!(button {
  76. disabled: "{written_in_rust}",
  77. class: "{button_type}",
  78. "Rewrite it in rust"
  79. }))
  80. // ANCHOR_END: variable_attributes
  81. }
  82. #[rustfmt::skip]
  83. pub fn CustomAttributes(cx: Scope) -> Element {
  84. // ANCHOR: custom_attributes
  85. cx.render(rsx!(b {
  86. "customAttribute": "value",
  87. }))
  88. // ANCHOR_END: custom_attributes
  89. }
  90. #[rustfmt::skip]
  91. pub fn Formatting(cx: Scope) -> Element {
  92. // ANCHOR: formatting
  93. let coordinates = (42, 0);
  94. let country = "es";
  95. cx.render(rsx!(div {
  96. class: "country-{country}",
  97. "position": "{coordinates:?}",
  98. // arbitrary expressions are allowed,
  99. // as long as they don't contain `{}`
  100. div {
  101. "{country.to_uppercase()}"
  102. },
  103. div {
  104. "{7*6}"
  105. },
  106. // {} can be escaped with {{}}
  107. div {
  108. "{{}}"
  109. },
  110. }))
  111. // ANCHOR_END: formatting
  112. }
  113. #[rustfmt::skip]
  114. pub fn Expression(cx: Scope) -> Element {
  115. // ANCHOR: expression
  116. let text = "Dioxus";
  117. cx.render(rsx!(span {
  118. text.to_uppercase(),
  119. // create a list of text from 0 to 9
  120. (0..10).map(|i| rsx!{ i.to_string() })
  121. }))
  122. // ANCHOR_END: expression
  123. }
  124. #[rustfmt::skip]
  125. pub fn Loops(cx: Scope) -> Element {
  126. // ANCHOR: loops
  127. cx.render(rsx!{
  128. // use a for loop where the body itself is RSX
  129. div {
  130. // create a list of text from 0 to 9
  131. for i in 0..3 {
  132. // NOTE: the body of the loop is RSX not a rust statement
  133. div {
  134. "{i}"
  135. }
  136. }
  137. }
  138. // iterator equivalent
  139. div {
  140. (0..3).map(|i| rsx!{ div { "{i}" } })
  141. }
  142. })
  143. // ANCHOR_END: loops
  144. }
  145. #[rustfmt::skip]
  146. pub fn IfStatements(cx: Scope) -> Element {
  147. // ANCHOR: ifstatements
  148. cx.render(rsx!{
  149. // use if statements without an else
  150. if true {
  151. rsx!(div { "true" })
  152. }
  153. })
  154. // ANCHOR_END: ifstatements
  155. }