useVisualViewport
Tracks the browser's VisualViewport dimensions, offsets, and pinch-zoom scale (useful for virtual keyboards).
Loading preview...
The useVisualViewport hook subscribes to window.visualViewport resize and scroll events via useSyncExternalStore, tracking the visible viewport's width, height, offsetLeft, offsetTop, pageLeft, pageTop, and pinch-zoom scale.
Installation
Usage
Examples
Adjusting Bottom Bar Above Mobile On-Screen Keyboards
Compare window.innerHeight with visualViewport.height to detect when a mobile virtual keyboard shrinks the visible area.
Monitoring Pinch-Zoom Scale
Read scale to hide or adjust fixed overlays when the user pinch-zooms the page.
API Reference
VisualViewportState
Visual viewport dimensions, offsets, and zoom scale returned by the hook.
Property
Specification
widthnumber
Visual viewport width in CSS pixels.
heightnumber
Visual viewport height in CSS pixels.
offsetLeftnumber
Horizontal offset of the visual viewport relative to the layout viewport.
offsetTopnumber
Vertical offset of the visual viewport relative to the layout viewport.
pageLeftnumber
Horizontal coordinate of the visual viewport relative to the document.
pageTopnumber
Vertical coordinate of the visual viewport relative to the document.
scalenumber
Pinch-zoom scaling factor applied to the visual viewport.
isSupportedunion
Whether
window.visualViewport is natively supported in the current browser.Full Signature