use dioxus::prelude::*;
use crate::class::compose;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum LinkVariant {
#[default]
Default,
Inherit,
}
impl LinkVariant {
pub fn as_str(self) -> &'static str {
match self {
Self::Default => "default",
Self::Inherit => "inherit",
}
}
}
fn new_tab_rel(rel: Option<&str>) -> String {
let mut tokens: Vec<&str> = Vec::new();
for token in rel
.unwrap_or_default()
.split_whitespace()
.chain(["noopener", "noreferrer"])
{
if !tokens.contains(&token) {
tokens.push(token);
}
}
tokens.join(" ")
}
#[derive(Props, Clone, PartialEq)]
pub struct LinkProps {
#[props(default)]
pub new_tab: bool,
#[props(default)]
pub variant: LinkVariant,
#[props(into)]
pub rel: Option<String>,
#[props(into)]
pub target: Option<String>,
#[props(into)]
pub class: Option<String>,
pub onclick: Option<EventHandler<MouseEvent>>,
#[props(extends = GlobalAttributes, extends = a)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn Link(props: LinkProps) -> Element {
let class = compose([
Some("ui-link"),
(props.variant == LinkVariant::Inherit).then_some("ui-link--inherit"),
props.class.as_deref(),
]);
let rel = if props.new_tab {
Some(new_tab_rel(props.rel.as_deref()))
} else {
props.rel
};
let target = if props.new_tab {
Some("_blank".to_string())
} else {
props.target
};
let onclick = props.onclick;
rsx! {
a {
class,
"data-slot": "link",
"data-variant": props.variant.as_str(),
rel,
target,
onclick: move |event| {
if let Some(handler) = onclick {
handler.call(event);
}
},
..props.attributes,
{props.children}
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct SkipLinkProps {
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = a)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn SkipLink(props: SkipLinkProps) -> Element {
let class = compose([Some("ui-skip-link"), props.class.as_deref()]);
rsx! {
a { class, ..props.attributes, {props.children} }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ButtonLinkVariant {
#[default]
Primary,
Secondary,
Outline,
Ghost,
Inline,
Unstyled,
}
impl ButtonLinkVariant {
fn class(self) -> &'static str {
match self {
Self::Unstyled => "ui-button-link--unstyled",
Self::Inline => "ui-button ui-button--inline",
Self::Primary => {
"ui-button ui-button--primary rounded-[var(--ui-button-link-radius,9999px)] border"
}
Self::Secondary => {
"ui-button ui-button--secondary rounded-[var(--ui-button-link-radius,9999px)] border"
}
Self::Outline => {
"ui-button ui-button--outline rounded-[var(--ui-button-link-radius,9999px)] border"
}
Self::Ghost => {
"ui-button ui-button--ghost rounded-[var(--ui-button-link-radius,9999px)] border"
}
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ButtonLinkShape {
#[default]
Default,
Icon,
}
const BUTTON_LINK_LAYOUT: &str = "group inline-flex cursor-pointer items-center justify-center gap-2 text-sm font-semibold tracking-[0.01em]";
#[derive(Props, Clone, PartialEq)]
pub struct ButtonLinkProps {
#[props(default)]
pub variant: ButtonLinkVariant,
#[props(default)]
pub shape: ButtonLinkShape,
#[props(into)]
pub class: Option<String>,
pub onclick: Option<EventHandler<MouseEvent>>,
#[props(extends = GlobalAttributes, extends = a)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn ButtonLink(props: ButtonLinkProps) -> Element {
let styled = props.variant != ButtonLinkVariant::Unstyled;
let icon = props.shape == ButtonLinkShape::Icon && props.variant != ButtonLinkVariant::Inline;
let class = compose([
Some("ui-button-link"),
styled.then_some(BUTTON_LINK_LAYOUT),
Some(props.variant.class()),
icon.then_some("ui-button-link--icon"),
props.class.as_deref(),
]);
let onclick = props.onclick;
rsx! {
a {
class,
"data-slot": "button-link",
onclick: move |event| {
if let Some(handler) = onclick {
handler.call(event);
}
},
..props.attributes,
{props.children}
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct ButtonGroupProps {
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn ButtonGroup(props: ButtonGroupProps) -> Element {
let class = compose([Some("ui-button-group"), props.class.as_deref()]);
rsx! {
div { class, ..props.attributes, {props.children} }
}
}
#[cfg(test)]
mod tests {
use super::new_tab_rel;
#[test]
fn new_tab_rel_adds_tokens_once() {
assert_eq!(new_tab_rel(None), "noopener noreferrer");
assert_eq!(new_tab_rel(Some("me noopener")), "me noopener noreferrer");
assert_eq!(new_tab_rel(Some(" ")), "noopener noreferrer");
}
}