Skip to main content

lumen_dioxus/charts/extended/
motion.rs

1//! [`ChartMotion`]: the wrapper that marks a chart for motion between data updates.
2
3use dioxus::prelude::*;
4
5/// The motion token used by a [`ChartMotion`] wrapper.
6#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum ChartMotionDuration {
9    /// `--ui-duration-fast`.
10    Fast,
11    /// `--ui-duration`.
12    #[default]
13    Standard,
14    /// `--ui-duration-slow`.
15    Slow,
16}
17
18impl ChartMotionDuration {
19    /// The token name: `fast`, `standard`, or `slow`.
20    pub fn as_str(self) -> &'static str {
21        match self {
22            Self::Fast => "fast",
23            Self::Standard => "standard",
24            Self::Slow => "slow",
25        }
26    }
27}
28
29/// Props for [`ChartMotion`].
30#[derive(Props, Clone, PartialEq)]
31pub struct ChartMotionProps {
32    /// The motion token lumen-react's mark animations use.
33    #[props(default)]
34    pub duration: ChartMotionDuration,
35    /// Classes for the wrapper.
36    #[props(into)]
37    pub class: Option<String>,
38    /// Any other `<div>` or global attribute.
39    #[props(extends = GlobalAttributes, extends = div)]
40    pub attributes: Vec<Attribute>,
41    /// The chart to wrap.
42    pub children: Element,
43}
44
45/// Wraps a chart so data updates read as motion, without delaying accessible data.
46///
47/// lumen-react animates stable SVG marks between updates with the Web Animations API. That API
48/// has no equivalent on Dioxus Native, so lumen-dioxus renders the same `data-ui-chart-motion`
49/// wrapper and lets Lumen's CSS fade empty and error states in; marks update in place on every
50/// renderer.
51#[component]
52pub fn ChartMotion(props: ChartMotionProps) -> Element {
53    rsx! {
54        div {
55            class: props.class,
56            "data-ui-chart-motion": "true",
57            ..props.attributes,
58            {props.children}
59        }
60    }
61}