
์ค๋์ ์ค์ styled-components๋ฅผ ํ์ฉํด
Button, Title, InputText ์ปดํฌ๋ํธ๋ฅผ ๊ตฌํํ ์์ ๋ฅผ ํตํด
์ด๋ป๊ฒ ํ์ ๊ณผ ํ ๋ง๋ฅผ ํ์ฉํด์ ์ ์ง๋ณด์ํ๊ธฐ ์ข์ UI ์ปดํฌ๋ํธ๋ฅผ ๊ตฌ์ฑํ ์ ์๋์ง ์ ๋ฆฌํ๋ค.
theme๊ณผ styled-components๋ฅผ ํ์ฉํด ์ผ๊ด๋ ์คํ์ผ ์ ์ฉinterface Props {
children: React.ReactNode;
size: ButtonSize;
scheme: ButtonScheme;
disabled?: boolean;
isLoading?: boolean;
}
disabled๋ isLoading์ด ์ ์ฉ๋๋ฉด ํด๋ฆญ์ ๋ง๊ณ ์คํ์ผ๋ ์กฐ์ ๋จ.theme.button[size].fontSize, theme.buttonScheme[scheme] ๋ฑ ํ
๋ง๋ฅผ ํตํด ๋์ ์ผ๋ก ์คํ์ผ์ ๊ฐ์ ธ์จ๋ค.const ButtonStyle = styled.button<Omit<Props, "children">>`
font-size: ${({ theme, size }) => theme.button[size].fontSize};
padding: ${({ theme, size }) => theme.button[size].padding};
...
`
์ด๋ ๊ฒ ํ๋ฉด ํ ๋ง ์ค์ ๋ง ๋ฐ๊ฟ๋ ์ ์ฒด ํ๋ก์ ํธ ๋ฒํผ ์คํ์ผ์ ๋ฐ๊ฟ ์ ์์ด์ ์ ์ง๋ณด์์ ๋งค์ฐ ์ ๋ฆฌํ๋ค.
interface Props {
children: React.ReactNode;
size: "large" | "medium" | "small";
color?: ColorKey;
}
theme์ ํตํด ํฐํธ ํฌ๊ธฐ์ ์์์ ๋ฐ์์จ๋ค.color๊ฐ ์ ๋ฌ๋์ง ์์ผ๋ฉด ๊ธฐ๋ณธ ์์(primary)์ ์ฌ์ฉํ๋ค.const TitleStyle = styled.h1<Omit<Props, 'children'>>`
font-size: ${({ theme, size }) => theme.heading[size].fontSize};
color: ${({ theme, color }) => color ? theme.color[color] : theme.color.primary};
`;
์ด์ฒ๋ผ ์ ํ์ ์ผ๋ก ์คํ์ผ ์์ฑ์ ์ ์ดํ ์ ์๊ฒ ํ๋ฉด ๋ค์ํ ์ํฉ์์ ์ฌ์ฌ์ฉ์ฑ์ด ๋์์ง๋ค.
interface Props {
placeholder?: string;
}
forwardRef๋ฅผ ํตํด ์ธ๋ถ์์ input์ ์ง์ ์ ๊ทผํ ์ ์๋๋ก ๊ตฌ์ฑ๋จ.styled.input.attrs๋ก input ํ์
์ ๊ณ ์ ์ํค๋ฉด์๋ ์คํ์ผ์ theme ๊ธฐ๋ฐ์ผ๋ก ์ฒ๋ฆฌ.const InputTextStyled = styled.input.attrs({ type: "text" })`
padding: 0.25rem 0.75rem;
border: 1px solid ${({ theme }) => theme.color.border};
...
`;
์ด ํจํด์ ํผ ์์๋ฅผ ๋ง๋ค ๋ ์์ฃผ ์ฐ์ด๋ ํํ๋ค. ํนํ forwardRef๋ ์ ํจ์ฑ ๊ฒ์ฌ๋ ํฌ์ปค์ค ์ ์ด ๋ฑ์์ ์ ์ฉํ๋ค.
์ด๋ฌํ ๊ตฌ์ฑ์ "๋์์ธ ์์คํ
"์ ๋ง๋ค๊ธฐ ์ํ ๊ธฐ๋ณธ ๊ธฐ๋ฐ์ ์ ์ก์์ฃผ๋ ํจํด์ด๋ค.
ํนํ ๋ค์๊ณผ ๊ฐ์ ์ฅ์ ์ด ์๋ค:
styled-components์ TypeScript์ ์กฐํฉ์ ๊ฐ๋ ฅํ๋ค. ํ์
๊ธฐ๋ฐ์ ์คํ์ผ ๊ตฌ์ฑ์ผ๋ก ์ฌ์ฌ์ฉ์ฑ๊ณผ ์์ ์ฑ์ ๋์ผ ์ ์๋ค.theme์ ์ ๊ทน์ ์ผ๋ก ํ์ฉํ๋ฉด ์คํ์ผ ๋ณ๊ฒฝ์ด ๊ฐํธํ๋ค.Omit<Props, "children">์ ๊ฐ์ด ํ์
์ ์ ์ฐํ๊ฒ ํ์ฉํ๋ฉด styled-components์ ์ ํํ ํ์
์ ์ ๋ฌํ ์ ์๋ค.forwardRef๋ ํผ ๊ตฌ์ฑ ์ ๊ผญ ์ตํ์ผ ํ ํจํด์ด๋ค.