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๋ฅผ ํ์ฉํ ์์ฑ ๊ฐ ๋ณ๊ฒฝ ํ ์คํ์ผ๋ง ํ ๋ชจ์ต.
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> )