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.

CSS code after a function call nested in some media query breaks

See original GitHub issue

Current behavior:

This works perfectly

const mqUntil = (bp: number) => `@media (max-width: ${bp}px)`

const NavbarWrapper = styled.div`
  ${props => mqUntil(props.theme.breakpoints.mobile)} {
    padding-left: ${props => props.theme.space[3]};
    padding-right: ${props => props.theme.space[3]};
  }

  background-color: green;
`

Whereas, in some scenarios I couldn’t reproduce consistently, this breaks :

const mqUntil = (bp: number) => `@media (max-width: ${bp}px)`

export const CustomHorizontalPadding = (x: number) => css`
  padding-left: ${x}px;
  padding-right: ${x}px;
`

const NavbarWrapper = styled.div`
  ${props => mqUntil(props.theme.breakpoints.mobile)} {
    ${props => CustomHorizontalPadding(props.theme.space[3])}
  }

  /* HERE : ⚠️ this line is basically ignored */
  background-color: green;
`

Commenting ${props => CustomHorizontalPadding(props.theme.space[3])} (leaving just the empty media query) makes the background appear

To reproduce:

It’s very hard 😞 I have no clue why, but this exact same piece of code is working in other parts of application.

Environment information:

  • react version: 16.13.1
  • emotion version: 10.0.28

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
srmaguracommented, Jul 31, 2022

Hey @badsyntax, you can fix it like this:

const StyledButton = styled.button`
  ${true
    ? css`
    @media (min-width: 20px) {
      ${nestedMediaQuery}
    }`
    : nestedMediaQuery}
  background-color: red;
`;

The only change is adding css on the line with the question mark. It seems to just have been a case of bad syntax 😂.

The reason css is necessary there is that nestedMediaQuery is a SerializedStyles object. An object like that cannot be interpolated into a string using the normal JavaScript string interpolation. That’s why you have to make it a css tagged template literal which does know how to interpolate SerializedStyles objects.

0reactions
badsyntaxcommented, Aug 1, 2022

@srmagura thank you for the reply, solution, and explanation 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nested Media Queries - CSS-Tricks
A lot of times I see a behemoth media query that has literally every style adjustment for any selector in that screen size....
Read more >
Nested CSS3 media queries in SCSS/Sass - Stack Overflow
It makes sense to merge identical items ( screen ) but not the min-width s as they are different. I've found SASS actually...
Read more >
Using media queries - CSS: Cascading Style Sheets | MDN
Media queries allow you to apply CSS styles depending on a device's general type (such as print ... Media queries are used for...
Read more >
How to use media queries with styled components
So how to make it responsive using styled components? Define the breakpoints as a Javascript object; Define the devices for each breakpoint ......
Read more >
Handling Hover, Focus, and Other States - Tailwind CSS
Pseudo-elements, like ::before , ::after , ::placeholder , and ::selection; Media queries, like responsive breakpoints, dark mode, and prefers-reduced-motion ...
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