Skip to content

Commit 5ecd6e8

Browse files
committed
Expand only lowercase components
1 parent e6c488e commit 5ecd6e8

4 files changed

Lines changed: 84 additions & 3 deletions

File tree

packages/server-reason-react-ppx/cram/ppx.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ while [[ $# -gt 0 ]]; do
2121
shift 2
2222
;;
2323
-js)
24-
js_flag="-js"
24+
js_flag="-melange"
2525
shift
2626
;;
2727
*)

packages/server-reason-react-ppx/cram/styles.t/input.re

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@
66
<div className="lola" style={ReactDOM.Style.make(~backgroundColor="gainsboro", ())} styles=x />;
77
<div className="lola" styles=?x />;
88
<div style={ReactDOM.Style.make(~backgroundColor="gainsboro", ())} styles=?x />;
9-
<div className="lola" style={ReactDOM.Style.make(~backgroundColor="gainsboro", ())} styles=?x />;
9+
<div className="lola" style={ReactDOM.Style.make(~backgroundColor="gainsboro", ())} styles=?x />;
10+
11+
/* Module-qualified components should NOT get ~styles expanded.
12+
~styles is passed through as a regular prop. */
13+
<Foo.Bar styles=x />;
14+
<Foo.Bar styles=?x />;

packages/server-reason-react-ppx/cram/styles.t/run.t

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,77 @@ We need to output ML syntax here, otherwise refmt could not parse it.
133133
: ReactDOM.Style.t));
134134
])
135135
[]
136+
;;
137+
138+
Foo.Bar.make (Foo.Bar.makeProps ~styles:x ());;
139+
Foo.Bar.make (Foo.Bar.makeProps ?styles:x ())
140+
141+
In Melange mode (-js), ~styles is only expanded on lowercase (DOM) tags.
142+
Module-qualified components like Foo.Bar keep ~styles as a regular prop (not expanded).
143+
$ rm -f output.ml temp.ml
144+
$ ../ppx.sh --output ml -js input.re
145+
div ~className:(fst x) ~style:(snd x) ~children:[] () [@JSX];;
146+
147+
div
148+
?className:(match x with None -> None | Some x -> Some (fst x))
149+
?style:(match x with None -> None | Some x -> Some (snd x))
150+
~children:[] () [@JSX]
151+
;;
152+
153+
div ~className:(fst x ^ " " ^ "lola") ~style:(snd x) ~children:[] () [@JSX];;
154+
155+
div ~className:(fst x)
156+
~style:
157+
(ReactDOM.Style.combine
158+
(ReactDOM.Style.make ~backgroundColor:"gainsboro" ())
159+
(snd x))
160+
~children:[] () [@JSX]
161+
;;
162+
163+
div
164+
~className:(fst x ^ " " ^ "lola")
165+
~style:
166+
(ReactDOM.Style.combine
167+
(ReactDOM.Style.make ~backgroundColor:"gainsboro" ())
168+
(snd x))
169+
~children:[] () [@JSX]
170+
;;
171+
172+
div
173+
~className:
174+
(match match x with None -> None | Some x -> Some (fst x) with
175+
| None -> "lola"
176+
| Some x -> x ^ " " ^ "lola")
177+
?style:(match x with None -> None | Some x -> Some (snd x))
178+
~children:[] () [@JSX]
179+
;;
180+
181+
div
182+
?className:(match x with None -> None | Some x -> Some (fst x))
183+
~style:
184+
(match match x with None -> None | Some x -> Some (snd x) with
185+
| None -> ReactDOM.Style.make ~backgroundColor:"gainsboro" ()
186+
| Some x ->
187+
ReactDOM.Style.combine
188+
(ReactDOM.Style.make ~backgroundColor:"gainsboro" ())
189+
x)
190+
~children:[] () [@JSX]
191+
;;
192+
193+
div
194+
~className:
195+
(match match x with None -> None | Some x -> Some (fst x) with
196+
| None -> "lola"
197+
| Some x -> x ^ " " ^ "lola")
198+
~style:
199+
(match match x with None -> None | Some x -> Some (snd x) with
200+
| None -> ReactDOM.Style.make ~backgroundColor:"gainsboro" ()
201+
| Some x ->
202+
ReactDOM.Style.combine
203+
(ReactDOM.Style.make ~backgroundColor:"gainsboro" ())
204+
x)
205+
~children:[] () [@JSX]
206+
;;
207+
208+
Foo.Bar.createElement ~styles:x ~children:[] () [@JSX];;
209+
Foo.Bar.createElement ?styles:x ~children:[] () [@JSX]

packages/server-reason-react-ppx/server_reason_react_ppx.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1612,7 +1612,9 @@ let traverse =
16121612
(* In the case of expressions, it's the only transformation that needs to be done for JS. This expansion from "styles" prop into "className" and "style" props is a feature by styled-ppx. The existence of this here, is because dune/ppxlib doesn't allow more than one preprocess_impl and even that, the combination of styled-ppx and server-reason-react.ppx doesn't compose properly. *)
16131613
try
16141614
match expr.pexp_desc with
1615-
| Pexp_apply (({ pexp_desc = Pexp_ident _; pexp_loc = loc; _ } as tag), args)
1615+
(* Only expand ~styles on lowercase tags (DOM elements like div, span, etc.)
1616+
Uppercase components and bindings handle styles in their own way. *)
1617+
| Pexp_apply (({ pexp_desc = Pexp_ident { txt = Lident _; _ }; pexp_loc = loc; _ } as tag), args)
16161618
when has_jsx_attr expr.pexp_attributes ->
16171619
let new_args = Expand_styles_attribute.make ~loc args in
16181620
{ (pexp_apply ~loc (super#expression ctx tag) new_args) with pexp_attributes = attributes }

0 commit comments

Comments
 (0)