-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuseDraggableYContainer.ts
More file actions
55 lines (42 loc) · 1.62 KB
/
useDraggableYContainer.ts
File metadata and controls
55 lines (42 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { useRef, useState } from "react";
export default function useDraggableYContainer() {
const containerRef = useRef<HTMLElement | null>(null);
const [isStartDragging, setIsStartDragging] = useState<boolean>(false);
const [isScrollEnd, setIsScrollEnd] = useState<boolean>(false);
const [startY, setStartY] = useState(0);
const [movedY, setMovedY] = useState(0);
const touchSensitivity = 0.7; // 이동 감도 조절 값 (조절할 수 있음)
function handleTouchStart(event: React.TouchEvent<HTMLElement>) {
setIsStartDragging(true);
setStartY(event.touches[0].clientY); // 터치 시작 지점 저장
setMovedY(0);
}
function handleTouchMove(event: React.TouchEvent<HTMLElement>) {
if (!isStartDragging) return;
const deltaY = event.touches[0].clientY - startY;
// 이동 감도에 따라 이동 거리 조절
const adjustedDeltaY = deltaY / touchSensitivity;
setMovedY(adjustedDeltaY);
moveContainer(adjustedDeltaY);
}
function moveContainer(deltaY: number) {
const container = containerRef.current;
if (!container) return;
container.scrollTop -= deltaY;
// 컨테이너 끝에 도달했는지 여부 확인
setIsScrollEnd(container.scrollHeight - container.scrollTop === container.clientHeight);
}
function handleTouchEnd() {
setIsStartDragging(false);
}
return {
scrollableContainerProps: {
ref: containerRef,
onTouchStart: handleTouchStart,
onTouchMove: handleTouchMove,
onTouchEnd: handleTouchEnd,
},
isDragging: Math.abs(movedY) > 10, // 이동 거리에 따라 드래그 여부 결정
isScrollEnd,
};
}