rsx_usage.rs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 'static 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. fn main() {
  41. dioxus::launch(app)
  42. }
  43. use core::{fmt, str::FromStr};
  44. use std::fmt::Display;
  45. use baller::Baller;
  46. use dioxus::prelude::*;
  47. fn app() -> Element {
  48. let formatting = "formatting!";
  49. let formatting_tuple = ("a", "b");
  50. let lazy_fmt = format_args!("lazily formatted text");
  51. let asd = 123;
  52. rsx! {
  53. div {
  54. // Elements
  55. div {}
  56. h1 {"Some text"}
  57. h1 {"Some text with {formatting}"}
  58. h1 {"Formatting basic expressions {formatting_tuple.0} and {formatting_tuple.1}"}
  59. h1 {"Formatting without interpolation " {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. class: "asd",
  80. class: "{asd}",
  81. // if statements can be used to conditionally render attributes
  82. class: if formatting.contains("form") { "{asd}" },
  83. // longer if chains also work
  84. class: if formatting.contains("form") { "{asd}" } else if formatting.contains("my other form") { "{asd}" },
  85. class: if formatting.contains("form") { "{asd}" } else if formatting.contains("my other form") { "{asd}" } else { "{asd}" },
  86. div {
  87. class: {
  88. const WORD: &str = "expressions";
  89. format_args!("Arguments can be passed in through curly braces for complex {WORD}")
  90. }
  91. }
  92. }
  93. // dangerous_inner_html for both html and svg
  94. div { dangerous_inner_html: "<p>hello dangerous inner html</p>" }
  95. svg { dangerous_inner_html: "<circle r='50' cx='50' cy='50' />" }
  96. // Built-in idents can be used
  97. use {}
  98. link {
  99. as: "asd"
  100. }
  101. // Expressions can be used in element position too:
  102. {rsx!(p { "More templating!" })}
  103. // Iterators
  104. {(0..10).map(|i| rsx!(li { "{i}" }))}
  105. // Iterators within expressions
  106. {
  107. let data = std::collections::HashMap::<&'static str, &'static str>::new();
  108. // Iterators *should* have keys when you can provide them.
  109. // Keys make your app run faster. Make sure your keys are stable, unique, and predictable.
  110. // Using an "ID" associated with your data is a good idea.
  111. data.into_iter().map(|(k, v)| rsx!(li { key: "{k}", "{v}" }))
  112. }
  113. // Matching
  114. match true {
  115. true => rsx!( h1 {"Top text"}),
  116. false => rsx!( h1 {"Bottom text"})
  117. }
  118. // Conditional rendering
  119. // Dioxus conditional rendering is based around None/Some. We have no special syntax for conditionals.
  120. // You can convert a bool condition to rsx! with .then and .or
  121. {true.then(|| rsx!(div {}))}
  122. // Alternatively, you can use the "if" syntax - but both branches must be resolve to Element
  123. if false {
  124. h1 {"Top text"}
  125. } else {
  126. h1 {"Bottom text"}
  127. }
  128. // Using optionals for diverging branches
  129. // Note that since this is wrapped in curlies, it's interpreted as an expression
  130. {if true {
  131. Some(rsx!(h1 {"Top text"}))
  132. } else {
  133. None
  134. }}
  135. // returning "None" without a diverging branch is a bit noisy... but rare in practice
  136. {None as Option<()>}
  137. // can also just use empty fragments
  138. Fragment {}
  139. // Fragments let you insert groups of nodes without a parent.
  140. // This lets you make components that insert elements as siblings without a container.
  141. div {"A"}
  142. Fragment {
  143. div {"B"}
  144. div {"C"}
  145. Fragment {
  146. "D"
  147. Fragment {
  148. "E"
  149. "F"
  150. }
  151. }
  152. }
  153. // Components
  154. // Can accept any paths
  155. // Notice how you still get syntax highlighting and IDE support :)
  156. Baller {}
  157. baller::Baller {}
  158. crate::baller::Baller {}
  159. // Can take properties
  160. Taller { a: "asd" }
  161. // Can take optional properties
  162. Taller { a: "asd" }
  163. // Can pass in props directly as an expression
  164. {
  165. let props = TallerProps {a: "hello", children: VNode::empty() };
  166. rsx!(Taller { ..props })
  167. }
  168. // Spreading can also be overridden manually
  169. Taller {
  170. a: "not ballin!",
  171. ..TallerProps { a: "ballin!", children: VNode::empty() }
  172. }
  173. // Can take children too!
  174. Taller { a: "asd", div {"hello world!"} }
  175. // This component's props are defined *inline* with the `component` macro
  176. WithInline { text: "using functionc all syntax" }
  177. // Components can be generic too
  178. // This component takes i32 type to give you typed input
  179. TypedInput::<i32> {}
  180. // Type inference can be used too
  181. TypedInput { initial: 10.0 }
  182. // generic with the `component` macro
  183. Label { text: "hello generic world!" }
  184. Label { text: 99.9 }
  185. // Lowercase components work too, as long as they are access using a path
  186. baller::lowercase_component {}
  187. // For in-scope lowercase components, use the `self` keyword
  188. self::lowercase_helper {}
  189. // helper functions
  190. // Anything that implements IntoVnode can be dropped directly into Rsx
  191. {helper("hello world!")}
  192. // Strings can be supplied directly
  193. {String::from("Hello world!")}
  194. // So can format_args
  195. {format_args!("Hello {}!", "world")}
  196. // Or we can shell out to a helper function
  197. {format_dollars(10, 50)}
  198. }
  199. }
  200. }
  201. fn format_dollars(dollars: u32, cents: u32) -> String {
  202. format!("${dollars}.{cents:02}")
  203. }
  204. fn helper(text: &str) -> Element {
  205. rsx! {
  206. p { "{text}" }
  207. }
  208. }
  209. // no_case_check disables PascalCase checking if you *really* want a snake_case component.
  210. // This will likely be deprecated/removed in a future update that will introduce a more polished linting system,
  211. // something like Clippy.
  212. #[component(no_case_check)]
  213. fn lowercase_helper() -> Element {
  214. rsx! {
  215. "asd"
  216. }
  217. }
  218. mod baller {
  219. use super::*;
  220. #[component]
  221. /// This component totally balls
  222. pub fn Baller() -> Element {
  223. rsx! { "ballin'" }
  224. }
  225. // no_case_check disables PascalCase checking if you *really* want a snake_case component.
  226. // This will likely be deprecated/removed in a future update that will introduce a more polished linting system,
  227. // something like Clippy.
  228. #[component(no_case_check)]
  229. pub fn lowercase_component() -> Element {
  230. rsx! { "look ma, no uppercase" }
  231. }
  232. }
  233. /// Documentation for this component is visible within the rsx macro
  234. #[component]
  235. pub fn Taller(
  236. /// Fields are documented and accessible in rsx!
  237. a: &'static str,
  238. children: Element,
  239. ) -> Element {
  240. rsx! { {&children} }
  241. }
  242. #[derive(Props, Clone, PartialEq, Eq)]
  243. pub struct TypedInputProps<T: 'static + Clone + PartialEq> {
  244. #[props(optional, default)]
  245. initial: Option<T>,
  246. }
  247. #[allow(non_snake_case)]
  248. pub fn TypedInput<T>(props: TypedInputProps<T>) -> Element
  249. where
  250. T: FromStr + fmt::Display + PartialEq + Clone + 'static,
  251. <T as FromStr>::Err: std::fmt::Display,
  252. {
  253. if let Some(props) = props.initial {
  254. return rsx! { "{props}" };
  255. }
  256. VNode::empty()
  257. }
  258. #[component]
  259. fn WithInline(text: String) -> Element {
  260. rsx! {
  261. p { "{text}" }
  262. }
  263. }
  264. #[component]
  265. fn Label<T: Clone + PartialEq + Display + 'static>(text: T) -> Element {
  266. rsx! {
  267. p { "{text}" }
  268. }
  269. }