Skip to main content

stylex_transform/shared/utils/core/
props.rs

1use std::rc::Rc;
2use stylex_structures::pair::Pair;
3
4use indexmap::IndexMap;
5
6use crate::{
7  shared::{
8    enums::data_structures::{
9      flat_compiled_styles_value::FlatCompiledStylesValue, fn_result::FnResult,
10    },
11    structures::types::FlatCompiledStyles,
12    utils::{core::js_to_expr::NestedStringObject, css::common::normalize_css_property_name},
13  },
14  transform::styleq::common::{StyleQResult, styleq},
15};
16
17use super::parse_nullable_style::ResolvedArg;
18
19pub(crate) fn props(styles: &[ResolvedArg]) -> Option<FnResult> {
20  let StyleQResult {
21    class_name,
22    inline_style,
23    data_style_src,
24  } = styleq(styles);
25
26  let mut props_map: FlatCompiledStyles = IndexMap::new();
27
28  if !class_name.is_empty() {
29    props_map.insert(
30      "className".to_string(),
31      Rc::new(FlatCompiledStylesValue::String(class_name)),
32    );
33  }
34
35  if let Some(inline_style) = inline_style {
36    let pairs: Vec<Pair> = inline_style
37      .iter()
38      .filter_map(|(k, v)| {
39        if let FlatCompiledStylesValue::String(val) = v.as_ref() {
40          Some(Pair::new(normalize_css_property_name(k), val.clone()))
41        } else {
42          None
43        }
44      })
45      .collect();
46
47    props_map.insert(
48      "style".to_string(),
49      Rc::new(FlatCompiledStylesValue::KeyValues(pairs)),
50    );
51  }
52
53  if let Some(data_style_src) = data_style_src
54    && !data_style_src.is_empty()
55  {
56    props_map.insert(
57      "data-style-src".to_string(),
58      Rc::new(FlatCompiledStylesValue::String(data_style_src)),
59    );
60  }
61
62  Some(FnResult::Props(
63    NestedStringObject::FlatCompiledStylesValues(props_map),
64  ))
65}