rsx_usage.rs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //! A tour of the rsx! macro
  2. //! ------------------------
  3. //!
  4. //! This example serves as an informal quick reference of all the things that the rsx! macro can do.
  5. //!
  6. //! A full in-depth reference guide is available at: https://www.notion.so/rsx-macro-basics-ef6e367dec124f4784e736d91b0d0b19
  7. //!
  8. //! ### Elements
  9. //! - Create any element from its tag
  10. //! - Accept compile-safe attributes for each tag
  11. //! - Display documentation for elements
  12. //! - Arguments instead of String
  13. //! - Text
  14. //! - Inline Styles
  15. //!
  16. //! ## General Concepts
  17. //! - Iterators
  18. //! - Keys
  19. //! - Match statements
  20. //! - Conditional Rendering
  21. //!
  22. //! ### Events
  23. //! - Handle events with the "onXYZ" syntax
  24. //! - Closures can capture their environment with the 'a lifetime
  25. //!
  26. //!
  27. //! ### Components
  28. //! - Components can be made by specifying the name
  29. //! - Components can be referenced by path
  30. //! - Components may have optional parameters
  31. //! - Components may have their properties specified by spread syntax
  32. //! - Components may accept child nodes
  33. //! - Components that accept "onXYZ" get those closures bump allocated
  34. //!
  35. //! ### Fragments
  36. //! - Allow fragments using the built-in `Fragment` component
  37. //! - Accept a list of vnodes as children for a Fragment component
  38. //! - Allow keyed fragments in iterators
  39. //! - Allow top-level fragments
  40. //!
  41. fn main() {
  42. dioxus::desktop::launch(EXAMPLE);
  43. }
  44. /// When trying to return "nothing" to Dioxus, you'll need to specify the type parameter or Rust will be sad.
  45. /// This type alias specifies the type for you so you don't need to write "None as Option<()>"
  46. const NONE_ELEMENT: Option<()> = None;
  47. use baller::Baller;
  48. use dioxus::prelude::*;
  49. pub static EXAMPLE: Component<()> = |cx| {
  50. let formatting = "formatting!";
  51. let formatting_tuple = ("a", "b");
  52. let lazy_fmt = format_args!("lazily formatted text");
  53. cx.render(rsx! {
  54. div {
  55. // Elements
  56. div {}
  57. h1 {"Some text"}
  58. h1 {"Some text with {formatting}"}
  59. h1 {"Formatting basic expressions {formatting_tuple.0} and {formatting_tuple.1}"}
  60. h2 {
  61. "Multiple"
  62. "Text"
  63. "Blocks"
  64. "Use comments as separators in html"
  65. }
  66. div {
  67. h1 {"multiple"}
  68. h2 {"nested"}
  69. h3 {"elements"}
  70. }
  71. div {
  72. class: "my special div"
  73. h1 {"Headers and attributes!"}
  74. }
  75. div {
  76. // pass simple rust expressions in
  77. class: lazy_fmt,
  78. id: format_args!("attributes can be passed lazily with std::fmt::Arguments"),
  79. div {
  80. class: {
  81. const WORD: &str = "expressions";
  82. format_args!("Arguments can be passed in through curly braces for complex {}", WORD)
  83. }
  84. }
  85. }
  86. // Expressions can be used in element position too:
  87. {rsx!(p { "More templating!" })}
  88. // {html!(<p>"Even HTML templating!!"</p>)}
  89. // Iterators
  90. {(0..10).map(|i| rsx!(li { "{i}" }))}
  91. {{
  92. let data = std::collections::HashMap::<&'static str, &'static str>::new();
  93. // Iterators *should* have keys when you can provide them.
  94. // Keys make your app run faster. Make sure your keys are stable, unique, and predictable.
  95. // Using an "ID" associated with your data is a good idea.
  96. data.into_iter().map(|(k, v)| rsx!(li { key: "{k}" "{v}" }))
  97. }}
  98. // Matching
  99. {match true {
  100. true => rsx!( h1 {"Top text"}),
  101. false => rsx!( h1 {"Bottom text"})
  102. }}
  103. // Conditional rendering
  104. // Dioxus conditional rendering is based around None/Some. We have no special syntax for conditionals.
  105. // You can convert a bool condition to rsx! with .then and .or
  106. {true.then(|| rsx!(div {}))}
  107. // True conditions need to be rendered (same reasons as matching)
  108. {if true {
  109. rsx!(cx, h1 {"Top text"})
  110. } else {
  111. rsx!(cx, h1 {"Bottom text"})
  112. }}
  113. // returning "None" is a bit noisy... but rare in practice
  114. {None as Option<()>}
  115. // Use the Dioxus type-alias for less noise
  116. {NONE_ELEMENT}
  117. // can also just use empty fragments
  118. Fragment {}
  119. // Fragments let you insert groups of nodes without a parent.
  120. // This lets you make components that insert elements as siblings without a container.
  121. div {"A"}
  122. Fragment {
  123. div {"B"}
  124. div {"C"}
  125. Fragment {
  126. "D"
  127. Fragment {
  128. "heavily nested fragments is an antipattern"
  129. "they cause Dioxus to do unnecessary work"
  130. "don't use them carelessly if you can help it"
  131. }
  132. }
  133. }
  134. // Components
  135. // Can accept any paths
  136. // Notice how you still get syntax highlighting and IDE support :)
  137. Baller {}
  138. baller::Baller { }
  139. crate::baller::Baller {}
  140. // Can take properties
  141. Taller { a: "asd" }
  142. // Can take optional properties
  143. Taller { a: "asd" }
  144. // Can pass in props directly as an expression
  145. {{
  146. let props = TallerProps {a: "hello", children: Default::default()};
  147. rsx!(Taller { ..props })
  148. }}
  149. // Spreading can also be overridden manually
  150. Taller {
  151. ..TallerProps { a: "ballin!", children: Default::default() }
  152. a: "not ballin!"
  153. }
  154. // Can take children too!
  155. Taller { a: "asd", div {"hello world!"} }
  156. // helper functions
  157. {helper(&cx, "hello world!")}
  158. }
  159. })
  160. };
  161. fn helper<'a>(cx: &'a ScopeState, text: &str) -> Element<'a> {
  162. rsx!(cx, p { "{text}" })
  163. }
  164. mod baller {
  165. use super::*;
  166. #[derive(Props, PartialEq)]
  167. pub struct BallerProps {}
  168. /// This component totally balls
  169. pub fn Baller(_: Scope<BallerProps>) -> Element {
  170. todo!()
  171. }
  172. }
  173. #[derive(Props)]
  174. pub struct TallerProps<'a> {
  175. a: &'static str,
  176. children: Element<'a>,
  177. }
  178. /// This component is taller than most :)
  179. pub fn Taller<'a>(_: Scope<'a, TallerProps<'a>>) -> Element {
  180. let b = true;
  181. todo!()
  182. }