use dioxus::prelude::*;
use crate::class::{Orientation, compose};
const DEFAULT_PROGRESS_MAX: f64 = 100.0;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ProgressState {
pub max: f64,
pub value: f64,
pub percentage: f64,
}
pub fn normalize_progress(max: f64, value: f64) -> ProgressState {
let max = if max.is_finite() && max > 0.0 {
max
} else {
DEFAULT_PROGRESS_MAX
};
let value = if value.is_finite() {
value.clamp(0.0, max)
} else {
0.0
};
ProgressState {
max,
value,
percentage: value / max * 100.0,
}
}
#[derive(Props, Clone, PartialEq)]
pub struct ProgressProps {
#[props(default)]
pub value: f64,
#[props(default = DEFAULT_PROGRESS_MAX)]
pub max: f64,
#[props(default)]
pub show_value: bool,
#[props(into)]
pub value_label: Option<String>,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
}
fn progress_value_text(state: ProgressState, label: Option<&str>) -> String {
label.map_or_else(
|| format!("{}%", (state.percentage + 0.5).floor()),
str::to_string,
)
}
#[component]
pub fn Progress(props: ProgressProps) -> Element {
let state = normalize_progress(props.max, props.value);
let show_value = props.show_value;
let class = compose([
Some("ui-progress"),
show_value.then_some("ui-progress--with-value"),
props.class.as_deref(),
]);
let value_label = props
.value_label
.filter(|label| show_value && !label.is_empty());
let value_text = show_value.then(|| progress_value_text(state, value_label.as_deref()));
rsx! {
div {
aria_valuemax: "{state.max}",
aria_valuemin: "0",
aria_valuenow: "{state.value}",
aria_valuetext: value_label,
class,
role: "progressbar",
..props.attributes,
if let Some(text) = value_text {
span { class: "ui-progress__track",
span {
class: "ui-progress__bar",
"data-slot": "progress-indicator",
style: "width: {state.percentage}%",
}
}
span { aria_hidden: "true", class: "ui-progress__value", "{text}" }
} else {
span {
class: "ui-progress__bar",
"data-slot": "progress-indicator",
style: "width: {state.percentage}%",
}
}
}
}
}
fn has_name(value: Option<&str>) -> bool {
value.is_some_and(|text| !text.trim().is_empty())
}
#[derive(Props, Clone, PartialEq)]
pub struct SpinnerProps {
#[props(into)]
pub aria_label: Option<String>,
#[props(into)]
pub aria_labelledby: Option<String>,
#[props(into)]
pub role: Option<String>,
pub aria_hidden: Option<bool>,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = span)]
pub attributes: Vec<Attribute>,
}
#[component]
pub fn Spinner(props: SpinnerProps) -> Element {
let named = has_name(props.aria_label.as_deref()) || has_name(props.aria_labelledby.as_deref());
let aria_hidden = match props.aria_hidden {
Some(hidden) => Some(hidden.to_string()),
None if named => None,
None => Some("true".to_string()),
};
let role = match props.role {
Some(role) => Some(role),
None if named => Some("status".to_string()),
None => None,
};
let class = compose([Some("ui-spinner"), props.class.as_deref()]);
rsx! {
span {
aria_hidden,
aria_label: props.aria_label,
aria_labelledby: props.aria_labelledby,
class,
role,
..props.attributes,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum AvatarSize {
Sm,
#[default]
Md,
Lg,
}
impl AvatarSize {
pub fn class(self) -> Option<&'static str> {
match self {
Self::Sm => Some("ui-avatar--sm"),
Self::Md => None,
Self::Lg => Some("ui-avatar--lg"),
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct AvatarProps {
#[props(into)]
pub src: Option<String>,
#[props(default, into)]
pub alt: String,
#[props(default, into)]
pub fallback: String,
#[props(default)]
pub size: AvatarSize,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = span)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn Avatar(props: AvatarProps) -> Element {
let class = compose([
Some("ui-avatar"),
props.size.class(),
props.class.as_deref(),
]);
let named = props.src.is_none() && !props.alt.is_empty();
let role = named.then_some("img");
let aria_label = named.then(|| props.alt.clone());
rsx! {
span { aria_label, class, role, ..props.attributes,
if let Some(src) = props.src {
img { alt: props.alt, src }
} else if !props.fallback.is_empty() {
span { {props.fallback} }
}
{props.children}
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct SeparatorProps {
#[props(default)]
pub orientation: Orientation,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = hr)]
pub attributes: Vec<Attribute>,
}
#[component]
pub fn Separator(props: SeparatorProps) -> Element {
let orientation = props.orientation.as_str();
let modifier = format!("ui-separator--{orientation}");
let class = compose([
Some("ui-separator"),
Some(modifier.as_str()),
props.class.as_deref(),
]);
rsx! {
hr {
aria_orientation: orientation,
class,
"data-orientation": orientation,
..props.attributes,
}
}
}
#[cfg(test)]
mod tests {
use super::{AvatarSize, normalize_progress, progress_value_text};
#[test]
fn progress_value_text_rounds_like_math_round() {
assert_eq!(
progress_value_text(normalize_progress(100.0, 62.0), None),
"62%"
);
assert_eq!(
progress_value_text(normalize_progress(100.0, 62.5), None),
"63%"
);
assert_eq!(
progress_value_text(normalize_progress(100.0, 62.49), None),
"62%"
);
assert_eq!(
progress_value_text(normalize_progress(3.0, 1.0), None),
"33%"
);
assert_eq!(
progress_value_text(normalize_progress(f64::NAN, f64::NAN), None),
"0%"
);
assert_eq!(
progress_value_text(normalize_progress(50.0, 80.0), None),
"100%"
);
assert_eq!(
progress_value_text(normalize_progress(100.0, 62.0), Some("3 of 5 files")),
"3 of 5 files"
);
assert_eq!(
progress_value_text(normalize_progress(100.0, 62.0), Some("")),
""
);
}
#[test]
fn avatar_sizes_map_to_classes() {
assert_eq!(AvatarSize::default(), AvatarSize::Md);
assert_eq!(AvatarSize::Sm.class(), Some("ui-avatar--sm"));
assert_eq!(AvatarSize::Md.class(), None);
assert_eq!(AvatarSize::Lg.class(), Some("ui-avatar--lg"));
}
#[test]
fn progress_clamps_like_lumen_core() {
let state = normalize_progress(100.0, 62.0);
assert!((state.percentage - 62.0).abs() < f64::EPSILON);
let over = normalize_progress(50.0, 80.0);
assert!((over.value - 50.0).abs() < f64::EPSILON);
assert!((over.percentage - 100.0).abs() < f64::EPSILON);
let invalid = normalize_progress(f64::NAN, f64::INFINITY);
assert!((invalid.max - 100.0).abs() < f64::EPSILON);
assert!(invalid.value.abs() < f64::EPSILON);
let negative = normalize_progress(-5.0, -10.0);
assert!((negative.max - 100.0).abs() < f64::EPSILON);
assert!(negative.value.abs() < f64::EPSILON);
}
}