main.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #![allow(
  2. unused,
  3. dead_code,
  4. non_upper_case_globals,
  5. non_camel_case_types,
  6. non_snake_case
  7. )]
  8. mod antipatterns;
  9. mod basics;
  10. mod children;
  11. mod conditional_rendering;
  12. mod controlled_inputs;
  13. mod custom_elements;
  14. mod empty;
  15. mod fragments;
  16. mod global_css;
  17. mod inline_styles;
  18. mod iterators;
  19. mod listener;
  20. mod memo;
  21. mod noderefs;
  22. mod signals;
  23. mod spreadpattern;
  24. mod statemanagement;
  25. mod suspense;
  26. mod task;
  27. mod testing;
  28. mod tostring;
  29. fn main() {}
  30. use std::rc::Rc;
  31. use dioxus::prelude::*;
  32. static App: FC<()> = |cx| {
  33. let (selection, set_selection) = use_state(cx, || None as Option<usize>).classic();
  34. let body = match selection {
  35. Some(id) => rsx!(in cx, ReferenceItem { selected: *id }),
  36. None => rsx!(in cx, div { "Select an concept to explore" }),
  37. };
  38. cx.render(rsx! {
  39. div {
  40. ScrollSelector { onselect: move |id| set_selection(id) }
  41. {body}
  42. }
  43. })
  44. };
  45. // this is its own component to stay memoized
  46. #[derive(Props)]
  47. struct ScrollSelectorProps<'a> {
  48. onselect: &'a dyn Fn(Option<usize>),
  49. }
  50. fn ScrollSelector<'a>(cx: Context<'a, ScrollSelectorProps>) -> VNode<'a> {
  51. let selection_list = (&REFERENCES).iter().enumerate().map(|(id, _)| {
  52. rsx! {
  53. li {
  54. h3 {}
  55. }
  56. }
  57. });
  58. cx.render(rsx! {
  59. div {
  60. h1 {""}
  61. ul {
  62. {selection_list}
  63. button {
  64. onclick: move |_| (cx.onselect)(Some(10))
  65. }
  66. }
  67. }
  68. })
  69. }
  70. #[derive(PartialEq, Props)]
  71. struct ReferenceItemProps {
  72. selected: usize,
  73. }
  74. static ReferenceItem: FC<ReferenceItemProps> = |cx| {
  75. let (caller, name, code) = REFERENCES[cx.selected];
  76. // Create the component using the factory API directly
  77. let caller_node = LazyNodes::new(move |f| f.component(caller, (), None, &[]));
  78. cx.render(rsx! {
  79. div {
  80. // Source of the left, rendered on the right
  81. div {
  82. code { "{code}" }
  83. }
  84. div {
  85. {caller_node}
  86. }
  87. }
  88. })
  89. };
  90. static REFERENCES: &[(FC<()>, &str, &str)] = &[
  91. (basics::Example, "Basics", include_str!("./basics.rs")),
  92. (children::Example, "Children", include_str!("./children.rs")),
  93. (
  94. conditional_rendering::Example,
  95. "Conditional Rendering",
  96. include_str!("./conditional_rendering.rs"),
  97. ),
  98. // TODO
  99. (
  100. controlled_inputs::Example,
  101. "Controlled Inputs",
  102. include_str!("./controlled_inputs.rs"),
  103. ),
  104. (empty::Example, "empty", include_str!("./empty.rs")),
  105. (
  106. custom_elements::Example,
  107. "Custom Elements & Web Components",
  108. include_str!("./custom_elements.rs"),
  109. ),
  110. (
  111. fragments::Example,
  112. "Fragments",
  113. include_str!("./fragments.rs"),
  114. ),
  115. (
  116. iterators::Example,
  117. "Iterators",
  118. include_str!("./iterators.rs"),
  119. ),
  120. (
  121. global_css::Example,
  122. "Global CSS",
  123. include_str!("./global_css.rs"),
  124. ),
  125. (
  126. inline_styles::Example,
  127. "Inline Styles",
  128. include_str!("./inline_styles.rs"),
  129. ),
  130. (listener::Example, "Listener", include_str!("./listener.rs")),
  131. (memo::Example, "Memo", include_str!("./memo.rs")),
  132. (
  133. spreadpattern::Example,
  134. "Spread Pattern",
  135. include_str!("./spreadpattern.rs"),
  136. ),
  137. (suspense::Example, "Suspense", include_str!("./suspense.rs")),
  138. (task::Example, "Task", include_str!("./task.rs")),
  139. (tostring::Example, "Tostring", include_str!("./tostring.rs")),
  140. (
  141. antipatterns::Example,
  142. "Anti-patterns",
  143. include_str!("./antipatterns.rs"),
  144. ),
  145. /*
  146. TODO!
  147. */
  148. (signals::Example, "Signals", include_str!("./signals.rs")),
  149. (noderefs::Example, "NodeRefs", include_str!("./noderefs.rs")),
  150. (
  151. statemanagement::Example,
  152. "State Management",
  153. include_str!("./statemanagement.rs"),
  154. ),
  155. (testing::Example, "Testing", include_str!("./testing.rs")),
  156. ];