Skip to content

Instantly share code, notes, and snippets.

@MartijnHols
Last active March 20, 2025 17:14
Show Gist options
  • Save MartijnHols/e9f4f787efa9190885a708468f63c5bb to your computer and use it in GitHub Desktop.
Save MartijnHols/e9f4f787efa9190885a708468f63c5bb to your computer and use it in GitHub Desktop.
React hooks for getting the document height that updates when the On Screen Keyboard/Virtual Keyboard toggles
The latest version is available at https://martijnhols.nl/gists/how-to-get-document-height-ios-safari-osk
import { useEffect } from 'react'
const useOnScreenKeyboardScrollFix = () => {
useEffect(() => {
const handleScroll = () => {
window.scrollTo(0, 0)
}
window.addEventListener('scroll', handleScroll)
return () => {
window.removeEventListener('scroll', handleScroll)
}
}, [])
}
export default useOnScreenKeyboardScrollFix
@iPommes
Copy link

iPommes commented Dec 29, 2024

Little tricky. Sucks that mobile-PWA development is so harsh sometimes. But this solution works like a charm. Love it. Many thanks!

@tomdyqin
Copy link

tomdyqin commented Jan 1, 2025

when i use useOnScreenKeyboardScrollFix, the scroll content in FullViewportContainer will stop scroll, so i can not scroll into view on focusin

@tomdyqin
Copy link

tomdyqin commented Jan 1, 2025

how can i get keyboard height

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment