-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor(useGetTravelogue): queryFn 수정 * chore: datepicker 추가에 따른 변경 * fix(globalStyle): 모바일 환경에서 주소창이 전체 화면 크기에 포함되는 문제 해결 * refactor(Header): 불 필요한 z-index 제거 * fix(ModalBottomSheet): useBottomSheet 분리 및 모바일 환경에서 작동하도록 수정 * refactor: z-index 추가 * refactor(useBottomSheet): 상수화
- Loading branch information
Showing
6 changed files
with
124 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { useCallback, useEffect, useRef, useState } from "react"; | ||
|
||
const BOTTOM_SHEET = { | ||
openPosition: 0, | ||
closedPosition: 350, | ||
closeThreshold: 200, | ||
}; | ||
|
||
const useBottomSheet = (isOpen: boolean, onClose: () => void) => { | ||
const [startY, setStartY] = useState<number | null>(null); | ||
const [currentY, setCurrentY] = useState<number>(0); | ||
const sheetRef = useRef<HTMLDivElement | null>(null); | ||
|
||
useEffect(() => { | ||
if (isOpen) { | ||
setCurrentY(BOTTOM_SHEET.openPosition); | ||
} else setCurrentY(BOTTOM_SHEET.closedPosition); | ||
}, [isOpen]); | ||
|
||
useEffect(() => { | ||
const sheet = sheetRef.current; | ||
if (!sheet) return; | ||
|
||
const handleStart = (clientY: number) => setStartY(clientY); | ||
|
||
const handleMove = (clientY: number) => { | ||
if (startY === null) return; | ||
|
||
const diff = clientY - startY; | ||
if (diff > 0) { | ||
setCurrentY(diff); | ||
} | ||
}; | ||
|
||
const handleEnd = () => { | ||
if (currentY > BOTTOM_SHEET.closeThreshold) { | ||
onClose(); | ||
} else { | ||
setCurrentY(BOTTOM_SHEET.openPosition); | ||
} | ||
setStartY(null); | ||
}; | ||
|
||
const handleTouchStart = (e: TouchEvent) => { | ||
const touchedYPosition = e.touches[0].clientY; | ||
|
||
handleStart(touchedYPosition); | ||
}; | ||
const handleTouchMove = (e: TouchEvent) => { | ||
e.preventDefault(); | ||
|
||
const touchedYPosition = e.touches[0].clientY; | ||
|
||
handleMove(touchedYPosition); | ||
}; | ||
const handleTouchEnd = handleEnd; | ||
|
||
const handleMouseDown = (e: MouseEvent) => { | ||
const clickedYPosition = e.clientY; | ||
|
||
handleStart(clickedYPosition); | ||
}; | ||
const handleMouseMove = (e: MouseEvent) => { | ||
const clickedYPosition = e.clientY; | ||
|
||
if (startY !== null) { | ||
handleMove(clickedYPosition); | ||
} | ||
}; | ||
const handleMouseUp = handleEnd; | ||
|
||
sheet.addEventListener("touchstart", handleTouchStart); | ||
sheet.addEventListener("touchmove", handleTouchMove); | ||
sheet.addEventListener("touchend", handleTouchEnd); | ||
|
||
sheet.addEventListener("mousedown", handleMouseDown); | ||
document.addEventListener("mousemove", handleMouseMove); | ||
document.addEventListener("mouseup", handleMouseUp); | ||
|
||
return () => { | ||
sheet.removeEventListener("touchstart", handleTouchStart); | ||
sheet.removeEventListener("touchmove", handleTouchMove); | ||
sheet.removeEventListener("touchend", handleTouchEnd); | ||
|
||
sheet.removeEventListener("mousedown", handleMouseDown); | ||
document.removeEventListener("mousemove", handleMouseMove); | ||
document.removeEventListener("mouseup", handleMouseUp); | ||
}; | ||
}, [startY, currentY, onClose]); | ||
|
||
const handleClickEsc = useCallback( | ||
(e: KeyboardEvent) => { | ||
const isEscClicked = e.key === "Escape"; | ||
|
||
if (isEscClicked) onClose(); | ||
}, | ||
[onClose], | ||
); | ||
|
||
useEffect(() => { | ||
document.addEventListener("keydown", handleClickEsc); | ||
|
||
return () => { | ||
document.removeEventListener("keydown", handleClickEsc); | ||
}; | ||
}, [handleClickEsc]); | ||
|
||
return { | ||
sheetRef, | ||
currentY, | ||
}; | ||
}; | ||
|
||
export default useBottomSheet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters