mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-27 14:40:44 +00:00
feat: better link
This commit is contained in:
parent
a7e0fffb43
commit
8ca505b65b
1 changed files with 40 additions and 8 deletions
|
@ -34,6 +34,17 @@ pub struct LinkProps<'a> {
|
||||||
#[props(default, strip_option)]
|
#[props(default, strip_option)]
|
||||||
title: Option<&'a str>,
|
title: Option<&'a str>,
|
||||||
|
|
||||||
|
#[props(default = true)]
|
||||||
|
autodetect: bool,
|
||||||
|
|
||||||
|
/// Is this link an external link?
|
||||||
|
#[props(default = false)]
|
||||||
|
external: bool,
|
||||||
|
|
||||||
|
/// New tab?
|
||||||
|
#[props(default = false)]
|
||||||
|
new_tab: bool,
|
||||||
|
|
||||||
children: Element<'a>,
|
children: Element<'a>,
|
||||||
|
|
||||||
#[props(default)]
|
#[props(default)]
|
||||||
|
@ -41,17 +52,38 @@ pub struct LinkProps<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element {
|
pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element {
|
||||||
// log::trace!("render Link to {}", cx.props.to);
|
|
||||||
if let Some(service) = cx.consume_context::<RouterService>() {
|
if let Some(service) = cx.consume_context::<RouterService>() {
|
||||||
|
let LinkProps {
|
||||||
|
to,
|
||||||
|
href,
|
||||||
|
class,
|
||||||
|
id,
|
||||||
|
title,
|
||||||
|
autodetect,
|
||||||
|
external,
|
||||||
|
new_tab,
|
||||||
|
children,
|
||||||
|
..
|
||||||
|
} = cx.props;
|
||||||
|
|
||||||
|
let is_http = to.starts_with("http") || to.starts_with("https");
|
||||||
|
let outerlink = (*autodetect && is_http) || *external;
|
||||||
|
|
||||||
|
let prevent_default = if outerlink { "" } else { "onclick" };
|
||||||
|
|
||||||
return cx.render(rsx! {
|
return cx.render(rsx! {
|
||||||
a {
|
a {
|
||||||
href: "{cx.props.to}",
|
href: "{to}",
|
||||||
class: format_args!("{}", cx.props.class.unwrap_or("")),
|
class: format_args!("{}", class.unwrap_or("")),
|
||||||
id: format_args!("{}", cx.props.id.unwrap_or("")),
|
id: format_args!("{}", id.unwrap_or("")),
|
||||||
title: format_args!("{}", cx.props.title.unwrap_or("")),
|
title: format_args!("{}", title.unwrap_or("")),
|
||||||
|
prevent_default: "{prevent_default}",
|
||||||
prevent_default: "onclick",
|
target: format_args!("{}", if *new_tab { "_blank" } else { "" }),
|
||||||
onclick: move |_| service.push_route(cx.props.to),
|
onclick: move |_| {
|
||||||
|
if !outerlink {
|
||||||
|
service.push_route(to);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
&cx.props.children
|
&cx.props.children
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue