euv_ui/component/router/view/impl.rs
1use crate::*;
2
3use std::rc::Rc;
4
5/// Implementation of route configuration construction.
6impl EuvRouteConfig {
7 /// Creates a new route configuration.
8 ///
9 /// # Arguments
10 ///
11 /// - `&'static str` - The route path.
12 /// - `F: Fn() -> VirtualNode + 'static` - The component function.
13 ///
14 /// # Returns
15 ///
16 /// - `EuvRouteConfig` - The route configuration.
17 pub fn new<F>(path: &'static str, component: F) -> Self
18 where
19 F: Fn() -> VirtualNode + 'static,
20 {
21 Self {
22 path,
23 component: Rc::new(component),
24 }
25 }
26}
27
28/// Implementation of router navigation and viewport utilities.
29impl Router {
30 /// Reads the current hash-based route from the browser URL.
31 ///
32 /// # Returns
33 ///
34 /// - `String` - The hash fragment without the leading `#`, or `DEFAULT_ROUTE_PATH` if empty.
35 pub fn current_route() -> String {
36 let window: Window = window().expect("no global window exists");
37 let hash: String = window.location().hash().unwrap_or_default();
38 let route: String = hash
39 .strip_prefix(ROUTE_HASH_PREFIX)
40 .unwrap_or(&hash)
41 .to_string();
42 if route.is_empty() {
43 DEFAULT_ROUTE_PATH.to_string()
44 } else {
45 route
46 }
47 }
48
49 /// Navigates to a new hash-based route.
50 ///
51 /// Always defers the actual `location.set_hash()` call to `queueMicrotask`
52 /// to prevent synchronous `hashchange` dispatch while any caller frame is still
53 /// on the stack. This avoids wasm_bindgen's `"closure invoked recursively
54 /// or after being dropped"` error which occurs when `set_hash()` fires
55 /// `hashchange` synchronously and the handler (or the reactive update chain
56 /// it triggers) calls `navigate()` again before the original dispatch finishes.
57 ///
58 /// Multiple rapid `navigate()` calls before the microtask fires are coalesced:
59 /// only the **last** target route wins, as earlier routes were superseded by
60 ///
61 /// # Arguments
62 ///
63 /// - `R: AsRef<str>` - The target route path.
64 pub fn navigate<R>(route: R)
65 where
66 R: AsRef<str>,
67 {
68 let route_string: String = route.as_ref().to_string();
69 DEFERRED_NAVIGATION.with(|cell: &Cell<Option<String>>| cell.set(Some(route_string)));
70 let deferred_closure: Closure<dyn FnMut()> = Closure::wrap(Box::new(move || {
71 let target_route: Option<String> =
72 DEFERRED_NAVIGATION.with(|cell: &Cell<Option<String>>| cell.take());
73 if let Some(route_value) = target_route {
74 let nav_window: Window = web_sys::window().expect("no global window exists");
75 let nav_location: Location = nav_window.location();
76 let nav_new_hash: String = format!("{ROUTE_HASH_PREFIX}{route_value}");
77 let _ = nav_location.set_hash(&nav_new_hash);
78 }
79 }));
80 let window: Window = window().expect("no global window exists");
81 window.queue_microtask(deferred_closure.as_ref().unchecked_ref::<Function>());
82 deferred_closure.forget();
83 }
84
85 /// Creates a link click handler that navigates to the given route.
86 ///
87 /// Calls `event.prevent_default()` to prevent the `<a>` element's
88 /// default hash navigation, then programmatically navigates via
89 /// `navigate()`. Without `preventDefault`, both the `<a href>` default
90 /// behavior and `navigate()` would fire, potentially creating duplicate
91 /// history entries and causing incorrect browser back/forward behavior.
92 ///
93 /// # Arguments
94 ///
95 /// - `R: AsRef<str>` - The target route path.
96 ///
97 /// # Returns
98 ///
99 /// - `NativeEventHandler` - An event handler for click events.
100 pub fn link_handler<R>(route: R) -> NativeEventHandler
101 where
102 R: AsRef<str>,
103 {
104 let route_string: String = route.as_ref().to_string();
105 NativeEventHandler::create("click", move |event: Event| {
106 event.prevent_default();
107 Self::navigate(&route_string);
108 })
109 }
110
111 /// Checks whether the current viewport width qualifies as a mobile device.
112 ///
113 /// Uses `MOBILE_BREAKPOINT` (768px) as the threshold.
114 ///
115 /// # Returns
116 ///
117 /// - `bool` - `true` if the viewport width is less than the mobile breakpoint.
118 pub fn is_mobile() -> bool {
119 let window: Window = window().expect("no global window exists");
120 let width: f64 = window
121 .inner_width()
122 .ok()
123 .map(|value: JsValue| Number::from(value).value_of())
124 .unwrap_or(0.0);
125 width < MOBILE_BREAKPOINT as f64
126 }
127}