CSS code after a function call nested in some media query breaks
See original GitHub issueCurrent 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.1emotion
version: 10.0.28
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey @badsyntax, you can fix it like this:
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 thatnestedMediaQuery
is aSerializedStyles
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 acss
tagged template literal which does know how to interpolateSerializedStyles
objects.@srmagura thank you for the reply, solution, and explanation 😃