rsxt.rs 2.2 KB

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