๐Ÿ’ป My Work/๐ŸŽจ Material Design

[Material Design] Material-UI(MUI)๋กœ react & styled component ์‚ฌ์šฉํ•˜๊ธฐ <๋ฐ˜์‘ํ˜• Breakpoints>

Jaeseo Kim 2022. 9. 18. 00:28

๋ฐ˜์‘ํ˜•์„ ์œ„ํ•œ breakpoint ์ž…๋‹ˆ๋‹ค.

์ผ์ • ๋„ˆ๋น„ ์ด์ƒ ํ˜น์€ ์ดํ•˜๊ฐ€ ๋์„ ๋•Œ, ์ปดํฌ๋„ŒํŠธ์˜ ์†์„ฑ์„ ๋ฐ”๊พธ์–ด ํ™”๋ฉด์— ๋งž๊ฒŒ ์กฐ์ ˆํ•˜๋Š” ๊ฒƒ์ด์ฃ !

์ด๋•Œ์˜ ์ผ์ • ๋„ˆ๋น„๊ฐ€ breakpoint ์ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๐Ÿ˜„

https://mui.com/material-ui/customization/breakpoints/

 

Breakpoints - Material UI

API that enables the use of breakpoints in a wide variety of contexts.

mui.com

 

00. Default breakpoints


๋ฉ”ํ…Œ๋ฆฌ์–ผ ๋””์ž์ธ์˜ breakpoint ๊ธฐ๋ณธ๊ฐ’์ž…๋‹ˆ๋‹ค.

xs, sm, md, lg, xl ๋กœ ๋„ˆ๋น„๊ฐ€ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

01. breakpoints ์ปค์Šคํ„ฐ๋งˆ์ด์ง•


๋ฌผ๋ก ! ์ปค์Šคํ„ฐ๋งˆ์ด์ง• ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค๐Ÿ”ฅ๐Ÿ”ฅ

https://avoc-o-d.tistory.com/45

 

[Material Design] Material-UI(MUI)๋กœ react & styled component ์‚ฌ์šฉํ•˜๊ธฐ <๊ธฐ๋ณธ theme๋ฅผ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•ํ•˜๊ธฐ>

https://mui.com/material-ui/customization/theming/ Theming - Material UI Customize MUI with your theme. You can change the colors, the typography and much more. mui.com theme๋ฅผ ์ •์˜ํ•˜๋ฉด, ์ปดํฌ๋„ŒํŠธ๋ฅผ..

avoc-o-d.tistory.com

 

const theme = createTheme({
  breakpoints: {
  /** ์•„๋ž˜ ์ˆซ์ž๋“ค์„ ๋ณธ์ธ์˜ ํ”„๋กœ์ ํŠธ์— ๋งž๊ฒŒ ์„ค์ •ํ•˜์„ธ์š” */
    values: {
      xs: 0,
      sm: 600,
      md: 900,
      lg: 1200,
      xl: 1536,
    },
  },
});

 

๊ผญ xs, sm, md, lg, xl ๊ฐ€ ์•„๋‹ˆ๊ณ , ์•„๋ž˜์ฒ˜๋Ÿผ ์ž์œ ๋กœ์ด ์ด๋ฆ„์„ ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

const theme = createTheme({
  breakpoints: {
    values: {
      mobile: 0,
      tablet: 640,
      laptop: 1024,
      desktop: 1200,
    },
  },
});

 

 

02. ์ ์šฉ


์œ„ ์˜ˆ์‹œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

md(900px) ์ด์ƒ์ผ ๋• ๋ฐ•์Šค์˜ ๋„ˆ๋น„๊ฐ€ 50rem

sm(600px) ์ด์ƒ์ด๊ณ , md(900px) ์ดํ•˜์ผ ๋• ๋ฐ•์Šค์˜ ๋„ˆ๋น„๊ฐ€ 24rem

xs(0px) ์ด์ƒ์ด๊ณ , sm(600px) ์ดํ•˜์ผ ๋• ๋ฐ•์Šค์˜ ๋„ˆ๋น„๊ฐ€ 15rem

๊ฐ€ ๋˜๋„๋ก ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

import "./styles.css";
import React from "react";
import { Box, Typography } from "@mui/material";
import { useTheme } from "@mui/material/styles";

export default function App() {
  /**
   * (๋ฐ˜์‘ํ˜•) breakPoints์— ๋”ฐ๋ผ HTML์— ์ ์šฉํ•˜๋Š” ์Šคํƒ€์ผ์„ ์ „ํ™˜ํ•˜๋„๋ก ํ•จ.
   *
   * useTheme : ํ˜„์žฌ ์ •์˜ํ•œ ํ…Œ๋งˆ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” hook
   * - ํ˜„์žฌ ์ •์˜ํ•œ breakPoints์— ์ ‘๊ทผ ๊ฐ€๋Šฅ
   */
  const theme = useTheme();

  const boxSx = {
    backgroundColor: "#FFDAD6",
    [theme.breakpoints.down("sm") && theme.breakpoints.up("xs")]: {
      width: "15rem"
    },
    [theme.breakpoints.down("md") && theme.breakpoints.up("sm")]: {
      width: "24rem"
    },
    [theme.breakpoints.up("md")]: { width: "50rem" }
  };
  return (
    <div className="App">
      <Box sx={boxSx}>
        <Typography variant="h1">๋ฐ•์Šค</Typography>
      </Box>
    </div>
  );
}

 

 

์œ„ ์˜ˆ์‹œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

md(900px) ์ด์ƒ์ผ ๋• row๋ฐฉํ–ฅ(๊ฐ€๋กœ)

md(900px) ์ดํ•˜์ผ ๋• column๋ฐฉํ–ฅ(์„ธ๋กœ)

์œผ๋กœ ์Œ“์ด๋„๋ก ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

import "./styles.css";
import React from "react";
import { Box, Typography } from "@mui/material";
import { useTheme } from "@mui/material/styles";

export default function App() {
  /**
   * (๋ฐ˜์‘ํ˜•) breakPoints์— ๋”ฐ๋ผ HTML์— ์ ์šฉํ•˜๋Š” ์Šคํƒ€์ผ์„ ์ „ํ™˜ํ•˜๋„๋ก ํ•จ.
   *
   * useTheme : ํ˜„์žฌ ์ •์˜ํ•œ ํ…Œ๋งˆ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” hook
   * - ํ˜„์žฌ ์ •์˜ํ•œ breakPoints์— ์ ‘๊ทผ ๊ฐ€๋Šฅ
   */
  const theme = useTheme();

  const boxSx = {
    display: "flex",
    gap: (theme) => theme.spacing(4),
    [theme.breakpoints.down("md")]: {
      flexDirection: "column"
    },
    [theme.breakpoints.up("md")]: { flexDirection: "row" }
  };
  return (
    <div className="App">
      <Box sx={boxSx}>
        <Typography variant="h1">Item1</Typography>
        <Typography variant="h1">Item2</Typography>
        <Typography variant="h1">Item3</Typography>
        <Typography variant="h1">Item4</Typography>
      </Box>
    </div>
  );
}

 

๐Ÿคฉ๋ฐ˜์‘ํ˜•~๐Ÿคฉ