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
use dioxus::prelude::*;
use freya_elements::elements as dioxus_elements;
use freya_elements::events::MouseEvent;
use freya_hooks::{use_applied_theme, ExternalLinkThemeWith};
use crate::Tooltip;
/// [`ExternalLink`] component properties.
#[derive(Props, Clone, PartialEq)]
pub struct ExternalLinkProps {
/// Theme override.
pub theme: Option<ExternalLinkThemeWith>,
/// Inner children for the ExternalLink.
pub children: Element,
/// Handler for the `onerror` event.
pub onerror: Option<EventHandler<()>>,
/// Whether to show a tooltip with the URL or not.
#[props(optional, default = true)]
pub show_tooltip: bool,
/// The ExternalLink destination URL.
#[props(into)]
pub url: String,
}
/// `Link` for external locations, e.g websites.
///
/// # Props
/// See [`ExternalLinkProps`].
///
/// # Styling
/// Inherits the [`ExternalLinkTheme`](freya_hooks::ExternalLinkTheme) theme.
///
/// # Example
///
/// ```no_run
/// # use freya::prelude::*;
/// fn app() -> Element {
/// rsx!(
/// ExternalLink {
/// url: "https://github.com",
/// label {
/// "GitHub"
/// }
/// }
/// )
/// }
/// ```
///
#[allow(non_snake_case)]
pub fn ExternalLink(props: ExternalLinkProps) -> Element {
let theme = use_applied_theme!(&props.theme, external_link);
let is_hovering = use_signal(|| false);
let onmouseover = move |_: MouseEvent| {
*is_hovering.write() = true;
};
let onmouseleave = move |_: MouseEvent| {
*is_hovering.write() = false;
};
let onclick = {
let url = props.url.clone();
move |_: MouseEvent| {
let res = open::that(url.clone());
if let (Err(_), Some(onerror)) = (res, props.onerror.as_ref()) {
onerror.call(());
}
// TODO(marc2332): Log unhandled errors
}
};
let color = if *is_hovering.read() {
theme.highlight_color.as_ref()
} else {
"inherit"
};
rsx!(
rect {
onmouseover,
onmouseleave,
onclick,
color: "{color}",
{props.children}
}
rect {
height: "0",
width: "0",
layer: "-999",
rect {
width: "100v",
if *is_hovering.read() && props.show_tooltip {
Tooltip {
url: props.url.clone()
}
}
}
}
)
}