-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #350 from MovieReviewComment/feature/issue-325/swiper
[#325] Implement Swiper
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
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,52 @@ | ||
'use client'; | ||
|
||
import clsx from 'clsx'; | ||
|
||
import { useSwiper } from '@/hooks/common/use-swiper'; | ||
|
||
import { getValidChildrenOfType } from '@/lib/utils/common/get-valid-children-of-type'; | ||
|
||
function Swiper({ children }: { children: React.ReactNode }) { | ||
const { offsetX, onTouchStart, currentIdx, onIndicatorClick, isSwiping, containerRef } = | ||
useSwiper(); | ||
|
||
const slides = getValidChildrenOfType(children, SwiperSlide); | ||
|
||
return ( | ||
<div | ||
className="w-full max-w-full overflow-hidden touch-pan-y relative" | ||
onTouchStart={onTouchStart} | ||
onMouseDown={onTouchStart} | ||
> | ||
<ul | ||
ref={containerRef} | ||
className={clsx('min-w-full flex list-none p-0 m-0', { | ||
'transition-none': isSwiping, | ||
'transition-[transfrom_1s_ease-out]': !isSwiping, | ||
})} | ||
style={{ transform: `translate3d(${offsetX}px, 0, 0)` }} | ||
> | ||
{slides} | ||
</ul> | ||
|
||
<ul className="absolute bottom-2 right-1/2 translate-x-1/2 flex justify-center list-none p-0"> | ||
{Array.from({ length: slides.length }).map((_, idx) => ( | ||
<li | ||
key={idx} | ||
className={clsx( | ||
'w-3 h-3 mx-1 my-0 rounded-full bg-gray-300 cursor-pointer', | ||
idx === currentIdx && 'bg-gray-700' | ||
)} | ||
onClick={() => onIndicatorClick(idx)} | ||
/> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
} | ||
|
||
function SwiperSlide({ children }: { children: React.ReactNode }) { | ||
return <li className="w-full flex-shrink-0 relative">{children}</li>; | ||
} | ||
|
||
export default Object.assign(Swiper, { Slide: SwiperSlide }); |