dioxus/packages/web/examples/rsxt.rs

84 lines
2.2 KiB
Rust
Raw Normal View History

2021-03-11 17:27:01 +00:00
#![allow(non_snake_case)]
2021-03-02 05:14:28 +00:00
use dioxus_core as dioxus;
use dioxus::{events::on::MouseEvent, prelude::*};
2021-03-02 05:14:28 +00:00
use dioxus_web::WebsysRenderer;
fn main() {
2021-03-11 17:27:01 +00:00
wasm_logger::init(wasm_logger::Config::new(log::Level::Trace));
console_error_panic_hook::set_once();
2021-03-13 15:02:57 +00:00
wasm_bindgen_futures::spawn_local(async {
2021-03-16 15:03:59 +00:00
let props = ExampleProps { initial_name: "..?"};
2021-03-14 00:11:06 +00:00
WebsysRenderer::new_with_props(Example, props)
2021-03-13 15:02:57 +00:00
.run()
.await
.unwrap()
});
2021-03-02 05:14:28 +00:00
}
2021-03-12 22:21:06 +00:00
#[derive(PartialEq, Props)]
struct ExampleProps {
2021-03-13 15:02:57 +00:00
initial_name: &'static str,
2021-03-12 22:21:06 +00:00
}
static Example: FC<ExampleProps> = |ctx| {
let name = use_state_new(&ctx, move || ctx.initial_name);
2021-03-13 15:02:57 +00:00
2021-03-02 05:14:28 +00:00
ctx.render(rsx! {
div {
class: "py-12 px-4 text-center w-full max-w-2xl mx-auto"
span {
2021-03-02 05:14:28 +00:00
class: "text-sm font-semibold"
"Dioxus Example: Jack and Jill"
2021-03-02 05:14:28 +00:00
}
h2 {
2021-03-02 05:14:28 +00:00
class: "text-5xl mt-2 mb-6 leading-tight font-semibold font-heading"
"Hello, {name}"
2021-03-02 05:14:28 +00:00
}
2021-03-13 15:02:57 +00:00
2021-05-16 06:06:02 +00:00
CustomButton { name: "Jack!", handler: move |_| name.set("Jack") }
CustomButton { name: "Jill!", handler: move |_| name.set("Jill") }
CustomButton { name: "Bob!", handler: move |_| name.set("Bob")}
Placeholder {val: name}
Placeholder {val: name}
2021-03-02 05:14:28 +00:00
}
})
};
2021-03-12 22:21:06 +00:00
2021-03-13 15:02:57 +00:00
#[derive(Props)]
2021-03-29 16:31:47 +00:00
struct ButtonProps<'src, F: Fn(MouseEvent)> {
2021-03-16 15:03:59 +00:00
name: &'src str,
2021-03-29 16:31:47 +00:00
handler: F
2021-03-13 15:02:57 +00:00
}
2021-06-08 18:00:29 +00:00
fn CustomButton<'a, F: Fn(MouseEvent)>(ctx: Context<'a, ButtonProps<'a, F>>) -> VNode {
2021-03-12 22:21:06 +00:00
ctx.render(rsx!{
button {
class: "inline-block py-4 px-8 mr-6 leading-none text-white bg-indigo-600 hover:bg-indigo-900 font-semibold rounded shadow"
onmouseover: {&ctx.handler}
"{ctx.name}"
2021-03-12 22:21:06 +00:00
}
})
2021-03-13 15:02:57 +00:00
}
2021-03-12 22:21:06 +00:00
2021-03-29 16:31:47 +00:00
impl<F: Fn(MouseEvent)> PartialEq for ButtonProps<'_, F> {
2021-03-16 15:03:59 +00:00
fn eq(&self, other: &Self) -> bool {
false
}
}
2021-05-16 06:06:02 +00:00
#[derive(Props, PartialEq)]
struct PlaceholderProps {
val: &'static str
}
2021-06-08 18:00:29 +00:00
fn Placeholder(ctx: Context<PlaceholderProps>) -> VNode {
2021-05-16 06:06:02 +00:00
ctx.render(rsx!{
div {
"child: {ctx.val}"
2021-05-16 06:06:02 +00:00
}
})
}