Skip to content

Instantly share code, notes, and snippets.

@dgca
Last active July 10, 2020 17:34
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 dgca/6f44469986ae2d6979ead7897f9ebe2f to your computer and use it in GitHub Desktop.
Save dgca/6f44469986ae2d6979ead7897f9ebe2f to your computer and use it in GitHub Desktop.
Utility functions to facilitate writing media queries in Styled Components
/**
* @example
* import styled from "styled-components";
* import {bp, atAndBelow} from '../path/to/styled-components-breakpoint-utils';
*
* const Text = styled.p`
* font-size: 20px;
*
* ${atAndBelow(bp.s, (css) => css`
* font-size: 18px;
* `)}
* `
*/
import { css } from "styled-components";
const xs = "450px";
const s = "768px";
const m = "1170px";
const l = "1440px";
export const bp = {
xs,
s,
m,
l,
};
export function atAndBelow(breakpoint, styles) {
return css`
@media screen and (max-width: ${breakpoint}) {
${styles(css)}
}
`;
}
export function atAndAbove(breakpoint, styles) {
return css`
@media screen and (min-width: ${breakpoint}) {
${styles(css)}
}
`;
}
export function between(min, max, styles, inclusive = true) {
if (inclusive) {
return css`
@media screen and (min-width: ${min}) and (max-width: ${max}) {
${styles(css)}
}
`;
}
return css`
@media screen and (min-width: calc(${min} - 1px)) and (max-width: calc(${max}) + 1px) {
${styles(css)}
}
`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment