lumen-dioxus 6.0.0

Lumen UI components for Dioxus: web, desktop, mobile, and Dioxus Native (Blitz).
Documentation
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! Brand and decoration surfaces: `Backdrop`, `Graphic`, `AnimatedLogo`, `AnimatedPortrait`,
//! and `Illustration`.

use dioxus::prelude::*;

use super::illustration_data::{IllustrationShape, IllustrationVariant};
use super::shared::js_number;
use crate::class::compose;

/// Whether an optional label names the element, like a truthy `label` in lumen-react.
fn has_label(label: Option<&str>) -> bool {
    label.is_some_and(|text| !text.is_empty())
}

/// Pattern drawn by a [`Backdrop`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum BackdropVariant {
    /// Soft aurora gradients.
    #[default]
    Aurora,
    /// A dot grid.
    Dots,
    /// A line grid.
    Grid,
    /// Light rays.
    Rays,
}

impl BackdropVariant {
    /// The class suffix.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Aurora => "aurora",
            Self::Dots => "dots",
            Self::Grid => "grid",
            Self::Rays => "rays",
        }
    }
}

/// Color of a [`Backdrop`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum BackdropTone {
    /// The accent color.
    Accent,
    /// The brand color.
    #[default]
    Brand,
    /// Neutral ink.
    Neutral,
}

impl BackdropTone {
    /// The class suffix.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Accent => "accent",
            Self::Brand => "brand",
            Self::Neutral => "neutral",
        }
    }
}

/// Strength of a [`Backdrop`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum BackdropIntensity {
    /// A faint pattern.
    Subtle,
    /// The default strength.
    #[default]
    Medium,
    /// A pronounced pattern.
    Strong,
}

impl BackdropIntensity {
    /// The class suffix.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Subtle => "subtle",
            Self::Medium => "medium",
            Self::Strong => "strong",
        }
    }
}

/// Props for [`Backdrop`].
#[derive(Props, Clone, PartialEq)]
pub struct BackdropProps {
    /// Strength of the pattern.
    #[props(default)]
    pub intensity: BackdropIntensity,
    /// Color of the pattern.
    #[props(default)]
    pub tone: BackdropTone,
    /// The pattern.
    #[props(default)]
    pub variant: BackdropVariant,
    /// 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>,
    /// Content placed over the pattern.
    pub children: Element,
}

/// A decorative pattern behind a section's content.
#[component]
pub fn Backdrop(props: BackdropProps) -> Element {
    let variant = format!("ui-backdrop--{}", props.variant.as_str());
    let tone = format!("ui-backdrop--{}", props.tone.as_str());
    let intensity = format!("ui-backdrop--{}", props.intensity.as_str());
    let class = compose([
        Some("ui-backdrop"),
        Some(variant.as_str()),
        Some(tone.as_str()),
        Some(intensity.as_str()),
        props.class.as_deref(),
    ]);

    rsx! {
        div { class, ..props.attributes,
            div { class: "ui-backdrop__content", {props.children} }
        }
    }
}

/// Size of a [`Graphic`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum GraphicSize {
    /// Small.
    Sm,
    /// Medium.
    #[default]
    Md,
    /// Large.
    Lg,
}

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

/// Color of a [`Graphic`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum GraphicTone {
    /// The accent color.
    Accent,
    /// The brand color.
    #[default]
    Brand,
    /// Neutral ink.
    Neutral,
}

impl GraphicTone {
    /// The class suffix.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Accent => "accent",
            Self::Brand => "brand",
            Self::Neutral => "neutral",
        }
    }
}

/// Frame drawn by a [`Graphic`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum GraphicVariant {
    /// A soft glow.
    Glow,
    /// A grid frame.
    Grid,
    /// Orbit rings.
    #[default]
    Orbit,
}

impl GraphicVariant {
    /// The class suffix.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Glow => "glow",
            Self::Grid => "grid",
            Self::Orbit => "orbit",
        }
    }
}

/// Props for [`Graphic`].
#[derive(Props, Clone, PartialEq)]
pub struct GraphicProps {
    /// Accessible name. With a label the graphic is an image; without one it is hidden.
    #[props(into)]
    pub label: Option<String>,
    /// Frame size.
    #[props(default)]
    pub size: GraphicSize,
    /// Frame color.
    #[props(default)]
    pub tone: GraphicTone,
    /// Frame style.
    #[props(default)]
    pub variant: GraphicVariant,
    /// 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 icon or mark framed by the graphic.
    pub children: Element,
}

/// A decorative frame around an icon or mark.
#[component]
pub fn Graphic(props: GraphicProps) -> Element {
    let labelled = has_label(props.label.as_deref());
    let variant = format!("ui-graphic--{}", props.variant.as_str());
    let tone = format!("ui-graphic--{}", props.tone.as_str());
    let size = format!("ui-graphic--{}", props.size.as_str());
    let class = compose([
        Some("ui-graphic"),
        Some(variant.as_str()),
        Some(tone.as_str()),
        Some(size.as_str()),
        props.class.as_deref(),
    ]);

    rsx! {
        div {
            aria_hidden: (!labelled).then_some("true"),
            aria_label: props.label,
            class,
            role: labelled.then_some("img"),
            ..props.attributes,
            div { class: "ui-graphic__content", {props.children} }
        }
    }
}

/// Entrance of an [`AnimatedLogo`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum AnimatedLogoAnimation {
    /// The whole mark reveals at once.
    #[default]
    Reveal,
    /// The mark's parts draw in order.
    Sequence,
}

impl AnimatedLogoAnimation {
    /// The `data-animation` value.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Reveal => "reveal",
            Self::Sequence => "sequence",
        }
    }
}

