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

[#325] Implement Swiper #350

Merged
merged 1 commit into from
May 4, 2024
Merged
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
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 });