Skip to main content

stylex_transform/shared/utils/core/
attrs.rs

1use std::rc::Rc;
2
3use indexmap::IndexMap;
4
5use crate::shared::{
6  enums::data_structures::{
7    flat_compiled_styles_value::FlatCompiledStylesValue, fn_result::FnResult,
8  },
9  utils::{core::js_to_expr::NestedStringObject, css::common::inline_style_to_css_string},
10};
11
12use super::{parse_nullable_style::ResolvedArg, props::props};
13
14pub(crate) fn attrs(styles: &[ResolvedArg]) -> Option<FnResult> {
15  let props = props(styles)?;
16
17  let attrs = props.as_props()?.as_values()?;
18
19  let mut attrs_map: IndexMap<String, Rc<FlatCompiledStylesValue>> = IndexMap::new();
20
21  if let Some(class_name) = attrs.get("className") {
22    attrs_map.insert("class".to_string(), class_name.clone());
23  }
24
25  if let Some(data_style_src) = attrs.get("data-style-src") {
26    attrs_map.insert("data-style-src".to_string(), data_style_src.clone());
27  }
28
29  if let Some(style_value) = attrs.get("style") {
30    match style_value.as_ref() {
31      FlatCompiledStylesValue::KeyValues(pairs) => {
32        let css_string = inline_style_to_css_string(pairs);
33        attrs_map.insert(
34          "style".to_string(),
35          Rc::new(FlatCompiledStylesValue::String(css_string)),
36        );
37      },
38      _ => {
39        attrs_map.insert("style".to_string(), style_value.clone());
40      },
41    }
42  }
43
44  Some(FnResult::Attrs(
45    NestedStringObject::FlatCompiledStylesValues(attrs_map),
46  ))
47}