#![doc = include_str!("../YEW.md")]
use crate::common::{
Color, Placement, Shape, Size, Variant, base_anchor_style, base_badge_style, base_label_style,
};
use yew::prelude::*;
#[derive(Properties, PartialEq, Clone)]
pub struct AnchorProps {
#[prop_or_default]
pub children: Children,
#[prop_or_default]
pub class: &'static str,
#[prop_or_default]
pub style: &'static str,
#[prop_or_default]
pub id: &'static str,
#[prop_or_default]
pub data_testid: &'static str,
}
#[function_component(Anchor)]
pub fn badge_anchor(props: &AnchorProps) -> Html {
html! {
<span
id={props.id}
class={format!("badge-anchor {}", props.class)}
style={format!("{} {}", base_anchor_style(), props.style)}
data-testid={props.data_testid}
>
{ for props.children.iter() }
</span>
}
}
#[derive(Properties, PartialEq, Clone)]
pub struct LabelProps {
#[prop_or_default]
pub children: Children,
#[prop_or_default]
pub class: &'static str,
#[prop_or_default]
pub style: &'static str,
#[prop_or_default]
pub id: &'static str,
}
#[function_component(Label)]
pub fn badge_label(props: &LabelProps) -> Html {
html! {
<span
id={props.id}
class={format!("badge__label {}", props.class)}
style={format!("{} {}", base_label_style(), props.style)}
>
{ for props.children.iter() }
</span>
}
}
#[derive(Properties, PartialEq, Clone)]
pub struct BadgeProps {
#[prop_or_default]
pub children: Children,
#[prop_or_default]
pub color: Color,
#[prop_or_default]
pub variant: Variant,
#[prop_or_default]
pub size: Size,
#[prop_or_default]
pub placement: Placement,
#[prop_or_default]
pub shape: Shape,
#[prop_or_default]
pub class: &'static str,
#[prop_or_default]
pub style: &'static str,
#[prop_or_default]
pub id: &'static str,
#[prop_or("Badge")]
pub aria_label: &'static str,
#[prop_or_default]
pub data_testid: &'static str,
}
#[function_component(Badge)]
pub fn badge(props: &BadgeProps) -> Html {
let has_children = props.children.iter().next().is_some();
let color_style = match props.variant {
Variant::Primary => props.color.to_primary_style(),
Variant::Secondary => props.color.to_secondary_style(),
Variant::Soft => props.color.to_soft_style(),
};
let size_style = if has_children {
props.size.to_label_style()
} else {
props.size.to_dot_style()
};
let full_style = format!(
"{} {} {} {} {}",
base_badge_style(),
color_style,
size_style,
props.placement.to_style(props.shape),
props.style,
);
let badge_class = format!(
"badge {} {} {} {} {}",
props.variant.to_class(),
props.color.to_class(),
props.size.to_class(),
props.placement.to_class(),
props.class,
);
html! {
<span
id={props.id}
class={badge_class}
style={full_style}
role="status"
aria-label={props.aria_label}
aria-atomic="true"
data-testid={props.data_testid}
>
if has_children {
<Label>
{ for props.children.iter() }
</Label>
}
</span>
}
}