1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use std::borrow::Cow;

use dioxus::prelude::*;
use dioxus_router::prelude::{
    navigator,
    IntoRoutable,
};
use freya_elements::{
    elements as dioxus_elements,
    events::MouseEvent,
};
use freya_hooks::{
    use_applied_theme,
    LinkThemeWith,
};
use winit::event::MouseButton;

use crate::Tooltip;

/// Tooltip configuration for the [`Link`] component.
#[derive(Clone, PartialEq)]
pub enum LinkTooltip {
    /// No tooltip at all.
    None,
    /// Default tooltip.
    ///
    /// - For a route, this is the same as [`None`](crate::LinkTooltip::None).
    /// - For a URL, this is the value of that URL.
    Default,
    /// Custom tooltip to always show.
    Custom(String),
}

/// Similar to [`Link`](dioxus_router::components::Link), but you can use it in Freya.
/// Both internal routes (dioxus-router) and external links are supported. When using internal routes
/// make sure the Link is descendant of a [`Router`](dioxus_router::components::Router) component.
///
/// # Styling
///
/// Inherits the [`LinkTheme`](freya_hooks::LinkTheme) theme.
///
/// # Example
///
/// With Dioxus Router:
///
/// ```rust
/// # use dioxus::prelude::*;
/// # use dioxus_router::prelude::*;
/// # use freya_elements::elements as dioxus_elements;
/// # use freya_components::Link;
/// # #[derive(Routable, Clone)]
/// # #[rustfmt::skip]
/// # enum AppRouter {
/// #     #[route("/")]
/// #     Settings,
/// #     #[route("/..routes")]
/// #     NotFound
/// # }
/// # #[component]
/// # fn Settings() -> Element { rsx!(rect { })}
/// # #[component]
/// # fn NotFound() -> Element { rsx!(rect { })}
/// # fn link_example_good() -> Element {
/// rsx! {
///     Link {
///         to: AppRouter::Settings,
///         label { "App Settings" }
///     }
/// }
/// # }
/// ```
///
/// With external routes:
///
/// ```rust
/// # use dioxus::prelude::*;
/// # use freya_elements::elements as dioxus_elements;
/// # use freya_components::Link;
/// # fn link_example_good() -> Element {
/// rsx! {
///     Link {
///         to: "https://crates.io/crates/freya",
///         label { "Freya crates.io" }
///     }
/// }
/// # }
/// ```
#[allow(non_snake_case)]
#[component]
pub fn Link(
    /// Theme override.
    #[props(optional)]
    theme: Option<LinkThemeWith>,
    /// The route or external URL string to navigate to.
    #[props(into)]
    to: IntoRoutable,
    /// Inner children for the Link.
    children: Element,
    /// This event will be fired if opening an external link fails.
    #[props(optional)]
    onerror: Option<EventHandler<()>>,
    /// A little text hint to show when hovering over the anchor.
    ///
    /// Setting this to [`None`] is the same as [`LinkTooltip::Default`].
    /// To remove the tooltip, set this to [`LinkTooltip::None`].
    #[props(optional)]
    tooltip: Option<LinkTooltip>,
) -> Element {
    let theme = use_applied_theme!(&theme, link);
    let mut is_hovering = use_signal(|| false);

    let url = if let IntoRoutable::FromStr(ref url) = to {
        Some(url.clone())
    } else {
        None
    };

    let onmouseenter = move |_: MouseEvent| {
        is_hovering.set(true);
    };

    let onmouseleave = move |_: MouseEvent| {
        is_hovering.set(false);
    };

    let onclick = {
        to_owned![url, to];
        move |event: MouseEvent| {
            if !matches!(event.trigger_button, Some(MouseButton::Left)) {
                return;
            }

            // Open the url if there is any
            // otherwise change the dioxus router route
            if let Some(url) = &url {
                let res = open::that(url);

                if let (Err(_), Some(onerror)) = (res, onerror.as_ref()) {
                    onerror.call(());
                }

                // TODO(marc2332): Log unhandled errors
            } else {
                let router = navigator();
                router.push(to.clone());
            }
        }
    };

    let color = if *is_hovering.read() {
        theme.highlight_color
    } else {
        Cow::Borrowed("inherit")
    };

    let tooltip = match tooltip {
        None | Some(LinkTooltip::Default) => url.clone(),
        Some(LinkTooltip::None) => None,
        Some(LinkTooltip::Custom(str)) => Some(str),
    };

    let main_rect = rsx! {
        rect {
            onmouseenter,
            onmouseleave,
            onclick,
            color: "{color}",
            {children}
        }
    };

    let Some(tooltip) = tooltip else {
        return rsx!({ main_rect });
    };

    rsx! {
        {main_rect}
        rect {
            height: "0",
            layer: "-999",
            rect {
                width: "100v",
                if *is_hovering.read() {
                    Tooltip {
                        url: tooltip
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod test {
    use dioxus_router::prelude::{
        Outlet,
        Routable,
        Router,
    };
    use freya::prelude::*;
    use freya_testing::prelude::*;

    #[tokio::test]
    pub async fn link() {
        #[derive(Routable, Clone)]
        #[rustfmt::skip]
        enum Route {
            #[layout(Layout)]
            #[route("/")]
            Home,
            #[route("/somewhere")]
            Somewhere,
            #[route("/..routes")]
            NotFound
        }

        #[allow(non_snake_case)]
        #[component]
        fn NotFound() -> Element {
            rsx! {
                label {
                    "Not found"
                }
            }
        }

        #[allow(non_snake_case)]
        #[component]
        fn Home() -> Element {
            rsx! {
                label {
                    "Home"
                }
            }
        }

        #[allow(non_snake_case)]
        #[component]
        fn Somewhere() -> Element {
            rsx! {
                label {
                    "Somewhere"
                }
            }
        }

        #[allow(non_snake_case)]
        #[component]
        fn Layout() -> Element {
            rsx!(
                Link {
                    to: Route::Home,
                    Button {
                        label { "Home" }
                    }
                }
                Link {
                    to: Route::Somewhere,
                    Button {
                        label { "Somewhere" }
                    }
                }
                Outlet::<Route> {}
            )
        }

        fn link_app() -> Element {
            rsx!(Router::<Route> {})
        }

        let mut utils = launch_test(link_app);

        utils.wait_for_update().await;
        utils.wait_for_update().await;

        // Check route is Home
        assert_eq!(utils.root().get(2).get(0).text(), Some("Home"));

        // Go to the "Somewhere" route
        utils.push_event(PlatformEvent::Mouse {
            name: EventName::Click,
            cursor: (5., 70.).into(),
            button: Some(MouseButton::Left),
        });

        utils.wait_for_update().await;
        utils.wait_for_update().await;

        // Check route is Somewhere
        assert_eq!(utils.root().get(2).get(0).text(), Some("Somewhere"));

        // Go to the "Home" route again
        utils.push_event(PlatformEvent::Mouse {
            name: EventName::Click,
            cursor: (5., 5.).into(),
            button: Some(MouseButton::Left),
        });

        utils.wait_for_update().await;
    }
}