hot_reload.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. use dioxus::prelude::*;
  2. fn main() {
  3. dioxus::desktop::launch(app);
  4. }
  5. fn app(cx: Scope) -> Element {
  6. let rsx_code = use_state(&cx, || {
  7. r##"div {
  8. width: "100%",
  9. height: "500px",
  10. onclick: move |_| {
  11. count.modify(|count| *count + 10);
  12. },
  13. p {
  14. "High-Five counter: {count.to_string():?}",
  15. }
  16. div {
  17. width: "{count}px",
  18. height: "10px",
  19. background_color: "red",
  20. }
  21. Comp {
  22. color: "#083289"
  23. }
  24. Comp {
  25. color: "green"
  26. }
  27. {
  28. (0..10).map(|i| {
  29. cx.render(rsx!{p {"{i}"}})
  30. })
  31. }
  32. }"##
  33. .to_string()
  34. });
  35. let submitted_rsx_code = use_state(&cx, || None);
  36. cx.render(rsx! {
  37. div {
  38. display: "flex",
  39. flex_direction: "row",
  40. width: "100%",
  41. height: "100%",
  42. Editable{
  43. current_code: submitted_rsx_code.get().clone(),
  44. },
  45. textarea {
  46. width: "50em",
  47. height: "50em",
  48. value: rsx_code,
  49. oninput: move |evt| {
  50. rsx_code.set(evt.value.clone());
  51. },
  52. }
  53. button {
  54. height: "100%",
  55. width: "10%",
  56. onclick: move |_|{
  57. submitted_rsx_code.set(Some(rsx_code.get().clone()));
  58. },
  59. "submit"
  60. }
  61. }
  62. })
  63. }
  64. #[derive(PartialEq, Props)]
  65. struct EditableProps {
  66. #[props(!optional)]
  67. current_code: Option<String>,
  68. }
  69. fn Editable(cx: Scope<EditableProps>) -> Element {
  70. let count = use_state(&cx, || 170);
  71. if let Some(code) = cx.props.current_code.as_ref() {
  72. let rsx_index: RsxContext = cx.consume_context().unwrap();
  73. rsx_index.insert(
  74. CodeLocation {
  75. file: r"examples\hot_reload.rs".to_string(),
  76. line: 94,
  77. column: 15,
  78. },
  79. code.clone(),
  80. );
  81. }
  82. cx.render(rsx! {
  83. div {
  84. width: "100%",
  85. height: "500px",
  86. onclick: move |_| {
  87. count.modify(|count| *count + 10);
  88. },
  89. p {
  90. "High-Five counter: {count.to_string():?}",
  91. }
  92. div {
  93. width: "{count}px",
  94. height: "10px",
  95. background_color: "red",
  96. }
  97. Comp {
  98. color: "#083289"
  99. }
  100. Comp {
  101. color: "green"
  102. }
  103. {
  104. (0..10).map(|i| {
  105. cx.render(rsx!{p {"{i}"}})
  106. })
  107. }
  108. }
  109. })
  110. }
  111. #[derive(PartialEq, Props)]
  112. struct CompProps {
  113. color: &'static str,
  114. }
  115. fn Comp(cx: Scope<CompProps>) -> Element {
  116. cx.render(rsx! {
  117. h1 {
  118. color: "{cx.props.color}",
  119. "Hello, from a component!"
  120. }
  121. })
  122. }