meme_editor.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // ANCHOR: all
  2. #![allow(non_snake_case)]
  3. use dioxus::events::FormData;
  4. use dioxus::prelude::*;
  5. fn main() {
  6. dioxus_desktop::launch(MemeEditor);
  7. }
  8. // ANCHOR: meme_editor
  9. fn MemeEditor(cx: Scope) -> Element {
  10. let container_style = r"
  11. display: flex;
  12. flex-direction: column;
  13. gap: 16px;
  14. margin: 0 auto;
  15. width: fit-content;
  16. ";
  17. let caption = use_state(&cx, || "me waiting for my rust code to compile".to_string());
  18. cx.render(rsx! {
  19. div {
  20. style: "{container_style}",
  21. h1 { "Meme Editor" },
  22. Meme {
  23. caption: caption,
  24. },
  25. CaptionEditor {
  26. caption: caption,
  27. on_input: move |event: FormEvent| {caption.set(event.value.clone());},
  28. },
  29. }
  30. })
  31. }
  32. // ANCHOR_END: meme_editor
  33. // ANCHOR: meme_component
  34. #[inline_props]
  35. fn Meme<'a>(cx: Scope<'a>, caption: &'a str) -> Element<'a> {
  36. let container_style = r#"
  37. position: relative;
  38. width: fit-content;
  39. "#;
  40. let caption_container_style = r#"
  41. position: absolute;
  42. bottom: 0;
  43. left: 0;
  44. right: 0;
  45. padding: 16px 8px;
  46. "#;
  47. let caption_style = r"
  48. font-size: 32px;
  49. margin: 0;
  50. color: white;
  51. text-align: center;
  52. ";
  53. cx.render(rsx!(
  54. div {
  55. style: "{container_style}",
  56. img {
  57. src: "https://i.imgflip.com/2zh47r.jpg",
  58. height: "500px",
  59. },
  60. div {
  61. style: "{caption_container_style}",
  62. p {
  63. style: "{caption_style}",
  64. "{caption}"
  65. }
  66. }
  67. }
  68. ))
  69. }
  70. // ANCHOR_END: meme_component
  71. // ANCHOR: caption_editor
  72. #[inline_props]
  73. fn CaptionEditor<'a>(
  74. cx: Scope<'a>,
  75. caption: &'a str,
  76. on_input: EventHandler<'a, FormData>,
  77. ) -> Element<'a> {
  78. let input_style = r"
  79. border: none;
  80. background: cornflowerblue;
  81. padding: 8px 16px;
  82. margin: 0;
  83. border-radius: 4px;
  84. color: white;
  85. ";
  86. cx.render(rsx!(input {
  87. style: "{input_style}",
  88. value: "{caption}",
  89. oninput: move |event| on_input.call(event),
  90. }))
  91. }
  92. // ANCHOR_END: caption_editor
  93. // ANCHOR_END: all