flat_router.rs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //! This example shows how to use the `Router` component to create a simple navigation system.
  2. //! The more complex router example uses all of the router features, while this simple example showcases
  3. //! just the `Layout` and `Route` features.
  4. //!
  5. //! Layouts let you wrap chunks of your app with a component. This is useful for things like a footers, heeaders, etc.
  6. //! Routes are enum variants with that match the name of a component in scope. This way you can create a new route
  7. //! in your app simply by adding the variant to the enum and creating a new component with the same name. You can
  8. //! override this of course.
  9. use dioxus::prelude::*;
  10. const STYLE: Asset = asset!("/examples/assets/flat_router.css");
  11. fn main() {
  12. dioxus::launch(|| {
  13. rsx! {
  14. document::Link { rel: "stylesheet", href: STYLE }
  15. Router::<Route> {}
  16. }
  17. })
  18. }
  19. #[derive(Routable, Clone)]
  20. #[rustfmt::skip]
  21. enum Route {
  22. #[layout(Footer)] // wrap the entire app in a footer
  23. #[route("/")]
  24. Home {},
  25. #[route("/games")]
  26. Games {},
  27. #[route("/play")]
  28. Play {},
  29. #[route("/settings")]
  30. Settings {},
  31. }
  32. #[component]
  33. fn Footer() -> Element {
  34. rsx! {
  35. nav {
  36. Link { to: Route::Home {}, class: "nav-btn", "Home" }
  37. Link { to: Route::Games {}, class: "nav-btn", "Games" }
  38. Link { to: Route::Play {}, class: "nav-btn", "Play" }
  39. Link { to: Route::Settings {}, class: "nav-btn", "Settings" }
  40. }
  41. div { id: "content",
  42. Outlet::<Route> {}
  43. }
  44. }
  45. }
  46. #[component]
  47. fn Home() -> Element {
  48. rsx!(
  49. h1 { "Home" }
  50. p { "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." }
  51. )
  52. }
  53. #[component]
  54. fn Games() -> Element {
  55. rsx!(
  56. h1 { "Games" }
  57. // Dummy text that talks about video games
  58. p { "Lorem games are sit amet Sed do eiusmod tempor et dolore magna aliqua." }
  59. )
  60. }
  61. #[component]
  62. fn Play() -> Element {
  63. rsx!(
  64. h1 { "Play" }
  65. p { "Always play with your full heart adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." }
  66. )
  67. }
  68. #[component]
  69. fn Settings() -> Element {
  70. rsx!(
  71. h1 { "Settings" }
  72. p { "Settings are consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." }
  73. )
  74. }