Skip to content

Instantly share code, notes, and snippets.

@nishant8BITS
Last active July 19, 2018 09:38
Show Gist options
  • Save nishant8BITS/0ba4443da58b2f6a0a249a4a8415fb1a to your computer and use it in GitHub Desktop.
Save nishant8BITS/0ba4443da58b2f6a0a249a4a8415fb1a to your computer and use it in GitHub Desktop.
Media Query Utils for Style-Components
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