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 1 commit
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
2 changes: 1 addition & 1 deletion src/@components/@common/hooks/useDraggableYContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function useDraggableYContainer() {
const [startY, setStartY] = useState(0);
const [movedY, setMovedY] = useState(0);

const touchSensitivity = 0.7; // 이동 감도 조절 값 (조절할 수 있음)
const touchSensitivity = 10; // 이동 감도 조절 값 (조절할 수 있음)

function handleTouchStart(event: React.TouchEvent<HTMLElement>) {
setIsStartDragging(true);
Expand Down
41 changes: 24 additions & 17 deletions src/@components/@common/hooks/useDraggingContainer.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,47 @@
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가 좀 더 가독성이 낫다고 느끼신다면 고쳐보겠습니다!


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 standardX = useRef(0);
const [draggedX, setDraggedX] = useState(0);
const standardRef = useRef(0);
const [dragged, setDragged] = useState(0);

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

currentX.current = event.pageX;
const page = dragDirection === "X" ? event.pageX : event.pageY;
currentRef.current = page;
Copy link
Member

Choose a reason for hiding this comment

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

👍🏼👍🏼


initializeForDraggedX(event.pageX);
initializeForDragged(page);
}

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

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

moveContainerByCurrentX(container, event.pageX);
const page = dragDirection === "X" ? event.pageX : event.pageY;

moveContainerByCurrent(container, page);

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

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

function handleMouseUpOrLeave() {
Expand All @@ -43,8 +50,8 @@ export default function useDraggingContainer() {

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

return {
Expand All @@ -55,6 +62,6 @@ export default function useDraggingContainer() {
onMouseUp: handleMouseUpOrLeave,
onMouseLeave: handleMouseUpOrLeave,
},
isDragging: draggedX > 10,
isDragging: dragged > 10,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const BEST_PIICKLE_TOTAL_COUNT = 4;

export default function RecommendItem(props: RecommendProps) {
const { recommendList } = props;
const { scrollableContainerProps, isDragging } = useDraggingContainer();
const { scrollableContainerProps, isDragging } = useDraggingContainer("X");

return (
<St.RecommemdItemContainer>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from "react";

import IcSubmitBtn from "../../../../asset/icon/IcSubmitBtn";
import useDraggableYContainer from "../../../@common/hooks/useDraggableYContainer";
import useDraggingContainer from "../../../@common/hooks/useDraggingContainer";
import useDrawer from "../../../@common/hooks/useDrawer";
import Modal from "../../../@common/Modal";
import { CommentList, handleCommentController } from "../../hooks/useComments";
Expand All @@ -16,7 +16,7 @@ interface CommentModalProps {

export default function CommentModal(props: CommentModalProps) {
const { cardId, comments, onClickBackground, handleSubmitComment } = props;
const { scrollableContainerProps, isScrollEnd } = useDraggableYContainer();
const { scrollableContainerProps } = useDraggingContainer("Y");
const { drawerProps, knobRef } = useDrawer(onClickBackground);

const [answer, setAnswer] = useState<string>("");
Expand Down Expand Up @@ -46,7 +46,7 @@ export default function CommentModal(props: CommentModalProps) {
</St.Comment>
))}
</St.Comments>
{!isScrollEnd && <St.Gradient />}
{/* {!isScrollEnd && <St.Gradient />} */}
<St.InputWrapper>
<St.Input
placeholder="답변하기"
Expand Down
2 changes: 1 addition & 1 deletion src/@components/MainPage/BestPiickle/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import St from "./style";

export default function BestPiickle() {
const { bestPiickle } = useBestPiickle();
const { scrollableContainerProps, isDragging } = useDraggingContainer();
const { scrollableContainerProps, isDragging } = useDraggingContainer("X");

return (
<St.Root>
Expand Down
2 changes: 1 addition & 1 deletion src/@components/MainPage/Medley/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import MedleyCard from "./MedleyCard";
import * as St from "./style";

export default function Medley() {
const { scrollableContainerProps, isDragging } = useDraggingContainer();
const { scrollableContainerProps, isDragging } = useDraggingContainer("X");
const { randomMedleyLists } = useMedleyLists();

return (
Expand Down