230524 - React(css module)

๋ฐฑ์Šน์—ฐยท2023๋…„ 5์›” 24์ผ
1

๐Ÿšฉ React

CSS Module

๐Ÿ“ ์„ค๋ช…

  • css module์„ ์‚ฌ์šฉํ•˜๋ฉด css ํด๋ž˜์Šค๊ฐ€ ์ค‘์ฒฉ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • css ํŒŒ์ผ๋ช… ๋’ค์— .module์„ ๋ถ™์—ฌ์ฃผ๋ฉด ๋จ


โœ’๏ธ ์˜ˆ์ œ ์ฝ”๋“œ ์ž‘์„ฑ

์ž…๋ ฅ

App.js

import "./App.css";
import React, { useState } from 'react'
import Button1 from "./components/Button1";
import Button2 from "./components/Button2";
import CheckBox from "./components/CheckBox";

function App() {
  // useState๋กœ ์ฒดํฌ ์—ฌ๋ถ€ ํŒ๋‹จ
  const [check, setChecked] = useState(false);
  const onChange = (event) => {
    setChecked(event.target.checked); // ์ฒดํฌ๋˜์–ด์žˆ์œผ๋ฉด check์•ˆ์— ๋„ฃ์Œ
  }

  return (
    <>
      <Button1 />
      <Button2 />
      <hr />
      <CheckBox checked={check} onChange={onChange}>
        <span>์•ฝ๊ด€ ๋‚ด์šฉ์ด ๋“ค์–ด๊ฐˆ ๋ถ€๋ถ„</span>
      </CheckBox>
    </>
  );
}

export default App;



Button1.jsx

import React from 'react'
import style from "./Button1.module.css";
// ๊ณ ์œ  ์ด๋ฆ„์„ ๋งŒ๋“ค์–ด์„œ ๊ฐ€์ ธ์˜ด

const Button1 = () => {
  return (
    <button className={style.button}>Button1</button>
  )
}

export default Button1



Button1.module.css

.button {
  background-color: skyblue;
  padding: 20px 40px;
  margin: 30px;
}



Button2.jsx

import React from "react";
import aa from "./Button2.module.css";

const Button2 = () => {
  return (
    <>
      <button className={aa.button}>Button2</button>
      <span className="text">๋ฒ„ํŠผ2 ์ปดํฌ๋„ŒํŠธ</span>
    </>
  );
};

export default Button2;



Button2.module.css

.button {
  background-color: coral;
  padding: 30px 60px;
  margin: 30px;
  font-size: 2rem;
}



CheckBox.jsx

// rafce
import React from "react";
// react-icons : ์ปดํฌ๋„ŒํŠธ์ฒ˜๋Ÿผ ์‚ฌ์šฉ
import { MdUpdate, MdUpdateDisabled } from "react-icons/md";
import cx from "./CheckBox.module.scss";

const CheckBox = ({ children, checked, ...rest }) => {
  return (
    <div className={cx.checkbox}>
      <label>
        <input type="checkbox" checked={checked} {...rest} />
        {checked ? <MdUpdate className={cx.checked} /> : <MdUpdateDisabled />}
      </label>

      {/* App.js์—์„œ ๋ฐ›์•„์˜ด */}
      <p>{children}</p>
    </div>
  );
};

export default CheckBox;



CheckBox.module.scss

.checkbox {

  input {
    width: 0;
    height: 0;
    position: absolute;
    opacity: 0;
  }
}

.checked { color: skyblue; }

// ์ „์—ญ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ๋•Œ(๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ)
:global .text { color: cornflowerblue; }



์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด


๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






profile
๊ณต๋ถ€ํ•˜๋Š” ๋ฒจ๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€