Skip to main content

stylex_transform/shared/transformers/
stylex_default_maker.rs

1use std::rc::Rc;
2
3use indexmap::IndexMap;
4
5use crate::shared::enums::data_structures::flat_compiled_styles_value::FlatCompiledStylesValue;
6use crate::shared::utils::core::js_to_expr::NestedStringObject;
7use stylex_constants::constants::common::COMPILED_KEY;
8use stylex_structures::stylex_state_options::StyleXStateOptions;
9
10/// Creates a default marker object that can be used with stylex.props()
11/// to add a marker class for ancestor/sibling state observers.
12///
13/// # Arguments
14/// * `options` - Reference to StyleXStateOptions to get the class name prefix
15///
16/// # Returns
17/// A map with the default marker class name as both key and value,
18/// plus a `$$css` marker set to true
19pub(crate) fn stylex_default_marker(options: &StyleXStateOptions) -> NestedStringObject {
20  let prefix = if !options.class_name_prefix.is_empty() {
21    format!("{}-", options.class_name_prefix)
22  } else {
23    String::new()
24  };
25
26  let marker_class = format!("{}default-marker", prefix);
27
28  let mut result = IndexMap::new();
29
30  result.insert(
31    marker_class.clone(),
32    Rc::new(FlatCompiledStylesValue::String(marker_class)),
33  );
34
35  result.insert(
36    COMPILED_KEY.to_string(),
37    Rc::new(FlatCompiledStylesValue::Bool(true)),
38  );
39
40  NestedStringObject::FlatCompiledStylesValues(result)
41}
42
43#[cfg(test)]
44mod tests {
45  use super::*;
46
47  #[test]
48  fn test_default_marker_with_prefix() {
49    let options = StyleXStateOptions {
50      class_name_prefix: "x".to_string(),
51      ..Default::default()
52    };
53
54    let result = stylex_default_marker(&options);
55
56    let map = result
57      .as_values()
58      .expect("Expected FlatCompiledStylesValues");
59
60    assert!(map.contains_key("x-default-marker"));
61    assert!(map.contains_key("$$css"));
62
63    if let Some(FlatCompiledStylesValue::String(s)) =
64      map.get("x-default-marker").map(|v| v.as_ref())
65    {
66      assert_eq!(s, "x-default-marker");
67    } else {
68      panic!("Expected string value for marker class");
69    }
70
71    if let Some(FlatCompiledStylesValue::Bool(b)) = map.get("$$css").map(|v| v.as_ref()) {
72      assert!(b);
73    } else {
74      panic!("Expected boolean value for $$css");
75    }
76  }
77
78  #[test]
79  fn test_default_marker_with_custom_prefix() {
80    let options = StyleXStateOptions {
81      class_name_prefix: "custom".to_string(),
82      ..Default::default()
83    };
84
85    let result = stylex_default_marker(&options);
86
87    let map = result
88      .as_values()
89      .expect("Expected FlatCompiledStylesValues");
90
91    assert!(map.contains_key("custom-default-marker"));
92
93    if let Some(FlatCompiledStylesValue::String(s)) =
94      map.get("custom-default-marker").map(|v| v.as_ref())
95    {
96      assert_eq!(s, "custom-default-marker");
97    } else {
98      panic!("Expected string value for marker class");
99    }
100  }
101
102  #[test]
103  fn test_default_marker_with_empty_prefix() {
104    let options = StyleXStateOptions {
105      class_name_prefix: "".to_string(),
106      ..Default::default()
107    };
108
109    let result = stylex_default_marker(&options);
110
111    let map = result
112      .as_values()
113      .expect("Expected FlatCompiledStylesValues");
114
115    assert!(map.contains_key("default-marker"));
116
117    if let Some(FlatCompiledStylesValue::String(s)) = map.get("default-marker").map(|v| v.as_ref())
118    {
119      assert_eq!(s, "default-marker");
120    } else {
121      panic!("Expected string value for marker class");
122    }
123  }
124
125  #[test]
126  fn test_default_marker_always_has_css_marker() {
127    let options = StyleXStateOptions::default();
128    let result = stylex_default_marker(&options);
129
130    let map = result
131      .as_values()
132      .expect("Expected FlatCompiledStylesValues");
133
134    assert!(map.contains_key("$$css"));
135    assert_eq!(map.len(), 2); // marker class + $$css
136  }
137}