1use dioxus::prelude::*;
5
6use super::illustration_data::{IllustrationShape, IllustrationVariant};
7use super::shared::js_number;
8use crate::class::compose;
9
10fn has_label(label: Option<&str>) -> bool {
12 label.is_some_and(|text| !text.is_empty())
13}
14
15#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
17#[non_exhaustive]
18pub enum BackdropVariant {
19 #[default]
21 Aurora,
22 Dots,
24 Grid,
26 Rays,
28}
29
30impl BackdropVariant {
31 pub fn as_str(self) -> &'static str {
33 match self {
34 Self::Aurora => "aurora",
35 Self::Dots => "dots",
36 Self::Grid => "grid",
37 Self::Rays => "rays",
38 }
39 }
40}
41
42#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
44#[non_exhaustive]
45pub enum BackdropTone {
46 Accent,
48 #[default]
50 Brand,
51 Neutral,
53}
54
55impl BackdropTone {
56 pub fn as_str(self) -> &'static str {
58 match self {
59 Self::Accent => "accent",
60 Self::Brand => "brand",
61 Self::Neutral => "neutral",
62 }
63 }
64}
65
66#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
68#[non_exhaustive]
69pub enum BackdropIntensity {
70 Subtle,
72 #[default]
74 Medium,
75 Strong,
77}
78
79impl BackdropIntensity {
80 pub fn as_str(self) -> &'static str {
82 match self {
83 Self::Subtle => "subtle",
84 Self::Medium => "medium",
85 Self::Strong => "strong",
86 }
87 }
88}
89
90#[derive(Props, Clone, PartialEq)]
92pub struct BackdropProps {
93 #[props(default)]
95 pub intensity: BackdropIntensity,
96 #[props(default)]
98 pub tone: BackdropTone,
99 #[props(default)]
101 pub variant: BackdropVariant,
102 #[props(into)]
104 pub class: Option<String>,
105 #[props(extends = GlobalAttributes, extends = div)]
107 pub attributes: Vec<Attribute>,
108 pub children: Element,
110}
111
112#[component]
114pub fn Backdrop(props: BackdropProps) -> Element {
115 let variant = format!("ui-backdrop--{}", props.variant.as_str());
116 let tone = format!("ui-backdrop--{}", props.tone.as_str());
117 let intensity = format!("ui-backdrop--{}", props.intensity.as_str());
118 let class = compose([
119 Some("ui-backdrop"),
120 Some(variant.as_str()),
121 Some(tone.as_str()),
122 Some(intensity.as_str()),
123 props.class.as_deref(),
124 ]);
125
126 rsx! {
127 div { class, ..props.attributes,
128 div { class: "ui-backdrop__content", {props.children} }
129 }
130 }
131}
132
133#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
135#[non_exhaustive]
136pub enum GraphicSize {
137 Sm,
139 #[default]
141 Md,
142 Lg,
144}
145
146impl GraphicSize {
147 pub fn as_str(self) -> &'static str {
149 match self {
150 Self::Sm => "sm",
151 Self::Md => "md",
152 Self::Lg => "lg",
153 }
154 }
155}
156
157#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
159#[non_exhaustive]
160pub enum GraphicTone {
161 Accent,
163 #[default]
165 Brand,
166 Neutral,
168}
169
170impl GraphicTone {
171 pub fn as_str(self) -> &'static str {
173 match self {
174 Self::Accent => "accent",
175 Self::Brand => "brand",
176 Self::Neutral => "neutral",
177 }
178 }
179}
180
181#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
183#[non_exhaustive]
184pub enum GraphicVariant {
185 Glow,
187 Grid,
189 #[default]
191 Orbit,
192}
193
194impl GraphicVariant {
195 pub fn as_str(self) -> &'static str {
197 match self {
198 Self::Glow => "glow",
199 Self::Grid => "grid",
200 Self::Orbit => "orbit",
201 }
202 }
203}
204
205#[derive(Props, Clone, PartialEq)]
207pub struct GraphicProps {
208 #[props(into)]
210 pub label: Option<String>,
211 #[props(default)]
213 pub size: GraphicSize,
214 #[props(default)]
216 pub tone: GraphicTone,
217 #[props(default)]
219 pub variant: GraphicVariant,
220 #[props(into)]
222 pub class: Option<String>,
223 #[props(extends = GlobalAttributes, extends = div)]
225 pub attributes: Vec<Attribute>,
226 pub children: Element,
228}
229
230#[component]
232pub fn Graphic(props: GraphicProps) -> Element {
233 let labelled = has_label(props.label.as_deref());
234 let variant = format!("ui-graphic--{}", props.variant.as_str());
235 let tone = format!("ui-graphic--{}", props.tone.as_str());
236 let size = format!("ui-graphic--{}", props.size.as_str());
237 let class = compose([
238 Some("ui-graphic"),
239 Some(variant.as_str()),
240 Some(tone.as_str()),
241 Some(size.as_str()),
242 props.class.as_deref(),
243 ]);
244
245 rsx! {
246 div {
247 aria_hidden: (!labelled).then_some("true"),
248 aria_label: props.label,
249 class,
250 role: labelled.then_some("img"),
251 ..props.attributes,
252 div { class: "ui-graphic__content", {props.children} }
253 }
254 }
255}
256
257#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
259#[non_exhaustive]
260pub enum AnimatedLogoAnimation {
261 #[default]
263 Reveal,
264 Sequence,
266}
267
268impl AnimatedLogoAnimation {
269 pub fn as_str(self) -> &'static str {
271 match self {
272 Self::Reveal => "reveal",
273 Self::Sequence => "sequence",
274 }
275 }
276}
277
278#[derive(Props, Clone, PartialEq)]
280pub struct AnimatedLogoProps {
281 #[props(default)]
283 pub animation: AnimatedLogoAnimation,
284 #[props(into)]
286 pub class: Option<String>,
287 #[props(extends = GlobalAttributes, extends = span)]
289 pub attributes: Vec<Attribute>,
290 pub children: Element,
292}
293
294#[component]
299pub fn AnimatedLogo(props: AnimatedLogoProps) -> Element {
300 let class = compose([Some("ui-animated-logo"), props.class.as_deref()]);
301
302 rsx! {
303 span {
304 class,
305 "data-animation": props.animation.as_str(),
306 "data-ui-animated-logo": "true",
307 ..props.attributes,
308 {props.children}
309 }
310 }
311}
312
313#[derive(Props, Clone, PartialEq)]
315pub struct AnimatedPortraitProps {
316 #[props(into)]
318 pub class: Option<String>,
319 #[props(extends = GlobalAttributes, extends = div)]
321 pub attributes: Vec<Attribute>,
322 pub children: Element,
324}
325
326#[component]
331pub fn AnimatedPortrait(props: AnimatedPortraitProps) -> Element {
332 let class = compose([
333 Some(
334 "ui-animated-portrait relative isolate w-full animate-reveal-lcp motion-reduce:animate-none",
335 ),
336 props.class.as_deref(),
337 ]);
338
339 rsx! {
340 div { class, ..props.attributes, {props.children} }
341 }
342}
343
344#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
346#[non_exhaustive]
347pub enum IllustrationSize {
348 Sm,
350 #[default]
352 Md,
353 Lg,
355}
356
357impl IllustrationSize {
358 pub fn as_str(self) -> &'static str {
360 match self {
361 Self::Sm => "sm",
362 Self::Md => "md",
363 Self::Lg => "lg",
364 }
365 }
366}
367
368#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
370#[non_exhaustive]
371pub enum IllustrationTone {
372 Accent,
374 #[default]
376 Auto,
377 Brand,
379 Neutral,
381}
382
383impl IllustrationTone {
384 pub fn as_str(self) -> &'static str {
386 match self {
387 Self::Accent => "accent",
388 Self::Auto => "auto",
389 Self::Brand => "brand",
390 Self::Neutral => "neutral",
391 }
392 }
393}
394
395fn point_list(values: &[f64]) -> String {
396 let (pairs, _) = values.as_chunks::<2>();
397
398 pairs
399 .iter()
400 .map(|[x, y]| format!("{},{}", js_number(*x), js_number(*y)))
401 .collect::<Vec<_>>()
402 .join(" ")
403}
404
405fn render_shape(shape: IllustrationShape) -> Element {
406 match shape {
407 IllustrationShape::Circle { cx, cy, r } => rsx! {
408 circle { cx: js_number(cx), cy: js_number(cy), r: js_number(r) }
409 },
410 IllustrationShape::RoundedRect {
411 x,
412 y,
413 width,
414 height,
415 radius,
416 } => rsx! {
417 rect {
418 height: js_number(height),
419 rx: js_number(radius),
420 width: js_number(width),
421 x: js_number(x),
422 y: js_number(y),
423 }
424 },
425 IllustrationShape::Line(points) => {
426 let coordinate = |index: usize| points.get(index).copied().map(js_number);
427
428 rsx! {
429 line { x1: coordinate(0), x2: coordinate(2), y1: coordinate(1), y2: coordinate(3) }
430 }
431 }
432 IllustrationShape::Polygon(points) => rsx! {
433 polygon { points: point_list(points) }
434 },
435 IllustrationShape::Polyline(points) => rsx! {
436 polyline { points: point_list(points) }
437 },
438 }
439}
440
441#[derive(Props, Clone, PartialEq)]
443pub struct IllustrationProps {
444 #[props(into)]
446 pub label: Option<String>,
447 #[props(default)]
449 pub size: IllustrationSize,
450 #[props(default)]
452 pub tone: IllustrationTone,
453 #[props(default)]
455 pub variant: IllustrationVariant,
456 #[props(into)]
458 pub class: Option<String>,
459 #[props(extends = GlobalAttributes, extends = span)]
461 pub attributes: Vec<Attribute>,
462}
463
464#[component]
467pub fn Illustration(props: IllustrationProps) -> Element {
468 let labelled = has_label(props.label.as_deref());
469 let variant = format!("ui-illustration--{}", props.variant.as_str());
470 let tone = format!("ui-illustration--{}", props.tone.as_str());
471 let size = format!("ui-illustration--{}", props.size.as_str());
472 let class = compose([
473 Some("ui-illustration"),
474 Some(variant.as_str()),
475 Some(tone.as_str()),
476 Some(size.as_str()),
477 props.class.as_deref(),
478 ]);
479
480 rsx! {
481 span {
482 aria_hidden: (!labelled).then_some("true"),
483 aria_label: props.label,
484 class,
485 role: labelled.then_some("img"),
486 ..props.attributes,
487 svg { "aria-hidden": "true", fill: "none", view_box: "0 0 120 120",
488 circle { class: "ui-illustration__wash", cx: "60", cy: "60", r: "48" }
489 for shape in props.variant.shapes() {
490 {render_shape(*shape)}
491 }
492 }
493 }
494 }
495}
496
497#[cfg(test)]
498mod tests {
499 use super::point_list;
500
501 #[test]
502 fn point_lists_pair_coordinates() {
503 assert_eq!(point_list(&[1.0, 2.0, 3.5, 4.0]), "1,2 3.5,4");
504 assert_eq!(point_list(&[1.0, 2.0, 3.0]), "1,2");
505 assert_eq!(point_list(&[]), "");
506 }
507}