rustyle_css/
view_transitions.rs

1//! View Transitions API support
2//!
3//! Provides support for the CSS View Transitions API for smooth page transitions.
4
5use crate::style::register_global_style;
6
7/// View transition name
8#[derive(Clone, Debug)]
9pub struct ViewTransitionName(pub String);
10
11impl ViewTransitionName {
12    /// Create a new view transition name
13    pub fn new(name: &str) -> Self {
14        Self(name.to_string())
15    }
16
17    /// Convert to CSS
18    pub fn to_css(&self) -> String {
19        format!("view-transition-name: {};", self.0)
20    }
21}
22
23/// View transition configuration
24#[derive(Clone, Debug)]
25pub struct ViewTransition {
26    pub name: Option<String>,
27    pub duration: Option<String>,
28    pub timing_function: Option<String>,
29}
30
31impl ViewTransition {
32    /// Create a new view transition
33    pub fn new() -> Self {
34        Self {
35            name: None,
36            duration: None,
37            timing_function: None,
38        }
39    }
40
41    /// Set transition name
42    pub fn name(mut self, name: &str) -> Self {
43        self.name = Some(name.to_string());
44        self
45    }
46
47    /// Set duration
48    pub fn duration(mut self, duration: &str) -> Self {
49        self.duration = Some(duration.to_string());
50        self
51    }
52
53    /// Set timing function
54    pub fn timing_function(mut self, timing: &str) -> Self {
55        self.timing_function = Some(timing.to_string());
56        self
57    }
58
59    /// Convert to CSS
60    pub fn to_css(&self) -> String {
61        let mut props = Vec::new();
62
63        if let Some(ref name) = self.name {
64            props.push(format!("view-transition-name: {};", name));
65        }
66
67        if let Some(ref duration) = self.duration {
68            props.push(format!("view-transition-duration: {};", duration));
69        }
70
71        if let Some(ref timing) = self.timing_function {
72            props.push(format!("view-transition-timing-function: {};", timing));
73        }
74
75        props.join(" ")
76    }
77}
78
79impl Default for ViewTransition {
80    fn default() -> Self {
81        Self::new()
82    }
83}
84
85/// Register a view transition style globally
86pub fn register_view_transition(css: &str) {
87    register_global_style(css);
88}