lumen-dioxus 6.0.0

Lumen UI components for Dioxus: web, desktop, mobile, and Dioxus Native (Blitz).
Documentation
//! `LumenTheme`: the color scheme and appearance preset for a subtree.

use dioxus::prelude::*;

use crate::class::compose;

/// Reactive, renderer-independent theme identity for isolated Native SVG images.
#[derive(Clone, Copy)]
pub(crate) struct ThemeContext(pub Signal<(LumenScheme, LumenPreset)>);

/// Light or dark color scheme.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum LumenScheme {
    /// Lumen light.
    #[default]
    Light,
    /// Lumen dark.
    Dark,
}

impl LumenScheme {
    /// The `data-theme` and `data-lumen-scheme` value.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Light => "light",
            Self::Dark => "dark",
        }
    }

    /// The other scheme, for theme toggles.
    #[must_use]
    pub fn toggled(self) -> Self {
        match self {
            Self::Light => Self::Dark,
            Self::Dark => Self::Light,
        }
    }
}

/// An optional Lumen appearance preset. Every preset has light and dark variants.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum LumenPreset {
    /// The default Lumen 6 appearance.
    #[default]
    Default,
    /// The Lumen 5 look.
    Classic,
    /// Warm ivory surfaces with burgundy actions and serif headings.
    Paper,
    /// Cool neutrals with cyan-teal actions and soft brand-tinted shadows.
    Luminous,
    /// Dense product UI: graphite neutrals, electric indigo actions, and a lime accent.
    Voltage,
    /// Expressive and graphic: vermilion actions, ink borders, and solid offset shadows.
    Signal,
    /// Dense and quiet pro-tool UI: pure grays with ink actions, one blue for selection, and flat
    /// bordered surfaces.
    Studio,
    /// Bright and expressive, for AI products and developer tools: a violet-to-blue gradient on
    /// filled brand surfaces, brand-tinted glow elevation, and Geist type. A renderer that
    /// cannot draw the gradient shows the solid brand color.
    Aura,
}

impl LumenPreset {
    /// The `data-lumen-preset` value.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Default => "default",
            Self::Classic => "classic",
            Self::Paper => "paper",
            Self::Luminous => "luminous",
            Self::Voltage => "voltage",
            Self::Signal => "signal",
            Self::Studio => "studio",
            Self::Aura => "aura",
        }
    }
}

/// Props for [`LumenTheme`].
#[derive(Props, Clone, PartialEq)]
pub struct LumenThemeProps {
    /// Color scheme for this subtree.
    #[props(default)]
    pub scheme: LumenScheme,
    /// Appearance preset for this subtree.
    #[props(default)]
    pub preset: LumenPreset,
    /// 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>,
    /// The themed content, usually the whole app.
    pub children: Element,
}

/// Applies a Lumen color scheme and appearance preset to its children, and paints the canvas
/// background, text color, and font.
///
/// Lumen's stylesheet reads `data-theme`, `data-lumen-scheme`, and `data-lumen-preset` from any
/// ancestor, so this works on every renderer, including Dioxus Native, where an app cannot set
/// attributes on `<html>`. Nest it to theme a section differently.
#[component]
pub fn LumenTheme(props: LumenThemeProps) -> Element {
    let mut current = use_signal(|| (props.scheme, props.preset));
    use_context_provider(|| ThemeContext(current));
    if *current.peek() != (props.scheme, props.preset) {
        current.set((props.scheme, props.preset));
    }
    let scheme = props.scheme.as_str();
    let class = compose([Some("ui-dioxus-theme"), props.class.as_deref()]);

    rsx! {
        div {
            class,
            "data-theme": scheme,
            "data-lumen-scheme": scheme,
            "data-lumen-preset": props.preset.as_str(),
            ..props.attributes,
            {props.children}
        }
    }
}

#[cfg(test)]
mod tests {
    use dioxus::prelude::*;

    use super::{LumenPreset, LumenScheme, LumenTheme};

    #[test]
    fn scheme_toggles_and_serializes() {
        assert_eq!(LumenScheme::Light.toggled(), LumenScheme::Dark);
        assert_eq!(LumenScheme::Dark.as_str(), "dark");
        assert_eq!(LumenPreset::default().as_str(), "default");
        assert_eq!(LumenPreset::Classic.as_str(), "classic");
        assert_eq!(LumenPreset::Paper.as_str(), "paper");
        assert_eq!(LumenPreset::Luminous.as_str(), "luminous");
        assert_eq!(LumenPreset::Voltage.as_str(), "voltage");
        assert_eq!(LumenPreset::Signal.as_str(), "signal");
        assert_eq!(LumenPreset::Studio.as_str(), "studio");
        assert_eq!(LumenPreset::Aura.as_str(), "aura");
    }

    #[test]
    fn aura_sets_the_preset_attribute_on_its_subtree() {
        let html = dioxus_ssr::render_element(rsx! {
            LumenTheme { scheme: LumenScheme::Dark, preset: LumenPreset::Aura, "Launch" }
        });

        assert!(html.contains(r#"data-lumen-preset="aura""#), "{html}");
        assert!(html.contains(r#"data-lumen-scheme="dark""#), "{html}");
    }

    #[test]
    fn aura_gradient_layers_over_a_solid_brand_fill() {
        let css = crate::LUMEN_CSS;

        assert!(css.contains(r#"[data-lumen-preset="aura"]"#));
        assert!(css.contains("--_aura-brand-fill: linear-gradient("));

        // The solid color stays under the gradient image, so a renderer that cannot draw the
        // gradient still paints the brand fill.
        let start = css
            .find(".ui-button--primary {")
            .expect("primary button rule");
        let end = start + css[start..].find('}').expect("primary button rule end");
        let rule = &css[start..end];
        assert!(
            rule.contains("background-color: hsl(var(--brand-solid));"),
            "{rule}"
        );
        assert!(rule.contains("var(--ui-brand-fill, none)"), "{rule}");
    }
}