lumen-dioxus 6.0.0

Lumen UI components for Dioxus: web, desktop, mobile, and Dioxus Native (Blitz).
Documentation
//! Text and labelling primitives: `Eyebrow`, `FloatingBadge`, `Item`, `Kbd`, `Marker`, `Note`,
//! `Pill`, and `Typography`.

use dioxus::prelude::*;

use crate::class::{Glass, compose};

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

/// A short kicker shown above a heading.
#[component]
pub fn Eyebrow(props: EyebrowProps) -> Element {
    let class = compose([Some("ui-eyebrow"), props.class.as_deref()]);

    rsx! {
        p { class, ..props.attributes, {props.children} }
    }
}

/// Props for [`FloatingBadge`].
#[derive(Props, Clone, PartialEq)]
pub struct FloatingBadgeProps {
    /// Extra classes appended after the Lumen class.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<span>` or global attribute.
    #[props(extends = GlobalAttributes, extends = span)]
    pub attributes: Vec<Attribute>,
    /// Badge content, usually a count.
    pub children: Element,
}

/// A badge pinned to the corner of a positioned parent, such as an avatar or icon button.
#[component]
pub fn FloatingBadge(props: FloatingBadgeProps) -> Element {
    let class = compose([Some("ui-floating-badge"), props.class.as_deref()]);

    rsx! {
        span { class, ..props.attributes, {props.children} }
    }
}

/// The element an [`Item`] renders.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ItemElement {
    /// A `<div>`.
    #[default]
    Div,
    /// An `<article>`.
    Article,
    /// A `<li>`, for items inside a list.
    Li,
    /// A `<section>`.
    Section,
}

/// Props for [`Item`].
#[derive(Props, Clone, PartialEq)]
pub struct ItemProps {
    /// The rendered element. Use [`ItemElement::Li`] inside `ul` and `ol`.
    #[props(default)]
    pub r#as: ItemElement,
    /// Glass intensity.
    #[props(default, into)]
    pub glass: Glass,
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other global attribute.
    #[props(extends = GlobalAttributes)]
    pub attributes: Vec<Attribute>,
    /// Item content: media, title, description, and actions.
    pub children: Element,
}

/// A list row or tile that lines up media, text, and actions.
#[component]
pub fn Item(props: ItemProps) -> Element {
    let glass = props.glass.class("ui-item");
    let class = compose([Some("ui-item"), glass.as_deref(), props.class.as_deref()]);

    match props.r#as {
        ItemElement::Div => rsx! {
            div { class, "data-slot": "item", ..props.attributes, {props.children} }
        },
        ItemElement::Article => rsx! {
            article { class, "data-slot": "item", ..props.attributes, {props.children} }
        },
        ItemElement::Li => rsx! {
            li { class, "data-slot": "item", ..props.attributes, {props.children} }
        },
        ItemElement::Section => rsx! {
            section { class, "data-slot": "item", ..props.attributes, {props.children} }
        },
    }
}

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

/// A keyboard key or shortcut.
#[component]
pub fn Kbd(props: KbdProps) -> Element {
    let class = compose([Some("ui-kbd"), props.class.as_deref()]);

    rsx! {
        kbd { class, ..props.attributes, {props.children} }
    }
}

/// Tone of a [`Marker`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum MarkerVariant {
    /// Brand tone.
    #[default]
    Default,
    /// Positive status.
    Success,
    /// Cautionary status.
    Warning,
    /// Error status.
    Danger,
}

impl MarkerVariant {
    /// The class suffix and `data-variant` value.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Default => "default",
            Self::Success => "success",
            Self::Warning => "warning",
            Self::Danger => "danger",
        }
    }
}

