Repository navigation
Description
Steps to reproduce
// npm i @mui/system@9.5.0 @mui/styled-engine@npm:@mui/styled-engine-sc@9.5.0 styled-components@6.6.0 react react-dom
const React = require('react');
const { renderToString } = require('react-dom/server');
const { ServerStyleSheet } = require('styled-components');
const { createStyled } = require('@mui/system');
const styled = createStyled();
function printCss(label, Component, props) {
const sheet = new ServerStyleSheet();
renderToString(sheet.collectStyles(React.createElement(Component, props)));
console.log(label, sheet.getStyleTags());
sheet.seal();
}
const Inner = styled('div')`color: red;`;
printCss('Inner before wrapping:', Inner, { sx: { margin: '4px' } });
const Outer = styled(Inner)`color: blue;`;
printCss('Outer:', Outer, { sx: { margin: '4px' } });
printCss('Inner after wrapping:', Inner, { sx: { margin: '8px' } });Current behavior
Creating Outer permanently removes sx support from Inner. Style tags trimmed to the rules:
Inner before wrapping: .etZbJT{color:red;margin:4px;}
Outer: .cHZHdU{color:red;}
.dORAon{color:blue;margin:4px;}
Inner after wrapping: .cHZHdU{color:red;}
createStyled calls internal_mutateStyles on the wrapped tag to filter styleFunctionSx out of it. Under styled-engine-sc, that function writes to tag.componentStyle.rules, a private styled-components field that Inner also reads when it renders. So the filter changes Inner itself, not just the new wrapper.
The field doesn't exist in the styled-components v7 prereleases, so the filter silently does nothing there. Inner keeps its sx, but Outer emits it twice:
Outer: .gWxSPG{color:red;margin:4px;}
.gYjYxu{color:blue;margin:4px;}
Expected behavior
sx runs once, in the outermost component, and Inner used on its own keeps working after something wraps it.
Context
I maintain styled-components. This can be done with public API on both 6.x and v7. Each layer sets its id in a transient prop with attrs. In a wrapped chain the outermost attrs value wins and every layer reads it, so only the outermost layer runs sx:
// createStyled
+let nextLayerId = 0;
const styled = (tag, inputOptions = {}) => {
- mutateStyles(tag, (styles) => styles.filter((style) => style !== styleFunctionSx));
+ const layerId = ++nextLayerId;
// ...
- const defaultStyledResolver = styledEngineStyled(tag, { /* ... */ });
+ const defaultStyledResolver = styledEngineStyled(tag, { /* ... */ }).attrs({ $sxOwner: layerId });
// ...
if (!skipSx) {
- expressionsTail.push(styleFunctionSx);
+ expressionsTail.push((props) => (props.$sxOwner === layerId ? styleFunctionSx(props) : null));
}The engine's development wrapper also needs fn.attrs = stylesFactory.attrs. The marker is set even when skipSx is on, so a skipSx wrapper still removes the inner sx from its own output, as it does today.
With that patch, the repro prints this on 6.6.0:
Inner before wrapping: .etZbJT{color:red;margin:4px;}
Outer: .cHZHdU{color:red;}
.dORAon{color:blue;margin:4px;}
Inner after wrapping: .juXnPX{color:red;margin:8px;}
The v7 prerelease gives the same result: sx runs once in Outer, and Inner keeps it. I also checked three layers, a skipSx wrapper, object styles, and wrapping a plain component, using createStyled directly. I did not run your test suite or test Material UI components.
Emotion has no attrs, so this would need to sit behind an engine-level helper, with the Emotion engine keeping its current path. I only tested the styled-components engine.
The recipe for this pattern, and the styled-components tests that lock the behavior it relies on, are in styled-components/styled-components#5854
Your environment
@mui/system 9.5.0, @mui/styled-engine-sc 9.5.0, React 19, Node 24, styled-components 6.6.0 and 7.0.0-prerelease-20261103032356
Search keywords: styled-engine-sc, sx, componentStyle, internal_mutateStyles
Activity
henricop commentedon Nov 7, 2026
I can investigate this on the current branch, starting with an SSR regression for the independent Inner after wrapping. I will check the styled-components path and the existing Emotion deduplication behavior before choosing an engine-level boundary, and keep the work scoped to preserving independent sx support and running it once in composed layers. Please let me know if a fix is already in progress.
github-actions commentedon Nov 15, 2026
This is a real bug when
@mui/system'sstyled()/createStyled()runs on the styled-components engine (@mui/styled-engine-sc). Your analysis matches the current code.Before wrapping a styled component,
styled()removes thesxstyle function from that component's styles. The goal is to makesxrun only once, in the outer component. The two engines handle this differently:sxstill works.styled(Inner)runs,Innerused on its own ignoressxfor good. Your before/after output shows this.The styled-components v7 double-
sxoutput you describe follows from the same code: with no field to change there, the filter does nothing and both layers runsx.Your repro is a self-contained Node script with clear expected and actual output. I haven't run it or tried the patch you proposed.
Possible workaround: wrap a plain function component instead of the styled component, so
styled()has nothing to change onInner:When the wrapped tag isn't an HTML tag,
styled()doesn't forwardsxby default. Sosxshould run only inOuter, andInnerkeeps its ownsxwhen you use it on its own. The trade-off:Inner's styled-components identity is no longer part ofOuter, which matters if you targetOuterwith component selectors.Next steps
sxruns only in the outermost layer" behavior should move behind an engine-level helper. styled-components would use theattrsmarker you proposed, and Emotion would keep its current path. Because of your v7 findings, a fix should also cover the styled-components v7 prereleases.Agent-generated diagnosis; conclusions have not been independently verified. Workflow run.
🤖 Triage · Claude Code · claude-opus-5-5 · high effort · 15 turns · 0m56s · $0.33 · run
github-actions commentedon Nov 18, 2026
This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.
@quantizor Thanks again for creating this issue! If you have a moment, we would love to hear your thoughts on how we handled it with this short feedback form.