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

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

Jaeseo Kim 2022. 9. 15. 23:57

๊ฐ„ํŽธํ•˜๊ฒŒ ๋ฐ˜์‘ํ˜• ํฌ๊ธฐ๋ฅผ ์„ค์ •ํ•ด๋ด…์‹œ๋‹ค!

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

index.js

import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App";

import "./styles.css";
import { createGlobalStyle } from "styled-components";

const GlobalStyles = createGlobalStyle`
  html {
    font-size: 62.5%; 
  }
`;

const rootElement = document.getElementById("root");
const root = createRoot(rootElement);
root.render(
  <StrictMode>
    <GlobalStyles />
    <App />
  </StrictMode>
);

App.js

import "./styles.css";
import React from "react";
import Typography from "@mui/material/Typography";
import {
  createTheme,
  ThemeProvider,
  responsiveFontSizes
} from "@mui/material/styles";

let theme = createTheme({
  typography: {
    fontFamily: "Noto Sans KR",
    htmlFontSize: 10
  }
});

/**
 * (๋ฐ˜์‘ํ˜•) ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•œ theme์˜ ๊ธ€๊ผด ํฌ๊ธฐ๊ฐ€ ํ™”๋ฉด ํฌ๊ธฐ์— ๋งž๊ฒŒ ๋ณ€ํ•ด์•ผ ํ•จ.
 * ๋”ฐ๋ผ์„œ, responsiveFontSizes๋กœ theme๋ฅผ ๊ฐ์‹ธ์คŒ.
 */
theme = responsiveFontSizes(theme);

export default function App() {
  return (
    <ThemeProvider theme={theme}>
      <div className="App">
        <Typography variant="h1">๋ฐ˜์‘ํ˜•</Typography>
      </div>
    </ThemeProvider>
  );
}

 

00. rem


https://www.sitepoint.com/understanding-and-using-rem-units-in-css/

 

Rem in CSS: Understanding and Using rem Units - SitePoint

Adrian Sandu explains the purpose and advantages of rem units, demonstrating ways to use em and rem units in CSS layouts.

www.sitepoint.com

font size๋ฅผ ์ผ๊ด€์„ฑ ์žˆ๊ฒŒ ์ •ํ•˜๊ธฐ ์œ„ํ•ด์„ , rem ๋‹จ์œ„๋ฅผ ์จ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. rem์ด๋ž€ root ํฌ๊ธฐ์— ์˜์กดํ•œ ๋‹จ์œ„์ž…๋‹ˆ๋‹ค. (root๋Š” html์ž…๋‹ˆ๋‹ค.)

๊ทธ๋Ÿฐ๋ฐ ํ˜„์žฌ root์˜ defalutํฌ๊ธฐ๋Š” 16px์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ 10px์„ rem์œผ๋กœ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„  16์œผ๋กœ ๋‚˜๋ˆˆ 0.625rem์„ ์จ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ.. 16์œผ๋กœ ๋‚˜๋ˆ„๊ธฐ ๊ท€์ฐฎ์ž–์•„์š”. ์•„์˜ˆ rem์œผ๋กœ ํ‘œํ˜„ํ•˜๊ธฐ ํŽธํ•˜๊ฒŒ ๋ฐ”๊ฟ”์ค์‹œ๋‹ค!๐Ÿ”ฅ

 

html์˜ defalut font-size์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ 16px ์ธ ๊ฒƒ์ด์ฃ .

html {
    font-size: 100%;
}

๊ทธ๋Ÿผ 10px์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด 0.625rem ๋Œ€์‹ , (๋น„๊ต์  ์‰ฝ๊ฒŒ 10์œผ๋กœ ๋‚˜๋ˆˆ) 1.0rem์„ ์จ์ฃผ๊ธฐ ์œ„ํ•ด์„  ์•„๋ž˜์ฒ˜๋Ÿผ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

html {
  font-size: 62.5%; /* 62.5% of 16px = 10px */
}

index.js์— createGlobalStyle์œผ๋กœ ์ ์šฉํ•œ ๋ชจ์Šต์ž…๋‹ˆ๋‹ค.

import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App";

