Styled-component attrs

Style-component๋กœ input ํƒœ๊ทธ์˜ ์†์„ฑ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

<input class="text_input" type="text" placeholder="html also good!" required /><!-- in CSS --> .text_input { color: red };
Html์—์„œ์˜ ์ธํ’‹๊ณผ ์Šคํƒ€์ผ๋ง.
" use strict "; const TextInput = styled.input.attrs({ type: "text", required: true, placeholder: "์Šคํƒ€์ผ ์ปดํฌ๋„ŒํŠธ ๊ฟ€ํ…œ!" })` color: red; `;
Style-components๋ฅผ ํ™œ์šฉํ•œ ์†์„ฑ ๊ฐ’ ๋ณ€๊ฒฝ ํ›„ ์Šคํƒ€์ผ๋ง ํ•œ ๋ชจ์Šต.
notion image

attrs ๋ฅผ ์ด์šฉํ•ด์„œ ํƒœ๊ทธ์˜ ์†์„ฑ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

const InputCheckbox = styled.input.attrs({ type: 'checkbox', checked: true, })` border-radius: 5px; color: red; `;

mixin(์Šคํƒ€์ผ ์‹œํŠธ ์ „์ฒด์—์„œ ์žฌ์‚ฌ์šฉ ํ•  CSS ์„ ์–ธ ๊ทธ๋ฃน ์„ ์ •์˜ํ•˜๋Š” ๊ธฐ๋Šฅ)

const awesomeCard = css` box-shadow: 0 4px 6px blue, 0 1px 3px blue; background-color: white; border-radius: 10px; padding: 20px; `; const InputCheckbox = styled.input.attrs({ type: 'checkbox', checked: true, })` border-radius: 5px; color: red; ${awesomeCard} // <== ์ถ”๊ฐ€ `;

ThemeProvider ๋ฅผ ์ด์šฉํ•ด์„œ React Child component๊นŒ์ง€ ์ œ๊ณต๋œ ํ…Œ๋งˆ๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•.

๋จผ์ € theme.js ๋ฅผ ์ƒ์„ฑ
const theme = { mainColor: 'red', dangerColor: 'blue', successColor: 'gray', } export default theme;
App.js ์—์„œ ThemeProvider์™€ theme๋ฅผ import ํ•œ๋’ค, ์ปดํฌ๋„ŒํŠธ ์ตœ์ƒ๋‹จ์— ThemeProvider ์ปดํฌ๋„ŒํŠธ์— theme๋ฅผ props๋กœ ๋„ฃ์–ด์ค€๋‹ค.
import styled, { createGlobalStyle, ThemeProvider } from 'styled-components'; import themes from './themes'; const Card = styled.div` background-color: white; `; const Button = styled.button` border-radius: 30px; padding: 25px 15px; background-color: ${props => props.theme.dangerColor} // <== props.theme์˜ ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ `; class App extends Component { render() { return ( <ThemeProvider theme={themes}> // <== props <React.Fragment> <GlobalStyle /> <Container> <Form /> </Container> </React.Fragment> </ThemeProvider>); } } const Form = () => ( <Card> <Button>Hello</Button> </Card> )