euv_ui/component/router/view/impl.rs
1use super::*;
2
3/// Implementation of route configuration construction.
4impl EuvRouteConfig {
5 /// Creates a new route configuration.
6 ///
7 /// # Arguments
8 ///
9 /// - `&'static str` - The route path.
10 /// - `F: Fn() -> VirtualNode + 'static` - The component function.
11 ///
12 /// # Returns
13 ///
14 /// - `EuvRouteConfig` - The route configuration.
15 pub fn new<F>(path: &'static str, component: F) -> Self
16 where
17 F: Fn() -> VirtualNode + 'static,
18 {
19 Self {
20 path,
21 component: Rc::new(component),
22 }
23 }
24}
25
26/// Implementation of router navigation and viewport utilities.
27impl Router {
28 /// Reads the current hash-based route from the browser URL.
29 ///
30 /// # Returns
31 ///
32 /// - `String` - The hash fragment without the leading `#`, or `DEFAULT_ROUTE_PATH` if empty.
33 pub fn current_route() -> String {
34 let Some(window) = window() else {
35 return String::new();
36 };
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 Some(nav_window) = web_sys::window() else {
75 return;
76 };
77 let nav_location: Location = nav_window.location();
78 let nav_new_hash: String = format!("{ROUTE_HASH_PREFIX}{route_value}");
79 let _: Result<(), JsValue> = nav_location.set_hash(&nav_new_hash);
80 }
81 }));
82 let Some(window) = window() else {
83 return;
84 };
85 window.queue_microtask(deferred_closure.as_ref().unchecked_ref::<Function>());
86 deferred_closure.forget();
87 }
88
89 /// Creates a link click handler that navigates to the given route.
90 ///
91 /// Calls `event.prevent_default()` to prevent the `<a>` element's
92 /// default hash navigation, then programmatically navigates via
93 /// `navigate()`. Without `preventDefault`, both the `<a href>` default
94 /// behavior and `navigate()` would fire, potentially creating duplicate
95 /// history entries and causing incorrect browser back/forward behavior.
96 ///
97 /// # Arguments
98 ///
99 /// - `R: AsRef<str>` - The target route path.
100 ///
101 /// # Returns
102 ///
103 /// - `NativeEventHandler` - An event handler for click events.
104 pub fn link_handler<R>(route: R) -> NativeEventHandler
105 where
106 R: AsRef<str>,
107 {
108 let route_string: String = route.as_ref().to_string();
109 NativeEventHandler::create("click", move |event: Event| {
110 event.prevent_default();
111 Self::navigate(&route_string);
112 })
113 }
114
115 /// Checks whether the current viewport width qualifies as a mobile device.
116 ///
117 /// Uses `MOBILE_BREAKPOINT` (768px) as the threshold.
118 ///
119 /// # Returns
120 ///
121 /// - `bool` - `true` if the viewport width is less than the mobile breakpoint.
122 pub fn is_mobile() -> bool {
123 let Some(window) = window() else {
124 return false;
125 };
126 let width: f64 = window
127 .inner_width()
128 .ok()
129 .map(|value: JsValue| Number::from(value).value_of())
130 .unwrap_or_default();
131 width < MOBILE_BREAKPOINT as f64
132 }
133}