Skip to content

styled-components v6: syntax error in one line prevents remaining styles from applying #5598

Closed

Description

@ttkien

📝 Description

In styled-components v6, if a line inside a styled block contains invalid CSS syntax, it causes all subsequent styles to be ignored.

In v5, the same malformed line would be silently ignored, and the rest of the valid styles would still be applied.


💥 Reproduction Example

const Circle = styled.div`
  width: 100px;
  height: 100px;
  border-radius: 50%;
  line-height: ${({ theme }) => "14px}"}; // ⛔️ Syntax error: redundant `}`
  background-color: green; // ❌ This is ignored in v6
`;

In v6, because of the syntax error on the line-height line, background-color: green; is also ignored.

Code example in code sandbox
https://codesandbox.io/p/sandbox/styled-components-v6-examples-forked-9hm43z

Demo
Image

Environment

  • styled-components: 5.3.11 -> 6.0.7
  • react: 18.0.0

Expected Behavior (as in v5)

  • The invalid line is ignored.
  • background-color: green; is still applied.
  • The background should render as a green.

Actual Behavior in v6

  • The error in line-height causes background-color and any further styles to be ignored entirely.
  • The background does not turn green.

Activity

  1. changed the title [-]Styled-components v6: syntax error in one line prevents remaining styles from applying[/-] [+]styled-components v6: syntax error in one line prevents remaining styles from applying[/+] on Aug 7, 2025
  2. SirFancyWalrus commented on Aug 17, 2025

    @SirFancyWalrus

    After looking into this issue, I believe it's an edge case of the Style Rule parsing. styled-components allows for nested styling rules/objects, so when it parses your extra } character it's treated as the end of an object (that could be nested). Here are some screenshots of a small test I used to understand what rules were generated using your example:

    Image 1: Your example case with what I imagine is the output CSS you're expecting

    Image

    Image 2: The actual output CSS

    Image

    We expect

    ".b {
            width: 100px;
            height: 100px;
            border-radius: 50%;
            background-color: green;
    }"
    

    or maybe

    ".b {
            width: 100px;
            height: 100px;
            border-radius: 50%;
            line-height: 14px};
            background-color: green;
    }"
    

    where the invalid line silently fails and doesn't impact the end Component. But since the output is actually

    ".b {
            width: 100px;
            height: 100px;
            border-radius: 50%;
            line-height: 14px
    }
    background-color: green;
    "
    

    the Styling outside of {} is not included and applied (it's outside of the definition).

    Path Forward - Open Discussion

    I don't know if fixing this "issue" is reasonable since the parsing logic would need to understand the developer's intent. For example, when parsing the following object start/end characters {}}, how do you know which ending brace is the "correct" one to ignore when one of them was added in error?

    Errors like the following are handled and properly render styles surrounding the failing line since it doesn't interact with the nested rules parsing:

    line-height: ${({ theme }) => "14px\nffdg$%# \n "};
    
  3. ttkien commented on Aug 18, 2025

    @ttkien
    Author

    Thank you @SirFancyWalrus for your thoughtful response and for taking the time to investigate this.

    You’ve made an excellent point about the challenges of parsing ambiguous syntax like {}}. That said, the core issue from a developer’s perspective is the need to isolate the impact of an error. A typo in a single CSS property should not silently invalidate all subsequent, otherwise valid properties within the same component. This cascading failure significantly complicates debugging.

    This highlights the key difference in behavior between styled-components v5 and v6. It appears that styled-components v5 employed a more forgiving parsing strategy. When encountering an invalid line such as line-height: 14px};, it would simply discard that line and continue parsing the rest. This approach was more resilient because it effectively contained the error at its source without impacting unrelated styles.

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

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions