router_cfg.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. use std::sync::Arc;
  2. use crate::contexts::router::RoutingCallback;
  3. use crate::history::HistoryProvider;
  4. use crate::routable::Routable;
  5. use dioxus::prelude::*;
  6. use crate::prelude::*;
  7. /// Global configuration options for the router.
  8. ///
  9. /// This implements [`Default`] and follows the builder pattern, so you can use it like this:
  10. /// ```rust,no_run
  11. /// # use dioxus_router::prelude::*;
  12. /// # use dioxus::prelude::*;
  13. /// # #[inline_props]
  14. /// # fn Index(cx: Scope) -> Element {
  15. /// # todo!()
  16. /// # }
  17. /// #[derive(Clone, Routable)]
  18. /// enum Route {
  19. /// #[route("/")]
  20. /// Index {},
  21. /// }
  22. /// let cfg = RouterConfig::default().history(WebHistory::<Route>::default());
  23. /// ```
  24. pub struct RouterConfig<R: Routable> {
  25. pub(crate) failure_external_navigation: fn(Scope) -> Element,
  26. pub(crate) history: Box<dyn HistoryProvider<R>>,
  27. pub(crate) on_update: Option<RoutingCallback<R>>,
  28. }
  29. #[cfg(feature = "serde")]
  30. impl<R: Routable + Clone> Default for RouterConfig<R>
  31. where
  32. <R as std::str::FromStr>::Err: std::fmt::Display,
  33. R: serde::Serialize + serde::de::DeserializeOwned,
  34. {
  35. fn default() -> Self {
  36. Self {
  37. failure_external_navigation: FailureExternalNavigation::<R>,
  38. history: {
  39. #[cfg(all(target_arch = "wasm32", feature = "web"))]
  40. let history = Box::<WebHistory<R>>::default();
  41. #[cfg(not(all(target_arch = "wasm32", feature = "web")))]
  42. let history = Box::<MemoryHistory<R>>::default();
  43. history
  44. },
  45. on_update: None,
  46. }
  47. }
  48. }
  49. #[cfg(not(feature = "serde"))]
  50. impl<R: Routable + Clone> Default for RouterConfig<R>
  51. where
  52. <R as std::str::FromStr>::Err: std::fmt::Display,
  53. {
  54. fn default() -> Self {
  55. Self {
  56. failure_external_navigation: FailureExternalNavigation::<R>,
  57. history: {
  58. #[cfg(all(target_arch = "wasm32", feature = "web"))]
  59. let history = Box::<WebHistory<R>>::default();
  60. #[cfg(not(all(target_arch = "wasm32", feature = "web")))]
  61. let history = Box::<MemoryHistory<R>>::default();
  62. history
  63. },
  64. on_update: None,
  65. }
  66. }
  67. }
  68. impl<R: Routable> RouterConfig<R> {
  69. /// A function to be called whenever the routing is updated.
  70. ///
  71. /// The callback is invoked after the routing is updated, but before components and hooks are
  72. /// updated.
  73. ///
  74. /// If the callback returns a [`NavigationTarget`] the router will replace the current location
  75. /// with it. If no navigation failure was triggered, the router will then updated dependent
  76. /// components and hooks.
  77. ///
  78. /// The callback is called no more than once per rerouting. It will not be called if a
  79. /// navigation failure occurs.
  80. ///
  81. /// Defaults to [`None`].
  82. pub fn on_update(
  83. self,
  84. callback: impl Fn(GenericRouterContext<R>) -> Option<NavigationTarget<R>> + 'static,
  85. ) -> Self {
  86. Self {
  87. on_update: Some(Arc::new(callback)),
  88. ..self
  89. }
  90. }
  91. /// The [`HistoryProvider`] the router should use.
  92. ///
  93. /// Defaults to a default [`MemoryHistory`].
  94. pub fn history(self, history: impl HistoryProvider<R> + 'static) -> Self {
  95. Self {
  96. history: Box::new(history),
  97. ..self
  98. }
  99. }
  100. /// A component to render when an external navigation fails.
  101. ///
  102. /// Defaults to a router-internal component called [`FailureExternalNavigation`]
  103. pub fn failure_external_navigation(self, component: fn(Scope) -> Element) -> Self {
  104. Self {
  105. failure_external_navigation: component,
  106. ..self
  107. }
  108. }
  109. }