rsx_overview.rs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. pub fn Empty(cx: Scope) -> Element {
  19. // ANCHOR: empty
  20. cx.render(rsx!(div {}))
  21. // ANCHOR_END: empty
  22. }
  23. pub fn Children(cx: Scope) -> Element {
  24. // ANCHOR: children
  25. cx.render(rsx!(ol {
  26. li {"First Item"}
  27. li {"Second Item"}
  28. li {"Third Item"}
  29. }))
  30. // ANCHOR_END: children
  31. }
  32. pub fn Fragments(cx: Scope) -> Element {
  33. // ANCHOR: fragments
  34. cx.render(rsx!(
  35. p {"First Item"},
  36. p {"Second Item"},
  37. Fragment {
  38. span { "a group" },
  39. span { "of three" },
  40. span { "items" },
  41. }
  42. ))
  43. // ANCHOR_END: fragments
  44. }
  45. pub fn Attributes(cx: Scope) -> Element {
  46. // ANCHOR: attributes
  47. cx.render(rsx!(a {
  48. href: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
  49. class: "primary_button",
  50. "Log In"
  51. }))
  52. // ANCHOR_END: attributes
  53. }
  54. pub fn VariableAttributes(cx: Scope) -> Element {
  55. // ANCHOR: variable_attributes
  56. let written_in_rust = true;
  57. let button_type = "button";
  58. cx.render(rsx!(button {
  59. disabled: "{written_in_rust}",
  60. class: "{button_type}",
  61. "Rewrite it in rust"
  62. }))
  63. // ANCHOR_END: variable_attributes
  64. }
  65. pub fn CustomAttributes(cx: Scope) -> Element {
  66. // ANCHOR: custom_attributes
  67. cx.render(rsx!(b {
  68. "customAttribute": "value",
  69. "Rust is Cool"
  70. }))
  71. // ANCHOR_END: custom_attributes
  72. }
  73. pub fn Formatting(cx: Scope) -> Element {
  74. // ANCHOR: formatting
  75. let coordinates = (42, 0);
  76. let country = "es";
  77. cx.render(rsx!(div {
  78. class: "country-{country}",
  79. "Coordinates: {coordinates:?}",
  80. // arbitrary expressions are allowed,
  81. // as long as they don't contain `{}`
  82. div {
  83. "{country.to_uppercase()}"
  84. },
  85. div {
  86. "{7*6}"
  87. },
  88. }))
  89. // ANCHOR_END: formatting
  90. }
  91. pub fn Expression(cx: Scope) -> Element {
  92. // ANCHOR: expression
  93. let text = "Dioxus";
  94. cx.render(rsx!(span {
  95. [text.to_uppercase()]
  96. }))
  97. // ANCHOR_END: expression
  98. }