impulse_thaw/link/
link.rs

1use leptos::{either::EitherOf3, prelude::*};
2use thaw_utils::{class_list, mount_style};
3
4#[component]
5pub fn Link(
6    #[prop(optional, into)] class: MaybeProp<String>,
7    #[prop(optional)] span: bool,
8    /// If true, changes styling when the link is being used alongside other text content.
9    #[prop(optional, into)]
10    inline: Signal<bool>,
11    #[prop(optional, into)] href: Option<Signal<String>>,
12    /// Whether the link is disabled.
13    #[prop(optional, into)]
14    disabled: Signal<bool>,
15    /// When set, allows the link to be focusable even when it has been disabled.
16    #[prop(optional, into)]
17    disabled_focusable: Signal<bool>,
18    children: Children,
19) -> impl IntoView {
20    mount_style("link", include_str!("./link.css"));
21
22    let link_disabled = Memo::new(move |_| disabled.get() || disabled_focusable.get());
23    let class = class_list![
24        "thaw-link",
25        ("thaw-link--inline", move || inline.get()),
26        ("thaw-link--disabled", move || link_disabled.get()),
27        ("thaw-link--disabled-focusable", move || link_disabled.get()),
28        class
29    ];
30
31    let tabindex = Memo::new(move |_| {
32        if disabled_focusable.get() {
33            Some("0")
34        } else if disabled.get() {
35            Some("-1")
36        } else {
37            None
38        }
39    });
40
41    if let Some(href) = href {
42        EitherOf3::A(view! {
43            <a
44                role="link"
45                class=class
46                href=href
47                tabindex=tabindex
48                aria-disabled=move || link_disabled.get().then_some("true")
49            >
50                {children()}
51            </a>
52        })
53    } else if span {
54        EitherOf3::B(view! { <span class=class>{children()}</span> })
55    } else {
56        EitherOf3::C(view! {
57            <button
58                class=class
59                disabled=move || disabled.get().then_some("")
60                aria-disabled=move || link_disabled.get().then_some("true")
61            >
62                {children()}
63            </button>
64        })
65    }
66}