만족스럽지는 못하지만 책에있는 모든 기능구현을 끝내고 오늘부터 React JS 공부를 다시 시작했다. 첫 챕터부터 매우 만족스러웠다 팀플할때 애먹었던 부분중 하나가 styled-component를 사용하여 CSS를 할 때 였는데 이번 챕터에서 가려운 부분을 많이 긁어주었다.
이제 styled-component를 이용해서 속성값변경 자식선택해서꾸미기, 애니매이션, 다크모드 등 많은 기능들을 사용할 수 있을 것같다. 벌써 할 수 있는것들이 눈에 훤하다!!! 신나는구만ㅎㅎ
오늘 배운 styled-component를 정리해보자면
const Box = styled.div`
background-color: ${(prop) => prop.color};
width: 100px;
height: 100px;
`;
//상속
const Circle = styled(Box)`
border-radius: 50px;
`;
이와같은 방식으로 상속해서 사용하여 코드량을 줄일 수 있다.
const Input = styled.input.attrs({ required: true, minLength: 10 })`
margin: 10px;
background-color: transparent;
`;
이와같이 사용할 수 있는데 이렇게 한다면 Input이라는 컴포넌트를 사용할때 required , minLength : 10이 자동으로 속성값에 들어가서 사용할 수 있다.
import styled, { keyframes } from "styled-components";
export const rotationAnimation = keyframes`
0%{
transform: rotateY(0deg);
background-color: #ffecd2;
}
50%{
transform: rotateY(180deg);
background-color: #fcb69f;
}
100%{
transform: rotateY(0deg);
background-color: #ffecd2;
}
`;
const Animation = styled.div`
display: flex;
justify-content: center;
align-items: center;
height: 30px;
width: 30px;
background-color: #ff7eb3;
border-radius: 50px;
animation: ${rotationAnimation} 10s linear infinite;
`;
const Emoji = styled.span`
&:active {
opacity: 0;
}
`;
// 위의 animation component
const Animation = styled.div`
display: flex;
justify-content: center;
align-items: center;
height: 30px;
width: 30px;
background-color: #ff7eb3;
border-radius: 50px;
animation: ${rotationAnimation} 10s linear infinite;
span {
font-size: 5px;
&:hover {
font-size: 30px;
}
}
${Emoji} {
&:active {
opacity: 0;
}
}
`;
// 사용코드
<Animation>
<span>😎</span>
</Animation>
<Animation>
<p>😎</p>
</Animation>
<Animation>
<Emoji>😎</Emoji>
</Animation>
이렇게되면 중간의 P는 적용이 안되고 첫번째 세번째만 적용된다.
// 부모
import { ThemeProvider } from "styled-components";
const darkTheme = {
textColor: "whitesmoke",
backgroundColor: "#111",
};
const lightTheme = {
textColor: "#111",
backgroundColor: "whitesmoke",
};
// 사용코드
<ThemeProvider theme={darkTheme}>
<StyledCompnentPractice />
</ThemeProvider>
// 자식
const Background = styled.div`
background-color: ${(prop) => prop.theme.backgroundColor};
`;
// 사용코드
<Background>
<Father>
<Box color="teal"></Box>
<Circle color="tomato"></Circle>
</Father>
</Background>
이렇게 부모쪽에서 theme을 설정해주고(theme compnent에는 theme속성값이 반드시 들어가있어야한다) 자식component에서 props.theme으로 받아서 설정해주면 자식쪽 코드를 건드리지않고 자동 변경되게된다.
이렇게 styled-component를 한 번 총 정리하게 되었다. 아직 몇가지 빠진것들이 있지만 앞으로 차츰차츰 배워가며 보충하고 오늘 배운것 만으로도 코드 수정할게 여러개 생각난다ㅎㅎ,,,
ps.
오늘은 친구들과 저녁에 약속이 있어서 공부를 많이 하지 못했다. 어제 클론코딩이 끝나기도 했고...
남은시간동안은 코드잇 실습문제들을 풀다가 내일부터 다시 빡시게 공부를 시작해야겠다.
오랜만에 친구들 만나니깐 즐거웠다!
다시 열코, 열공 해야지 ㅎㅇㅌ!