clean out router
This commit is contained in:
15 files changed
+4
-1318
No files matched your search
@@ -1,139 +0,0 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{use_route, RouterCore};
|
||||
use dioxus::prelude::*;
|
||||
|
||||
/// Props for the [`Link`](struct.Link.html) component.
|
||||
#[derive(Props)]
|
||||
pub struct LinkProps<'a> {
|
||||
/// The route to link to. This can be a relative path, or a full URL.
|
||||
///
|
||||
/// ```rust, ignore
|
||||
/// // Absolute path
|
||||
/// Link { to: "/home", "Go Home" }
|
||||
///
|
||||
/// // Relative path
|
||||
/// Link { to: "../", "Go Up" }
|
||||
/// ```
|
||||
pub to: &'a str,
|
||||
|
||||
/// Set the class of the inner link ['a'](https://www.w3schools.com/tags/tag_a.asp) element.
|
||||
///
|
||||
/// This can be useful when styling the inner link element.
|
||||
#[props(default, strip_option)]
|
||||
pub class: Option<&'a str>,
|
||||
|
||||
/// Set the class added to the inner link when the current route is the same as the "to" route.
|
||||
///
|
||||
/// To set all of the active classes inside a Router at the same time use the `active_class`
|
||||
/// prop on the Router component. If both the Router prop as well as this prop are provided then
|
||||
/// this one has precedence. By default set to `"active"`.
|
||||
#[props(default, strip_option)]
|
||||
pub active_class: Option<&'a str>,
|
||||
|
||||
/// Set the ID of the inner link ['a'](https://www.w3schools.com/tags/tag_a.asp) element.
|
||||
///
|
||||
/// This can be useful when styling the inner link element.
|
||||
#[props(default, strip_option)]
|
||||
pub id: Option<&'a str>,
|
||||
|
||||
/// Set the title of the window after the link is clicked..
|
||||
#[props(default, strip_option)]
|
||||
pub title: Option<&'a str>,
|
||||
|
||||
/// Autodetect if a link is external or not.
|
||||
///
|
||||
/// This is automatically set to `true` and will use http/https detection
|
||||
#[props(default = true)]
|
||||
pub autodetect: bool,
|
||||
|
||||
/// Is this link an external link?
|
||||
#[props(default = false)]
|
||||
pub external: bool,
|
||||
|
||||
/// New tab?
|
||||
#[props(default = false)]
|
||||
pub new_tab: bool,
|
||||
|
||||
/// Pass children into the `<a>` element
|
||||
pub children: Element<'a>,
|
||||
}
|
||||
|
||||
/// A component that renders a link to a route.
|
||||
///
|
||||
/// `Link` components are just [`<a>`](https://www.w3schools.com/tags/tag_a.asp) elements
|
||||
/// that link to different pages *within* your single-page app.
|
||||
///
|
||||
/// If you need to link to a resource outside of your app, then just use a regular
|
||||
/// `<a>` element directly.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust, ignore
|
||||
/// fn Header(cx: Scope) -> Element {
|
||||
/// cx.render(rsx!{
|
||||
/// Link { to: "/home", "Go Home" }
|
||||
/// })
|
||||
/// }
|
||||
/// ```
|
||||
pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element {
|
||||
let svc = cx.use_hook(|| cx.consume_context::<Arc<RouterCore>>());
|
||||
|
||||
let LinkProps {
|
||||
to,
|
||||
class,
|
||||
id,
|
||||
title,
|
||||
autodetect,
|
||||
external,
|
||||
new_tab,
|
||||
children,
|
||||
active_class,
|
||||
..
|
||||
} = cx.props;
|
||||
|
||||
let is_http = to.starts_with("http") || to.starts_with("https");
|
||||
let outerlink = (*autodetect && is_http) || *external;
|
||||
let prevent_default = if outerlink { "" } else { "onclick" };
|
||||
|
||||
let active_class_name = match active_class {
|
||||
Some(c) => (*c).into(),
|
||||
None => {
|
||||
let active_from_router = match svc {
|
||||
Some(service) => service.cfg.active_class.clone(),
|
||||
None => None,
|
||||
};
|
||||
active_from_router.unwrap_or_else(|| "active".into())
|
||||
}
|
||||
};
|
||||
|
||||
let route = use_route(&cx);
|
||||
let url = route.url();
|
||||
let path = url.path();
|
||||
let active = path == cx.props.to;
|
||||
let active_class = if active { active_class_name } else { "".into() };
|
||||
|
||||
cx.render(rsx! {
|
||||
a {
|
||||
href: "{to}",
|
||||
class: format_args!("{} {}", class.unwrap_or(""), active_class),
|
||||
id: format_args!("{}", id.unwrap_or("")),
|
||||
title: format_args!("{}", title.unwrap_or("")),
|
||||
prevent_default: "{prevent_default}",
|
||||
target: format_args!("{}", if * new_tab { "_blank" } else { "" }),
|
||||
onclick: move |_| {
|
||||
if !outerlink {
|
||||
if let Some(service) = svc {
|
||||
service.push_route(to, cx.props.title.map(|f| f.to_string()), None);
|
||||
} else {
|
||||
log::error!(
|
||||
"Attempted to create a Link to {} outside of a Router context", cx.props
|
||||
.to,
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
children
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
use dioxus::prelude::*;
|
||||
|
||||
use crate::use_router;
|
||||
|
||||
/// The props for the [`Router`](fn.Router.html) component.
|
||||
#[derive(Props)]
|
||||
pub struct RedirectProps<'a> {
|
||||
/// The route to link to. This can be a relative path, or a full URL.
|
||||
///
|
||||
/// ```rust, ignore
|
||||
/// // Absolute path
|
||||
/// Redirect { from: "", to: "/home" }
|
||||
///
|
||||
/// // Relative path
|
||||
/// Redirect { from: "", to: "../" }
|
||||
/// ```
|
||||
pub to: &'a str,
|
||||
|
||||
/// The route to link from. This can be a relative path, or a full URL.
|
||||
///
|
||||
/// ```rust, ignore
|
||||
/// // Absolute path
|
||||
/// Redirect { from: "", to: "/home" }
|
||||
///
|
||||
/// // Relative path
|
||||
/// Redirect { from: "", to: "../" }
|
||||
/// ```
|
||||
pub from: Option<&'a str>,
|
||||
}
|
||||
|
||||
/// If this component is rendered, it will redirect the user to the given route.
|
||||
///
|
||||
/// It will replace the current route rather than pushing the current one to the stack.
|
||||
pub fn Redirect<'a>(cx: Scope<'a, RedirectProps<'a>>) -> Element {
|
||||
let router = use_router(&cx);
|
||||
|
||||
let immediate_redirect = cx.use_hook(|| {
|
||||
if let Some(from) = cx.props.from {
|
||||
router.register_total_route(from.to_string(), cx.scope_id());
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
});
|
||||
|
||||
if *immediate_redirect || router.should_render(cx.scope_id()) {
|
||||
router.replace_route(cx.props.to, None, None);
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
use dioxus::prelude::*;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{RouteContext, RouterCore};
|
||||
|
||||
/// Props for the [`Route`](struct.Route.html) component.
|
||||
#[derive(Props)]
|
||||
pub struct RouteProps<'a> {
|
||||
/// The path to match.
|
||||
pub to: &'a str,
|
||||
|
||||
/// The component to render when the path matches.
|
||||
pub children: Element<'a>,
|
||||
}
|
||||
|
||||
/// A component that conditionally renders children based on the current location.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
///```rust, ignore
|
||||
/// rsx!(
|
||||
/// Router {
|
||||
/// Route { to: "/home", Home {} }
|
||||
/// Route { to: "/about", About {} }
|
||||
/// Route { to: "/Blog", Blog {} }
|
||||
/// }
|
||||
/// )
|
||||
/// ```
|
||||
pub fn Route<'a>(cx: Scope<'a, RouteProps<'a>>) -> Element {
|
||||
let router_root = cx
|
||||
.use_hook(|| cx.consume_context::<Arc<RouterCore>>())
|
||||
.as_ref()?;
|
||||
|
||||
cx.use_hook(|| {
|
||||
// create a bigger, better, longer route if one above us exists
|
||||
let total_route = match cx.consume_context::<RouteContext>() {
|
||||
Some(ctx) => ctx.total_route,
|
||||
None => cx.props.to.to_string(),
|
||||
};
|
||||
|
||||
// provide our route context
|
||||
let route_context = cx.provide_context(RouteContext {
|
||||
declared_route: cx.props.to.to_string(),
|
||||
total_route,
|
||||
});
|
||||
|
||||
// submit our rout
|
||||
router_root.register_total_route(route_context.total_route, cx.scope_id());
|
||||
});
|
||||
|
||||
log::trace!("Checking Route: {:?}", cx.props.to);
|
||||
|
||||
if router_root.should_render(cx.scope_id()) {
|
||||
log::trace!("Route should render: {:?}", cx.scope_id());
|
||||
cx.render(rsx!(&cx.props.children))
|
||||
} else {
|
||||
log::trace!("Route should *not* render: {:?}", cx.scope_id());
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
use crate::{cfg::RouterCfg, RouterCore};
|
||||
use dioxus::prelude::*;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// The props for the [`Router`](fn.Router.html) component.
|
||||
#[derive(Props)]
|
||||
pub struct RouterProps<'a> {
|
||||
/// The routes and elements that should be rendered when the path matches.
|
||||
///
|
||||
/// If elements are not contained within Routes, the will be rendered
|
||||
/// regardless of the path.
|
||||
pub children: Element<'a>,
|
||||
|
||||
/// The URL to point at
|
||||
///
|
||||
/// This will be used to trim any latent segments from the URL when your app is
|
||||
/// not deployed to the root of the domain.
|
||||
pub base_url: Option<&'a str>,
|
||||
|
||||
/// Hook into the router when the route is changed.
|
||||
///
|
||||
/// This lets you easily implement redirects
|
||||
#[props(default)]
|
||||
pub onchange: EventHandler<'a, Arc<RouterCore>>,
|
||||
|
||||
/// Set the active class of all Link components contained in this router.
|
||||
///
|
||||
/// This is useful if you don't want to repeat the same `active_class` prop value in every Link.
|
||||
/// By default set to `"active"`.
|
||||
pub active_class: Option<&'a str>,
|
||||
|
||||
/// Set the initial url.
|
||||
pub initial_url: Option<String>,
|
||||
}
|
||||
|
||||
/// A component that conditionally renders children based on the current location of the app.
|
||||
///
|
||||
/// Uses BrowserRouter in the browser and HashRouter everywhere else.
|
||||
///
|
||||
/// Will fallback to HashRouter is BrowserRouter is not available, or through configuration.
|
||||
#[allow(non_snake_case)]
|
||||
pub fn Router<'a>(cx: Scope<'a, RouterProps<'a>>) -> Element {
|
||||
let svc = cx.use_hook(|| {
|
||||
cx.provide_context(RouterCore::new(
|
||||
&cx,
|
||||
RouterCfg {
|
||||
base_url: cx.props.base_url.map(|s| s.to_string()),
|
||||
active_class: cx.props.active_class.map(|s| s.to_string()),
|
||||
initial_url: cx.props.initial_url.clone(),
|
||||
},
|
||||
))
|
||||
});
|
||||
|
||||
// next time we run the rout_found will be filled
|
||||
if svc.route_found.get().is_none() {
|
||||
cx.props.onchange.call(svc.clone());
|
||||
}
|
||||
|
||||
cx.render(rsx!(&cx.props.children))
|
||||
}
|
||||
Reference in new issue
Block a user