Unverified Commit 950ef1a5 authored by Maia Teegarden's avatar Maia Teegarden Committed by GitHub

Fix root global jsx style (#29505)

* Fix root global jsx style

* Build next-swc binaries

* Prettier fix

* Build next-swc binaries
parent 7cdce8fd
...@@ -64,16 +64,26 @@ pub enum JSXStyle { ...@@ -64,16 +64,26 @@ pub enum JSXStyle {
impl Fold for StyledJSXTransformer { impl Fold for StyledJSXTransformer {
fn fold_jsx_element(&mut self, el: JSXElement) -> JSXElement { fn fold_jsx_element(&mut self, el: JSXElement) -> JSXElement {
if self.has_styled_jsx && is_styled_jsx(&el) { if is_styled_jsx(&el) {
match self.replace_jsx_style(&el) { let parent_has_styled_jsx = self.has_styled_jsx;
Ok(el) => return el, if !parent_has_styled_jsx {
Err(_) => return el, self.check_for_jsx_styles(Some(&el), &el.children);
}
let el = match self.replace_jsx_style(&el) {
Ok(el) => el,
Err(_) => el,
};
if !parent_has_styled_jsx {
self.reset_styles_state();
} }
} else if self.has_styled_jsx { return el;
}
if self.has_styled_jsx {
return el.fold_children_with(self); return el.fold_children_with(self);
} }
self.check_children_for_jsx_styles(&el.children); self.check_for_jsx_styles(None, &el.children);
let el = el.fold_children_with(self); let el = el.fold_children_with(self);
self.reset_styles_state(); self.reset_styles_state();
...@@ -85,7 +95,7 @@ impl Fold for StyledJSXTransformer { ...@@ -85,7 +95,7 @@ impl Fold for StyledJSXTransformer {
return fragment.fold_children_with(self); return fragment.fold_children_with(self);
} }
self.check_children_for_jsx_styles(&fragment.children); self.check_for_jsx_styles(None, &fragment.children);
let fragment = fragment.fold_children_with(self); let fragment = fragment.fold_children_with(self);
self.reset_styles_state(); self.reset_styles_state();
...@@ -319,19 +329,28 @@ impl Fold for StyledJSXTransformer { ...@@ -319,19 +329,28 @@ impl Fold for StyledJSXTransformer {
} }
impl StyledJSXTransformer { impl StyledJSXTransformer {
fn check_children_for_jsx_styles(&mut self, children: &Vec<JSXElementChild>) { fn check_for_jsx_styles(&mut self, el: Option<&JSXElement>, children: &Vec<JSXElementChild>) {
let mut styles = vec![]; let mut styles = vec![];
for i in 0..children.len() { let mut process_style = |el: &JSXElement| {
if let JSXElementChild::JSXElement(child_el) = &children[i] {
if is_styled_jsx(&child_el) {
self.file_has_styled_jsx = true; self.file_has_styled_jsx = true;
self.has_styled_jsx = true; self.has_styled_jsx = true;
let expr = get_style_expr(&child_el); let expr = get_style_expr(el);
let style_info = self.get_jsx_style(expr, is_global(&child_el)); let style_info = self.get_jsx_style(expr, is_global(el));
styles.insert(0, style_info); styles.insert(0, style_info);
};
if el.is_some() && is_styled_jsx(el.unwrap()) {
process_style(el.unwrap());
} else {
for i in 0..children.len() {
if let JSXElementChild::JSXElement(child_el) = &children[i] {
if is_styled_jsx(&child_el) {
process_style(&child_el);
} }
} }
} }
};
if self.has_styled_jsx { if self.has_styled_jsx {
let (static_class_name, class_name) = let (static_class_name, class_name) =
compute_class_names(&styles, self.style_import_name.as_ref().unwrap()); compute_class_names(&styles, self.style_import_name.as_ref().unwrap());
...@@ -518,6 +537,7 @@ impl StyledJSXTransformer { ...@@ -518,6 +537,7 @@ impl StyledJSXTransformer {
self.has_styled_jsx = false; self.has_styled_jsx = false;
self.static_class_name = None; self.static_class_name = None;
self.class_name = None; self.class_name = None;
self.styles = vec![];
} }
} }
......
...@@ -3,5 +3,5 @@ const Test = ()=><div className={"jsx-d47d6adadf14e957"}> ...@@ -3,5 +3,5 @@ const Test = ()=><div className={"jsx-d47d6adadf14e957"}>
<_JSXStyle id={"d47d6adadf14e957"}>{"body {color:red}\n:hover {color:red;\ndisplay:-webkit-box;\ndisplay:-webkit-flex;\ndisplay:-ms-flexbox;\ndisplay:flex;\n-webkit-animation:foo 1s ease-out;\nanimation:foo 1s ease-out}\ndiv a {display:none}\n[data-test] >div {color:red}"}</_JSXStyle> <_JSXStyle id={"d47d6adadf14e957"}>{"body {color:red}\n:hover {color:red;\ndisplay:-webkit-box;\ndisplay:-webkit-flex;\ndisplay:-ms-flexbox;\ndisplay:flex;\n-webkit-animation:foo 1s ease-out;\nanimation:foo 1s ease-out}\ndiv a {display:none}\n[data-test] >div {color:red}"}</_JSXStyle>
</div> </div>
; ;
const Test2 = ()=><style global jsx>{"p { color: red }"}</style> const Test2 = ()=><_JSXStyle id={"94239b6d6b42c9b5"}>{"p {color:red}"}</_JSXStyle>
; ;
\ No newline at end of file
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "styled-jsx/style";
const Test = ()=><style global jsx>{"p { color: red }"}</style> const Test = ()=><_JSXStyle id={"94239b6d6b42c9b5"}>{"p {color:red}"}</_JSXStyle>
; ;
export default (()=><div className={"jsx-3822e6e1fb9fa41a"}> export default (()=><div className={"jsx-3822e6e1fb9fa41a"}>
<p className={"jsx-3822e6e1fb9fa41a"}>test</p> <p className={"jsx-3822e6e1fb9fa41a"}>test</p>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment