Last active
July 19, 2018 09:38
-
-
Save nishant8BITS/0ba4443da58b2f6a0a249a4a8415fb1a to your computer and use it in GitHub Desktop.
Media Query Utils for Style-Components
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { css } from 'styled-components'; | |
const sizes = { | |
largedesktop:1200, | |
desktop: 992, | |
tablet: 768, | |
phone: 576 | |
} | |
// Iterate through the sizes and create a media template | |
const media = Object.keys(sizes).reduce((acc, label) => { | |
acc[label] = (...args) => css` | |
@media (max-width: ${sizes[label] / 16}em) { | |
${css(...args)} | |
} | |
` | |
return acc | |
}, {}) | |
export default media; | |
const Content = styled.div` | |
height: 3em; | |
width: 3em; | |
background: papayawhip; | |
/* Now we have our methods on media and can use them instead of raw queries */ | |
${media.desktop`background: dodgerblue;`} | |
${media.tablet`background: mediumseagreen;`} | |
${media.phone`background: palevioletred;`} | |
`; | |
render( | |
<Content /> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment