rsxt.rs 1.9 KB

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