-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: Drawer 컴포넌트 headless-ui로 리팩터링 * test: body scroll 막기 위한 textarea blink 애니메이션 적용 * feat: touchevent preventDefault 호출 * fix: textarea 내부에 스크롤 없는 경우에도 toucmove 브라우저 동작 막음 * fix: touchmove 이벤트 조건문 수정 * fix: textarea scrollbar 유무 판단 로직 수정 * fix: useBodyScrollLock -> useRemoveScroll 수정 - eventListner option 타입 정의 (FixedEventListenerOptions) - nonPassive option 정의 * fix: textarea 내부에서 바깥 영역 스크롤 되는 현상 해결 * refactor: 불필요한 log, textarea 배경색 제거, event handler memoization * chore: touchstart handler 선언부 상위로 이동 * feat: comment drawer 열릴 때 text 끝에 focus되도록 수정 - Drawer.CloseButton이 foucs를 받을 수 있도록 button 컨테이너 추가
- Loading branch information
Showing
6 changed files
with
180 additions
and
69 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,66 @@ | ||
import { useCallback, useEffect, useRef } from 'react'; | ||
import { nonPassive } from '@/utils/eventListener'; | ||
|
||
type Options = { | ||
enabled?: boolean; | ||
}; | ||
|
||
const getTouchXY = (event: TouchEvent | WheelEvent) => | ||
'changedTouches' in event | ||
? [event.changedTouches[0].clientX, event.changedTouches[0].clientY] | ||
: [0, 0]; | ||
|
||
const useRemoveVerticalScroll = (options?: Options) => { | ||
const enabled = options?.enabled; | ||
|
||
const touchStartRef = useRef([0, 0]); | ||
|
||
const scrollTouchStart = useCallback((event: TouchEvent) => { | ||
touchStartRef.current = getTouchXY(event); | ||
}, []); | ||
|
||
const shouldLock = useCallback((event: TouchEvent | WheelEvent) => { | ||
if (!event.target) return; | ||
|
||
const node = event.target as HTMLElement; | ||
const { clientHeight, scrollHeight, scrollTop } = node; | ||
|
||
const touch = getTouchXY(event); | ||
const touchStart = touchStartRef.current; | ||
const deltaY = 'deltaY' in event ? event.deltaY : touchStart[1] - touch[1]; | ||
|
||
const isDeltaYPositive = deltaY > 0; // scroll down | ||
|
||
const isScrollToTopEnd = !isDeltaYPositive && scrollTop === 0; | ||
const isScrollToBottomEnd = | ||
isDeltaYPositive && scrollTop + clientHeight === scrollHeight; | ||
|
||
if ( | ||
node.tagName !== 'TEXTAREA' || | ||
isScrollToTopEnd || | ||
isScrollToBottomEnd | ||
) { | ||
if (event.cancelable) { | ||
event.preventDefault(); | ||
} | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (!enabled) { | ||
return; | ||
} | ||
|
||
document.addEventListener('wheel', shouldLock, nonPassive); | ||
document.addEventListener('touchmove', shouldLock, nonPassive); | ||
document.addEventListener('touchstart', scrollTouchStart, nonPassive); | ||
|
||
return () => { | ||
document.removeEventListener('wheel', shouldLock, nonPassive); | ||
document.removeEventListener('touchmove', shouldLock, nonPassive); | ||
document.removeEventListener('touchstart', scrollTouchStart, nonPassive); | ||
}; | ||
}, [enabled, shouldLock, scrollTouchStart]); | ||
}; | ||
|
||
export default useRemoveVerticalScroll; |
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,32 @@ | ||
/** | ||
* 참고 | ||
* https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#safely_detecting_option_support | ||
* */ | ||
|
||
type FixedEventListenerOptions = | ||
| (AddEventListenerOptions & EventListenerOptions) | ||
| boolean; | ||
|
||
let passiveSupported = false; | ||
|
||
const emptyListener = () => void {}; | ||
|
||
try { | ||
const options: FixedEventListenerOptions = { | ||
get passive() { | ||
// This function will be called when the browser | ||
// attempts to access the passive property. | ||
passiveSupported = true; | ||
return false; | ||
}, | ||
}; | ||
|
||
window.addEventListener('test', emptyListener, options); | ||
window.removeEventListener('test', emptyListener, options); | ||
} catch { | ||
passiveSupported = false; | ||
} | ||
|
||
export const nonPassive: FixedEventListenerOptions = passiveSupported | ||
? { passive: false } | ||
: false; |
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