CSS를 불러와서 사용할 때 클래스 이름을 고유한 값, 즉 [파일 이름]_[클래스 이름]__[해시값] 형태로 자동으로 만들어서 컴포넌트 스타일 클래스 이름이 중첩되는 현상을 방지해 주는 기술
[컴포넌트 이름].module.css
라는 이름의 css 를 만들어 준다.styles['my-class']
와 같이 가져옴${styles.one}
${styles.two}
와 같이 조회해야함. 이런 번거로움을 개선한 것이 classnames 라이브러리의 bind기능classNames 라이브러리
- classNames의 bind 기능 이용해 조건부 렌더링이나 여러 개의 클래스를 사용시 좀 더 편하게 사용할 수 있게 함.
Usage
- 사용법
npm install classnames
- component 파일 상단에 import classNames from 'classnames/bind';
- const cx = classNames.bind(styles);
- className={cx("클래스 이름")} 과 같이 해당 스타일 가져옴
여러 클래스 사용하기
cx('one', 'two')
조건부 스타일링
cx("my-component", { condition: true, }); cx("my-component", ["another", "classnames"]);
사용예
// javascript import styles from "../styles/Box.module.css"; import { MdCheckBox, MdCheckBoxOutlineBlank } from "react-icons/md"; import classNames from "classnames/bind"; const cx = classNames.bind(styles); const CheckBox = ({ children, checked, ...rest }) => { return ( <div className={cx("checkbox")}> <label> <input type="checkbox" checked={checked} {...rest} /> <div className={cx("icon")}> {checked ? <MdCheckBox /> : <MdCheckBoxOutlineBlank />} </div> </label> <p className={cx("make-this-local")}>local</p> <p className="aqua">global</p> {/*글로벌 스타일을 적용할 수 있음*/} <span>{children}</span> </div> ); }; export default CheckBox;
// css :global .aqua { color: aqua; } :local .make-this-local { color: palevioletred; }
: global
→ 글로벌하게 적용되는 CSS임을 알 수 있게 해줌: local
→ 그 해당 컴포넌트에서만 사용될 수 있는 클래스임을 정의// components/CheckBox.js
import React from "react";
function CheckBox({ checked, children, ...rest }) {
return (
<div>
<label>
<input type="checkbox" checked={checked} {...rest} />
<div>{checked ? "체크됨" : "체크 안됨"}</div>
</label>
<span>{children}</span>
</div>
);
}
export default CheckBox;
...rest
를 사용한 이유는, CheckBox 컴포넌트에게 전달하게 될 name
, onChange
같은 값을 그대로 input 에게 넣어주기 위함// App.js
import React, { useState } from "react";
import CheckBox from "./components/CheckBox";
function App() {
const [check, setCheck] = useState(false);
const onChange = (e) => {
setCheck(e.target.checked);
};
return (
<div>
<CheckBox onChange={onChange} checked={check}>
다음 약관에 모두 동의
</CheckBox>
<p>
<b>check: </b>
{check ? "true" : "false"}
</p>
</div>
);
}
export default App;
react-icons
아이콘들을 컴포넌트 형태로 쉽게 사용하기 React Icons
Install> npm i react-icons
Usage
import { IconName } from "react-icons/IconTitle"; ... <IconName/>
// components/CheckBox.js
import React from "react";
import { MdCheckBox, MdCheckBoxOutlineBlank } from "react-icons/md";
function CheckBox({ checked, children, ...rest }) {
return (
<div>
<label>
<input type="checkbox" checked={checked} {...rest} />
<div>{checked ? <MdCheckBox /> : <MdCheckBoxOutlineBlank />}</div>
</label>
<span>{children}</span>
</div>
);
}
export default CheckBox;
// components/CheckBox.module.css
.checkbox {
display: flex;
align-items: center;
}
.checkbox label {
cursor: pointer;
}
/* 실제 input 을 숨기기 위한 코드 */
.checkbox input {
width: 0;
height: 0;
position: absolute;
opacity: 0;
}
.checkbox span {
font-size: 1.2rem;
font-weight: bold;
}
.icon {
display: flex;
align-items: center;
/* 아이콘의 크기는 폰트 사이즈로 조정 가능 */
font-size: 2rem;
margin-right: 0.25rem;
color: #adb5bd;
}
.checked {
color: #339af0;
}
<div class="CheckBox_checkbox__Y6fhT">...</div>
와 같은 고유 클래스명 생성됨// components/CheckBox.js
import React from "react";
import { MdCheckBox, MdCheckBoxOutlineBlank } from "react-icons/md";
import styles from "./CheckBox.module.css";
function CheckBox({ checked, children, ...rest }) {
return (
<div className={styles.checkbox}>
<label>
<input type="checkbox" checked={checked} {...rest} />
<div className={styles.icon}>
{checked ? (
<MdCheckBox className={styles.checked} />
) : (
<MdCheckBoxOutlineBlank />
)}
</div>
</label>
<span>{children}</span>
</div>
);
}
export default CheckBox;
classnames
라이브러리의 bind 활용styles.icon
이런 식으로 객체안에 있는 값을 조회해야함-
가 들어가 있다면 > styles['my-class']
${styles.one} ${styles.two}
${styles.one} ${condition ? styles.two : ''}
import React from "react";
import { MdCheckBox, MdCheckBoxOutlineBlank } from "react-icons/md";
import styles from "./CheckBox.module.css";
import classNames from "classnames/bind";
const cx = classNames.bind(styles);
function CheckBox({ checked, children, ...rest }) {
return (
<div className={cx("checkbox")}>
<label>
<input type="checkbox" checked={checked} {...rest} />
<div className={cx("icon")}>
{checked ? (
<MdCheckBox className={cx("checked")} />
) : (
<MdCheckBoxOutlineBlank />
)}
</div>
</label>
<span>{children}</span>
</div>
);
}
export default CheckBox;
cx('클래스이름')
과 같은 형식으로 편하게 사용 할 수 있다cx("one", "two");
cx("my-component", {
condition: true,
});
cx("my-component", ["another", "classnames"]);
CSS Module 은 Sass 에서도 사용 할 수 있다. (컴포넌트.module.scss
)
// .module.css
:global .my-global-name {
}
// .module.scss
:global {
.my-global-name {
}
}
// .module.css
:local .make-this-local {
}
// .module.scss
:local {
.make-this-local {
}
}