dioxus/examples/flat_router.rs

87 lines
2.4 KiB
Rust
Raw Normal View History

2024-02-14 20:33:07 +00:00
//! This example shows how to use the `Router` component to create a simple navigation system.
//! The more complex router example uses all of the router features, while this simple example showcases
2024-02-14 20:33:07 +00:00
//! just the `Layout` and `Route` features.
//!
//! Layouts let you wrap chunks of your app with a component. This is useful for things like a footers, heeaders, etc.
//! Routes are enum variants with that match the name of a component in scope. This way you can create a new route
//! in your app simply by adding the variant to the enum and creating a new component with the same name. You can
//! override this of course.
2022-03-05 21:10:17 +00:00
use dioxus::prelude::*;
2022-03-04 18:08:25 +00:00
const STYLE: &str = asset!("./examples/assets/flat_router.css");
2022-03-04 18:08:25 +00:00
fn main() {
2024-01-20 08:11:55 +00:00
launch(|| {
rsx! {
head::Link { rel: "stylesheet", href: STYLE }
2024-01-20 08:11:55 +00:00
Router::<Route> {}
}
})
2023-06-02 17:33:47 +00:00
}
#[derive(Routable, Clone)]
#[rustfmt::skip]
enum Route {
2024-02-14 20:33:07 +00:00
#[layout(Footer)] // wrap the entire app in a footer
2023-06-02 17:33:47 +00:00
#[route("/")]
Home {},
2024-01-20 08:11:55 +00:00
2023-06-02 17:33:47 +00:00
#[route("/games")]
Games {},
2024-01-20 08:11:55 +00:00
2023-06-02 17:33:47 +00:00
#[route("/play")]
Play {},
2024-01-20 08:11:55 +00:00
2023-06-02 17:33:47 +00:00
#[route("/settings")]
Settings {},
}
2022-12-16 10:55:20 +00:00
#[component]
fn Footer() -> Element {
2024-01-16 19:18:46 +00:00
rsx! {
2024-01-20 08:11:55 +00:00
nav {
Link { to: Route::Home {}, class: "nav-btn", "Home" }
Link { to: Route::Games {}, class: "nav-btn", "Games" }
Link { to: Route::Play {}, class: "nav-btn", "Play" }
Link { to: Route::Settings {}, class: "nav-btn", "Settings" }
2022-03-04 18:08:25 +00:00
}
2024-02-14 20:33:07 +00:00
div { id: "content",
Outlet::<Route> {}
}
2022-12-16 10:55:20 +00:00
}
}
#[component]
fn Home() -> Element {
2024-02-14 20:33:07 +00:00
rsx!(
h1 { "Home" }
p { "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." }
)
2022-12-16 10:55:20 +00:00
}
#[component]
fn Games() -> Element {
2024-02-14 20:33:07 +00:00
rsx!(
h1 { "Games" }
// Dummy text that talks about video games
p { "Lorem games are sit amet Sed do eiusmod tempor et dolore magna aliqua." }
)
2022-12-16 10:55:20 +00:00
}
#[component]
fn Play() -> Element {
2024-02-14 20:33:07 +00:00
rsx!(
h1 { "Play" }
p { "Always play with your full heart adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." }
)
2022-12-16 10:55:20 +00:00
}
#[component]
fn Settings() -> Element {
2024-02-14 20:33:07 +00:00
rsx!(
h1 { "Settings" }
p { "Settings are consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." }
)
2022-03-04 18:08:25 +00:00
}