Created
March 29, 2018 10:35
-
-
Save KhalilZaidoun/6bed55301d3fa060baffa38f3ecf867e to your computer and use it in GitHub Desktop.
Media templates utility for styled-components library
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 = { | |
big: 1600, | |
desktop: 1024, | |
laptop: 768, | |
tablet: 430, | |
}; | |
// taken from: https://github.com/styled-components/styled-components/blob/master/docs/tips-and-tricks.md#media-templates | |
export const media = Object.keys(sizes).reduce((acc, 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; | |
acc[label] = (...args) => css` | |
@media (min-width: ${emSize}em) { | |
${css(...args)} | |
} | |
`; | |
return acc; | |
}, {}); | |
/* | |
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