router_cfg.rs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. impl<R: Routable + Clone> Default for RouterConfig<R>
  30. where
  31. <R as std::str::FromStr>::Err: std::fmt::Display,
  32. {
  33. fn default() -> Self {
  34. Self {
  35. failure_external_navigation: FailureExternalNavigation::<R>,
  36. history: {
  37. #[cfg(all(target_arch = "wasm32", feature = "web"))]
  38. let history = Box::<MemoryHistory<R>>::default();
  39. #[cfg(not(all(target_arch = "wasm32", feature = "web")))]
  40. let history = Box::<MemoryHistory<R>>::default();
  41. history
  42. },
  43. on_update: None,
  44. }
  45. }
  46. }
  47. impl<R: Routable> RouterConfig<R> {
  48. /// A function to be called whenever the routing is updated.
  49. ///
  50. /// The callback is invoked after the routing is updated, but before components and hooks are
  51. /// updated.
  52. ///
  53. /// If the callback returns a [`NavigationTarget`] the router will replace the current location
  54. /// with it. If no navigation failure was triggered, the router will then updated dependent
  55. /// components and hooks.
  56. ///
  57. /// The callback is called no more than once per rerouting. It will not be called if a
  58. /// navigation failure occurs.
  59. ///
  60. /// Defaults to [`None`].
  61. pub fn on_update(
  62. self,
  63. callback: impl Fn(GenericRouterContext<R>) -> Option<NavigationTarget<R>> + 'static,
  64. ) -> Self {
  65. Self {
  66. on_update: Some(Arc::new(callback)),
  67. ..self
  68. }
  69. }
  70. /// The [`HistoryProvider`] the router should use.
  71. ///
  72. /// Defaults to a default [`MemoryHistory`].
  73. pub fn history(self, history: impl HistoryProvider<R> + 'static) -> Self {
  74. Self {
  75. history: Box::new(history),
  76. ..self
  77. }
  78. }
  79. /// A component to render when an external navigation fails.
  80. ///
  81. /// Defaults to a router-internal component called [`FailureExternalNavigation`]
  82. pub fn failure_external_navigation(self, component: fn(Scope) -> Element) -> Self {
  83. Self {
  84. failure_external_navigation: component,
  85. ..self
  86. }
  87. }
  88. }