import "./styles.css";
import { createGlobalStyle } from "styled-components";

const GlobalStyles = createGlobalStyle`
  html {
    font-size: 62.5%; 
  }
`;

const rootElement = document.getElementById("root");
const root = createRoot(rootElement);
root.render(
  <StrictMode>
    <GlobalStyles />
    <App />
  </StrictMode>
);

 

 

01. theme


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

 

Typography - Material UI

The theme provides a set of type sizes that work well together, and also with the layout grid.

mui.com

htmlFontSize๋ฅผ 10์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

const theme = createTheme({
  typography: {
    // Tell MUI what's the font-size on the html element is.
    htmlFontSize: 10,
  },
});

๊ทธ ํ›„, ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•œ theme ๊ธ€๊ผด ํฌ๊ธฐ๊ฐ€ ๋ฐ˜์‘ํ˜•์ด ์ ์šฉ๋˜๋„๋ก responsiveFontSizes๋กœ theme๋ฅผ ๊ฐ์‹ธ์ค๋‹ˆ๋‹ค.

/**
 * (๋ฐ˜์‘ํ˜•) ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•œ theme์˜ ๊ธ€๊ผด ํฌ๊ธฐ๊ฐ€ ํ™”๋ฉด ํฌ๊ธฐ์— ๋งž๊ฒŒ ๋ณ€ํ•ด์•ผ ํ•จ.
 * ๋”ฐ๋ผ์„œ, responsiveFontSizes๋กœ theme๋ฅผ ๊ฐ์‹ธ์คŒ.
 */
theme = responsiveFontSizes(theme);

 

๋งˆ์ง€๋ง‰์œผ๋กœ theme๋ฅผ ์ธ์ž๋กœ ๋„˜๊ฒจ์ค€ ThemeProvider๋กœ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฐ์‹ธ์ค๋‹ˆ๋‹ค.

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

 

02. ์ ์šฉ


width, height ๋“ฑ๋“ฑ ํฌ๊ธฐ๋ฅผ ์ •ํ•ด์ค˜์•ผํ•˜๋Š” ๋‹จ์œ„๋“ค์€ rem์œผ๋กœ ์‚ฌ์šฉํ•ด์ค๋‹ˆ๋‹ค.

์œ„ ์˜ˆ์‹œ๋Š” height๊ฐ€ 7.2rem(72px)์„ ์ ์šฉํ•œ ๋ฒ„ํŠผ์ž…๋‹ˆ๋‹ค.

import "./styles.css";
import React from "react";
import Button from "@mui/material/Button";
import {
  createTheme,
  ThemeProvider,
  responsiveFontSizes
} from "@mui/material/styles";

let theme = createTheme({
  typography: {
    fontFamily: "Noto Sans KR",
    htmlFontSize: 10
  }
});
/**
 * (๋ฐ˜์‘ํ˜•) ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•œ theme์˜ ๊ธ€๊ผด ํฌ๊ธฐ๊ฐ€ ํ™”๋ฉด ํฌ๊ธฐ์— ๋งž๊ฒŒ ๋ณ€ํ•ด์•ผ ํ•จ.
 * ๋”ฐ๋ผ์„œ, responsiveFontSizes๋กœ theme๋ฅผ ๊ฐ์‹ธ์คŒ.
 */
theme = responsiveFontSizes(theme);

export default function App() {
  return (
    <ThemeProvider theme={theme}>
      <div className="App">
        <Button variant="contained" sx={{ height: "7.2rem" }}>
          ๋ฐ˜์‘ํ˜• ๋ฒ„ํŠผ
        </Button>
      </div>
    </ThemeProvider>
  );
}

 

๊ทธ๋Ÿผ ์ด์ œ ์–ด๋–ค ์ปดํฌ๋„ŒํŠธ๋ฅผ ์“ฐ๋“  ๋ฉ”ํ…Œ๋ฆฌ์–ผ ๋””์ž์ธ์—์„œ ์ œ๊ณตํ•ด์ฃผ๋Š” ์ปดํฌ๋„ŒํŠธ์—๋Š” ๋ฐ˜์‘ํ˜•์ด ์ ์šฉ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค..!!!!