2024-02-14 21:48:58 +00:00
|
|
|
//! An advanced usage of the router with nested routes and redirects.
|
|
|
|
//!
|
|
|
|
//! Dioxus implements an enum-based router, which allows you to define your routes in a type-safe way.
|
|
|
|
//! However, since we need to bake quite a bit of logic into the enum, we have to add some extra syntax.
|
|
|
|
//!
|
|
|
|
//! Note that you don't need to use advanced features like nest, redirect, etc, since these can all be implemented
|
|
|
|
//! manually, but they are provided as a convenience.
|
|
|
|
|
2022-01-05 22:30:12 +00:00
|
|
|
use dioxus::prelude::*;
|
2021-11-03 04:35:56 +00:00
|
|
|
|
2022-01-05 22:30:12 +00:00
|
|
|
fn main() {
|
2024-01-18 02:41:31 +00:00
|
|
|
launch_desktop(|| {
|
|
|
|
rsx! {
|
2024-02-14 21:48:58 +00:00
|
|
|
style { {include_str!("./assets/router.css")} }
|
2024-01-18 02:41:31 +00:00
|
|
|
Router::<Route> {}
|
|
|
|
}
|
|
|
|
});
|
2021-11-03 04:35:56 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 21:48:58 +00:00
|
|
|
// Turn off rustfmt since we're doing layouts and routes in the same enum
|
2024-01-16 21:54:09 +00:00
|
|
|
#[derive(Routable, Clone, Debug, PartialEq)]
|
2023-06-01 19:10:33 +00:00
|
|
|
#[rustfmt::skip]
|
|
|
|
enum Route {
|
2024-02-14 21:48:58 +00:00
|
|
|
// Wrap Home in a Navbar Layout
|
2023-06-01 19:10:33 +00:00
|
|
|
#[layout(NavBar)]
|
2024-02-14 21:48:58 +00:00
|
|
|
// The default route is always "/" unless otherwise specified
|
2023-06-01 19:10:33 +00:00
|
|
|
#[route("/")]
|
|
|
|
Home {},
|
2024-02-14 21:48:58 +00:00
|
|
|
|
|
|
|
// Wrap the next routes in a layout and a nest
|
2023-06-01 19:10:33 +00:00
|
|
|
#[nest("/blog")]
|
2024-02-14 21:48:58 +00:00
|
|
|
#[layout(Blog)]
|
|
|
|
// At "/blog", we want to show a list of blog posts
|
|
|
|
#[route("/")]
|
|
|
|
BlogList {},
|
|
|
|
|
|
|
|
// At "/blog/:name", we want to show a specific blog post, using the name slug
|
|
|
|
#[route("/:name")]
|
|
|
|
BlogPost { name: String },
|
|
|
|
|
|
|
|
// We need to end the blog layout and nest
|
|
|
|
// Note we don't need either - we could've just done `/blog/` and `/blog/:name` without nesting,
|
|
|
|
// but it's a bit cleaner this way
|
|
|
|
#[end_layout]
|
2023-06-01 19:10:33 +00:00
|
|
|
#[end_nest]
|
2024-02-14 21:48:58 +00:00
|
|
|
|
|
|
|
// And the regular page layout
|
2023-06-01 19:10:33 +00:00
|
|
|
#[end_layout]
|
2024-02-14 21:48:58 +00:00
|
|
|
|
|
|
|
// Add some redirects for the `/myblog` route
|
2023-06-01 19:10:33 +00:00
|
|
|
#[nest("/myblog")]
|
|
|
|
#[redirect("/", || Route::BlogList {})]
|
|
|
|
#[redirect("/:name", |name: String| Route::BlogPost { name })]
|
|
|
|
#[end_nest]
|
2024-02-14 21:48:58 +00:00
|
|
|
|
|
|
|
// Finally, we need to handle the 404 page
|
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
|
|
|
}
|
2021-11-03 04:35:56 +00:00
|
|
|
|
2023-09-15 14:13:36 +00:00
|
|
|
#[component]
|
2024-01-14 04:51:37 +00:00
|
|
|
fn NavBar() -> Element {
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! {
|
2024-02-14 21:48:58 +00:00
|
|
|
nav { id: "navbar",
|
|
|
|
Link { to: Route::Home {}, "Home" }
|
|
|
|
Link { to: Route::BlogList {}, "Blog" }
|
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-15 14:13:36 +00:00
|
|
|
#[component]
|
2024-01-14 04:51:37 +00:00
|
|
|
fn Home() -> Element {
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! { h1 { "Welcome to the Dioxus Blog!" } }
|
2023-06-01 19:10:33 +00:00
|
|
|
}
|
2022-01-05 22:30:12 +00:00
|
|
|
|
2023-09-15 14:13:36 +00:00
|
|
|
#[component]
|
2024-01-14 04:51:37 +00:00
|
|
|
fn Blog() -> Element {
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! {
|
2023-06-01 19:10:33 +00:00
|
|
|
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-15 14:13:36 +00:00
|
|
|
#[component]
|
2024-01-14 04:51:37 +00:00
|
|
|
fn BlogList() -> Element {
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! {
|
2023-06-01 19:10:33 +00:00
|
|
|
h2 { "Choose a post" }
|
2024-02-14 21:48:58 +00:00
|
|
|
div { id: "blog-list",
|
|
|
|
Link { to: Route::BlogPost { name: "Blog post 1".into() },
|
|
|
|
"Read the first blog post"
|
2023-06-01 19:10:33 +00:00
|
|
|
}
|
2024-02-14 21:48:58 +00:00
|
|
|
Link { to: Route::BlogPost { name: "Blog post 2".into() },
|
|
|
|
"Read the second blog post"
|
2023-06-01 19:10:33 +00:00
|
|
|
}
|
2022-12-16 10:55:20 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-11 17:27:02 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 21:48:58 +00:00
|
|
|
// We can use the `name` slug to show a specific blog post
|
|
|
|
// In theory we could read from the filesystem or a database here
|
2023-09-15 14:13:36 +00:00
|
|
|
#[component]
|
2024-01-14 04:51:37 +00:00
|
|
|
fn BlogPost(name: String) -> Element {
|
2024-02-14 21:48:58 +00:00
|
|
|
let contents = match name.as_str() {
|
|
|
|
"Blog post 1" => "This is the first blog post. It's not very interesting.",
|
|
|
|
"Blog post 2" => "This is the second blog post. It's not very interesting either.",
|
|
|
|
_ => "This blog post doesn't exist.",
|
|
|
|
};
|
|
|
|
|
|
|
|
rsx! {
|
|
|
|
h2 { "{name}" }
|
|
|
|
p { "{contents}" }
|
|
|
|
}
|
2022-01-05 22:30:12 +00:00
|
|
|
}
|
2022-12-16 10:55:20 +00:00
|
|
|
|
2023-09-15 14:13:36 +00:00
|
|
|
#[component]
|
2024-01-14 04:51:37 +00:00
|
|
|
fn PageNotFound(route: Vec<String>) -> Element {
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! {
|
2023-06-01 19:10:33 +00:00
|
|
|
h1 { "Page not found" }
|
|
|
|
p { "We are terribly sorry, but the page you requested doesn't exist." }
|
2024-01-16 15:40:17 +00:00
|
|
|
pre { color: "red", "log:\nattemped to navigate to: {route:?}" }
|
2023-06-01 19:10:33 +00:00
|
|
|
}
|
2022-12-16 10:55:20 +00:00
|
|
|
}
|