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.

Components with multiple media queries only outputting the first media query in v2.1.1

See original GitHub issue

Version

2.1.1

Also using babel-plugin-styled-components: version 1.1.17

Expected Behavior

Using the following CSS block should obviously result in 3 media queries being outputted for this styled-component.

const ContentSizer = styled.div`
    display: none;
    flex: 0 0 auto;
    width: 25%;
    margin: 0;

    @media(min-width: 1920px) {
      width: 20%;
    };

    @media(max-width: 768px) {
      width: calc(100% / 3);
    };

    @media(max-width: 480px) {
      width: 100%;
    };
`;

Actual Behavior

Only the first media query is outputted to the stylesheet and the others do not appear at all.

I thought this might be an issue with a breakpoint helper I was using or with the pre-processing issue described here.

To simplify things, I:

  • Hardcoded the @media queries
  • Removed babel-plugin-styled-components from my .babelrc and rebuilt
  • Switched up the order and amount of media queries in the test component

None of those fixed the issue. However what did fix them was downgrading to styled-components v.2.0.0. With v2.0.0 I can use my breakpoint helper and the preprocessing babel plugin without a problem and my simplified test case also works fine of course.

I don’t have additional time at the moment to produce more of a repro but I may try to do that later on. Anyone else seeing this issue?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
k15acommented, Jul 21, 2017

This is a problem the semi colon after the closing braces.

Quick fix would be:

const ContentSizer = styled.div`
    display: none;
    flex: 0 0 auto;
    width: 25%;
    margin: 0;

    @media(min-width: 1920px) {
      width: 20%;
    }

    @media(max-width: 768px) {
      width: calc(100% / 3);
    }

    @media(max-width: 480px) {
      width: 100%;
    }
`;

@thysultan Could you check if you can fix this in stylis?

0reactions
ryanheatherscommented, Jul 25, 2017

@philpl that sounds great. After figuring out what was wrong with my media queries, I looked back through my codebase and found a bunch of other cases where I’d closed interpolated helper functions with semicolons e.g.

const ExampleWithRogueSemicolon = styled.div`
    ${breakpoint('small', css`
      width: 100%;
    `)};
`;

In hindsight it’s obvious those won’t work as expected, but it can tricky to spot and currently styled-components swallows the error and you’re left scratching your head why nothing got outputted to the browser.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using media queries - CSS: Cascading Style Sheets | MDN
Syntax · Media types define the broad category of device for which the media query applies: all , print , screen . ·...
Read more >
Multiple media queries, only apply first - Stack Overflow
If you only want the first media query to apply when the screen is less than or equal to 768px, use min-width. Save...
Read more >
Media Queries Level 4 - W3C
Media Queries allow authors to test and query values or features of the user agent or display device, independent of the document being...
Read more >
How to use media queries with styled components
#2 Update the components to adapt based on device size ; import · from 'styled-components' ;; import { ; } from './device' ;;...
Read more >
Media Queries - Tizen Docs
Media Queries · In CSS. You can define attribute values depending on given conditions: @media only screen and (max-width: 480px) {styles} · In...
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