question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

styled-components' `css` prop not working via SWC transform

See original GitHub issue

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System: Platform: darwin Arch: x64 Version: Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:22 PDT 2022; root:xnu-8020.121.3~4/RELEASE_X86_64 Binaries: Node: 14.19.3 npm: 6.14.17 Yarn: 1.22.19 pnpm: N/A Relevant packages: next: 12.1.6 react: 17.0.2 react-dom: 17.0.2

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

The styled-components transform for SWC isn’t working as expected when using the css prop to add styles.

Instead of applying the styles a css HTML attribute is added to the element.

Expected Behavior

When using the css prop in JSX I expect the styled to be applied to the rendered HTML instead of the value being passed as a prop and rendered as an HTML attribute.

Link to reproduction

https://codesandbox.io/s/next-12-styled-components-transform-bug-t53kxi

To Reproduce

In the CodeSandbox I have two styled components that set a different text color. Then I have two tags where I set different colors but via the css prop.

The styles added via the css prop are not applied.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

2reactions
adrienrncommented, Jul 26, 2022

Second this!

NB: It seems that this only happens when using strings and not template strings (truly hope I’m wrong because this should not make any difference unless some transpiling magic), at least, in my project that seems to be correlated.

Forking the codesandbox, I see the following behaviour.

  1. Initial load of the page, works! image

  2. Making a change to the css="" element, live reload recompiles, color does not update: image

  3. Making a change to the css={``} element, live reload recompiles, color updates: image

  4. Reloading the page, both changes apply correctly: image

https://codesandbox.io/s/next-12-styled-components-transform-bug-forked-mt35lq?file=/pages/index.js:280-286

0reactions
github-actions[bot]commented, Sep 23, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tooling - styled-components
By adding a unique identifier to every styled component, this plugin avoids checksum mismatches due to different class generation on the client and...
Read more >
Warning: Prop `className` did not match. when using styled ...
My issue is that when I reload the page deployed by npm run dev, it throws the error of server not matching the...
Read more >
Styled Component CSS Prop Not Working w/ Next.js
If the css styled-components prop isn't working in Next. js, most answers will point you to the babel plugin ('babel-plugin-styled-components') ...
Read more >
Advanced Features: Next.js Compiler
We're working to port babel-plugin-styled-components to the Next.js Compiler. ... Automatically sets up transform using SWC; Loading .env (and all variants) ...
Read more >
The css Prop - Emotion
The primary way to style elements with emotion is the css prop. ... This method will not work with Create React App or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found