rustyle_css/tokens/
animation.rs

1//! Animation token system
2//!
3//! Provides consistent animation durations and easing functions.
4
5use crate::css::Time;
6
7/// Animation duration scale
8#[derive(Clone, Debug)]
9pub struct AnimationDurationScale {
10    pub fast: Time,
11    pub base: Time,
12    pub slow: Time,
13    pub slower: Time,
14}
15
16impl AnimationDurationScale {
17    /// Create new animation duration scale
18    pub fn new(fast: Time, base: Time, slow: Time, slower: Time) -> Self {
19        Self {
20            fast,
21            base,
22            slow,
23            slower,
24        }
25    }
26}
27
28/// Easing function definitions
29#[derive(Clone, Debug)]
30pub struct EasingFunctions {
31    pub linear: String,
32    pub ease_in: String,
33    pub ease_out: String,
34    pub ease_in_out: String,
35    pub ease_in_cubic: String,
36    pub ease_out_cubic: String,
37    pub ease_in_out_cubic: String,
38}
39
40impl EasingFunctions {
41    /// Create new easing functions
42    pub fn new(
43        linear: &str,
44        ease_in: &str,
45        ease_out: &str,
46        ease_in_out: &str,
47        ease_in_cubic: &str,
48        ease_out_cubic: &str,
49        ease_in_out_cubic: &str,
50    ) -> Self {
51        Self {
52            linear: linear.to_string(),
53            ease_in: ease_in.to_string(),
54            ease_out: ease_out.to_string(),
55            ease_in_out: ease_in_out.to_string(),
56            ease_in_cubic: ease_in_cubic.to_string(),
57            ease_out_cubic: ease_out_cubic.to_string(),
58            ease_in_out_cubic: ease_in_out_cubic.to_string(),
59        }
60    }
61}
62
63/// Comprehensive animation token system
64#[derive(Clone, Debug)]
65pub struct AnimationTokens {
66    pub duration: AnimationDurationScale,
67    pub easing: EasingFunctions,
68}
69
70impl AnimationTokens {
71    /// Create new animation tokens
72    pub fn new(duration: AnimationDurationScale, easing: EasingFunctions) -> Self {
73        Self { duration, easing }
74    }
75
76    /// Convert animation tokens to CSS custom properties
77    pub fn to_css_vars(&self) -> String {
78        format!(
79            "  --animation-duration-fast: {};\n  --animation-duration-base: {};\n  --animation-duration-slow: {};\n  --animation-duration-slower: {};\n  --animation-easing-linear: {};\n  --animation-easing-in: {};\n  --animation-easing-out: {};\n  --animation-easing-in-out: {};\n  --animation-easing-in-cubic: {};\n  --animation-easing-out-cubic: {};\n  --animation-easing-in-out-cubic: {};\n",
80            self.duration.fast.to_css(),
81            self.duration.base.to_css(),
82            self.duration.slow.to_css(),
83            self.duration.slower.to_css(),
84            self.easing.linear,
85            self.easing.ease_in,
86            self.easing.ease_out,
87            self.easing.ease_in_out,
88            self.easing.ease_in_cubic,
89            self.easing.ease_out_cubic,
90            self.easing.ease_in_out_cubic,
91        )
92    }
93}
94
95impl Default for AnimationTokens {
96    fn default() -> Self {
97        Self {
98            duration: AnimationDurationScale::new(
99                Time::milliseconds(150.0),
100                Time::milliseconds(200.0),
101                Time::milliseconds(300.0),
102                Time::milliseconds(500.0),
103            ),
104            easing: EasingFunctions::new(
105                "linear",
106                "ease-in",
107                "ease-out",
108                "ease-in-out",
109                "cubic-bezier(0.4, 0, 1, 1)",
110                "cubic-bezier(0, 0, 0.2, 1)",
111                "cubic-bezier(0.4, 0, 0.2, 1)",
112            ),
113        }
114    }
115}