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.

How to use styled-jsx with responsive design (media queries)?

See original GitHub issue

Are there any best practices for using styled-jsx with responsive design (media queries)?

I’m experimenting with react-responsive but it doesn’t seem to accept <style jsx> inside like:

<MediaQuery query='(max-width: 600px)'>
	<style jsx>{`
		.my-element {
			background-color: tomato;
		}
	`}</style>
</MediaQuery>

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:18

github_iconTop GitHub Comments

4reactions
giuseppegcommented, Jan 6, 2018

@joncursi that’s correct. You’d need do add an event listener and keep a boolean in the state:

class Media extends React.Component {
  _media = window.matchMedia(this.props.query)
 state = {
    matches: this._media.matches
 }
 onChange = ({matches}) => {  this.setState({ matches }) }
 componentDidMount() {
   this._media.addListener(onChange)
 }
 componentWillUnmount() {
   this._media.removeListener(onChange)
 }
 render() {
   return this.state.matches ? children : null
 }
}

which you can use like this

<Media query="max-width: 600px"><MyComponent /></Media>

I didn’t test or try the code above but it should work. I believe that https://www.npmjs.com/package/react-media is implemented in a similar way

4reactions
giuseppegcommented, Dec 9, 2017

Anyway this is going out of topic 😃 I think that option 1) and 2) from my comment https://github.com/zeit/styled-jsx/issues/254#issuecomment-313841098 are a good way to go.

If you need now you can also use dynamic styles eg.

({children}) => {
 const small = window.matchMedia('max-width: 600px').matches
 return (
    <div>
        {children}
        <style jsx>{`div { background: ${small ? 'tomato' : 'hotpink' }`}</style>
    </div>
 )
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use media queries with styled components
Non responsive website Click the button below to Edit on CodeSandbox Edit Styled components media queries (before).
Read more >
Style JSX is not working properly with @media-query in NextJS
I am using <style jsx> in Next JS to style my components. When that code is compiled @media query isn't executed successfully.
Read more >
Media queries with styled components - Mario Kandut
1. Define breakpoints · 2. Define devices · 3. Apply media query in styled-component.
Read more >
React inline styles and media queries using a ... - Medium
I'm a big fan of React inline styles. Co-locating styles with the component and using javascript to define them fits my programmer brain ......
Read more >
How To Use Media Queries in React - Upmostly
Media queries are used in responsive design. They are used to apply different CSS rules to different types of devices, most commonly screen...
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