/// Props for [`Marker`].
#[derive(Props, Clone, PartialEq)]
pub struct MarkerProps {
    /// Tone.
    #[props(default)]
    pub variant: MarkerVariant,
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<span>` or global attribute.
    #[props(extends = GlobalAttributes, extends = span)]
    pub attributes: Vec<Attribute>,
    /// Marker content, such as a step number.
    pub children: Element,
}

/// A small numbered or labelled point, such as a step or map pin label.
#[component]
pub fn Marker(props: MarkerProps) -> Element {
    let variant = (props.variant != MarkerVariant::Default)
        .then(|| format!("ui-marker--{}", props.variant.as_str()));
    let class = compose([
        Some("ui-marker"),
        variant.as_deref(),
        props.class.as_deref(),
    ]);

    rsx! {
        span {
            class,
            "data-variant": props.variant.as_str(),
            ..props.attributes,
            {props.children}
        }
    }
}

/// Where a [`Note`] draws its accent border.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum NoteBorderPosition {
    /// Border on the inline start edge.
    #[default]
    Left,
    /// Border on the inline end edge.
    Right,
    /// No accent border.
    None,
}

impl NoteBorderPosition {
    /// The class suffix.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Left => "left",
            Self::Right => "right",
            Self::None => "none",
        }
    }
}

/// Props for [`Note`].
#[derive(Props, Clone, PartialEq)]
pub struct NoteProps {
    /// Optional label shown above the content.
    #[props(into)]
    pub label: Option<String>,
    /// Accent border position.
    #[props(default)]
    pub border_position: NoteBorderPosition,
    /// 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>,
    /// Note content.
    pub children: Element,
}

/// An editorial aside with an optional label and an accent border.
#[component]
pub fn Note(props: NoteProps) -> Element {
    let border = format!("ui-note--border-{}", props.border_position.as_str());
    let class = compose([
        Some("ui-note"),
        Some(border.as_str()),
        props.class.as_deref(),
    ]);
    let label = props.label.filter(|label| !label.is_empty());

    rsx! {
        div { class, ..props.attributes,
            if let Some(label) = label {
                p { class: "ui-note__label", {label} }
            }
            div { class: "ui-note__content", {props.children} }
        }
    }
}

/// Visual treatment of a [`Pill`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum PillVariant {
    /// Quiet neutral fill.
    #[default]
    Neutral,
    /// Brand fill.
    Brand,
    /// Bordered.
    Outline,
}

impl PillVariant {
    /// The class suffix and `data-variant` value.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Neutral => "neutral",
            Self::Brand => "brand",
            Self::Outline => "outline",
        }
    }
}

/// Props for [`Pill`].
#[derive(Props, Clone, PartialEq)]
pub struct PillProps {
    /// Visual treatment.
    #[props(default)]
    pub variant: PillVariant,
    /// Optional count shown after the label.
    #[props(into)]
    pub count: Option<String>,
    /// Renders the pill as a link to this URL.
    #[props(into)]
    pub href: Option<String>,
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other global attribute.
    #[props(extends = GlobalAttributes)]
    pub attributes: Vec<Attribute>,
    /// Pill label.
    pub children: Element,
}

/// A rounded tag or filter chip, optionally a link, with an optional count.
#[component]
pub fn Pill(props: PillProps) -> Element {
    let variant = (props.variant != PillVariant::Neutral)
        .then(|| format!("ui-pill--{}", props.variant.as_str()));
    let class = compose([Some("ui-pill"), variant.as_deref(), props.class.as_deref()]);
    let content = rsx! {
        {props.children}
        if let Some(count) = props.count {
            span { class: "ui-pill__count", {count} }
        }
    };

    if let Some(href) = props.href {
        rsx! {
            a {
                class,
                "data-variant": props.variant.as_str(),
                href,
                ..props.attributes,
                {content}
            }
        }
    } else {
        rsx! {
            span {
                class,
                "data-variant": props.variant.as_str(),
                ..props.attributes,
                {content}
            }
        }
    }
}

/// Props for [`Typography`].
#[derive(Props, Clone, PartialEq)]
pub struct TypographyProps {
    /// Extra classes appended after the Lumen class.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<div>` or global attribute.
    #[props(extends = GlobalAttributes, extends = div)]
    pub attributes: Vec<Attribute>,
    /// Headings, paragraphs, and lists to style.
    pub children: Element,
}

/// Applies Lumen's type scale to plain HTML content inside it.
#[component]
pub fn Typography(props: TypographyProps) -> Element {
    let class = compose([Some("ui-typography"), props.class.as_deref()]);

    rsx! {
        div { class, ..props.attributes, {props.children} }
    }
}