Skip to content

Commit 4cfd2f7

Browse files
authored
[system] Skip styled component from being transformed (#46129)
1 parent 2043d80 commit 4cfd2f7

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

packages/mui-system/src/createStyled/createStyled.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,15 @@ export default function createStyled(input = {}) {
152152
});
153153

154154
const transformStyle = (style) => {
155-
// On the server Emotion doesn't use React.forwardRef for creating components, so the created
156-
// component stays as a function. This condition makes sure that we do not interpolate functions
157-
// which are basically components used as a selectors.
158-
if (typeof style === 'function' && style.__emotion_real !== style) {
155+
// - On the server Emotion doesn't use React.forwardRef for creating components, so the created
156+
// component stays as a function. This condition makes sure that we do not interpolate functions
157+
// which are basically components used as a selectors.
158+
// - `style` could be a styled component from a babel plugin for component selectors, This condition
159+
// makes sure that we do not interpolate them.
160+
if (style.__emotion_real === style) {
161+
return style;
162+
}
163+
if (typeof style === 'function') {
159164
return function styleFunctionProcessor(props) {
160165
return processStyle(props, style);
161166
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import * as React from 'react';
2+
import { styled } from '@mui/material/styles';
3+
4+
const Child = styled('div', {
5+
target: 'child', // simulate a class name from @emotion/babel-plugin
6+
})({
7+
color: 'blue',
8+
});
9+
10+
const Parent = styled('div')`
11+
${Child} {
12+
color: red;
13+
}
14+
`;
15+
16+
const Parent2 = styled('div')({
17+
[Child]: {
18+
color: 'red',
19+
},
20+
});
21+
22+
export default function ComponentSelector() {
23+
return (
24+
<div>
25+
<Child>I am blue.</Child>
26+
<Parent>
27+
<Child>I am red (literal).</Child>
28+
</Parent>
29+
<Parent2>
30+
<Child>I am red (object).</Child>
31+
</Parent2>
32+
</div>
33+
);
34+
}

0 commit comments

Comments
 (0)