Skip to content

Instantly share code, notes, and snippets.

@joaotanaca
Created July 22, 2021 13:11
Show Gist options
  • Save joaotanaca/c9b5944faae9142e338c51b62d86480c to your computer and use it in GitHub Desktop.
Save joaotanaca/c9b5944faae9142e338c51b62d86480c to your computer and use it in GitHub Desktop.
Mixin for styled-components with types
import { css } from "styled-components";
const breakpoints = {
xs: "480px",
sm: "768px",
md: "992px",
lg: "1200px"
};
type cssParams = Parameters<typeof css>;
const keys = Object.keys(breakpoints) as Array<keyof typeof breakpoints>;
export const respondTo = keys.reduce((accumulator, label) => {
accumulator[label] = (...args: cssParams) => {
return css`
@media (min-width: ${breakpoints[label]}) {
${css(...args)};
}
`;
};
return accumulator;
}, {} as Record<keyof typeof breakpoints, Function>);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment