2021-11-03 04:35:56 +00:00
|
|
|
mod utils;
|
|
|
|
|
2021-12-01 03:48:05 +00:00
|
|
|
use std::{cell::RefCell, rc::Rc};
|
2021-11-03 04:35:56 +00:00
|
|
|
|
2021-12-25 22:18:05 +00:00
|
|
|
use dioxus::Attribute;
|
2021-11-03 04:35:56 +00:00
|
|
|
use dioxus_core as dioxus;
|
2021-12-25 22:18:05 +00:00
|
|
|
|
2021-11-03 04:35:56 +00:00
|
|
|
use dioxus_core::prelude::*;
|
2021-12-25 22:18:05 +00:00
|
|
|
use dioxus_core_macro::{format_args_f, rsx, Props};
|
2021-11-03 04:35:56 +00:00
|
|
|
use dioxus_html as dioxus_elements;
|
2021-12-19 04:03:59 +00:00
|
|
|
// use wasm_bindgen::{JsCast, JsValue};
|
2021-11-03 04:35:56 +00:00
|
|
|
|
2021-11-22 20:22:42 +00:00
|
|
|
use crate::utils::strip_slash_suffix;
|
2021-11-03 04:35:56 +00:00
|
|
|
|
2021-12-25 22:18:05 +00:00
|
|
|
/// Initialize the app's router service and provide access to `Link` components
|
|
|
|
pub fn use_router<R: 'static>(cx: &ScopeState, f: impl Fn(&str) -> R) -> &R {
|
|
|
|
let r = f("/");
|
|
|
|
cx.use_hook(
|
|
|
|
|_| {
|
|
|
|
//
|
|
|
|
r
|
|
|
|
},
|
|
|
|
|f| f,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-11-22 20:22:42 +00:00
|
|
|
pub trait Routable: 'static + Send + Clone + PartialEq {}
|
|
|
|
impl<T> Routable for T where T: 'static + Send + Clone + PartialEq {}
|
2021-11-19 05:49:04 +00:00
|
|
|
|
2021-11-03 04:35:56 +00:00
|
|
|
#[derive(Props)]
|
2021-12-15 02:46:19 +00:00
|
|
|
pub struct LinkProps<'a, R: Routable> {
|
2021-11-03 04:35:56 +00:00
|
|
|
to: R,
|
2021-11-22 20:22:42 +00:00
|
|
|
|
|
|
|
/// The url that gets pushed to the history stack
|
|
|
|
///
|
|
|
|
/// You can either put it your own inline method or just autoderive the route using `derive(Routable)`
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
///
|
|
|
|
/// Link { to: Route::Home, href: |_| "home".to_string() }
|
|
|
|
///
|
|
|
|
/// // or
|
|
|
|
///
|
|
|
|
/// Link { to: Route::Home, href: Route::as_url }
|
|
|
|
///
|
|
|
|
/// ```
|
2021-12-25 22:18:05 +00:00
|
|
|
#[props(default, setter(strip_option))]
|
|
|
|
href: Option<&'a str>,
|
|
|
|
|
|
|
|
#[props(default, setter(strip_option))]
|
|
|
|
class: Option<&'a str>,
|
2021-11-22 20:22:42 +00:00
|
|
|
|
2021-12-15 02:46:19 +00:00
|
|
|
children: Element<'a>,
|
2021-12-25 22:18:05 +00:00
|
|
|
|
|
|
|
#[props(default)]
|
|
|
|
attributes: Option<&'a [Attribute<'a>]>,
|
2021-11-03 04:35:56 +00:00
|
|
|
}
|
|
|
|
|
2021-12-15 02:46:19 +00:00
|
|
|
pub fn Link<'a, R: Routable>(cx: Scope<'a, LinkProps<'a, R>>) -> Element {
|
2021-12-25 22:18:05 +00:00
|
|
|
// let service = todo!();
|
2021-12-19 04:03:59 +00:00
|
|
|
// let service: todo!() = use_router_service::<R>(&cx)?;
|
2021-12-25 22:18:05 +00:00
|
|
|
let class = cx.props.class.unwrap_or("");
|
|
|
|
cx.render(rsx! {
|
|
|
|
a {
|
|
|
|
href: "#",
|
|
|
|
class: "{class}",
|
|
|
|
{&cx.props.children}
|
|
|
|
// onclick: move |_| service.push_route(cx.props.to.clone()),
|
|
|
|
// href: format_args!("{}", (cx.props.href)(&cx.props.to)),
|
|
|
|
}
|
|
|
|
})
|
2021-11-03 04:35:56 +00:00
|
|
|
}
|