Skip to content

[system] Wrapping a styled component removes the inner component's sx under styled-engine-sc #49672

Closed

Description

@quantizor

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

  1. added
    status: waiting for maintainer
    on Nov 3, 2026
  2. henricop commented on Nov 7, 2026

    @henricop
    Contributor

    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.

  3. added
    scope: system
    on Nov 10, 2026
  4. added
    type: bug
    package: styled-engine-sc
    ai: needs-triage
    and removed
    scope: system
    status: waiting for maintainer
    on Nov 15, 2026
  5. github-actions commented on Nov 15, 2026

    @github-actions
    Contributor

    This is a real bug when @mui/system's styled() / createStyled() runs on the styled-components engine (@mui/styled-engine-sc). Your analysis matches the current code.

    Before wrapping a styled component, styled() removes the sx style function from that component's styles. The goal is to make sx run only once, in the outer component. The two engines handle this differently:

    • Emotion: the change only affects the style list that the new wrapper copies. The inner component keeps rendering with its own styles, so its sx still works.
    • styled-components: the change is written into the inner component's own style rules, which it reads at render time. After styled(Inner) runs, Inner used on its own ignores sx for good. Your before/after output shows this.

    The styled-components v7 double-sx output you describe follows from the same code: with no field to change there, the filter does nothing and both layers run sx.

    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 on Inner:

    const Outer = styled((props) => <Inner {...props} />)`color: blue;`;

    When the wrapped tag isn't an HTML tag, styled() doesn't forward sx by default. So sx should run only in Outer, and Inner keeps its own sx when you use it on its own. The trade-off: Inner's styled-components identity is no longer part of Outer, which matters if you target Outer with component selectors.

    Next steps

    • Maintainers: decide whether the "sx runs only in the outermost layer" behavior should move behind an engine-level helper. styled-components would use the attrs marker you proposed, and Emotion would keep its current path. Because of your v7 findings, a fix should also cover the styled-components v7 prereleases.
    • You: nothing else is needed. The report has versions, expected and actual output, and a runnable repro.

    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

  6. added and removed
    ai: needs-triage
    on Nov 15, 2026
  7. github-actions commented on Nov 18, 2026

    @github-actions
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ai: triagedpackage: styled-engine-sctype: bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions