1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

styled-componetsのメディアクエリ共通化

Last updated at Posted at 2021-04-20

styled-componentsのメディアクエリ共通化方法を探していましたが、どれも自分にはしっくりこなかったので自作しました。

styled-componentsのメディアクエリ共通化の選択肢(しっくりこなかったやつ)

  • react-responsive
  • styled-media-query

styled-componentsのメディアクエリ定数を自作

以下の記事を参考に定数を自作しました。

// src/styles/const/size
const mediaDeclaration = "@media screen and";
const breakPoint = {
  sm: 480,
  md: 768,
  lg: 1024,
};
export const mediaQuery = {
  xs: `${mediaDeclaration} (min-width: ${breakPoint.sm}px)`,
  sm: `${mediaDeclaration} (min-width: ${breakPoint.md}px)`,
  lg: `${mediaDeclaration} (min-width: ${breakPoint.lg}px)`,
};
// src/components/atoms/Button
import styled from "styled-components"
import { mediaQuery } from "../../styles/const/size"

//jsx省略

//style
const Button = styled.button`
  background-color: black;
  ${mediaQuery.lg} {
    background-color: blue;
  }
`;
1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?