rsx_usage.rs 9.7 KB

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