route.rs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. use dioxus_core::Element;
  2. use dioxus_core as dioxus;
  3. use dioxus_core::prelude::*;
  4. use dioxus_core_macro::Props;
  5. use dioxus_core_macro::*;
  6. use dioxus_html as dioxus_elements;
  7. use crate::{RouteContext, RouterService};
  8. #[derive(Props)]
  9. pub struct RouteProps<'a> {
  10. to: &'a str,
  11. children: Element<'a>,
  12. #[props(default)]
  13. fallback: bool,
  14. }
  15. pub fn Route<'a>(cx: Scope<'a, RouteProps<'a>>) -> Element {
  16. // now we want to submit
  17. let router_root = cx
  18. .use_hook(|_| cx.consume_context::<RouterService>())
  19. .as_ref()?;
  20. cx.use_hook(|_| {
  21. // create a bigger, better, longer route if one above us exists
  22. let total_route = match cx.consume_context::<RouteContext>() {
  23. Some(ctx) => ctx.total_route.to_string(),
  24. None => cx.props.to.to_string(),
  25. };
  26. // log::trace!("total route for {} is {}", cx.props.to, total_route);
  27. // provide our route context
  28. let route_context = cx.provide_context(RouteContext {
  29. declared_route: cx.props.to.to_string(),
  30. total_route,
  31. });
  32. // submit our rout
  33. router_root.register_total_route(
  34. route_context.total_route.clone(),
  35. cx.scope_id(),
  36. cx.props.fallback,
  37. );
  38. Some(RouteInner {})
  39. });
  40. // log::trace!("Checking route {}", cx.props.to);
  41. if router_root.should_render(cx.scope_id()) {
  42. cx.render(rsx!(&cx.props.children))
  43. } else {
  44. None
  45. }
  46. }
  47. struct RouteInner {}
  48. impl Drop for RouteInner {
  49. fn drop(&mut self) {
  50. // todo!()
  51. }
  52. }