Skip to Content
ohooks

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

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useVisualViewport.json

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
width
number
Visual viewport width in CSS pixels.
height
number
Visual viewport height in CSS pixels.
offsetLeft
number
Horizontal offset of the visual viewport relative to the layout viewport.
offsetTop
number
Vertical offset of the visual viewport relative to the layout viewport.
pageLeft
number
Horizontal coordinate of the visual viewport relative to the document.
pageTop
number
Vertical coordinate of the visual viewport relative to the document.
scale
number
Pinch-zoom scaling factor applied to the visual viewport.
isSupported
union
Whether window.visualViewport is natively supported in the current browser.