Skip to content

Commit

Permalink
Merge pull request #350 from MovieReviewComment/feature/issue-325/swiper
Browse files Browse the repository at this point in the history
[#325] Implement Swiper
  • Loading branch information
2wheeh authored May 4, 2024
2 parents 762e063 + 0b46b9d commit 8ef8600
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions ui/src/components/common/client/swiper.tsx
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 });

0 comments on commit 8ef8600

Please sign in to comment.