Fix the router and stub out a number of crates to get compiling
This commit is contained in:
226 files changed
+1057
-1482
No files matched your search
@@ -4,9 +4,9 @@ use dioxus::prelude::*;
|
||||
|
||||
/// The default component to render when an external navigation fails.
|
||||
#[allow(non_snake_case)]
|
||||
pub fn FailureExternalNavigation(cx: Scope) -> Element {
|
||||
pub fn FailureExternalNavigation() -> Element {
|
||||
#[allow(deprecated)]
|
||||
let router = use_router(cx);
|
||||
let router = use_router();
|
||||
|
||||
render! {
|
||||
h1 { "External Navigation Failure!" }
|
||||
|
||||
@@ -4,10 +4,10 @@ use tracing::error;
|
||||
use crate::utils::use_router_internal::use_router_internal;
|
||||
|
||||
/// The properties for a [`GoBackButton`] or a [`GoForwardButton`].
|
||||
#[derive(Debug, Props)]
|
||||
pub struct HistoryButtonProps<'a> {
|
||||
#[derive(Debug, Props, Clone, PartialEq)]
|
||||
pub struct HistoryButtonProps {
|
||||
/// The children to render within the generated HTML button tag.
|
||||
pub children: Element<'a>,
|
||||
pub children: Element,
|
||||
}
|
||||
|
||||
/// A button to go back through the navigation history. Similar to a browsers back button.
|
||||
@@ -31,14 +31,14 @@ pub struct HistoryButtonProps<'a> {
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn App(cx: Scope) -> Element {
|
||||
/// fn App() -> Element {
|
||||
/// render! {
|
||||
/// Router::<Route> {}
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn Index(cx: Scope) -> Element {
|
||||
/// fn Index() -> Element {
|
||||
/// render! {
|
||||
/// GoBackButton {
|
||||
/// "go back"
|
||||
@@ -54,11 +54,14 @@ pub struct HistoryButtonProps<'a> {
|
||||
/// # );
|
||||
/// ```
|
||||
#[component]
|
||||
pub fn GoBackButton<'a>(cx: Scope<'a, HistoryButtonProps<'a>>) -> Element {
|
||||
let HistoryButtonProps { children } = cx.props;
|
||||
pub fn GoBackButton(
|
||||
/// The props...
|
||||
props: HistoryButtonProps,
|
||||
) -> Element {
|
||||
let HistoryButtonProps { children } = props;
|
||||
|
||||
// hook up to router
|
||||
let router = match use_router_internal(cx) {
|
||||
let router = match use_router_internal() {
|
||||
Some(r) => r,
|
||||
#[allow(unreachable_code)]
|
||||
None => {
|
||||
@@ -103,14 +106,14 @@ pub fn GoBackButton<'a>(cx: Scope<'a, HistoryButtonProps<'a>>) -> Element {
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn App(cx: Scope) -> Element {
|
||||
/// fn App() -> Element {
|
||||
/// render! {
|
||||
/// Router::<Route> {}
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn Index(cx: Scope) -> Element {
|
||||
/// fn Index() -> Element {
|
||||
/// render! {
|
||||
/// GoForwardButton {
|
||||
/// "go forward"
|
||||
@@ -126,11 +129,14 @@ pub fn GoBackButton<'a>(cx: Scope<'a, HistoryButtonProps<'a>>) -> Element {
|
||||
/// # );
|
||||
/// ```
|
||||
#[component]
|
||||
pub fn GoForwardButton<'a>(cx: Scope<'a, HistoryButtonProps<'a>>) -> Element {
|
||||
let HistoryButtonProps { children } = cx.props;
|
||||
pub fn GoForwardButton(
|
||||
/// Props...
|
||||
props: HistoryButtonProps,
|
||||
) -> Element {
|
||||
let HistoryButtonProps { children } = props;
|
||||
|
||||
// hook up to router
|
||||
let router = match use_router_internal(cx) {
|
||||
let router = match use_router_internal() {
|
||||
Some(r) => r,
|
||||
#[allow(unreachable_code)]
|
||||
None => {
|
||||
|
||||
@@ -22,6 +22,16 @@ pub enum IntoRoutable {
|
||||
Route(Rc<dyn Any>),
|
||||
}
|
||||
|
||||
impl PartialEq for IntoRoutable {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
match (self, other) {
|
||||
(IntoRoutable::FromStr(a), IntoRoutable::FromStr(b)) => a == b,
|
||||
(IntoRoutable::Route(a), IntoRoutable::Route(b)) => Rc::ptr_eq(a, b),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Routable> From<R> for IntoRoutable {
|
||||
fn from(value: R) -> Self {
|
||||
IntoRoutable::Route(Rc::new(value) as Rc<dyn Any>)
|
||||
@@ -68,26 +78,26 @@ impl From<&Url> for IntoRoutable {
|
||||
}
|
||||
|
||||
/// The properties for a [`Link`].
|
||||
#[derive(Props)]
|
||||
pub struct LinkProps<'a> {
|
||||
#[derive(Props, Clone, PartialEq)]
|
||||
pub struct LinkProps {
|
||||
/// A class to apply to the generate HTML anchor tag if the `target` route is active.
|
||||
pub active_class: Option<&'a str>,
|
||||
pub active_class: Option<String>,
|
||||
/// The children to render within the generated HTML anchor tag.
|
||||
pub children: Element<'a>,
|
||||
pub children: Element,
|
||||
/// The class attribute for the generated HTML anchor tag.
|
||||
///
|
||||
/// If `active_class` is [`Some`] and the `target` route is active, `active_class` will be
|
||||
/// appended at the end of `class`.
|
||||
pub class: Option<&'a str>,
|
||||
pub class: Option<String>,
|
||||
/// The id attribute for the generated HTML anchor tag.
|
||||
pub id: Option<&'a str>,
|
||||
pub id: Option<String>,
|
||||
/// When [`true`], the `target` route will be opened in a new tab.
|
||||
///
|
||||
/// This does not change whether the [`Link`] is active or not.
|
||||
#[props(default)]
|
||||
pub new_tab: bool,
|
||||
/// The onclick event handler.
|
||||
pub onclick: Option<EventHandler<'a, MouseEvent>>,
|
||||
pub onclick: Option<EventHandler<MouseEvent>>,
|
||||
#[props(default)]
|
||||
/// Whether the default behavior should be executed if an `onclick` handler is provided.
|
||||
///
|
||||
@@ -98,14 +108,14 @@ pub struct LinkProps<'a> {
|
||||
pub onclick_only: bool,
|
||||
/// The rel attribute for the generated HTML anchor tag.
|
||||
///
|
||||
/// For external `target`s, this defaults to `noopener noreferrer`.
|
||||
pub rel: Option<&'a str>,
|
||||
/// For external `a`s, this defaults to `noopener noreferrer`.
|
||||
pub rel: Option<String>,
|
||||
/// The navigation target. Roughly equivalent to the href attribute of an HTML anchor tag.
|
||||
#[props(into)]
|
||||
pub to: IntoRoutable,
|
||||
}
|
||||
|
||||
impl Debug for LinkProps<'_> {
|
||||
impl Debug for LinkProps {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("LinkProps")
|
||||
.field("active_class", &self.active_class)
|
||||
@@ -150,14 +160,14 @@ impl Debug for LinkProps<'_> {
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn App(cx: Scope) -> Element {
|
||||
/// fn App() -> Element {
|
||||
/// render! {
|
||||
/// Router::<Route> {}
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn Index(cx: Scope) -> Element {
|
||||
/// fn Index() -> Element {
|
||||
/// render! {
|
||||
/// render! {
|
||||
/// Link {
|
||||
@@ -182,7 +192,7 @@ impl Debug for LinkProps<'_> {
|
||||
/// # );
|
||||
/// ```
|
||||
#[allow(non_snake_case)]
|
||||
pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element {
|
||||
pub fn Link(props: LinkProps) -> Element {
|
||||
let LinkProps {
|
||||
active_class,
|
||||
children,
|
||||
@@ -194,10 +204,10 @@ pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element {
|
||||
rel,
|
||||
to,
|
||||
..
|
||||
} = cx.props;
|
||||
} = props;
|
||||
|
||||
// hook up to router
|
||||
let router = match use_router_internal(cx) {
|
||||
let router = match use_router_internal() {
|
||||
Some(r) => r,
|
||||
#[allow(unreachable_code)]
|
||||
None => {
|
||||
@@ -210,7 +220,7 @@ pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element {
|
||||
};
|
||||
|
||||
let current_url = router.current_route_string();
|
||||
let href = match to {
|
||||
let href = match &to {
|
||||
IntoRoutable::FromStr(url) => url.to_string(),
|
||||
IntoRoutable::Route(route) => router.any_route_to_string(&**route),
|
||||
};
|
||||
@@ -227,16 +237,17 @@ pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element {
|
||||
let is_router_nav = !is_external && !new_tab;
|
||||
let prevent_default = is_router_nav.then_some("onclick").unwrap_or_default();
|
||||
let rel = rel
|
||||
.or_else(|| is_external.then_some("noopener noreferrer"))
|
||||
.or_else(|| is_external.then_some("noopener noreferrer".to_string()))
|
||||
.unwrap_or_default();
|
||||
|
||||
let do_default = onclick.is_none() || !onclick_only;
|
||||
|
||||
let action = move |event| {
|
||||
if do_default && is_router_nav {
|
||||
router.push_any(router.resolve_into_routable(to.clone()));
|
||||
}
|
||||
|
||||
if let Some(handler) = onclick {
|
||||
if let Some(handler) = onclick.clone() {
|
||||
handler.call(event);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -30,7 +30,7 @@ use dioxus::prelude::*;
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn Index(cx: Scope) -> Element {
|
||||
/// fn Index() -> Element {
|
||||
/// render! {
|
||||
/// div {
|
||||
/// "Index"
|
||||
@@ -39,7 +39,7 @@ use dioxus::prelude::*;
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn Wrapper(cx: Scope) -> Element {
|
||||
/// fn Wrapper() -> Element {
|
||||
/// render! {
|
||||
/// h1 { "App" }
|
||||
/// Outlet::<Route> {} // The content of child routes will be rendered here
|
||||
@@ -47,7 +47,7 @@ use dioxus::prelude::*;
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn Child(cx: Scope) -> Element {
|
||||
/// fn Child() -> Element {
|
||||
/// render! {
|
||||
/// p {
|
||||
/// "Child"
|
||||
@@ -56,7 +56,7 @@ use dioxus::prelude::*;
|
||||
/// }
|
||||
///
|
||||
/// # #[component]
|
||||
/// # fn App(cx: Scope) -> Element {
|
||||
/// # fn App() -> Element {
|
||||
/// # render! {
|
||||
/// # Router::<Route> {
|
||||
/// # config: || RouterConfig::default().history(MemoryHistory::with_initial_path(Route::Child {}))
|
||||
@@ -68,6 +68,6 @@ use dioxus::prelude::*;
|
||||
/// # let _ = vdom.rebuild();
|
||||
/// # assert_eq!(dioxus_ssr::render(&vdom), "<h1>App</h1><p>Child</p>");
|
||||
/// ```
|
||||
pub fn Outlet<R: Routable + Clone>(cx: Scope) -> Element {
|
||||
OutletContext::<R>::render(cx)
|
||||
pub fn Outlet<R: Routable + Clone>() -> Element {
|
||||
OutletContext::<R>::render()
|
||||
}
|
||||
@@ -61,6 +61,15 @@ where
|
||||
config: RouterConfigFactory<R>,
|
||||
}
|
||||
|
||||
impl<T: Routable> Clone for RouterProps<T>
|
||||
where
|
||||
<T as FromStr>::Err: std::fmt::Display,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "serde"))]
|
||||
impl<R: Routable> Default for RouterProps<R>
|
||||
where
|
||||
@@ -111,26 +120,27 @@ where
|
||||
|
||||
#[cfg(not(feature = "serde"))]
|
||||
/// A component that renders the current route.
|
||||
pub fn Router<R: Routable + Clone>(cx: Scope<RouterProps<R>>) -> Element
|
||||
pub fn Router<R: Routable + Clone>(props: RouterProps<R>) -> Element
|
||||
where
|
||||
<R as FromStr>::Err: std::fmt::Display,
|
||||
{
|
||||
use crate::prelude::{outlet::OutletContext, RouterContext};
|
||||
|
||||
use_context_provider(cx, || {
|
||||
RouterContext::new(
|
||||
(cx.props
|
||||
.config
|
||||
.config
|
||||
.take()
|
||||
.expect("use_context_provider ran twice"))(),
|
||||
cx.schedule_update_any(),
|
||||
)
|
||||
});
|
||||
use_context_provider(cx, || OutletContext::<R> {
|
||||
current_level: 0,
|
||||
_marker: std::marker::PhantomData,
|
||||
});
|
||||
todo!();
|
||||
// use_context_provider(|| {
|
||||
// RouterContext::new(
|
||||
// (props
|
||||
// .config
|
||||
// .config
|
||||
// .take()
|
||||
// .expect("use_context_provider ran twice"))(),
|
||||
// schedule_update_any(),
|
||||
// )
|
||||
// });
|
||||
// use_context_provider(|| OutletContext::<R> {
|
||||
// current_level: 0,
|
||||
// _marker: std::marker::PhantomData,
|
||||
// });
|
||||
|
||||
render! {
|
||||
Outlet::<R> {}
|
||||
@@ -144,7 +154,7 @@ where
|
||||
<R as FromStr>::Err: std::fmt::Display,
|
||||
R: serde::Serialize + serde::de::DeserializeOwned,
|
||||
{
|
||||
use_context_provider(cx, || {
|
||||
use_context_provider(|| {
|
||||
RouterContext::new(
|
||||
(cx.props
|
||||
.config
|
||||
@@ -154,7 +164,7 @@ where
|
||||
cx.schedule_update_any(),
|
||||
)
|
||||
});
|
||||
use_context_provider(cx, || OutletContext::<R> {
|
||||
use_context_provider(|| OutletContext::<R> {
|
||||
current_level: 0,
|
||||
_marker: std::marker::PhantomData,
|
||||
});
|
||||
|
||||
@@ -16,34 +16,31 @@ impl<R> Clone for OutletContext<R> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn use_outlet_context<R: 'static>(cx: &ScopeState) -> &OutletContext<R> {
|
||||
let outlet_context = cx.use_hook(|| {
|
||||
cx.consume_context().unwrap_or(OutletContext::<R> {
|
||||
pub(crate) fn use_outlet_context<R: 'static>() -> OutletContext<R> {
|
||||
use_hook(|| {
|
||||
consume_context().unwrap_or(OutletContext::<R> {
|
||||
current_level: 1,
|
||||
_marker: std::marker::PhantomData,
|
||||
})
|
||||
});
|
||||
outlet_context
|
||||
})
|
||||
}
|
||||
|
||||
impl<R> OutletContext<R> {
|
||||
pub(crate) fn render(cx: Scope) -> Element<'_>
|
||||
pub(crate) fn render() -> Element
|
||||
where
|
||||
R: Routable + Clone,
|
||||
{
|
||||
let router = use_router_internal(cx)
|
||||
.as_ref()
|
||||
.expect("Outlet must be inside of a router");
|
||||
let outlet: &OutletContext<R> = use_outlet_context(cx);
|
||||
let router = use_router_internal().expect("Outlet must be inside of a router");
|
||||
let outlet: OutletContext<R> = use_outlet_context();
|
||||
let current_level = outlet.current_level;
|
||||
cx.provide_context({
|
||||
provide_context({
|
||||
OutletContext::<R> {
|
||||
current_level: current_level + 1,
|
||||
_marker: std::marker::PhantomData,
|
||||
}
|
||||
});
|
||||
|
||||
if let Some(error) = router.render_error(cx) {
|
||||
if let Some(error) = router.render_error() {
|
||||
if current_level == 0 {
|
||||
return Some(error);
|
||||
} else {
|
||||
@@ -51,6 +48,6 @@ impl<R> OutletContext<R> {
|
||||
}
|
||||
}
|
||||
|
||||
router.current::<R>().render(cx, current_level)
|
||||
router.current::<R>().render(current_level)
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
use std::cell::{RefCell, RefMut};
|
||||
use std::{
|
||||
any::Any,
|
||||
collections::HashSet,
|
||||
@@ -42,7 +43,7 @@ pub struct RouterContext {
|
||||
subscriber_update: Arc<dyn Fn(ScopeId)>,
|
||||
routing_callback: Option<AnyRoutingCallback>,
|
||||
|
||||
failure_external_navigation: fn(Scope) -> Element,
|
||||
failure_external_navigation: fn() -> Element,
|
||||
|
||||
any_route_to_string: fn(&dyn Any) -> String,
|
||||
}
|
||||
@@ -287,12 +288,12 @@ impl RouterContext {
|
||||
self.update_subscribers();
|
||||
}
|
||||
|
||||
pub(crate) fn render_error<'a>(&self, cx: Scope<'a>) -> Element<'a> {
|
||||
pub(crate) fn render_error(&self) -> Element {
|
||||
self.state
|
||||
.borrow()
|
||||
.unresolved_error
|
||||
.as_ref()
|
||||
.and_then(|_| (self.failure_external_navigation)(cx))
|
||||
.and_then(|_| (self.failure_external_navigation)())
|
||||
}
|
||||
|
||||
fn change_route(&self) -> Option<ExternalNavigationFailure> {
|
||||
|
||||
@@ -21,9 +21,9 @@ where
|
||||
/// # use dioxus_router::prelude::*;
|
||||
/// # use dioxus::prelude::*;
|
||||
/// # #[component]
|
||||
/// # fn Index(cx: Scope) -> Element { todo!() }
|
||||
/// # fn Index() -> Element { todo!() }
|
||||
/// # #[component]
|
||||
/// # fn OtherPage(cx: Scope) -> Element { todo!() }
|
||||
/// # fn OtherPage() -> Element { todo!() }
|
||||
/// #[derive(Clone, Routable, Debug, PartialEq)]
|
||||
/// enum Route {
|
||||
/// #[route("/")]
|
||||
|
||||
@@ -54,9 +54,9 @@ pub trait HistoryProvider<R: Routable> {
|
||||
/// # use dioxus_router::prelude::*;
|
||||
/// # use dioxus::prelude::*;
|
||||
/// # #[component]
|
||||
/// # fn Index(cx: Scope) -> Element { todo!() }
|
||||
/// # fn Index() -> Element { todo!() }
|
||||
/// # #[component]
|
||||
/// # fn OtherPage(cx: Scope) -> Element { todo!() }
|
||||
/// # fn OtherPage() -> Element { todo!() }
|
||||
/// #[derive(Clone, Routable, Debug, PartialEq)]
|
||||
/// enum Route {
|
||||
/// #[route("/")]
|
||||
@@ -90,10 +90,10 @@ pub trait HistoryProvider<R: Routable> {
|
||||
///
|
||||
/// ```rust
|
||||
/// # use dioxus_router::prelude::*;
|
||||
/// # use dioxus::prelude::*;
|
||||
/// # use dioxus::prelude::*;
|
||||
/// # #[component]
|
||||
/// # fn Index(cx: Scope) -> Element { todo!() }
|
||||
/// # fn Other(cx: Scope) -> Element { todo!() }
|
||||
/// # fn Index() -> Element { todo!() }
|
||||
/// # fn Other() -> Element { todo!() }
|
||||
/// #[derive(Clone, Routable, Debug, PartialEq)]
|
||||
/// enum Route {
|
||||
/// #[route("/")]
|
||||
@@ -121,9 +121,9 @@ pub trait HistoryProvider<R: Routable> {
|
||||
/// # use dioxus_router::prelude::*;
|
||||
/// # use dioxus::prelude::*;
|
||||
/// # #[component]
|
||||
/// # fn Index(cx: Scope) -> Element { todo!() }
|
||||
/// # fn Index() -> Element { todo!() }
|
||||
/// # #[component]
|
||||
/// # fn OtherPage(cx: Scope) -> Element { todo!() }
|
||||
/// # fn OtherPage() -> Element { todo!() }
|
||||
/// #[derive(Clone, Routable, Debug, PartialEq)]
|
||||
/// enum Route {
|
||||
/// #[route("/")]
|
||||
@@ -153,9 +153,9 @@ pub trait HistoryProvider<R: Routable> {
|
||||
/// # use dioxus_router::prelude::*;
|
||||
/// # use dioxus::prelude::*;
|
||||
/// # #[component]
|
||||
/// # fn Index(cx: Scope) -> Element { todo!() }
|
||||
/// # fn Index() -> Element { todo!() }
|
||||
/// # #[component]
|
||||
/// # fn OtherPage(cx: Scope) -> Element { todo!() }
|
||||
/// # fn OtherPage() -> Element { todo!() }
|
||||
/// #[derive(Clone, Routable, Debug, PartialEq)]
|
||||
/// enum Route {
|
||||
/// #[route("/")]
|
||||
@@ -186,9 +186,9 @@ pub trait HistoryProvider<R: Routable> {
|
||||
/// # use dioxus_router::prelude::*;
|
||||
/// # use dioxus::prelude::*;
|
||||
/// # #[component]
|
||||
/// # fn Index(cx: Scope) -> Element { todo!() }
|
||||
/// # fn Index() -> Element { todo!() }
|
||||
/// # #[component]
|
||||
/// # fn OtherPage(cx: Scope) -> Element { todo!() }
|
||||
/// # fn OtherPage() -> Element { todo!() }
|
||||
/// #[derive(Clone, Routable, Debug, PartialEq)]
|
||||
/// enum Route {
|
||||
/// #[route("/")]
|
||||
@@ -219,9 +219,9 @@ pub trait HistoryProvider<R: Routable> {
|
||||
/// # use dioxus_router::prelude::*;
|
||||
/// # use dioxus::prelude::*;
|
||||
/// # #[component]
|
||||
/// # fn Index(cx: Scope) -> Element { todo!() }
|
||||
/// # fn Index() -> Element { todo!() }
|
||||
/// # #[component]
|
||||
/// # fn OtherPage(cx: Scope) -> Element { todo!() }
|
||||
/// # fn OtherPage() -> Element { todo!() }
|
||||
/// #[derive(Clone, Routable, Debug, PartialEq)]
|
||||
/// enum Route {
|
||||
/// #[route("/")]
|
||||
@@ -248,9 +248,9 @@ pub trait HistoryProvider<R: Routable> {
|
||||
/// # use dioxus_router::prelude::*;
|
||||
/// # use dioxus::prelude::*;
|
||||
/// # #[component]
|
||||
/// # fn Index(cx: Scope) -> Element { todo!() }
|
||||
/// # fn Index() -> Element { todo!() }
|
||||
/// # #[component]
|
||||
/// # fn OtherPage(cx: Scope) -> Element { todo!() }
|
||||
/// # fn OtherPage() -> Element { todo!() }
|
||||
/// #[derive(Clone, Routable, Debug, PartialEq)]
|
||||
/// enum Route {
|
||||
/// #[route("/")]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use dioxus::prelude::ScopeState;
|
||||
use dioxus::prelude::{consume_context, use_hook};
|
||||
|
||||
use crate::prelude::{Navigator, RouterContext};
|
||||
|
||||
@@ -18,14 +18,14 @@ use crate::prelude::{Navigator, RouterContext};
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn App(cx: Scope) -> Element {
|
||||
/// fn App() -> Element {
|
||||
/// render! {
|
||||
/// Router::<Route> {}
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn Index(cx: Scope) -> Element {
|
||||
/// fn Index() -> Element {
|
||||
/// let navigator = use_navigator(&cx);
|
||||
///
|
||||
/// render! {
|
||||
@@ -37,7 +37,7 @@ use crate::prelude::{Navigator, RouterContext};
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn Dynamic(cx: Scope, id: usize) -> Element {
|
||||
/// fn Dynamic(id: usize) -> Element {
|
||||
/// render! {
|
||||
/// p {
|
||||
/// "Current ID: {id}"
|
||||
@@ -49,10 +49,9 @@ use crate::prelude::{Navigator, RouterContext};
|
||||
/// # let _ = vdom.rebuild();
|
||||
/// ```
|
||||
#[must_use]
|
||||
pub fn use_navigator(cx: &ScopeState) -> &Navigator {
|
||||
&*cx.use_hook(|| {
|
||||
let router = cx
|
||||
.consume_context::<RouterContext>()
|
||||
pub fn use_navigator() -> Navigator {
|
||||
use_hook(|| {
|
||||
let router = consume_context::<RouterContext>()
|
||||
.expect("Must be called in a descendant of a Router component");
|
||||
|
||||
Navigator(router)
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
use dioxus::prelude::ScopeState;
|
||||
|
||||
use crate::prelude::*;
|
||||
use crate::utils::use_router_internal::use_router_internal;
|
||||
|
||||
@@ -26,7 +24,7 @@ use crate::utils::use_router_internal::use_router_internal;
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn App(cx: Scope) -> Element {
|
||||
/// fn App() -> Element {
|
||||
/// render! {
|
||||
/// h1 { "App" }
|
||||
/// Router::<Route> {}
|
||||
@@ -34,7 +32,7 @@ use crate::utils::use_router_internal::use_router_internal;
|
||||
/// }
|
||||
///
|
||||
/// #[component]
|
||||
/// fn Index(cx: Scope) -> Element {
|
||||
/// fn Index() -> Element {
|
||||
/// let path: Route = use_route(&cx).unwrap();
|
||||
/// render! {
|
||||
/// h2 { "Current Path" }
|
||||
@@ -47,8 +45,8 @@ use crate::utils::use_router_internal::use_router_internal;
|
||||
/// # assert_eq!(dioxus_ssr::render(&vdom), "<h1>App</h1><h2>Current Path</h2><p>/</p>")
|
||||
/// ```
|
||||
#[must_use]
|
||||
pub fn use_route<R: Routable + Clone>(cx: &ScopeState) -> Option<R> {
|
||||
match use_router_internal(cx) {
|
||||
pub fn use_route<R: Routable + Clone>() -> Option<R> {
|
||||
match use_router_internal() {
|
||||
Some(r) => Some(r.current()),
|
||||
None => {
|
||||
#[cfg(debug_assertions)]
|
||||
|
||||
@@ -1,14 +1,10 @@
|
||||
use dioxus::prelude::ScopeState;
|
||||
|
||||
use crate::{prelude::RouterContext, utils::use_router_internal::use_router_internal};
|
||||
|
||||
#[deprecated = "prefer the `router()` function or `use_route` functions"]
|
||||
#[must_use]
|
||||
/// A hook that provides access to information about the router.
|
||||
pub fn use_router(cx: &ScopeState) -> &RouterContext {
|
||||
use_router_internal(cx)
|
||||
.as_ref()
|
||||
.expect("use_route must have access to a router")
|
||||
pub fn use_router() -> RouterContext {
|
||||
use_router_internal().expect("use_route must have access to a router")
|
||||
}
|
||||
|
||||
/// Aquire the router without subscribing to updates.
|
||||
|
||||
@@ -88,7 +88,7 @@ where
|
||||
W: tokio::io::AsyncWrite + Unpin + Send,
|
||||
{
|
||||
#[component]
|
||||
fn RenderPath<R>(cx: Scope, path: R) -> Element
|
||||
fn RenderPath<R>(path: R) -> Element
|
||||
where
|
||||
R: Routable,
|
||||
<R as FromStr>::Err: std::fmt::Display,
|
||||
|
||||
@@ -19,7 +19,7 @@ pub enum NavigationTarget<R> {
|
||||
/// # use dioxus_router::prelude::*;
|
||||
/// # use dioxus_router::navigation::NavigationTarget;
|
||||
/// # #[component]
|
||||
/// # fn Index(cx: Scope) -> Element {
|
||||
/// # fn Index() -> Element {
|
||||
/// # todo!()
|
||||
/// # }
|
||||
/// #[derive(Clone, Routable, PartialEq, Debug)]
|
||||
@@ -39,7 +39,7 @@ pub enum NavigationTarget<R> {
|
||||
/// # use dioxus_router::prelude::*;
|
||||
/// # use dioxus_router::navigation::NavigationTarget;
|
||||
/// # #[component]
|
||||
/// # fn Index(cx: Scope) -> Element {
|
||||
/// # fn Index() -> Element {
|
||||
/// # todo!()
|
||||
/// # }
|
||||
/// #[derive(Clone, Routable, PartialEq, Debug)]
|
||||
|
||||
@@ -203,7 +203,7 @@ pub trait Routable: FromStr + Display + Clone + 'static {
|
||||
const SITE_MAP: &'static [SiteMapSegment];
|
||||
|
||||
/// Render the route at the given level
|
||||
fn render<'a>(&self, cx: &'a ScopeState, level: usize) -> Element<'a>;
|
||||
fn render(&self, level: usize) -> Element;
|
||||
|
||||
/// Checks if this route is a child of the given route.
|
||||
///
|
||||
@@ -213,9 +213,9 @@ pub trait Routable: FromStr + Display + Clone + 'static {
|
||||
/// use dioxus::prelude::*;
|
||||
///
|
||||
/// #[component]
|
||||
/// fn Home(cx: Scope) -> Element { todo!() }
|
||||
/// fn Home() -> Element { todo!() }
|
||||
/// #[component]
|
||||
/// fn About(cx: Scope) -> Element { todo!() }
|
||||
/// fn About() -> Element { todo!() }
|
||||
///
|
||||
/// #[derive(Routable, Clone, PartialEq, Debug)]
|
||||
/// enum Route {
|
||||
@@ -255,9 +255,9 @@ pub trait Routable: FromStr + Display + Clone + 'static {
|
||||
/// use dioxus::prelude::*;
|
||||
///
|
||||
/// #[component]
|
||||
/// fn Home(cx: Scope) -> Element { todo!() }
|
||||
/// fn Home() -> Element { todo!() }
|
||||
/// #[component]
|
||||
/// fn About(cx: Scope) -> Element { todo!() }
|
||||
/// fn About() -> Element { todo!() }
|
||||
///
|
||||
/// #[derive(Routable, Clone, PartialEq, Debug)]
|
||||
/// enum Route {
|
||||
@@ -325,15 +325,15 @@ where
|
||||
}
|
||||
|
||||
trait RouteRenderable: Display + 'static {
|
||||
fn render<'a>(&self, cx: &'a ScopeState, level: usize) -> Element<'a>;
|
||||
fn render(&self, level: usize) -> Element;
|
||||
}
|
||||
|
||||
impl<R: Routable> RouteRenderable for R
|
||||
where
|
||||
<R as FromStr>::Err: Display,
|
||||
{
|
||||
fn render<'a>(&self, cx: &'a ScopeState, level: usize) -> Element<'a> {
|
||||
self.render(cx, level)
|
||||
fn render(&self, level: usize) -> Element {
|
||||
self.render(level)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ use crate::prelude::*;
|
||||
/// # use dioxus_router::prelude::*;
|
||||
/// # use dioxus::prelude::*;
|
||||
/// # #[component]
|
||||
/// # fn Index(cx: Scope) -> Element {
|
||||
/// # fn Index() -> Element {
|
||||
/// # todo!()
|
||||
/// # }
|
||||
/// #[derive(Clone, Routable)]
|
||||
@@ -25,7 +25,7 @@ use crate::prelude::*;
|
||||
/// let cfg = RouterConfig::default().history(WebHistory::<Route>::default());
|
||||
/// ```
|
||||
pub struct RouterConfig<R: Routable> {
|
||||
pub(crate) failure_external_navigation: fn(Scope) -> Element,
|
||||
pub(crate) failure_external_navigation: fn() -> Element,
|
||||
pub(crate) history: Option<Box<dyn AnyHistoryProvider>>,
|
||||
pub(crate) on_update: Option<RoutingCallback<R>>,
|
||||
}
|
||||
@@ -146,7 +146,7 @@ where
|
||||
/// A component to render when an external navigation fails.
|
||||
///
|
||||
/// Defaults to a router-internal component called [`FailureExternalNavigation`]
|
||||
pub fn failure_external_navigation(self, component: fn(Scope) -> Element) -> Self {
|
||||
pub fn failure_external_navigation(self, component: fn() -> Element) -> Self {
|
||||
Self {
|
||||
failure_external_navigation: component,
|
||||
..self
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use dioxus::prelude::{ScopeId, ScopeState};
|
||||
use dioxus::prelude::ScopeId;
|
||||
|
||||
use crate::prelude::*;
|
||||
|
||||
@@ -10,16 +10,18 @@ use crate::prelude::*;
|
||||
/// # Return values
|
||||
/// - [`None`], when the current component isn't a descendant of a [`Link`] component.
|
||||
/// - Otherwise [`Some`].
|
||||
pub(crate) fn use_router_internal(cx: &ScopeState) -> &Option<RouterContext> {
|
||||
let inner = cx.use_hook(|| {
|
||||
let router = cx.consume_context::<RouterContext>()?;
|
||||
pub(crate) fn use_router_internal() -> Option<RouterContext> {
|
||||
todo!()
|
||||
|
||||
let id = cx.scope_id();
|
||||
router.subscribe(id);
|
||||
// let inner = cx.use_hook(|| {
|
||||
// let router = cx.consume_context::<RouterContext>()?;
|
||||
|
||||
Some(Subscription { router, id })
|
||||
});
|
||||
cx.use_hook(|| inner.as_ref().map(|s| s.router.clone()))
|
||||
// let id = cx.scope_id();
|
||||
// router.subscribe(id);
|
||||
|
||||
// Some(Subscription { router, id })
|
||||
// });
|
||||
// cx.use_hook(|| inner.as_ref().map(|s| s.router.clone()))
|
||||
}
|
||||
|
||||
struct Subscription {
|
||||
|
||||
Reference in new issue
Block a user