11import { useState , useEffect } from 'react' ;
22import styles from './Carousel.module.scss' ;
33import RecipientCard from '../RecipientCard/RecipientCard' ;
4- import useWindowWidth from '../../hooks/useWindowWidth' ;
54
65export default function Carousel ( { recipients } ) {
76 const [ index , setIndex ] = useState ( 0 ) ;
87 const [ offsetX , setOffsetX ] = useState ( { } ) ; // 캐러셀 x좌표
98 const [ startX , setstartX ] = useState ( 0 ) ; // 터치 스크롤 시작 x좌표
109 const [ isBouncing , setBouncing ] = useState ( false ) ; // 캐러셀 끝이면 bouncing 모션
11- const isDesktop = useWindowWidth ( ) > 1200 ;
10+ const [ deviceType , setDeviceType ] = useState ( getDeviceType ( ) ) ;
11+ const windowSize = getDeviceType ( ) ;
12+ const isDesktop = windowSize === 'desktop' ;
13+ const isMobile = windowSize === 'mobile' ;
1214
1315 // 캐러셀 버튼 작동과정: button onclick --> settingIndex(), setIndex --> useEffect( setOffsetX(),[index] ): x좌표 상태 업데이트: 캐러셀 이동
1416 useEffect ( ( ) => {
15- setOffsetX ( {
16- transform : `translateX(-${ index * 295 } px)` ,
17- } ) ;
17+ if ( isMobile ) {
18+ setOffsetX ( {
19+ transform : `translateX(-${ index * 228 } px)` ,
20+ } ) ;
21+ } else {
22+ setOffsetX ( {
23+ transform : `translateX(-${ index * 295 } px)` ,
24+ } ) ;
25+ }
1826 } , [ index ] ) ;
1927
2028 function settingIndex ( direction ) {
2129 setIndex ( ( prev ) => ( direction === 'next' ? prev + 1 : prev - 1 ) ) ; // next? next index : back index
2230 }
2331
32+ // 화면 리사이즈 감지
33+ function getDeviceType ( ) {
34+ const width = window . innerWidth ;
35+ if ( width < 768 ) return 'mobile' ;
36+ if ( width <= 1200 ) return 'bigTablet' ;
37+ if ( width <= 1023 ) return 'tablet' ;
38+ return 'desktop' ;
39+ }
40+
41+ useEffect ( ( ) => {
42+ function handleResize ( ) {
43+ const newType = getDeviceType ( ) ;
44+ const isMobile = deviceType === 'mobile' ;
45+ const willBeMobile = newType === 'mobile' ;
46+
47+ // mobile → non-mobile 또는 non-mobile → mobile 로 변경될 때만
48+ const crossedMobileBoundary = isMobile !== willBeMobile ;
49+ if ( crossedMobileBoundary ) {
50+ setIndex ( 0 ) ;
51+ }
52+ if ( newType !== deviceType ) {
53+ setDeviceType ( newType ) ;
54+ }
55+ }
56+
57+ window . addEventListener ( 'resize' , handleResize ) ;
58+ return ( ) => window . removeEventListener ( 'resize' , handleResize ) ;
59+ } , [ deviceType ] ) ;
60+
2461 // 터치, 마우스 드래그 감지 --> 캐러셀 한 칸 이동
2562 function handleStart ( e ) {
2663 if ( isDesktop ) return ;
@@ -45,12 +82,22 @@ export default function Carousel({ recipients }) {
4582 return ;
4683 }
4784 } else if ( isNext ) {
48- if ( index === 5 ) {
49- setBouncing ( true ) ;
50- return ;
51- } else if ( index < 5 ) {
52- settingIndex ( 'next' ) ;
53- return ;
85+ if ( isMobile ) {
86+ if ( index === 6 ) {
87+ setBouncing ( true ) ;
88+ return ;
89+ } else if ( index < 6 ) {
90+ settingIndex ( 'next' ) ;
91+ return ;
92+ }
93+ } else {
94+ if ( index === 5 ) {
95+ setBouncing ( true ) ;
96+ return ;
97+ } else if ( index < 5 ) {
98+ settingIndex ( 'next' ) ;
99+ return ;
100+ }
54101 }
55102 }
56103 }
0 commit comments