lumen-dioxus 6.0.0

Lumen UI components for Dioxus: web, desktop, mobile, and Dioxus Native (Blitz).
Documentation
//! `Rating`, `Timeline` and `TimelineItem`, and `Descriptions` with its composable parts.

use dioxus::prelude::*;

use crate::class::compose;
use crate::hooks::use_controllable;
use crate::icons::{Icon, lucide};

/// Props for [`Rating`].
#[derive(Props, Clone, PartialEq)]
pub struct RatingProps {
    /// Controlled value. Leave unset to let the rating manage itself.
    pub value: Option<f64>,
    /// Initial value when uncontrolled.
    #[props(default)]
    pub default_value: f64,
    /// Number of stars. Defaults to 5.
    #[props(default = 5)]
    pub max: u32,
    /// Disables the stars and shows the rating as read-only.
    #[props(default)]
    pub readonly: bool,
    /// Called with the star the user picked, from 1 to `max`.
    pub on_value_change: Option<EventHandler<f64>>,
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<div>` or global attribute.
    #[props(extends = GlobalAttributes, extends = div)]
    pub attributes: Vec<Attribute>,
}

/// A row of star buttons. Each star is named `Rate {star} out of {max}`; activating one sets the
/// rating, which lumen-react leaves to the application.
#[component]
pub fn Rating(props: RatingProps) -> Element {
    let rating = use_controllable(props.value, || props.default_value, props.on_value_change);
    let value = rating.get();
    let max = props.max;
    let readonly = props.readonly;
    let class = compose([
        Some("ui-rating"),
        readonly.then_some("ui-rating--readonly"),
        props.class.as_deref(),
    ]);

    rsx! {
        div {
            class,
            "data-max": "{max}",
            "data-readonly": if readonly { "true" } else { "false" },
            "data-ui-rating": "true",
            "data-value": "{value}",
            ..props.attributes,
            for star in 1..=max {
                button {
                    key: "{star}",
                    aria_label: "Rate {star} out of {max}",
                    class: "ui-rating__star",
                    "data-active": if f64::from(star) <= value { "true" } else { "false" },
                    disabled: readonly,
                    r#type: "button",
                    onclick: move |_| {
                        if !readonly {
                            rating.set(f64::from(star));
                        }
                    },
                    Icon { icon: &lucide::STAR }
                }
            }
        }
    }
}

/// Props for [`Timeline`].
#[derive(Props, Clone, PartialEq)]
pub struct TimelineProps {
    /// Extra classes appended after the Lumen class.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<ol>` or global attribute.
    #[props(extends = GlobalAttributes, extends = ol)]
    pub attributes: Vec<Attribute>,
    /// The [`TimelineItem`]s.
    pub children: Element,
}

/// An ordered list of events.
#[component]
pub fn Timeline(props: TimelineProps) -> Element {
    let class = compose([Some("ui-timeline"), props.class.as_deref()]);

    rsx! {
        ol { class, "data-slot": "timeline", ..props.attributes, {props.children} }
    }
}

/// Props for [`TimelineItem`].
#[derive(Props, Clone, PartialEq)]
pub struct TimelineItemProps {
    /// Content of the marker, such as a step number or an icon.
    pub dot: Option<Element>,
    /// Extra classes appended after the Lumen class.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<li>` or global attribute.
    #[props(extends = GlobalAttributes, extends = li)]
    pub attributes: Vec<Attribute>,
    /// Event content.
    pub children: Element,
}

/// One event in a [`Timeline`]: a tail, a dot, and content.
#[component]
pub fn TimelineItem(props: TimelineItemProps) -> Element {
    let class = compose([Some("ui-timeline-item"), props.class.as_deref()]);

    rsx! {
        li { class, "data-slot": "timeline-item", ..props.attributes,
            div { class: "ui-timeline-item__tail", "data-slot": "timeline-item-tail" }
            div { class: "ui-timeline-item__dot", "data-slot": "timeline-item-dot",
                if let Some(dot) = props.dot {
                    {dot}
                }
            }
            div { class: "ui-timeline-item__content", "data-slot": "timeline-item-content", {props.children} }
        }
    }
}

