use crate::*;
use std::rc::Rc;
impl EuvRouteConfig {
pub fn new<F>(path: &'static str, component: F) -> Self
where
F: Fn() -> VirtualNode + 'static,
{
Self {
path,
component: Rc::new(component),
}
}
}
impl Router {
pub fn current_route() -> String {
let window: Window = window().expect("no global window exists");
let hash: String = window.location().hash().unwrap_or_default();
let route: String = hash.strip_prefix('#').unwrap_or(&hash).to_string();
if route.is_empty() {
"/".to_string()
} else {
route
}
}
pub fn navigate(route: &str) {
DEFERRED_NAVIGATION.with(|cell: &Cell<Option<String>>| cell.set(Some(route.to_string())));
let deferred_closure: Closure<dyn FnMut()> = Closure::wrap(Box::new(move || {
let target_route: Option<String> =
DEFERRED_NAVIGATION.with(|cell: &Cell<Option<String>>| cell.take());
if let Some(route_value) = target_route {
let nav_window: Window = web_sys::window().expect("no global window exists");
let nav_location: Location = nav_window.location();
let nav_new_hash: String = format!("#{}", route_value);
let _ = nav_location.set_hash(&nav_new_hash);
}
}));
let window: Window = window().expect("no global window exists");
window.queue_microtask(deferred_closure.as_ref().unchecked_ref::<Function>());
deferred_closure.forget();
}
pub fn link_handler(route: String) -> NativeEventHandler {
NativeEventHandler::create("click", move |event: Event| {
event.prevent_default();
Self::navigate(&route);
})
}
pub fn is_mobile() -> bool {
let window: Window = window().expect("no global window exists");
let width: f64 = window
.inner_width()
.ok()
.map(|value: JsValue| Number::from(value).value_of())
.unwrap_or(0.0);
width < MOBILE_BREAKPOINT as f64
}
}