-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35256 from suneox/fix/26401-scroll-on-virtual-vie…
…wport 26401 avoid scroll on virtual viewport
- Loading branch information
Showing
3 changed files
with
64 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* Detects input or text area focus on browser. Native doesn't support DOM so default to false | ||
*/ | ||
export default function useTackInputFocus(): boolean { | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import {useCallback, useEffect} from 'react'; | ||
import useDebouncedState from '@hooks/useDebouncedState'; | ||
|
||
/** | ||
* Detects input or text area focus on browsers, to avoid scrolling on virtual viewports | ||
*/ | ||
export default function useTackInputFocus(enable = false): boolean { | ||
const [, isInputFocusDebounced, setIsInputFocus] = useDebouncedState(false); | ||
|
||
const handleFocusIn = useCallback( | ||
(event: FocusEvent) => { | ||
const targetElement = event.target as HTMLElement; | ||
if (targetElement.tagName === 'INPUT' || targetElement.tagName === 'TEXTAREA') { | ||
setIsInputFocus(true); | ||
} | ||
}, | ||
[setIsInputFocus], | ||
); | ||
|
||
const handleFocusOut = useCallback( | ||
(event: FocusEvent) => { | ||
const targetElement = event.target as HTMLElement; | ||
if (targetElement.tagName === 'INPUT' || targetElement.tagName === 'TEXTAREA') { | ||
setIsInputFocus(false); | ||
} | ||
}, | ||
[setIsInputFocus], | ||
); | ||
|
||
const resetScrollPositionOnVisualViewport = useCallback(() => { | ||
window.scrollTo({top: 0}); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (!enable) { | ||
return; | ||
} | ||
window.addEventListener('focusin', handleFocusIn); | ||
window.addEventListener('focusout', handleFocusOut); | ||
window.visualViewport?.addEventListener('scroll', resetScrollPositionOnVisualViewport); | ||
return () => { | ||
window.removeEventListener('focusin', handleFocusIn); | ||
window.removeEventListener('focusout', handleFocusOut); | ||
window.visualViewport?.removeEventListener('scroll', resetScrollPositionOnVisualViewport); | ||
}; | ||
}, [enable, handleFocusIn, handleFocusOut, resetScrollPositionOnVisualViewport]); | ||
|
||
return isInputFocusDebounced; | ||
} |