Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- react ag grid
- 자바 향상된 for문
- MySQL
- 항해99
- 항해99 2기
- 자바 switch문
- Vue3
- 변수
- 자바 public
- 자바 스캐너
- 타입스크립트
- TypeScript
- 자바 for문
- 프로그래머스
- 자바 공배수
- 자바 삼항연산자
- react with typescript
- 정보처리기사실기
- 자바 if문
- java
- 자바 반복문
- 자바
- Til
- 자바 while문
- 자바 구구단 출력
- 이클립스 DB연동
- 자바 자동캐스팅
- 자바 강제 캐스팅
- 자바 조건문
- 조코딩
Archives
- Today
- Total
뇌 채우기 공간
[REACT] styled components 심화 사용법 본문
오늘은 그동안 styled components를 사용하면서 사용해보지 않았던 기능들에 대해서 공부를 해보자
1. GlobalStyle
createGrobalStyle을 import해와서 사용할 수 있다.
지금 이 스타일은 html body태그 전체에 회색으로 색지정을 해주고 있다.
import React from 'react';
import { createGlobalStyle } from 'styled-components';
const GlobalStyle = createGlobalStyle`
body {
background: #e9ecef;
}
`;
function App() {
return (
<>
<GlobalStyle />
<div>안녕하세요</div>
</>
);
}
export default App;
2. CSS Selector
컴포넌트를 하나하나 따로 만들지 않고 classname을 줘서 하나의 컴포넌트 안에서 자식 요소들에게 css를 주었다.
기능적으로 큰 의미가 없다면 이렇게 해도 무방하다.
import React from 'react';
import styled from 'styled-components';
const TodoHeadBlock = styled.div`
padding-top: 48px;
padding-left: 32px;
padding-right: 32px;
padding-bottom: 24px;
border-bottom: 1px solid #e9ecef;
h1 {
margin: 0;
font-size: 36px;
color: #343a40;
}
.day {
margin-top: 4px;
color: #868e96;
font-size: 21px;
}
.tasks-left {
color: #20c997;
font-size: 18px;
margin-top: 40px;
font-weight: bold;
}
`;
function TodoHead() {
return (
<TodoHeadBlock>
<h1>2019년 7월 10일</h1>
<div className="day">수요일</div>
<div className="tasks-left">할 일 2개 남음</div>
</TodoHeadBlock>
);
}
export default TodoHead;
3. Component Selector
const TodoItemBlock = styled.div`
display: flex;
align-items: center;
padding-top: 12px;
padding-bottom: 12px;
&:hover {
${Remove} {
display: initial;
}
}
`;
여기서 hover부분이 좀 생소하다
Remove라는 컴포넌트를 select하는 건데,
TodoItemBlock 컴포넌트가 hover될때 Remove컴포넌트의 display를 initial로 바꿔라는 뜻이다.
4. props 사용
const CircleButton = styled.button`
...
${props =>
props.open &&
css`
background: #ff6b6b;
&:hover {
background: #ff8787;
}
&:active {
background: #fa5252;
}
transform: translate(-50%, 50%) rotate(45deg);
`}
`;
이 CircleButton 컴포넌트가 props로 open을 받아왔는데 그게true일때 css를 바꿔준다.
728x90
'WEB > REACT' 카테고리의 다른 글
[REACT] redux-thunk vs redux-saga (0) | 2021.09.13 |
---|---|
[REACT] Context api (0) | 2021.09.12 |
[REACT] React를 왜 사용하는 걸까? (0) | 2021.09.11 |
[REACT] React swiper에 image lazy loading 적용하기/ intersection observer (0) | 2021.09.08 |
[REACT] Intersection Observer (0) | 2021.09.08 |