1
0

rsxt.rs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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| {
  21. let name = use_state_new(&ctx, move || ctx.initial_name);
  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!", handler: move |_| name.set("Jack") }
  34. CustomButton { name: "Jill!", handler: move |_| name.set("Jill") }
  35. CustomButton { name: "Bob!", handler: move |_| name.set("Bob")}
  36. Placeholder {val: name}
  37. Placeholder {val: name}
  38. }
  39. })
  40. };
  41. #[derive(Props)]
  42. struct ButtonProps<'src, F: Fn(MouseEvent)> {
  43. name: &'src str,
  44. handler: F
  45. }
  46. fn CustomButton<'a, F: Fn(MouseEvent)>(ctx: Context<'a, ButtonProps<'a, F>>) -> VNode {
  47. ctx.render(rsx!{
  48. button {
  49. class: "inline-block py-4 px-8 mr-6 leading-none text-white bg-indigo-600 hover:bg-indigo-900 font-semibold rounded shadow"
  50. onmouseover: {&ctx.handler}
  51. "{ctx.name}"
  52. }
  53. })
  54. }
  55. impl<F: Fn(MouseEvent)> PartialEq for ButtonProps<'_, F> {
  56. fn eq(&self, other: &Self) -> bool {
  57. false
  58. }
  59. }
  60. #[derive(Props, PartialEq)]
  61. struct PlaceholderProps {
  62. val: &'static str
  63. }
  64. fn Placeholder(ctx: Context<PlaceholderProps>) -> VNode {
  65. ctx.render(rsx!{
  66. div {
  67. "child: {ctx.val}"
  68. }
  69. })
  70. }