Skip to main content

orbital_base_components/data/link/
base.rs

1use leptos::{either::EitherOf3, prelude::*};
2
3#[component]
4pub fn BaseLink(
5    #[prop(optional, into)] class: MaybeProp<String>,
6    #[prop(optional)] span: bool,
7    #[prop(optional, into)] inline: Signal<bool>,
8    #[prop(optional)] href: Option<Signal<String>>,
9    #[prop(optional, into)] disabled: Signal<bool>,
10    #[prop(optional, into)] disabled_focusable: Signal<bool>,
11    children: Children,
12) -> impl IntoView {
13    let link_disabled = Memo::new(move |_| disabled.get() || disabled_focusable.get());
14    let merged_class = move || {
15        let mut parts = vec!["orbital-link".to_string()];
16        if inline.get() {
17            parts.push("orbital-link--inline".to_string());
18        }
19        if link_disabled.get() {
20            parts.push("orbital-link--disabled".to_string());
21            parts.push("orbital-link--disabled-focusable".to_string());
22        }
23        if let Some(extra) = class.get() {
24            if !extra.is_empty() {
25                parts.push(extra);
26            }
27        }
28        parts.join(" ")
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=merged_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=merged_class>{children()}</span> })
55    } else {
56        EitherOf3::C(view! {
57            <button
58                class=merged_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}