meme_editor.rs 2.2 KB

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