11import { ReservedKeywordPlatformPresets } from "@coli.codes/naming/reserved" ;
22import { react as react_config } from "@designto/config" ;
3- import type { JsxWidget } from "@web-builder/core" ;
3+ import type { JSXElementConfig , JsxWidget } from "@web-builder/core" ;
44import {
55 buildJsx ,
66 getWidgetStylesConfigMap ,
@@ -10,15 +10,22 @@ import {
1010} from "@web-builder/core/builders" ;
1111import {
1212 BlockStatement ,
13+ Identifier ,
1314 ImportDeclaration ,
1415 JSXAttribute ,
16+ Literal ,
17+ ObjectLiteralExpression ,
18+ PropertyAssignment ,
1519 Return ,
1620 ScopedVariableNamer ,
21+ StringLiteral ,
1722} from "coli" ;
1823import * as css from "@web-builder/styles" ;
1924import { react_imports } from "../react-import-specifications" ;
2025import { ReactWidgetModuleExportable } from "../react-module" ;
2126import { makeReactModuleFile , ReactModuleFile } from "../react-module-file" ;
27+ import { cssToJson } from "@web-builder/styles/_utils" ;
28+ import { CSSProperties } from "@coli.codes/css" ;
2229
2330/**
2431 * CSS In JS Style builder for React Framework
@@ -66,17 +73,57 @@ export class ReactCssInJSBuilder {
6673 }
6774
6875 private jsxBuilder ( widget : JsxWidget ) {
69- // TODO: add attributes transformer, inject style={{ ...}}
70-
71- // const transformer = (style: string) => {
72- // const styleobj = css.utils.cssToJson(style);
73- // new JSXAttribute("style", styleobj);
74- // };
75-
7676 return buildJsx (
7777 widget ,
7878 {
79- styledConfig : ( id ) => this . styledConfig ( id ) ,
79+ styledConfig : ( id ) => {
80+ const cfg = this . styledConfig ( id ) ;
81+ const _default_attr = cfg . attributes ;
82+
83+ const existingstyleattr = _default_attr ?. find (
84+ // where style refers to react's jsx style attribute
85+ ( a ) => a . name . name === "style"
86+ ) ;
87+
88+ let style : JSXAttribute ;
89+ if ( existingstyleattr ) {
90+ // ignore this case. (element already with style attriibute may be svg element)
91+ // this case is not supported. (should supported if the logic changes)
92+ } else {
93+ //
94+ const styledata : CSSProperties =
95+ ( cfg as JSXWithStyleElementConfig ) . style ?? { } ;
96+ const reactStyleData = cssToJson ( styledata ) ;
97+ const properties : PropertyAssignment [ ] = Object . keys (
98+ reactStyleData
99+ ) . map (
100+ ( key ) =>
101+ new PropertyAssignment ( {
102+ name : key as unknown as Identifier ,
103+ initializer : new StringLiteral ( reactStyleData [ key ] ) ,
104+ } )
105+ ) ;
106+
107+ style = new JSXAttribute (
108+ "style" ,
109+ new BlockStatement (
110+ new ObjectLiteralExpression ( {
111+ properties : properties ,
112+ } )
113+ )
114+ ) ;
115+ }
116+
117+ const newattributes = [
118+ ...( _default_attr ?? [ ] ) ,
119+ //
120+ style ,
121+ ] ;
122+
123+ cfg . attributes = newattributes ;
124+
125+ return cfg ;
126+ } ,
80127 } ,
81128 {
82129 self_closing_if_possible : true ,
0 commit comments