Skip to main content

visualviewport

Read the visual viewport's size, offset, and scale, and listen for its resize/scroll events, via the Visual Viewport API.

Import: @rtorcato/browser-common/visualviewport

๐Ÿ“– MDN: VisualViewport ยท ๐Ÿ“Š caniuse: visualviewport

Browser supportโ€‹

APIChromeEdgeFirefoxSafariiOS SafariBaseline
VisualViewportโœ… 61+โœ… 79+โœ… 91+โœ… 13+โœ… 13+Widely available

โœ… supported ยท โ— partial ยท โŒ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: VisualViewport

Exampleโ€‹

import {
isVisualViewportAvailable,
getVisualViewportInfo,
onVisualViewportChange,
} from '@rtorcato/browser-common/visualviewport'

if (isVisualViewportAvailable()) {
const info = getVisualViewportInfo()
console.log(info?.scale)

const off = onVisualViewportChange(() => updateLayout())
// later:
off()
}

Exportsโ€‹

  • isVisualViewportAvailable() โ€” feature check
  • getVisualViewportInfo() โ€” current { offsetLeft, offsetTop, width, height, scale }, or null
  • onVisualViewportChange(callback) โ€” subscribe to resize and scroll; returns an unsubscribe function

See the API reference for full signatures.