Skip to content

[styled-components] Using expression inside template literal along with attribute selector results in invalid CSS syntax. #4520

@skawinski

Description

@skawinski

Styling component based on the attribute state of its neighbour element results in invalid CSS markup. Space is being added between expression and attribute selector. It happens only when styled-components are being used with their default "styled" alias. Using any other name seem to cause prettier ignore insides of template literals.

It' quite a border use case, but unfortunately it results in broken CSS syntax not recognised by browser engine.

Prettier 1.12.1
Playground link

Input:

const x = styled.div``;

const a = anything.anything`
  ${x}[value=true] + & {
  }
`;

const b = styled.anything`
  ${x}[value=true] + & {
  }
`;

Output:

const x = styled.div``;

const a = anything.anything`
  ${x}[value=true] + & {
  }
`;

const b = styled.anything`
  ${x} [value=true] + & {
  }
`;

Expected behavior:

const x = styled.div``;

const a = anything.anything`
  ${x}[value=true] + & {
  }
`;

const b = styled.anything`
  ${x}[value=true] + & {
  }
`;

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:multiparserIssues with printing one language inside another, like CSS-in-JSlang:css/scss/lessIssues affecting CSS, Less or SCSSlocked-due-to-inactivityPlease open a new issue and fill out the template instead of commenting.priority:highCode is printed in a way that alters the AST, breaks syntax, or is a significant regression. Urgent!

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions