lumen-dioxus 6.0.0

Lumen UI components for Dioxus: web, desktop, mobile, and Dioxus Native (Blitz).
Documentation
//! Images: `Image` and `CoverImage`.

use dioxus::prelude::*;

use crate::class::compose;

/// How an [`Image`] fills its box.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ImageFit {
    /// Fill the box and crop the overflow.
    #[default]
    Cover,
    /// Fit inside the box without cropping.
    Contain,
}

impl ImageFit {
    /// The class suffix and `data-fit` value.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Cover => "cover",
            Self::Contain => "contain",
        }
    }
}

/// Corner radius of an [`Image`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ImageRadius {
    /// Square corners.
    None,
    /// Small radius.
    Sm,
    /// Medium radius.
    Md,
    /// Large radius.
    #[default]
    Lg,
    /// Fully rounded.
    Full,
}

impl ImageRadius {
    /// The class suffix.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::None => "none",
            Self::Sm => "sm",
            Self::Md => "md",
            Self::Lg => "lg",
            Self::Full => "full",
        }
    }
}

/// Loading strategy of an [`Image`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ImageLoading {
    /// Defer loading until the image nears the viewport.
    #[default]
    Lazy,
    /// Load immediately.
    Eager,
}

impl ImageLoading {
    /// The `loading` attribute value.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Lazy => "lazy",
            Self::Eager => "eager",
        }
    }
}

/// Props for [`Image`].
#[derive(Props, Clone, PartialEq)]
pub struct ImageProps {
    /// Image URL.
    #[props(into)]
    pub src: String,
    /// Alternative text. Use an empty string for decorative images.
    #[props(into)]
    pub alt: String,
    /// How the image fills its box.
    #[props(default)]
    pub fit: ImageFit,
    /// Corner radius.
    #[props(default)]
    pub radius: ImageRadius,
    /// Loading strategy. Lazy by default.
    #[props(default)]
    pub loading: ImageLoading,
    /// Inverts the image in dark themes, for black line art and logos.
    #[props(default)]
    pub invert_on_dark: bool,
    /// Intrinsic width in pixels, for the `width` attribute.
    #[props(into)]
    pub width: Option<String>,
    /// Intrinsic height in pixels, for the `height` attribute.
    #[props(into)]
    pub height: Option<String>,
    /// Called when the image loads.
    pub onload: Option<EventHandler<ImageEvent>>,
    /// Called when the image fails to load.
    pub onerror: Option<EventHandler<ImageEvent>>,
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<img>` or global attribute, such as `srcset` or `sizes`.
    #[props(extends = GlobalAttributes, extends = img)]
    pub attributes: Vec<Attribute>,
}

/// A responsive image with Lumen's fit, radius, and dark-mode treatments.
#[component]
pub fn Image(props: ImageProps) -> Element {
    let fit = format!("ui-image--fit-{}", props.fit.as_str());
    let radius = format!("ui-image--radius-{}", props.radius.as_str());
    let class = compose([
        Some("ui-image"),
        props.invert_on_dark.then_some("ui-image--invert-dark"),
        Some(fit.as_str()),
        Some(radius.as_str()),
        props.class.as_deref(),
    ]);
    let (onload, onerror) = (props.onload, props.onerror);

    rsx! {
        img {
            alt: props.alt,
            class,
            height: props.height,
            loading: props.loading.as_str(),
            src: props.src,
            width: props.width,
            onload: move |event| {
                if let Some(handler) = onload {
                    handler.call(event);
                }
            },
            onerror: move |event| {
                if let Some(handler) = onerror {
                    handler.call(event);
                }
            },
            ..props.attributes,
        }
    }
}

/// Props for [`CoverImage`].
#[derive(Props, Clone, PartialEq)]
pub struct CoverImageProps {
    /// Zooms the image slightly on hover.
    #[props(default)]
    pub hover: bool,
    /// Adds a gradient at the bottom edge so overlaid text stays readable.
    #[props(default)]
    pub show_bottom_gradient: bool,
    /// 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>,
    /// The image, usually an `img` or [`Image`].
    pub children: Element,
}

/// A cropped cover image frame for cards and article headers.
#[component]
pub fn CoverImage(props: CoverImageProps) -> Element {
    let class = compose([
        Some("ui-cover-image relative overflow-hidden bg-surface-muted"),
        props.hover.then_some("ui-cover-image--hover"),
        props.class.as_deref(),
    ]);

    rsx! {
        div { class, ..props.attributes,
            {props.children}
            if props.show_bottom_gradient {
                div { aria_hidden: "true", class: "ui-cover-image__bottom-gradient" }
            }
        }
    }
}