(์ค์ airbnb ์ฌ์ดํธ)
์์ฆ ๋ง๋ค๊ณ ์๋ ๊ฑด Carousel ๋ง๋ค๊ธฐ! ๐คธโโ๏ธ
์๋ฌด๋๋ ๋์ ์ธ ๋ถ๋ถ์ด ๋ค๋ฅธ ์ปดํฌ๋ํธ๋ณด๋ค ๋ง์ด ์๊ตฌ๋๋ ๋ถ๋ถ์ด๋ผ ์์ด ๋ง์ด ๊ฐ๋ค.
React๋ฅผ ์ ํ๋ฉด์ styled-components
๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํตํด CSS-in-JS
๋ฅผ ์ฌ์ฉํด์ค๊ธด ํ์ง๋ง, Carousel์ ๊ตฌํํ๋ฉด์ ์ ์ฉํ ํ์ ๊ฝค ๋ฐ๊ฒฌํด์ ๊ธ์ ๋จ๊ฒจ๋ณด๋ คํ๋ค. ๐
๋ถํธ์คํธ๋ฉ์์ ์ง์ํ๋ carousel์ ์ฐ๋ คํ์ผ๋ ์น์ ํ ์ ํ๋ธ ์จ์๋์ ๊ฐ์ข๋ฅผ ๋ณด๋ฉฐ ์ง์ ๊ตฌํํ๋ค.
๐ https://youtu.be/eywZbJ5PVjg
ํฌ๊ฒ ๋ณธ Carousel์ ๋ฐฉํฅํค๋ฒํผ๋ค๊ณผ ์ฌ๋ผ์ด๋๋ค์ ๋ฉํผ๋ก ์ด๋ฃจ์ด์ง๋ค.
const Carousel = () => (
<StyledCarousel className="border">
<CarouselKeyAnchor className="prev" direction="prev" />
<CarouselImageWrapper sources={srcArr} />
<CarouselKeyAnchor className="next" direction="next" />
</StyledCarousel>
);
์ค์ํ ์คํ์ผ์, ๋ฐฉํฅ๋ฒํผ์ ์ด๋ฏธ์ง ์์ ๋์ฌ์ผ ํ๋ค.
๋ฐ๋ผ์ ํ์์์ฑ์ ๋ฐ๊ณผ ๊ฐ์ด ์ค์ ํ๋ค.
const StyledCarouselKeyAnchor = styled.a`
...
z-index: 1;
position: absolute;
...
`;
์ฌ๊ธฐ๊น์งํ๋ฉด, prev๋ฒํผ๊ณผ next๋ฒํผ์ด ๊ฒน์ณ๋ณด์ผ ๊ฒ์ด๋ค.
์๋ํ๋ฉด ์์ง ๊ณตํต๋ ์์ฑ๊น์ง๋ง ์คฌ๊ธฐ ๋๋ฌธ์..
์ด์ ๋ถ๋ช
ํ ๋ฒํผ์ ๋ฐฉํฅ์ ๋ฐ๋ผ ๋ค๋ฅธ ์์น์ ๋๊ณ ์ถ์ ๊ฒ์ด๋ค. ๐ฅ
์ ์ด์ ์ด๊ฑธ ์ด๋ป๊ฒ ๊ตฌํํ ๊ฒ์ธ๊ฐ ์์๋ณด์.
์ด๋ ์ฌ์ฉํด์ผ ํ ๊ฒ์ props
๋ฐ์ดํฐ์ด๋ค.
๋ด๊ฐ ๋ณธ ๋ง์ ์์๋ ์์ฑ๊ฐ์ด ์ ๋ฌ๋์ง ์์์ ๋ ๊ธฐ๋ณธ ๊ฐ์ ์ค์ ์ ํ ๋ ์ฌ์ฉ์ ํ๊ณ ์์๋ค.
๋ฐ์ styled-component ๊ณต์๋ฌธ์์์ ์ ์ํ๊ณ ์๋ ์์๋ค.
const Button = styled.button`
/* Adapt the colors based on primary prop */
background: ${props => props.primary ? "palevioletred" : "white"};
color: ${props => props.primary ? "white" : "palevioletred"};
font-size: 1em;
margin: 1em;
padding: 0.25em 1em;
border: 2px solid palevioletred;
border-radius: 3px;
`;
render(
<div>
<Button>Normal</Button>
<Button primary>Primary</Button>
</div>
);
์ข ๋ ๋ฌธ์๋ฅผ ์ฐพ์๋ณธ ๊ฒฐ๊ณผ ํด๋ต์ ์ป์ด ๋ผ ์ ์์๋ค.
์กฐ๊ฑด์ ๋ฐ๋ผ ์์ฑ๊ฐ๋ง ์ป๋๊ฒ ๋ชฉ์ ์ด๋ผ๋ฉด ๋ด๊ฐ ์ป๊ณ ์ถ์ ๊ฐ๋ง ๋ฐํํ๋ฉด ๋๋ค. ์์ ์์์ฒ๋ผ!
ํ์ง๋ง ๋๋ ์์ฑ๊ฐ ๋ฟ๋ง ์๋๋ผ, ์์ฑ๋ ๋ฌ๋ฆฌํด์ผํ๋๊ฒ ๋ชฉ์ ์ด์๊ธฐ์ ์ด๋ฐ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ค.
const StyledCarouselKeyAnchor = styled.a`
...
${({ direction }) => {
if (direction === 'prev') {
return css`
left: 5px;
`;
}
return css`
right: 5px;
`;
}}
...
`;
ํฐ ์ฐจ์ด์ ์ 'css'
๋ฅผ ์ฌ์ฉํ๋ค๋ ์ ์ด๋ค.
์ฌ์ฉํ๋ ค๋ฉด styled-component์์ ๋ถ๋ฌ์ค๋ฉด ๋๋ค.
์ด์ฒ๋ผ ๊ณตํต ์์ฑ์ด ๋๋ถ๋ถ์ด๊ณ ์ผ๋ถ ์์ฑ๋ง์ ๋ฌ๋ฆฌํ๊ณ ์ถ์ ๋ ์ ์ฉํ๊ฒ ์ฐ์ด๋ ๊ธฐ์ ์ด๋ค.
์์ง์ ์ ์ ํํธ๋ง ๊ตฌํํ ์ํ์ด๊ณ ๋์ ํํธ๋ ๊ตฌํํ ์์ ์ด๋ค โ