Skip to content

Instantly share code, notes, and snippets.

@catalinberta
Last active February 12, 2021 06:58
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 catalinberta/97c8f2c20673408dea4251213d709f7f to your computer and use it in GitHub Desktop.
Save catalinberta/97c8f2c20673408dea4251213d709f7f to your computer and use it in GitHub Desktop.
styled-components responsive mixin
/*
This mixin exposes 4 params that allows
you to create any type of media query
@param1: first breakpoint [400px, 800px, etc]
@param2 (optional): type [min, max]
@param3 (optional): orientation [width, height]
@param4 (optional): second breakpoint [1200px, 1600px, etc]
*/
const theme = {
breakpoints: {
xs: "480px",
sm: "768px",
md: "992px",
lg: "1200px",
xl: "1600px",
xxl: "2000px",
uhd: "3000px",
},
responsive: (
breakpoint: string,
type: string = "min",
orientation: string = "width",
breakpoint2: string = ""
) =>
`@media (${type === "max" ? "max" : "min"}-${orientation}: ${breakpoint}) ${
type === "between" ? `and (max-${orientation}: ${breakpoint2})` : ""
} `,
};
/* Use like this: */
export const Container = styled.div(
({ theme }) => `
background-color: red;
${theme.responsive(theme.breakpoints.md, "max", "height")} {
background-color: green;
}
${theme.responsive(theme.breakpoints.sm, "min", "width", theme.breakpoits.md)} {
background-color: blue;
}
${theme.responsive(theme.breakpoints.uhd)} {
background-color: cyan;
content: "I am UHD"
}
`
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment