|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useRef } from 'react'; |
| 4 | +import ExperienceCard from '@/app/(with-header)/components/ExperienceCard'; |
| 5 | +import IconArrowRight from '@assets/svg/right-arrow'; |
| 6 | +import IconArrowLeft from '@assets/svg/left-arrow'; |
| 7 | + |
| 8 | +export default function PopularExperiences() { |
| 9 | + // 카드 슬라이더를 참조할 DOM ref |
| 10 | + const sliderRef = useRef<HTMLDivElement>(null); |
| 11 | + |
| 12 | + // 좌우 버튼 클릭 시 한 장씩 슬라이드 이동 |
| 13 | + const scrollByCard = (direction: 'left' | 'right') => { |
| 14 | + if (!sliderRef.current) return; |
| 15 | + |
| 16 | + // 첫 번째 카드 요소를 찾아서 너비 측정 |
| 17 | + const card = sliderRef.current.querySelector('.card'); |
| 18 | + if (!(card instanceof HTMLElement)) return; |
| 19 | + |
| 20 | + const cardWidth = card.offsetWidth; // 카드 너비 |
| 21 | + const gap = parseInt(getComputedStyle(sliderRef.current).gap) || 0; // gap 값 |
| 22 | + const distance = cardWidth + gap; // 한 번에 이동할 거리 |
| 23 | + |
| 24 | + // 슬라이더 스크롤 이동 (좌/우 방향에 따라) |
| 25 | + sliderRef.current.scrollBy({ |
| 26 | + left: direction === 'left' ? -distance : distance, |
| 27 | + behavior: 'smooth', |
| 28 | + }); |
| 29 | + }; |
| 30 | + |
| 31 | + return ( |
| 32 | + <section className='pt-24 md:pt-34 pl-24 lg:pl-0 pb-40 lg:pb-33 lg:max-w-1200 lg:w-full mx-auto'> |
| 33 | + {/* 섹션 제목 + 좌우 화살표 버튼 */} |
| 34 | + <div className='flex justify-between items-center pb-16 md:pb-32 mb-6'> |
| 35 | + <h2 className='text-xl md:text-3xl font-bold'>🔥 인기 체험</h2> |
| 36 | + <div className='flex gap-2'> |
| 37 | + <IconArrowLeft size={32} onClick={() => scrollByCard('left')} className='text-2xl px-3' /> |
| 38 | + <IconArrowRight size={32} onClick={() => scrollByCard('right')} className='text-2xl px-3' /> |
| 39 | + </div> |
| 40 | + </div> |
| 41 | + |
| 42 | + {/* 가로 슬라이드 카드 리스트 */} |
| 43 | + <div |
| 44 | + ref={sliderRef} |
| 45 | + className='flex gap-16 md:gap-32 lg:gap-24 overflow-x-auto scroll-smooth no-scrollbar' |
| 46 | + > |
| 47 | + {[...Array(4)].map((_, idx) => ( |
| 48 | + // 카드 wrapper: flex-shrink-0으로 크기 고정 + 'card' 클래스로 식별 |
| 49 | + <div key={idx} className='flex-shrink-0 card'> |
| 50 | + <ExperienceCard /> |
| 51 | + </div> |
| 52 | + ))} |
| 53 | + </div> |
| 54 | + </section> |
| 55 | + ); |
| 56 | +} |
0 commit comments