1
0

flat_router.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 exmaple 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. fn main() {
  11. launch(|| {
  12. rsx! {
  13. style { {include_str!("./assets/flat_router.css")} }
  14. Router::<Route> {}
  15. }
  16. })
  17. }
  18. #[derive(Routable, Clone)]
  19. #[rustfmt::skip]
  20. enum Route {
  21. #[layout(Footer)] // wrap the entire app in a footer
  22. #[route("/")]
  23. Home {},
  24. #[route("/games")]
  25. Games {},
  26. #[route("/play")]
  27. Play {},
  28. #[route("/settings")]
  29. Settings {},
  30. }
  31. #[component]
  32. fn Footer() -> Element {
  33. rsx! {
  34. nav {
  35. Link { to: Route::Home {}, class: "nav-btn", "Home" }
  36. Link { to: Route::Games {}, class: "nav-btn", "Games" }
  37. Link { to: Route::Play {}, class: "nav-btn", "Play" }
  38. Link { to: Route::Settings {}, class: "nav-btn", "Settings" }
  39. }
  40. div { id: "content",
  41. Outlet::<Route> {}
  42. }
  43. }
  44. }
  45. #[component]
  46. fn Home() -> Element {
  47. rsx!(
  48. h1 { "Home" }
  49. p { "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." }
  50. )
  51. }
  52. #[component]
  53. fn Games() -> Element {
  54. rsx!(
  55. h1 { "Games" }
  56. // Dummy text that talks about video games
  57. p { "Lorem games are sit amet Sed do eiusmod tempor et dolore magna aliqua." }
  58. )
  59. }
  60. #[component]
  61. fn Play() -> Element {
  62. rsx!(
  63. h1 { "Play" }
  64. p { "Always play with your full heart adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." }
  65. )
  66. }
  67. #[component]
  68. fn Settings() -> Element {
  69. rsx!(
  70. h1 { "Settings" }
  71. p { "Settings are consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." }
  72. )
  73. }