Skip to main content

lumen_dioxus/content/
image.rs

1//! Images: `Image` and `CoverImage`.
2
3use dioxus::prelude::*;
4
5use crate::class::compose;
6
7/// How an [`Image`] fills its box.
8#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
9#[non_exhaustive]
10pub enum ImageFit {
11    /// Fill the box and crop the overflow.
12    #[default]
13    Cover,
14    /// Fit inside the box without cropping.
15    Contain,
16}
17
18impl ImageFit {
19    /// The class suffix and `data-fit` value.
20    pub fn as_str(self) -> &'static str {
21        match self {
22            Self::Cover => "cover",
23            Self::Contain => "contain",
24        }
25    }
26}
27
28/// Corner radius of an [`Image`].
29#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
30#[non_exhaustive]
31pub enum ImageRadius {
32    /// Square corners.
33    None,
34    /// Small radius.
35    Sm,
36    /// Medium radius.
37    Md,
38    /// Large radius.
39    #[default]
40    Lg,
41    /// Fully rounded.
42    Full,
43}
44
45impl ImageRadius {
46    /// The class suffix.
47    pub fn as_str(self) -> &'static str {
48        match self {
49            Self::None => "none",
50            Self::Sm => "sm",
51            Self::Md => "md",
52            Self::Lg => "lg",
53            Self::Full => "full",
54        }
55    }
56}
57
58/// Loading strategy of an [`Image`].
59#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
60#[non_exhaustive]
61pub enum ImageLoading {
62    /// Defer loading until the image nears the viewport.
63    #[default]
64    Lazy,
65    /// Load immediately.
66    Eager,
67}
68
69impl ImageLoading {
70    /// The `loading` attribute value.
71    pub fn as_str(self) -> &'static str {
72        match self {
73            Self::Lazy => "lazy",
74            Self::Eager => "eager",
75        }
76    }
77}
78
79/// Props for [`Image`].
80#[derive(Props, Clone, PartialEq)]
81pub struct ImageProps {
82    /// Image URL.
83    #[props(into)]
84    pub src: String,
85    /// Alternative text. Use an empty string for decorative images.
86    #[props(into)]
87    pub alt: String,
88    /// How the image fills its box.
89    #[props(default)]
90    pub fit: ImageFit,
91    /// Corner radius.
92    #[props(default)]
93    pub radius: ImageRadius,
94    /// Loading strategy. Lazy by default.
95    #[props(default)]
96    pub loading: ImageLoading,
97    /// Inverts the image in dark themes, for black line art and logos.
98    #[props(default)]
99    pub invert_on_dark: bool,
100    /// Intrinsic width in pixels, for the `width` attribute.
101    #[props(into)]
102    pub width: Option<String>,
103    /// Intrinsic height in pixels, for the `height` attribute.
104    #[props(into)]
105    pub height: Option<String>,
106    /// Called when the image loads.
107    pub onload: Option<EventHandler<ImageEvent>>,
108    /// Called when the image fails to load.
109    pub onerror: Option<EventHandler<ImageEvent>>,
110    /// Extra classes appended after the Lumen classes.
111    #[props(into)]
112    pub class: Option<String>,
113    /// Any other `<img>` or global attribute, such as `srcset` or `sizes`.
114    #[props(extends = GlobalAttributes, extends = img)]
115    pub attributes: Vec<Attribute>,
116}
117
118/// A responsive image with Lumen's fit, radius, and dark-mode treatments.
119#[component]
120pub fn Image(props: ImageProps) -> Element {
121    let fit = format!("ui-image--fit-{}", props.fit.as_str());
122    let radius = format!("ui-image--radius-{}", props.radius.as_str());
123    let class = compose([
124        Some("ui-image"),
125        props.invert_on_dark.then_some("ui-image--invert-dark"),
126        Some(fit.as_str()),
127        Some(radius.as_str()),
128        props.class.as_deref(),
129    ]);
130    let (onload, onerror) = (props.onload, props.onerror);
131
132    rsx! {
133        img {
134            alt: props.alt,
135            class,
136            height: props.height,
137            loading: props.loading.as_str(),
138            src: props.src,
139            width: props.width,
140            onload: move |event| {
141                if let Some(handler) = onload {
142                    handler.call(event);
143                }
144            },
145            onerror: move |event| {
146                if let Some(handler) = onerror {
147                    handler.call(event);
148                }
149            },
150            ..props.attributes,
151        }
152    }
153}
154
155/// Props for [`CoverImage`].
156#[derive(Props, Clone, PartialEq)]
157pub struct CoverImageProps {
158    /// Zooms the image slightly on hover.
159    #[props(default)]
160    pub hover: bool,
161    /// Adds a gradient at the bottom edge so overlaid text stays readable.
162    #[props(default)]
163    pub show_bottom_gradient: bool,
164    /// Extra classes appended after the Lumen classes.
165    #[props(into)]
166    pub class: Option<String>,
167    /// Any other `<div>` or global attribute.
168    #[props(extends = GlobalAttributes, extends = div)]
169    pub attributes: Vec<Attribute>,
170    /// The image, usually an `img` or [`Image`].
171    pub children: Element,
172}
173
174/// A cropped cover image frame for cards and article headers.
175#[component]
176pub fn CoverImage(props: CoverImageProps) -> Element {
177    let class = compose([
178        Some("ui-cover-image relative overflow-hidden bg-surface-muted"),
179        props.hover.then_some("ui-cover-image--hover"),
180        props.class.as_deref(),
181    ]);
182
183    rsx! {
184        div { class, ..props.attributes,
185            {props.children}
186            if props.show_bottom_gradient {
187                div { aria_hidden: "true", class: "ui-cover-image__bottom-gradient" }
188            }
189        }
190    }
191}