rsxt.rs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #![allow(non_snake_case)]
  2. use std::rc::Rc;
  3. use dioxus::{events::on::MouseEvent, prelude::*};
  4. use dioxus_core as dioxus;
  5. use dioxus_web::WebsysRenderer;
  6. fn main() {
  7. wasm_logger::init(wasm_logger::Config::new(log::Level::Trace));
  8. console_error_panic_hook::set_once();
  9. wasm_bindgen_futures::spawn_local(async {
  10. let props = ExampleProps {
  11. initial_name: "..?",
  12. };
  13. WebsysRenderer::new_with_props(Example, props)
  14. .run()
  15. .await
  16. .unwrap()
  17. });
  18. }
  19. #[derive(PartialEq, Props)]
  20. struct ExampleProps {
  21. initial_name: &'static str,
  22. }
  23. static Example: FC<ExampleProps> = |cx| {
  24. let name = use_state(&cx, move || cx.initial_name);
  25. cx.render(rsx! {
  26. div {
  27. class: "py-12 px-4 text-center w-full max-w-2xl mx-auto"
  28. span {
  29. class: "text-sm font-semibold"
  30. "Dioxus Example: Jack and Jill"
  31. }
  32. h2 {
  33. class: "text-5xl mt-2 mb-6 leading-tight font-semibold font-heading"
  34. "Hello, {name}"
  35. }
  36. CustomButton { name: "Jack!", handler: move |_| name.set("Jack") }
  37. CustomButton { name: "Jill!", handler: move |_| name.set("Jill") }
  38. CustomButton { name: "Bob!", handler: move |_| name.set("Bob")}
  39. Placeholder {val: name}
  40. Placeholder {val: name}
  41. }
  42. })
  43. };
  44. #[derive(Props)]
  45. struct ButtonProps<'src, F: Fn(Rc<dyn MouseEvent>)> {
  46. name: &'src str,
  47. handler: F,
  48. }
  49. fn CustomButton<'a, F: Fn(MouseEvent)>(cx: Context<'a, ButtonProps<'a, F>>) -> VNode {
  50. cx.render(rsx!{
  51. button {
  52. class: "inline-block py-4 px-8 mr-6 leading-none text-white bg-indigo-600 hover:bg-indigo-900 font-semibold rounded shadow"
  53. onmouseover: {&cx.handler}
  54. "{cx.name}"
  55. }
  56. })
  57. }
  58. impl<F: Fn(MouseEvent)> PartialEq for ButtonProps<'_, F> {
  59. fn eq(&self, other: &Self) -> bool {
  60. false
  61. }
  62. }
  63. #[derive(Props, PartialEq)]
  64. struct PlaceholderProps {
  65. val: &'static str,
  66. }
  67. fn Placeholder(cx: Context<PlaceholderProps>) -> VNode {
  68. cx.render(rsx! {
  69. div {
  70. "child: {cx.val}"
  71. }
  72. })
  73. }