'use client' import * as React from 'react' // How far the visual viewport sits inside the layout viewport. iOS Safari does not shrink the // layout viewport for the on-screen keyboard, so a `fixed bottom-0` surface ends up behind it; // `bottom` is the keyboard's height (plus Safari's bar) to lift that surface by, and `top` is how // far Safari scrolled the visual viewport down when it raised the keyboard. export function useVisualViewportInset() { const [inset, setInset] = React.useState({ top: 0, bottom: 0 }) React.useEffect(() => { const viewport = window.visualViewport if (!viewport) return const measure = () => { const top = Math.max(0, Math.round(viewport.offsetTop)) const bottom = Math.max( 0, Math.round(window.innerHeight - viewport.height - viewport.offsetTop) ) setInset((current) => current.top === top && current.bottom === bottom ? current : { top, bottom } ) } viewport.addEventListener('resize', measure) viewport.addEventListener('scroll', measure) measure() return () => { viewport.removeEventListener('resize', measure) viewport.removeEventListener('scroll', measure) } }, []) return inset }