/// Props for [`AnimatedLogo`].
#[derive(Props, Clone, PartialEq)]
pub struct AnimatedLogoProps {
    /// Entrance style.
    #[props(default)]
    pub animation: AnimatedLogoAnimation,
    /// Extra classes appended after the Lumen class.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<span>` or global attribute.
    #[props(extends = GlobalAttributes, extends = span)]
    pub attributes: Vec<Attribute>,
    /// The logo mark, usually an inline SVG.
    pub children: Element,
}

/// A wrapper that animates a logo mark in with CSS.
///
/// lumen-react renders the same static wrapper; the Rive runtime is a separate, opt-in
/// `lumen-core/rive` entry that this component does not use, so nothing differs on Dioxus.
#[component]
pub fn AnimatedLogo(props: AnimatedLogoProps) -> Element {
    let class = compose([Some("ui-animated-logo"), props.class.as_deref()]);

    rsx! {
        span {
            class,
            "data-animation": props.animation.as_str(),
            "data-ui-animated-logo": "true",
            ..props.attributes,
            {props.children}
        }
    }
}

/// Props for [`AnimatedPortrait`].
#[derive(Props, Clone, PartialEq)]
pub struct AnimatedPortraitProps {
    /// 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 portrait parts: glow, orbits, card, and image.
    pub children: Element,
}

/// The animated portrait frame used on profile and hero sections.
///
/// Like lumen-react, it is a wrapper whose entrance is a CSS animation that stops under reduced
/// motion; compose the image and decorative parts as children.
#[component]
pub fn AnimatedPortrait(props: AnimatedPortraitProps) -> Element {
    let class = compose([
        Some(
            "ui-animated-portrait relative isolate w-full animate-reveal-lcp motion-reduce:animate-none",
        ),
        props.class.as_deref(),
    ]);

    rsx! {
        div { class, ..props.attributes, {props.children} }
    }
}

/// Size of an [`Illustration`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum IllustrationSize {
    /// Small.
    Sm,
    /// Medium.
    #[default]
    Md,
    /// Large.
    Lg,
}

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

/// Color of an [`Illustration`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum IllustrationTone {
    /// The accent color.
    Accent,
    /// The artwork's own semantic tone.
    #[default]
    Auto,
    /// The brand color.
    Brand,
    /// Neutral ink.
    Neutral,
}

impl IllustrationTone {
    /// The class suffix.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Accent => "accent",
            Self::Auto => "auto",
            Self::Brand => "brand",
            Self::Neutral => "neutral",
        }
    }
}

fn point_list(values: &[f64]) -> String {
    let (pairs, _) = values.as_chunks::<2>();

    pairs
        .iter()
        .map(|[x, y]| format!("{},{}", js_number(*x), js_number(*y)))
        .collect::<Vec<_>>()
        .join(" ")
}

fn render_shape(shape: IllustrationShape) -> Element {
    match shape {
        IllustrationShape::Circle { cx, cy, r } => rsx! {
            circle { cx: js_number(cx), cy: js_number(cy), r: js_number(r) }
        },
        IllustrationShape::RoundedRect {
            x,
            y,
            width,
            height,
            radius,
        } => rsx! {
            rect {
                height: js_number(height),
                rx: js_number(radius),
                width: js_number(width),
                x: js_number(x),
                y: js_number(y),
            }
        },
        IllustrationShape::Line(points) => {
            let coordinate = |index: usize| points.get(index).copied().map(js_number);

            rsx! {
                line { x1: coordinate(0), x2: coordinate(2), y1: coordinate(1), y2: coordinate(3) }
            }
        }
        IllustrationShape::Polygon(points) => rsx! {
            polygon { points: point_list(points) }
        },
        IllustrationShape::Polyline(points) => rsx! {
            polyline { points: point_list(points) }
        },
    }
}

/// Props for [`Illustration`].
#[derive(Props, Clone, PartialEq)]
pub struct IllustrationProps {
    /// Accessible name. With a label the artwork is an image; without one it is hidden.
    #[props(into)]
    pub label: Option<String>,
    /// Artwork size.
    #[props(default)]
    pub size: IllustrationSize,
    /// Artwork color.
    #[props(default)]
    pub tone: IllustrationTone,
    /// The artwork.
    #[props(default)]
    pub variant: IllustrationVariant,
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<span>` or global attribute.
    #[props(extends = GlobalAttributes, extends = span)]
    pub attributes: Vec<Attribute>,
}

/// Line artwork for empty, success, error, and offline states, drawn from Lumen's graphics
/// catalog.
#[component]
pub fn Illustration(props: IllustrationProps) -> Element {
    let labelled = has_label(props.label.as_deref());
    let variant = format!("ui-illustration--{}", props.variant.as_str());
    let tone = format!("ui-illustration--{}", props.tone.as_str());
    let size = format!("ui-illustration--{}", props.size.as_str());
    let class = compose([
        Some("ui-illustration"),
        Some(variant.as_str()),
        Some(tone.as_str()),
        Some(size.as_str()),
        props.class.as_deref(),
    ]);

    rsx! {
        span {
            aria_hidden: (!labelled).then_some("true"),
            aria_label: props.label,
            class,
            role: labelled.then_some("img"),
            ..props.attributes,
            svg { "aria-hidden": "true", fill: "none", view_box: "0 0 120 120",
                circle { class: "ui-illustration__wash", cx: "60", cy: "60", r: "48" }
                for shape in props.variant.shapes() {
                    {render_shape(*shape)}
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::point_list;

    #[test]
    fn point_lists_pair_coordinates() {
        assert_eq!(point_list(&[1.0, 2.0, 3.5, 4.0]), "1,2 3.5,4");
        assert_eq!(point_list(&[1.0, 2.0, 3.0]), "1,2");
        assert_eq!(point_list(&[]), "");
    }
}