router.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. use crate::{cfg::RouterCfg, RouterContext, RouterService};
  2. use dioxus::prelude::*;
  3. /// The props for the [`Router`](fn.Router.html) component.
  4. #[derive(Props)]
  5. pub struct RouterProps<'a> {
  6. /// The routes and elements that should be rendered when the path matches.
  7. ///
  8. /// If elements are not contained within Routes, the will be rendered
  9. /// regardless of the path.
  10. pub children: Element<'a>,
  11. /// The URL to point at
  12. ///
  13. /// This will be used to trim any latent segments from the URL when your app is
  14. /// not deployed to the root of the domain.
  15. pub base_url: Option<&'a str>,
  16. /// Hook into the router when the route is changed.
  17. ///
  18. /// This lets you easily implement redirects
  19. #[props(default)]
  20. pub onchange: EventHandler<'a, RouterContext>,
  21. /// Set the active class of all Link components contained in this router.
  22. ///
  23. /// This is useful if you don't want to repeat the same `active_class` prop value in every Link.
  24. /// By default set to `"active"`.
  25. pub active_class: Option<&'a str>,
  26. /// Set the initial url.
  27. pub initial_url: Option<String>,
  28. }
  29. /// A component that conditionally renders children based on the current location of the app.
  30. ///
  31. /// Uses BrowserRouter in the browser and HashRouter everywhere else.
  32. ///
  33. /// Will fallback to HashRouter is BrowserRouter is not available, or through configuration.
  34. #[allow(non_snake_case)]
  35. pub fn Router<'a>(cx: Scope<'a, RouterProps<'a>>) -> Element {
  36. let svc = cx.use_hook(|| {
  37. cx.provide_context(RouterService::new(
  38. cx,
  39. RouterCfg {
  40. base_url: cx.props.base_url.map(|s| s.to_string()),
  41. active_class: cx.props.active_class.map(|s| s.to_string()),
  42. initial_url: cx.props.initial_url.clone(),
  43. },
  44. ))
  45. });
  46. // next time we run the rout_found will be filled
  47. if svc.route_found.get().is_none() {
  48. cx.props.onchange.call(svc.clone());
  49. }
  50. cx.render(rsx!(&cx.props.children))
  51. }