-
Notifications
You must be signed in to change notification settings - Fork 5
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
))} | ||
</S.SlideTrack> | ||
</S.SlideWrapper> | ||
<S.PreviousButton onClick={handlePreviousSlide}>이전</S.PreviousButton> | ||
<S.NextButton onClick={handleNextSlide}>다음</S.NextButton> | ||
</S.Container> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 버건디 말씀과 다르게 useCarousel 로직 깔끔한데요 ㅎㅎ 뚝딱뚝딱 잘 만드셨네요 👍🏻 |
||
const carouselItems = React.Children.toArray(children); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]); | ||
|
||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
단순 질문) clone 슬라이드는 어떤 역할을 하는지 궁금합니다~! original과 clone 두 개가 있어서요 ㅎㅎ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
보통 무한 캐러셀을 구현할때 부드러운 애니메이션을 위해서 복제본을 만들더라구요 !
(가령 마지막 슬라이드에서 Next 버튼을 눌렀을때 다시 첫번째 슬라이드로 넘어가는 경우)
사용하고 있던 기존 캐러셀 라이브러리도 이런식으로 복제본을 만들어서 사용하길래 적용시켜보았습니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
그렇군요!! 설명 감사합니다 버건디👍