1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//! 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" }
}
}
}
}