Skip to main content

lumen_dioxus/motion/
decoration.rs

1//! Brand and decoration surfaces: `Backdrop`, `Graphic`, `AnimatedLogo`, `AnimatedPortrait`,
2//! and `Illustration`.
3
4use dioxus::prelude::*;
5
6use super::illustration_data::{IllustrationShape, IllustrationVariant};
7use super::shared::js_number;
8use crate::class::compose;
9
10/// Whether an optional label names the element, like a truthy `label` in lumen-react.
11fn has_label(label: Option<&str>) -> bool {
12    label.is_some_and(|text| !text.is_empty())
13}
14
15/// Pattern drawn by a [`Backdrop`].
16#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
17#[non_exhaustive]
18pub enum BackdropVariant {
19    /// Soft aurora gradients.
20    #[default]
21    Aurora,
22    /// A dot grid.
23    Dots,
24    /// A line grid.
25    Grid,
26    /// Light rays.
27    Rays,
28}
29
30impl BackdropVariant {
31    /// The class suffix.
32    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/// Color of a [`Backdrop`].
43#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
44#[non_exhaustive]
45pub enum BackdropTone {
46    /// The accent color.
47    Accent,
48    /// The brand color.
49    #[default]
50    Brand,
51    /// Neutral ink.
52    Neutral,
53}
54
55impl BackdropTone {
56    /// The class suffix.
57    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/// Strength of a [`Backdrop`].
67#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
68#[non_exhaustive]
69pub enum BackdropIntensity {
70    /// A faint pattern.
71    Subtle,
72    /// The default strength.
73    #[default]
74    Medium,
75    /// A pronounced pattern.
76    Strong,
77}
78
79impl BackdropIntensity {
80    /// The class suffix.
81    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/// Props for [`Backdrop`].
91#[derive(Props, Clone, PartialEq)]
92pub struct BackdropProps {
93    /// Strength of the pattern.
94    #[props(default)]
95    pub intensity: BackdropIntensity,
96    /// Color of the pattern.
97    #[props(default)]
98    pub tone: BackdropTone,
99    /// The pattern.
100    #[props(default)]
101    pub variant: BackdropVariant,
102    /// Extra classes appended after the Lumen classes.
103    #[props(into)]
104    pub class: Option<String>,
105    /// Any other `<div>` or global attribute.
106    #[props(extends = GlobalAttributes, extends = div)]
107    pub attributes: Vec<Attribute>,
108    /// Content placed over the pattern.
109    pub children: Element,
110}
111
112/// A decorative pattern behind a section's content.
113#[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/// Size of a [`Graphic`].
134#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
135#[non_exhaustive]
136pub enum GraphicSize {
137    /// Small.
138    Sm,
139    /// Medium.
140    #[default]
141    Md,
142    /// Large.
143    Lg,
144}
145
146impl GraphicSize {
147    /// The class suffix.
148    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/// Color of a [`Graphic`].
158#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
159#[non_exhaustive]
160pub enum GraphicTone {
161    /// The accent color.
162    Accent,
163    /// The brand color.
164    #[default]
165    Brand,
166    /// Neutral ink.
167    Neutral,
168}
169
170impl GraphicTone {
171    /// The class suffix.
172    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/// Frame drawn by a [`Graphic`].
182#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
183#[non_exhaustive]
184pub enum GraphicVariant {
185    /// A soft glow.
186    Glow,
187    /// A grid frame.
188    Grid,
189    /// Orbit rings.
190    #[default]
191    Orbit,
192}
193
194impl GraphicVariant {
195    /// The class suffix.
196    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/// Props for [`Graphic`].
206#[derive(Props, Clone, PartialEq)]
207pub struct GraphicProps {
208    /// Accessible name. With a label the graphic is an image; without one it is hidden.
209    #[props(into)]
210    pub label: Option<String>,
211    /// Frame size.
212    #[props(default)]
213    pub size: GraphicSize,
214    /// Frame color.
215    #[props(default)]
216    pub tone: GraphicTone,
217    /// Frame style.
218    #[props(default)]
219    pub variant: GraphicVariant,
220    /// Extra classes appended after the Lumen classes.
221    #[props(into)]
222    pub class: Option<String>,
223    /// Any other `<div>` or global attribute.
224    #[props(extends = GlobalAttributes, extends = div)]
225    pub attributes: Vec<Attribute>,
226    /// The icon or mark framed by the graphic.
227    pub children: Element,
228}
229
230/// A decorative frame around an icon or mark.
231#[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/// Entrance of an [`AnimatedLogo`].
258#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
259#[non_exhaustive]
260pub enum AnimatedLogoAnimation {
261    /// The whole mark reveals at once.
262    #[default]
263    Reveal,
264    /// The mark's parts draw in order.
265    Sequence,
266}
267
268impl AnimatedLogoAnimation {
269    /// The `data-animation` value.
270    pub fn as_str(self) -> &'static str {
271        match self {
272            Self::Reveal => "reveal",
273            Self::Sequence => "sequence",
274        }
275    }
276}
277
278/// Props for [`AnimatedLogo`].
279#[derive(Props, Clone, PartialEq)]
280pub struct AnimatedLogoProps {
281    /// Entrance style.
282    #[props(default)]
283    pub animation: AnimatedLogoAnimation,
284    /// Extra classes appended after the Lumen class.
285    #[props(into)]
286    pub class: Option<String>,
287    /// Any other `<span>` or global attribute.
288    #[props(extends = GlobalAttributes, extends = span)]
289    pub attributes: Vec<Attribute>,
290    /// The logo mark, usually an inline SVG.
291    pub children: Element,
292}
293
294/// A wrapper that animates a logo mark in with CSS.
295///
296/// lumen-react renders the same static wrapper; the Rive runtime is a separate, opt-in
297/// `lumen-core/rive` entry that this component does not use, so nothing differs on Dioxus.
298#[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/// Props for [`AnimatedPortrait`].
314#[derive(Props, Clone, PartialEq)]
315pub struct AnimatedPortraitProps {
316    /// Extra classes appended after the Lumen classes.
317    #[props(into)]
318    pub class: Option<String>,
319    /// Any other `<div>` or global attribute.
320    #[props(extends = GlobalAttributes, extends = div)]
321    pub attributes: Vec<Attribute>,
322    /// The portrait parts: glow, orbits, card, and image.
323    pub children: Element,
324}
325
326/// The animated portrait frame used on profile and hero sections.
327///
328/// Like lumen-react, it is a wrapper whose entrance is a CSS animation that stops under reduced
329/// motion; compose the image and decorative parts as children.
330#[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/// Size of an [`Illustration`].
345#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
346#[non_exhaustive]
347pub enum IllustrationSize {
348    /// Small.
349    Sm,
350    /// Medium.
351    #[default]
352    Md,
353    /// Large.
354    Lg,
355}
356
357impl IllustrationSize {
358    /// The class suffix.
359    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/// Color of an [`Illustration`].
369#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
370#[non_exhaustive]
371pub enum IllustrationTone {
372    /// The accent color.
373    Accent,
374    /// The artwork's own semantic tone.
375    #[default]
376    Auto,
377    /// The brand color.
378    Brand,
379    /// Neutral ink.
380    Neutral,
381}
382
383impl IllustrationTone {
384    /// The class suffix.
385    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/// Props for [`Illustration`].
442#[derive(Props, Clone, PartialEq)]
443pub struct IllustrationProps {
444    /// Accessible name. With a label the artwork is an image; without one it is hidden.
445    #[props(into)]
446    pub label: Option<String>,
447    /// Artwork size.
448    #[props(default)]
449    pub size: IllustrationSize,
450    /// Artwork color.
451    #[props(default)]
452    pub tone: IllustrationTone,
453    /// The artwork.
454    #[props(default)]
455    pub variant: IllustrationVariant,
456    /// Extra classes appended after the Lumen classes.
457    #[props(into)]
458    pub class: Option<String>,
459    /// Any other `<span>` or global attribute.
460    #[props(extends = GlobalAttributes, extends = span)]
461    pub attributes: Vec<Attribute>,
462}
463
464/// Line artwork for empty, success, error, and offline states, drawn from Lumen's graphics
465/// catalog.
466#[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}