Last active
October 14, 2018 01:31
-
-
Save jefflau/b6457361267afe2a5ef8fca6d12879d7 to your computer and use it in GitHub Desktop.
mediaQuery.js
This file contains 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 = { | |
giant: 1170, | |
desktop: 992, | |
tablet: 768, | |
phone: 376 | |
} | |
// iterate through the sizes and create a media template | |
export const media = Object.keys(sizes).reduce((accumulator, label) => { | |
// use em in breakpoints to work properly cross-browser and support users | |
// changing their browsers font-size: https://zellwk.com/blog/media-query-units/ | |
const emSize = sizes[label] / 16 | |
accumulator[label] = (...args) => css` | |
@media (max-width: ${emSize}em) { | |
${css(...args)} | |
} | |
` | |
return accumulator | |
}, {}) | |
export default media | |
// use it like this | |
import media from './mediaQuery.js' | |
const Container = styled.div` | |
color: #333; | |
${media.desktop`padding: 0 20px;`} | |
${media.tablet`padding: 0 10px;`} | |
${media.phone`padding: 0 5px;`} | |
` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment