Skip to content

Commit 36b3014

Browse files
authored
Fix(client): 캐러셀 Click / Drag 상태를 분간하는 처리에 autoPlay 옵션 분기 (team-bofit#471)
* fix: 캐러셀 문제의 문제 해결 * fix: 불필요한 코드 제거
1 parent 62de101 commit 36b3014

File tree

2 files changed

+50
-26
lines changed

2 files changed

+50
-26
lines changed

apps/client/src/widgets/home/components/info-section/recommended-info-section.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export const RecommendedInfoSection = ({
7777

7878
<Carousel slidesPerView={4.5} autoPlay className={styles.homeCardList}>
7979
{reportSummary.statuses?.map((card, index) => (
80-
<Carousel.Item key={index} style={{ width: 'auto' }}>
80+
<Carousel.Item key={index}>
8181
<HomeCard
8282
icon={
8383
<img

packages/bds-ui/src/components/carousel/hooks/use-carousel-touch.ts

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export const useCarouselTouch = ({
1616
carouselState,
1717
pauseOnHover,
1818
autoPlay,
19-
infinite,
2019
onStateUpdate,
2120
}: UseCarouselTouchProps): UseCarouselTouchReturn => {
2221
const [isHovered, setIsHovered] = useState(false);
@@ -27,15 +26,21 @@ export const useCarouselTouch = ({
2726
const [clickTarget, setClickTarget] = useState<EventTarget | null>(null);
2827

2928
/** 누를 때 시작 위치 저장 */
30-
const handlePointerDown = useCallback((e: PointerEvent<Element>) => {
31-
setIsDragging(true);
32-
setStartX(e.clientX);
33-
setDragOffset(0);
34-
setHasMoved(false);
35-
setClickTarget(e.target);
36-
setIsHovered(true);
37-
e.currentTarget.setPointerCapture(e.pointerId);
38-
}, []);
29+
const handlePointerDown = useCallback(
30+
(e: PointerEvent<Element>) => {
31+
setIsDragging(true);
32+
setStartX(e.clientX);
33+
setDragOffset(0);
34+
35+
if (!autoPlay) {
36+
setHasMoved(false);
37+
setClickTarget(e.target);
38+
}
39+
40+
e.currentTarget.setPointerCapture(e.pointerId);
41+
},
42+
[autoPlay],
43+
);
3944

4045
/** 움직일 때 드래그 오프셋 계산 */
4146
const handlePointerMove = useCallback(
@@ -47,27 +52,49 @@ export const useCarouselTouch = ({
4752
const dragDiff = startX - e.clientX;
4853
const diff = Math.abs(dragDiff);
4954

50-
if (diff > 10) {
55+
if (!autoPlay && diff > 10) {
5156
setHasMoved(true);
5257
}
5358

5459
const containerWidth = e.currentTarget.clientWidth;
5560
const dragOffsetPercent = (dragDiff / containerWidth) * 100;
5661
setDragOffset(dragOffsetPercent);
5762
},
58-
[isDragging, startX],
63+
[isDragging, startX, autoPlay],
5964
);
6065

6166
/** 뗄 때 드래그 거리 기준으로 컨트롤러를 통해 새로운 상태 계산
62-
* - 드래그 했는지 : true => 드래그 거리 기준으로 슬라이드 변경
63-
* - 드래그 안 했는지 : false => 클릭 이벤트로 간주하여 원래 타겟 클릭
67+
* - autoPlay=false: 드래그 여부에 따라 슬라이드 변경 or 클릭 이벤트 처리
68+
* - autoPlay=true: 항상 드래그로 처리 (onClick 무시)
6469
*/
70+
6571
const handlePointerUp = useCallback(
6672
(e: PointerEvent<Element>) => {
6773
if (!isDragging || !controller) {
6874
return;
6975
}
70-
if (hasMoved) {
76+
77+
if (!autoPlay) {
78+
if (hasMoved) {
79+
const diff = startX - e.clientX;
80+
const containerWidth = e.currentTarget.clientWidth || 1;
81+
const dragOffsetPercent = (diff / containerWidth) * 100;
82+
83+
const newState = controller.handleDragEnd(
84+
carouselState,
85+
dragOffsetPercent,
86+
{
87+
isAutoPlay: false,
88+
},
89+
);
90+
91+
onStateUpdate(newState);
92+
} else {
93+
if (clickTarget && clickTarget instanceof HTMLElement) {
94+
clickTarget.click();
95+
}
96+
}
97+
} else {
7198
const diff = startX - e.clientX;
7299
const containerWidth = e.currentTarget.clientWidth || 1;
73100
const dragOffsetPercent = (diff / containerWidth) * 100;
@@ -76,22 +103,21 @@ export const useCarouselTouch = ({
76103
carouselState,
77104
dragOffsetPercent,
78105
{
79-
isAutoPlay: autoPlay,
106+
isAutoPlay: true,
80107
},
81108
);
82109

83110
onStateUpdate(newState);
84-
} else {
85-
if (clickTarget && clickTarget instanceof HTMLElement) {
86-
clickTarget.click();
87-
}
88111
}
89112

90113
setIsDragging(false);
91114
setDragOffset(0);
92-
setHasMoved(false);
93-
setClickTarget(null);
94-
setIsHovered(false);
115+
116+
if (!autoPlay) {
117+
setHasMoved(false);
118+
setClickTarget(null);
119+
}
120+
95121
e.currentTarget.releasePointerCapture(e.pointerId);
96122
},
97123
[
@@ -102,12 +128,10 @@ export const useCarouselTouch = ({
102128
controller,
103129
carouselState,
104130
autoPlay,
105-
infinite,
106131
onStateUpdate,
107132
],
108133
);
109134

110-
/** 호버 상태 관리 */
111135
const handleMouseEnter = useCallback(() => {
112136
if (pauseOnHover) {
113137
setIsHovered(true);

0 commit comments

Comments
 (0)