LoginSignup
0
0

More than 3 years have passed since last update.

TypeScriptでstyled-components内のmediaQueryを管理する。

Posted at

背景

styled-componentsを利用することが多いのですが、プロジェクト全体でMediaQueryの指定がバラバラだったり、max-widthmin-widthが混合していてキモチ悪かった😥のでヘルパーを用意することでmediaQueryを管理しました。

コード

TypeScriptで書きました!!!よかったら参考にしてください。

mediaHelper.ts
type ScreenType = "desktop" | "tablet" | "phone";

const sizes: { [key in ScreenType]: number } = {
  desktop: 1024,
  tablet: 768,
  phone: 560,
};

export const media = (Object.keys(sizes) as Array<keyof typeof sizes>).reduce(
  (acc, key) => {
    acc[key] = (style: String) => {
      return `@media (max-width: ${sizes[key]}px) { ${style} }`;
    };
    return acc;
  },
  {} as { [key in ScreenType]: Function }
);

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0