๐Ÿ’ป์›น(Web)/React

[React]React ํ”„๋กœ์ ํŠธ์— CSS ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•(Nomad Coders ๊ฐ•์˜ ์ •๋ฆฌ)

stonesy 2023. 8. 15. 16:20
728x90

Nomad Coders ๊ฐ•์˜๋Š” ์˜ˆ์ „์— ๋“ค์—ˆ์—ˆ๋Š”๋ฐ ๋…ธ์…˜์— ์ •๋ฆฌํ•œ ๋‚ด์šฉ์„ ๋ธ”๋กœ๊ทธ์—๋„ ์ •๋ฆฌํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์„œ ๋ณต์Šต ๊ฒธ ์ •๋ฆฌ๋ฅผ ํ•ด๋ณด์•˜๋‹ค.

 

React ํ”„๋กœ์ ํŠธ์— CSS๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐฉ์‹์—๋Š” ์ง์ ‘ CSS๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐฉ์‹, module.css๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹, styled-components๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹ ๋“ฑ์ด ์žˆ๋‹ค.

 

1. ์ง์ ‘ CSS๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐฉ์‹

๊ฐ€์žฅ ๋‹จ์ˆœํ•œ ๋ฐฉ์‹์ธ๋ฐ, hover ๋“ฑ์˜ ์ด๋ฒคํŠธ ๋“ฑ์„ ์ ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

function Header(){
    return(<div style={{backgroundColor: "skyblue", width: "100%", height: 72}}>
        Header
    </div>)
}

 

2. module.css๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹

๋‚ด๊ฐ€ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•  ๋•Œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ํŒŒ์ผ์ด๋ฆ„.module.css๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง„ ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  styles๋ฅผ ๋ถˆ๋Ÿฌ์™€ ์‚ฌ์šฉํ•œ๋‹ค.

*Header.tsx ํŒŒ์ผ

import styles from "./Header.module.css";

function Header(){
    return(<div className={styles.header__container}>
        Header
    </div>)
}

*Header.module.css ํŒŒ์ผ

.header__container{
    background-color: skyblue;
    width: 100%;
    height: 72px;
}

 

module.css๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด className์„ ๋žœ๋คํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์ค˜์„œ ํ”„๋กœ์ ํŠธ ๋‚ด์—์„œ className์ด ๊ฒน์น˜๋Š” ๊ฒƒ์„ ๊ฑฑ์ •ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

 

3. styled-components๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹

styled-components๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ styled-components๋ฅผ ๋จผ์ € ์„ค์น˜ํ•˜์ž

โœจ์„ค์น˜

npm i styled-components

*Typescript๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ

npm i styled-components
npm install --save-dev @types/styled-components

 

styled-components ๋ฌธ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ฃผ์˜ํ•ด์•ผํ•  ์ ์€ ์ž‘์€ ๋”ฐ์˜ดํ‘œ๊ฐ€ ์•„๋‹ˆ๋ผ "๋ฐฑํ‹ฑ"์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

import styled from "styled-components";

const Header = styled.div`
    background-color: skyblue;
    width: 100%;
    height: 72px;
`;

function MainPage() {
    return (
        <div>
            <Header>
                Header
            </Header>
        </div>
    )
}

 

๐Ÿ”Žstyled-components์—์„œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ™•์žฅ์„ฑ ์žˆ๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

1. ์ƒ์†

import styled from "styled-components";

const Header = styled.div`
    width: 100%;
    height: 72px;
`;
const BlueHeader = styled(Header)`
    background-color: "skyblue";
`;
const PinkHeader = styled(Header)`
    background-color: "pink";
`;

function MainPage() {
    return (
        <div>
            <BlueHeader />
            <PinkHeader />
        </div>
    )
}

2. props๋ฅผ ์ด์šฉํ•ด ์ปดํฌ๋„ŒํŠธ์— ๊ฐ’ ์ „๋‹ฌํ•˜๊ธฐ

import styled from "styled-components";

interface IHeader{
    backgroundColor: string;
}
const Header = styled.div<IHeader>`
    background-color: ${props=>props.backgroundColor};
    width: 100%;
    height: 72px;
`;

function MainPage() {
    return (
        <div>
            <Header backgroundColor="skyblue" />
            <Header backgroundColor="pink" />
        </div>
    )
}

3. HTML tag๋ฅผ ๋ฐ”๊พธ๋Š” 'As', ์†์„ฑ ๊ฐ’์„ ์„ค์ •ํ•˜๋Š” 'Attrs'

styled-components์—์„œ 'As'๋ฅผ ํ†ตํ•ด์„œ HTML tag์˜ ์ด๋ฆ„์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.

<Header as="a" backgroundColor="skyblue">
	Header
</Header>

'Attrs'๋ฅผ ํ†ตํ•ด์„œ๋Š” HTML ํƒœ๊ทธ ๋‚ด์˜ ์†์„ฑ๊ฐ’์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.

const Input = styled.input.attrs({required: true, maxLength: 30})``;

function MainPage() {
    return (
        <div>
            <Input/>
        </div>
    )
}

 

*styled-components์—์„œ animation ์ ์šฉํ•˜๊ธฐ

const HeaderAnimation = keyframes`
    from{
        transform: translateY(20px);
    }
    to{
        transform: translateX(-20px);
    }
`;
const Header = styled.div`
    width: 100%;
    height: 72px;
    animation: ${HeaderAnimation} 1s linear infinite;
`;

function MainPage() {
    return (
        <div>
            <Header>
                Hello
            </Header>
        </div>
    )
}

 

728x90