Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

캐러셀 슬라이드 구현 (issue#57) #70

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions frontend/src/components/carousel/Carousel.styled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import styled from 'styled-components';

export const Container = styled.div`
display: flex;
flex-direction: column;
align-items: center;
overflow: hidden;
position: relative;
width: 100rem;
margin: 0 auto;
`;

export const SlideWrapper = styled.div`
width: 100%;
overflow: hidden;
position: relative;
`;

interface SlideTrackProps {
currentIndex: number;
isSliding: boolean;
}

export const SlideTrack = styled.ul<SlideTrackProps>`
display: flex;
flex-direction: row;
position: relative;
transform-style: preserve-3d;
transition: ${({ isSliding }) => (isSliding ? 'transform 400ms ease-in-out' : 'none')};
transform: ${({ currentIndex }) => `translateX(-${currentIndex * 100}%)`};
padding: 0;
margin: 0;
list-style: none;
`;

export const Slide = styled.li`
flex: 1 0 100%;
position: relative;
width: 100%;
transform-style: preserve-3d;
`;

export const Button = styled.button`
margin: 5px;
padding: 10px;
border: none;
background-color: var(--primary-400);
color: white;
cursor: pointer;
position: absolute;
border-radius: 0.5rem;
bottom: 50%;
&:hover {
background-color: var(--primary-300);
}
`;

export const PreviousButton = styled(Button)`
left: 3rem;
`;

export const NextButton = styled(Button)`
right: 3rem;
`;
25 changes: 25 additions & 0 deletions frontend/src/components/carousel/Carousel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import useCarousel from '@/hooks/useCarousel';
import type { PropsWithChildren } from 'react';
import * as S from './Carousel.styled';

export default function Carousel({ children }: PropsWithChildren) {
const { carouselItems, trackRef, currentIndex, isSliding, handleNextSlide, handlePreviousSlide } =
useCarousel({ children });

return (
<S.Container>
<S.SlideWrapper>
<S.SlideTrack ref={trackRef} currentIndex={currentIndex} isSliding={isSliding}>
{carouselItems.map((item, index) => (
<S.Slide key={`original-${index}`}>{item}</S.Slide>
))}
{carouselItems.map((item, index) => (
<S.Slide key={`clone-${index}`}>{item}</S.Slide>
))}
Comment on lines +13 to +18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

단순 질문) clone 슬라이드는 어떤 역할을 하는지 궁금합니다~! original과 clone 두 개가 있어서요 ㅎㅎ

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

보통 무한 캐러셀을 구현할때 부드러운 애니메이션을 위해서 복제본을 만들더라구요 !

(가령 마지막 슬라이드에서 Next 버튼을 눌렀을때 다시 첫번째 슬라이드로 넘어가는 경우)

스크린샷 2024-07-19 오후 3 05 29

사용하고 있던 기존 캐러셀 라이브러리도 이런식으로 복제본을 만들어서 사용하길래 적용시켜보았습니다!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그렇군요!! 설명 감사합니다 버건디👍

</S.SlideTrack>
</S.SlideWrapper>
<S.PreviousButton onClick={handlePreviousSlide}>이전</S.PreviousButton>
<S.NextButton onClick={handleNextSlide}>다음</S.NextButton>
</S.Container>
);
}
54 changes: 54 additions & 0 deletions frontend/src/hooks/useCarousel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type { PropsWithChildren } from 'react';
import React, { useState, useRef, useEffect } from 'react';

const useCarousel = ({ children }: PropsWithChildren) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

버건디 말씀과 다르게 useCarousel 로직 깔끔한데요 ㅎㅎ 뚝딱뚝딱 잘 만드셨네요 👍🏻

const carouselItems = React.Children.toArray(children);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 이런 방법으로도 배열을 만들 수 있군요!! 배워갑니다 👍

const carouselItemLength = carouselItems.length;
const [currentIndex, setCurrentIndex] = useState(0);
const [isSliding, setIsSliding] = useState(false);
const trackRef = useRef<HTMLUListElement>(null);

const handleTransitionEnd = () => {
if (currentIndex === carouselItemLength + 1) {
setCurrentIndex(1);
} else if (currentIndex === 0) {
setCurrentIndex(carouselItemLength);
}
setIsSliding(false);
};

useEffect(() => {
const track = trackRef.current;
if (track) {
track.addEventListener('transitionend', handleTransitionEnd);
return () => {
track.removeEventListener('transitionend', handleTransitionEnd);
};
}
}, [currentIndex, carouselItemLength]);

Check warning on line 28 in frontend/src/hooks/useCarousel.ts

View workflow job for this annotation

GitHub Actions / build-and-test

React Hook useEffect has a missing dependency: 'handleTransitionEnd'. Either include it or remove the dependency array

const handleNextSlide = () => {
if (!isSliding) {
setIsSliding(true);
setCurrentIndex((prevIndex) => prevIndex + 1);
}
};

const handlePreviousSlide = () => {
if (!isSliding) {
setIsSliding(true);
setCurrentIndex((prevIndex) => prevIndex - 1);
}
};

return {
carouselItems,
trackRef,
currentIndex,
isSliding,
handleNextSlide,
handlePreviousSlide,
};
};

export default useCarousel;
50 changes: 50 additions & 0 deletions frontend/src/pages/MissionListPage.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,61 @@
import MissionItem from '@/components/missionList/MissionItem';
import * as S from '@/components/missionList/MissionList.styled';
import { missionMocks } from '@/components/missionList/missionMocks';
import Carousel from '@/components/carousel/Carousel';

export default function MissionListPage() {
return (
<S.MissionListContainer>
<S.MissionListTitle>미션 풀고 리뷰 받고, Devel Up!</S.MissionListTitle>
<Carousel>
{/**
* 캐러셀 테스트 용으로 만들어본 이미지들입니다. 추후 수정 예정
*/}
<div
style={{
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
height: '100%',
}}
>
<img
src="https://hatrabbits.com/wp-content/uploads/2017/01/random.jpg"
width={'100%'}
height={'100%'}
/>
</div>
<div
style={{
display: 'flex',
justifyContent: 'center',
alignItems: 'center',

height: '100%',
}}
>
<img
src="https://cdn.pixabay.com/photo/2016/07/07/16/46/dice-1502706_640.jpg"
width={'100%'}
height={'100%'}
/>
</div>

<div
style={{
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
height: '100%',
}}
>
<img
src="https://cdn.pixabay.com/photo/2015/03/17/02/01/cubes-677092_1280.png"
width={'100%'}
height={'100%'}
/>
</div>
</Carousel>
<S.MissionList>
{missionMocks.map((mission) => (
<MissionItem key={mission.id} mission={mission} />
Expand Down
Loading