Skip to content

Instantly share code, notes, and snippets.

@MartijnHols
Last active September 12, 2025 14:28
Show Gist options
  • Select an option

  • Save MartijnHols/e9f4f787efa9190885a708468f63c5bb to your computer and use it in GitHub Desktop.

Select an option

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
@tomdyqin
Copy link

tomdyqin commented Jan 1, 2025

how can i get keyboard height

@noah-jacksonn
Copy link

Hi @MartijnHols, First of all, thank you so much for your work!!! I have the same issue on my app and I implemented your solution and it works but most of the time when the keyboard is open and the screen resizes + the scroll to top emitting, there is a glitch that the app slides from the bottom to the top... Here is an example, first focus has the glitch and the second don't:

Screen.Recording.2024-06-23.at.12.45.24.mov

same problem here. did you end up getting a fix?

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