lumen_dioxus/content/
image.rs1use dioxus::prelude::*;
4
5use crate::class::compose;
6
7#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
9#[non_exhaustive]
10pub enum ImageFit {
11 #[default]
13 Cover,
14 Contain,
16}
17
18impl ImageFit {
19 pub fn as_str(self) -> &'static str {
21 match self {
22 Self::Cover => "cover",
23 Self::Contain => "contain",
24 }
25 }
26}
27
28#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
30#[non_exhaustive]
31pub enum ImageRadius {
32 None,
34 Sm,
36 Md,
38 #[default]
40 Lg,
41 Full,
43}
44
45impl ImageRadius {
46 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#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
60#[non_exhaustive]
61pub enum ImageLoading {
62 #[default]
64 Lazy,
65 Eager,
67}
68
69impl ImageLoading {
70 pub fn as_str(self) -> &'static str {
72 match self {
73 Self::Lazy => "lazy",
74 Self::Eager => "eager",
75 }
76 }
77}
78
79#[derive(Props, Clone, PartialEq)]
81pub struct ImageProps {
82 #[props(into)]
84 pub src: String,
85 #[props(into)]
87 pub alt: String,
88 #[props(default)]
90 pub fit: ImageFit,
91 #[props(default)]
93 pub radius: ImageRadius,
94 #[props(default)]
96 pub loading: ImageLoading,
97 #[props(default)]
99 pub invert_on_dark: bool,
100 #[props(into)]
102 pub width: Option<String>,
103 #[props(into)]
105 pub height: Option<String>,
106 pub onload: Option<EventHandler<ImageEvent>>,
108 pub onerror: Option<EventHandler<ImageEvent>>,
110 #[props(into)]
112 pub class: Option<String>,
113 #[props(extends = GlobalAttributes, extends = img)]
115 pub attributes: Vec<Attribute>,
116}
117
118#[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#[derive(Props, Clone, PartialEq)]
157pub struct CoverImageProps {
158 #[props(default)]
160 pub hover: bool,
161 #[props(default)]
163 pub show_bottom_gradient: bool,
164 #[props(into)]
166 pub class: Option<String>,
167 #[props(extends = GlobalAttributes, extends = div)]
169 pub attributes: Vec<Attribute>,
170 pub children: Element,
172}
173
174#[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}