이제 유튜브 예시를 통해 스타일드 컴포넌트를 적용해보려고 한다.
스타일드 컴포넌트가 적용되어있으며 지원이 안되는 Mixin 기능을 사용하기 위해 따로 변수를 선언했다. 또한 props로 동적변환도 해보았다.


App
가장 메인이 되는 App의 리턴문부터 살펴보겠다.
function App() {
const [darkMode, setDarkMode] = useState(true);
return (
<Container>
<GlobalStyle darkMode={darkMode} />
<Header setDarkMode={setDarkMode} />
<Nav />
<Tab />
<StyledMain>
{contents.map((el) => (
<Content key={el.id} content={el} />
))}
</StyledMain>
</Container>
);
}
App의 스타일드 컴포넌트를 보기 전에 Mixin처럼 스타일들을 변수에 저장한 페이지부터 봐야할 필요가 있다.
styled
import { css } from "styled-components";
export const flexMixin = ({
direction = "row",
align = "stretch",
justify = "start",
gap = 0,
wrap = "nowrap",
}) => {
return css`
display: flex;
flex-direction: ${direction};
align-items: ${align};
justify-content: ${justify};
gap: ${gap};
flex-wrap: ${wrap};
`;
};
export const gap_padding_radius = "20px";
우선
import { css } from "styled-components";
여기서의 css란 리액트에서 css 코드를 작성하기 위한 헬퍼함수 주로 css 코드블록을 정의할 때 사용한다. 템플릿 리터럴을 사용하여 css를 작성할 수 있다.
한마디로 css 뒤에 ``를 쓰며 css 코드 쓰겠다고 리액트에게 바로 알려준다고 생각하면 된다.
이제 App 페이지로 돌아가서
const Container = styled.div`
display: grid;
grid-template-columns: 80px 1fr;
grid-template-rows: 80px 40px 1fr;
width: 100vw;
height: 100vh;
`;
const StyledMain = styled.main`
width: 100%;
padding: ${gap_padding_radius};
${flexMixin({ align: "flex-start", wrap: "wrap" })}
section {
width: 50%;
height: auto;
padding: 10px;
& > img {
width: 100%;
border-radius: 10px;
}
& > div {
${flexMixin({ gap: "10px" })}
@include flex(row, $gap: 10px);
padding-top: 10px;
}
div {
img {
width: 30px;
border-radius: 100%;
}
p:first-child {
font-weight: 600;
}
p:last-child {
font-size: 0.9rem;
color: gray;
}
}
}
`;
const GlobalStyle = createGlobalStyle`
::-webkit-scrollbar {
display: none;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
transition: background-color 0.2s;
}
${(props) =>
props.darkMode === true
? css`
* {
background-color: black !important;
color: white;
}
svg {
fill: white;
}
form {
button {
background-color: #626262 !important;
}
}
form div svg {
background-color: #626262;
}
section ul li {
background-color: #323232 !important;
&:first-child {
background-color: #626262 !important;
}
}
`
: ""}
`;
모든 컴포넌트들의 위와 같이 작동되고 있다.
회고: 복잡하다.. 어려운건 아닌데 구조 잘 알아야할 것 같다. props는 좀 헷갈리긴 하는데 많이 쓰면 될 것 같다.