Skip to content

Instantly share code, notes, and snippets.

@jefflau
Last active October 14, 2018 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jefflau/b6457361267afe2a5ef8fca6d12879d7 to your computer and use it in GitHub Desktop.
Save jefflau/b6457361267afe2a5ef8fca6d12879d7 to your computer and use it in GitHub Desktop.
mediaQuery.js
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