2022-01-05 22:30:12 +00:00
|
|
|
use dioxus::prelude::*;
|
2022-12-16 10:55:20 +00:00
|
|
|
use dioxus_router::prelude::*;
|
2021-11-03 04:35:56 +00:00
|
|
|
|
2022-01-05 22:30:12 +00:00
|
|
|
fn main() {
|
2023-06-01 19:10:33 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
dioxus_web::launch(App);
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
dioxus_desktop::launch(App);
|
2021-11-03 04:35:56 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 19:10:33 +00:00
|
|
|
// ANCHOR: router
|
|
|
|
#[derive(Routable, Clone)]
|
|
|
|
#[rustfmt::skip]
|
|
|
|
enum Route {
|
|
|
|
#[layout(NavBar)]
|
|
|
|
#[route("/")]
|
|
|
|
Home {},
|
|
|
|
#[nest("/blog")]
|
|
|
|
#[layout(Blog)]
|
|
|
|
#[route("/")]
|
|
|
|
BlogList {},
|
|
|
|
#[route("/blog/:name")]
|
|
|
|
BlogPost { name: String },
|
|
|
|
#[end_layout]
|
|
|
|
#[end_nest]
|
|
|
|
#[end_layout]
|
|
|
|
#[nest("/myblog")]
|
|
|
|
#[redirect("/", || Route::BlogList {})]
|
|
|
|
#[redirect("/:name", |name: String| Route::BlogPost { name })]
|
|
|
|
#[end_nest]
|
2023-07-18 23:34:27 +00:00
|
|
|
#[route("/:..route")]
|
2023-06-01 19:10:33 +00:00
|
|
|
PageNotFound {
|
|
|
|
route: Vec<String>,
|
|
|
|
},
|
2022-01-02 23:35:38 +00:00
|
|
|
}
|
2023-06-01 19:10:33 +00:00
|
|
|
// ANCHOR_END: router
|
2021-11-03 04:35:56 +00:00
|
|
|
|
2023-09-18 00:40:14 +00:00
|
|
|
#[component]
|
2023-06-01 19:10:33 +00:00
|
|
|
fn App(cx: Scope) -> Element {
|
|
|
|
render! {
|
2023-07-26 01:14:48 +00:00
|
|
|
Router::<Route> {}
|
2023-06-01 19:10:33 +00:00
|
|
|
}
|
2022-12-16 10:55:20 +00:00
|
|
|
}
|
|
|
|
|
2023-09-18 00:40:14 +00:00
|
|
|
#[component]
|
2023-06-01 19:10:33 +00:00
|
|
|
fn NavBar(cx: Scope) -> Element {
|
2022-12-16 10:55:20 +00:00
|
|
|
render! {
|
2023-06-01 19:10:33 +00:00
|
|
|
nav {
|
|
|
|
ul {
|
2023-07-24 18:57:56 +00:00
|
|
|
li { Link { to: Route::Home {}, "Home" } }
|
|
|
|
li { Link { to: Route::BlogList {}, "Blog" } }
|
2023-06-01 19:10:33 +00:00
|
|
|
}
|
2022-12-16 10:55:20 +00:00
|
|
|
}
|
2023-07-26 01:14:48 +00:00
|
|
|
Outlet::<Route> {}
|
2022-12-16 10:55:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-18 00:40:14 +00:00
|
|
|
#[component]
|
2023-06-01 19:10:33 +00:00
|
|
|
fn Home(cx: Scope) -> Element {
|
|
|
|
render! {
|
|
|
|
h1 { "Welcome to the Dioxus Blog!" }
|
|
|
|
}
|
|
|
|
}
|
2022-01-05 22:30:12 +00:00
|
|
|
|
2023-09-18 00:40:14 +00:00
|
|
|
#[component]
|
2023-06-01 19:10:33 +00:00
|
|
|
fn Blog(cx: Scope) -> Element {
|
|
|
|
render! {
|
|
|
|
h1 { "Blog" }
|
2023-07-26 01:14:48 +00:00
|
|
|
Outlet::<Route> {}
|
2023-06-01 19:10:33 +00:00
|
|
|
}
|
2021-11-03 04:35:56 +00:00
|
|
|
}
|
|
|
|
|
2023-09-18 00:40:14 +00:00
|
|
|
#[component]
|
2023-06-01 19:10:33 +00:00
|
|
|
fn BlogList(cx: Scope) -> Element {
|
2022-12-16 10:55:20 +00:00
|
|
|
render! {
|
2023-06-01 19:10:33 +00:00
|
|
|
h2 { "Choose a post" }
|
2022-12-16 10:55:20 +00:00
|
|
|
ul {
|
2023-06-01 19:10:33 +00:00
|
|
|
li {
|
|
|
|
Link {
|
2023-07-24 18:57:56 +00:00
|
|
|
to: Route::BlogPost { name: "Blog post 1".into() },
|
2023-06-01 19:10:33 +00:00
|
|
|
"Read the first blog post"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
li {
|
|
|
|
Link {
|
2023-07-24 18:57:56 +00:00
|
|
|
to: Route::BlogPost { name: "Blog post 2".into() },
|
2023-06-01 19:10:33 +00:00
|
|
|
"Read the second blog post"
|
|
|
|
}
|
|
|
|
}
|
2022-12-16 10:55:20 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-11 17:27:02 +00:00
|
|
|
}
|
|
|
|
|
2023-09-18 00:40:14 +00:00
|
|
|
#[component]
|
2023-06-01 19:10:33 +00:00
|
|
|
fn BlogPost(cx: Scope, name: String) -> Element {
|
|
|
|
render! {
|
|
|
|
h2 { "Blog Post: {name}"}
|
|
|
|
}
|
2022-01-05 22:30:12 +00:00
|
|
|
}
|
2022-12-16 10:55:20 +00:00
|
|
|
|
2023-09-18 00:40:14 +00:00
|
|
|
#[component]
|
2023-06-01 19:10:33 +00:00
|
|
|
fn PageNotFound(cx: Scope, route: Vec<String>) -> Element {
|
|
|
|
render! {
|
|
|
|
h1 { "Page not found" }
|
|
|
|
p { "We are terribly sorry, but the page you requested doesn't exist." }
|
|
|
|
pre {
|
|
|
|
color: "red",
|
|
|
|
"log:\nattemped to navigate to: {route:?}"
|
|
|
|
}
|
|
|
|
}
|
2022-12-16 10:55:20 +00:00
|
|
|
}
|