/// One plain-text row of [`Descriptions`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DescriptionsItem {
    /// The term.
    pub label: String,
    /// The detail.
    pub value: String,
}

impl DescriptionsItem {
    /// A row with a term and a detail.
    pub fn new(label: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            label: label.into(),
            value: value.into(),
        }
    }
}

/// Props for [`Descriptions`].
#[derive(Props, Clone, PartialEq)]
pub struct DescriptionsProps {
    /// Number of columns. Defaults to 1.
    #[props(default = 1)]
    pub columns: u32,
    /// Plain-text rows rendered before the children.
    #[props(default)]
    pub items: Vec<DescriptionsItem>,
    /// Extra classes appended after the Lumen class.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<dl>` or global attribute.
    #[props(extends = GlobalAttributes, extends = dl)]
    pub attributes: Vec<Attribute>,
    /// Rich rows composed with [`DescriptionItem`], [`DescriptionTerm`], and [`DescriptionDetail`].
    pub children: Element,
}

/// A key-value description list laid out in columns.
#[component]
pub fn Descriptions(props: DescriptionsProps) -> Element {
    let class = compose([Some("ui-descriptions"), props.class.as_deref()]);
    let columns = props.columns;

    rsx! {
        dl {
            class,
            "data-columns": "{columns}",
            style: "--ui-descriptions-columns:{columns}",
            ..props.attributes,
            for (index, item) in props.items.into_iter().enumerate() {
                div { key: "{index}", class: "ui-descriptions__item",
                    dt { class: "ui-descriptions__term", "{item.label}" }
                    dd { class: "ui-descriptions__detail", "{item.value}" }
                }
            }
            {props.children}
        }
    }
}

/// Props for [`DescriptionItem`].
#[derive(Props, Clone, PartialEq)]
pub struct DescriptionItemProps {
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<div>` or global attribute.
    #[props(extends = GlobalAttributes, extends = div)]
    pub attributes: Vec<Attribute>,
    /// A [`DescriptionTerm`] and its [`DescriptionDetail`]s.
    pub children: Element,
}

/// One rich row of [`Descriptions`].
#[component]
pub fn DescriptionItem(props: DescriptionItemProps) -> Element {
    let class = compose([
        Some("ui-description-item ui-descriptions__item"),
        props.class.as_deref(),
    ]);

    rsx! {
        div { class, "data-slot": "description-item", ..props.attributes, {props.children} }
    }
}

/// Props for [`DescriptionTerm`].
#[derive(Props, Clone, PartialEq)]
pub struct DescriptionTermProps {
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<dt>` or global attribute.
    #[props(extends = GlobalAttributes, extends = dt)]
    pub attributes: Vec<Attribute>,
    /// The term.
    pub children: Element,
}

/// The term of a [`DescriptionItem`].
#[component]
pub fn DescriptionTerm(props: DescriptionTermProps) -> Element {
    let class = compose([
        Some("ui-description-term ui-descriptions__term"),
        props.class.as_deref(),
    ]);

    rsx! {
        dt { class, "data-slot": "description-term", ..props.attributes, {props.children} }
    }
}

/// Props for [`DescriptionDetail`].
#[derive(Props, Clone, PartialEq)]
pub struct DescriptionDetailProps {
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<dd>` or global attribute.
    #[props(extends = GlobalAttributes, extends = dd)]
    pub attributes: Vec<Attribute>,
    /// The detail.
    pub children: Element,
}

/// The detail of a [`DescriptionItem`].
#[component]
pub fn DescriptionDetail(props: DescriptionDetailProps) -> Element {
    let class = compose([
        Some("ui-description-detail ui-descriptions__detail"),
        props.class.as_deref(),
    ]);

    rsx! {
        dd { class, "data-slot": "description-detail", ..props.attributes, {props.children} }
    }
}