create non generic wrappers for each route

This commit is contained in:
Evan Almloff committed 2023-05-23 19:31:30 -05:00
1 parent 3678fa1d3f
commit d3b21879fb
9 files changed
+58 -32

No files matched your search

@@ -1,4 +1,6 @@
use crate::{components::Link, hooks::use_route, navigation::NavigationTarget, routable::Routable};
use crate::{
components::GenericLink, hooks::use_route, navigation::NavigationTarget, routable::Routable,
};
use dioxus::prelude::*;
#[allow(non_snake_case)]
@@ -35,7 +37,7 @@ pub fn FailureNamedNavigation<R: Routable + Clone>(cx: Scope) -> Element {
"We are sorry for the inconvenience. The link below may help to fix the problem, but "
"there is no guarantee."
}
Link::<R> {
GenericLink::<R> {
target: NavigationTarget::External("https://google.com".into()),
"Click here to try to fix the failure."
}
@@ -55,7 +57,7 @@ pub fn FailureRedirectionLimit<R: Routable + Clone>(cx: Scope) -> Element {
"We are sorry for the inconvenience. The link below may help to fix the problem, but "
"there is no guarantee."
}
Link::<R> {
GenericLink::<R> {
target: NavigationTarget::External("https://google.com".into()),
"Click here to try to fix the failure."
}
+4 -4
View File
@@ -9,7 +9,7 @@ use crate::utils::use_router_internal::use_router_internal;
/// The properties for a [`Link`].
#[derive(Props)]
pub struct LinkProps<'a, R: Routable> {
pub struct GenericLinkProps<'a, R: Routable> {
/// A class to apply to the generate HTML anchor tag if the `target` route is active.
pub active_class: Option<&'a str>,
/// The children to render within the generated HTML anchor tag.
@@ -45,7 +45,7 @@ pub struct LinkProps<'a, R: Routable> {
pub target: NavigationTarget<R>,
}
impl<R: Routable> Debug for LinkProps<'_, R> {
impl<R: Routable> Debug for GenericLinkProps<'_, R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("LinkProps")
.field("active_class", &self.active_class)
@@ -125,8 +125,8 @@ impl<R: Routable> Debug for LinkProps<'_, R> {
/// # );
/// ```
#[allow(non_snake_case)]
pub fn Link<'a, R: Routable + Clone>(cx: Scope<'a, LinkProps<'a, R>>) -> Element {
let LinkProps {
pub fn GenericLink<'a, R: Routable + Clone>(cx: Scope<'a, GenericLinkProps<'a, R>>) -> Element {
let GenericLinkProps {
active_class,
children,
class,
+1 -1
View File
@@ -44,6 +44,6 @@ use dioxus::prelude::*;
/// # let _ = vdom.rebuild();
/// # assert_eq!(dioxus_ssr::render(&vdom), "<h1>App</h1><p>Content</p>");
/// ```
pub fn Outlet<R: Routable + Clone>(cx: Scope) -> Element {
pub fn GenericOutlet<R: Routable + Clone>(cx: Scope) -> Element {
OutletContext::render::<R>(cx)
}
+7 -7
View File
@@ -3,7 +3,7 @@ use log::error;
use std::{cell::RefCell, str::FromStr};
use crate::{
prelude::{Outlet, RouterContext},
prelude::{GenericOutlet, GenericRouterContext},
routable::Routable,
router_cfg::RouterConfiguration,
};
@@ -34,7 +34,7 @@ impl<R: Routable> From<RouterConfiguration<R>> for RouterCfg<R> {
/// The props for [`Router`].
#[derive(Props)]
pub struct RouterProps<R: Routable>
pub struct GenericRouterProps<R: Routable>
where
<R as FromStr>::Err: std::fmt::Display,
{
@@ -44,7 +44,7 @@ where
config: RouterCfg<R>,
}
impl<R: Routable> PartialEq for RouterProps<R>
impl<R: Routable> PartialEq for GenericRouterProps<R>
where
<R as FromStr>::Err: std::fmt::Display,
{
@@ -55,19 +55,19 @@ where
}
/// A component that renders the current route.
pub fn Router<R: Routable + Clone>(cx: Scope<RouterProps<R>>) -> Element
pub fn GenericRouter<R: Routable + Clone>(cx: Scope<GenericRouterProps<R>>) -> Element
where
<R as FromStr>::Err: std::fmt::Display,
{
use_context_provider(cx, || {
#[allow(unreachable_code, unused_variables)]
if let Some(outer) = cx.consume_context::<RouterContext<R>>() {
if let Some(outer) = cx.consume_context::<GenericRouterContext<R>>() {
let msg = "Router components should not be nested within each other";
error!("{msg}, inner will be inactive and transparent");
#[cfg(debug_assertions)]
panic!("{}", msg);
}
let router = RouterContext::new(
let router = GenericRouterContext::new(
cx.props.config.config.take().unwrap_or_default(),
cx.schedule_update_any(),
);
@@ -82,6 +82,6 @@ where
});
render! {
Outlet::<R> {}
GenericOutlet::<R> {}
}
}
+4 -4
View File
@@ -19,7 +19,7 @@ pub enum NavigationFailure<R: Routable> {
}
/// A function the router will call after every routing update.
pub type RoutingCallback<R> = Arc<dyn Fn(RouterContext<R>) -> Option<NavigationTarget<R>>>;
pub type RoutingCallback<R> = Arc<dyn Fn(GenericRouterContext<R>) -> Option<NavigationTarget<R>>>;
struct MutableRouterState<R>
where
@@ -43,7 +43,7 @@ where
}
/// A collection of router data that manages all routing functionality.
pub struct RouterContext<R>
pub struct GenericRouterContext<R>
where
R: Routable,
{
@@ -58,7 +58,7 @@ where
failure_redirection_limit: fn(Scope) -> Element,
}
impl<R: Routable> Clone for RouterContext<R> {
impl<R: Routable> Clone for GenericRouterContext<R> {
fn clone(&self) -> Self {
Self {
state: self.state.clone(),
@@ -72,7 +72,7 @@ impl<R: Routable> Clone for RouterContext<R> {
}
}
impl<R> RouterContext<R>
impl<R> GenericRouterContext<R>
where
R: Routable,
{
+3 -2
View File
@@ -1,11 +1,12 @@
use dioxus::prelude::ScopeState;
use crate::{
prelude::RouterContext, routable::Routable, utils::use_router_internal::use_router_internal,
prelude::GenericRouterContext, routable::Routable,
utils::use_router_internal::use_router_internal,
};
/// A hook that provides access to information about the router
pub fn use_router<R: Routable + Clone>(cx: &ScopeState) -> &RouterContext<R> {
pub fn use_generic_router<R: Routable + Clone>(cx: &ScopeState) -> &GenericRouterContext<R> {
use_router_internal(cx)
.as_ref()
.expect("use_route must have access to a router")
@@ -1,6 +1,6 @@
use dioxus::prelude::{ScopeId, ScopeState};
use crate::{contexts::router::RouterContext, routable::Routable};
use crate::{contexts::router::GenericRouterContext, routable::Routable};
/// A private hook to subscribe to the router.
///
@@ -12,9 +12,11 @@ use crate::{contexts::router::RouterContext, routable::Routable};
/// - Otherwise [`Some`].
///
/// [`use_router`]: crate::hooks::use_router
pub(crate) fn use_router_internal<R: Routable>(cx: &ScopeState) -> &Option<RouterContext<R>> {
pub(crate) fn use_router_internal<R: Routable>(
cx: &ScopeState,
) -> &Option<GenericRouterContext<R>> {
let inner = cx.use_hook(|| {
let router = cx.consume_context::<RouterContext<R>>()?;
let router = cx.consume_context::<GenericRouterContext<R>>()?;
let id = cx.scope_id();
router.subscribe(id);
@@ -25,7 +27,7 @@ pub(crate) fn use_router_internal<R: Routable>(cx: &ScopeState) -> &Option<Route
}
struct Subscription<R: Routable> {
router: RouterContext<R>,
router: GenericRouterContext<R>,
id: ScopeId,
}