dioxus/examples/router.rs

126 lines
3.5 KiB
Rust
Raw Normal View History

//! 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! {
style { {include_str!("./assets/router.css")} }
2024-01-18 02:41:31 +00:00
Router::<Route> {}
}
});
2021-11-03 04:35:56 +00:00
}
// Turn off rustfmt since we're doing layouts and routes in the same enum
#[derive(Routable, Clone, Debug, PartialEq)]
2023-06-01 19:10:33 +00:00
#[rustfmt::skip]
enum Route {
// Wrap Home in a Navbar Layout
2023-06-01 19:10:33 +00:00
#[layout(NavBar)]
// The default route is always "/" unless otherwise specified
2023-06-01 19:10:33 +00:00
#[route("/")]
Home {},
// Wrap the next routes in a layout and a nest
2023-06-01 19:10:33 +00:00
#[nest("/blog")]
#[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]
// And the regular page layout
2023-06-01 19:10:33 +00:00
#[end_layout]
// 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]
// 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>,
},
}
2021-11-03 04:35:56 +00:00
#[component]
fn NavBar() -> Element {
2024-01-16 19:18:46 +00:00
rsx! {
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
}
}
#[component]
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
#[component]
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
}
#[component]
fn BlogList() -> Element {
2024-01-16 19:18:46 +00:00
rsx! {
2023-06-01 19:10:33 +00:00
h2 { "Choose a post" }
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
}
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
}
// We can use the `name` slug to show a specific blog post
// In theory we could read from the filesystem or a database here
#[component]
fn BlogPost(name: String) -> Element {
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
#[component]
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
}