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

Feat/#333 refactor custom hook #334

Open
wants to merge 17 commits into
base: release/2.1.1
Choose a base branch
from
Open
Show file tree
Hide file tree
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
55 changes: 0 additions & 55 deletions src/@components/@common/hooks/useDraggableYContainer.ts

This file was deleted.

110 changes: 92 additions & 18 deletions src/@components/@common/hooks/useDraggingContainer.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,118 @@
import { useRef, useState } from "react";

export default function useDraggingContainer() {
type DragDirectionType = "X" | "Y";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'horizon'도 아닌, 'x'도 아닌, 'X'인 이유가 있을까요?
추상화 레벨 적절하게 열어진 것 다 좋은데, 이 인자값 인터페이스가 조금 어색해보여요
타이핑도 되어있고 해서 지금 상태도 괜찮지만, 가능하다면 좀 더 일반적으로 기대 가능한 인터페이스면 어떨까 싶습니다!!!!!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

읽는 사람들(개발자들)에게 x/y는 'x축', 'y축'이라는 통용된 개념으로 읽히기 때문에 horizon/vertical으로 굳이 길게 늘릴 필요가 없다고 생각했어요!

x/y와 X/Y 중 고민했는데, 속성명 중에 pageX, pageY가 있기 때문에 통일성을 주기 위해 X, Y를 사용했습니다.
x/y가 좀 더 가독성이 낫다고 느끼신다면 고쳐보겠습니다!


type EventMapperType = {
[key in DragDirectionType]: "pageX" | "pageY";
};

const eventMapper: EventMapperType = {
X: "pageX",
Y: "pageY",
};

const FIRST_TOUCH_EVENT_IDX = 0;

export default function useDraggingContainer(dragDirection: DragDirectionType) {
const containerRef = useRef<HTMLElement | null>(null);

const [isStartDragging, setIsStartDragging] = useState(false);
const currentX = useRef(0);
const currentRef = useRef(0);
const standardRef = useRef(0);

const standardX = useRef(0);
const [draggedX, setDraggedX] = useState(0);
const [isStartDragging, setIsStartDragging] = useState(false);
const [isArrivedEnd, setIsArrivedEnd] = useState(false);
const [draggedDistance, setDraggedDistance] = useState(0);

function handleMouseDown(event: React.MouseEvent<HTMLElement, MouseEvent>) {
setIsStartDragging(true);

currentX.current = event.pageX;
const page = getPageByEventType(event);
currentRef.current = page;
initializeForDraggedDistance(page);
}

initializeForDraggedX(event.pageX);
function handleTouchStart(event: React.TouchEvent<HTMLElement>) {
setIsStartDragging(true);

const page = getPageByEventType(event);
currentRef.current = page;
initializeForDraggedDistance(page);
}

function getPageByEventType(event: React.SyntheticEvent<HTMLElement>): number {
const eventType = eventMapper[dragDirection];
if (event.nativeEvent instanceof TouchEvent) {
return event.nativeEvent.touches[FIRST_TOUCH_EVENT_IDX][eventType];
}
if (event.nativeEvent instanceof MouseEvent) {
return event.nativeEvent[eventType];
}
return 0;
}

function initializeForDraggedX(standartX: number) {
setDraggedX(0);
standardX.current = standartX;
function initializeForDraggedDistance(standard: number) {
setDraggedDistance(0);
standardRef.current = standard;
}

function handleMouseMove(event: React.MouseEvent<HTMLElement, MouseEvent>) {
const container = containerRef.current;

if (!container) return;
if (!isStartDragging) return;

const page = getPageByEventType(event);
moveContainerByCurrent(container, page);
handleArrivedEnd(container);

setDraggedDistance(Math.abs(page - standardRef.current));
}

function handleTouchMove(event: React.TouchEvent<HTMLElement>) {
const container = containerRef.current;

if (!container) return;
if (!isStartDragging) return;

moveContainerByCurrentX(container, event.pageX);
const page = getPageByEventType(event);
moveContainerByCurrent(container, page);
handleArrivedEnd(container);

setDraggedX(Math.abs(event.pageX - standardX.current));
setDraggedDistance(Math.abs(page - standardRef.current));
}

function moveContainerByCurrentX(container: HTMLElement, movedMouseX: number) {
container.scrollLeft += currentX.current - movedMouseX;
currentX.current = movedMouseX;
function moveContainerByCurrent(container: HTMLElement, movedTrigger: number) {
const delta = currentRef.current - movedTrigger;
if (dragDirection === "X") {
container.scrollLeft += delta;
}
if (dragDirection === "Y") {
container.scrollTop += delta;
}
currentRef.current = movedTrigger;
}

function handleArrivedEnd(container: HTMLElement) {
if (dragDirection === "X") {
setIsArrivedEnd(container.scrollWidth - container.scrollLeft === container.clientWidth);
}
if (dragDirection === "Y") {
setIsArrivedEnd(container.scrollHeight - container.scrollTop === container.clientHeight);
}
}

function handleMouseUpOrLeave() {
reset();
}

function handleTouchEndOrCancel() {
reset();
}

function reset() {
setIsStartDragging(false);
currentX.current = 0;
standardX.current = 0;
currentRef.current = 0;
standardRef.current = 0;
}

return {
Expand All @@ -54,7 +122,13 @@ export default function useDraggingContainer() {
onMouseMove: handleMouseMove,
onMouseUp: handleMouseUpOrLeave,
onMouseLeave: handleMouseUpOrLeave,

onTouchStart: handleTouchStart,
onTouchMove: handleTouchMove,
onTouchEnd: handleTouchEndOrCancel,
onTouchCancel: handleTouchEndOrCancel,
},
isDragging: draggedX > 10,
isDragging: draggedDistance > 10,
isArrivedEnd,
};
}
Loading