Skip to main content

visibility

Check whether the page is visible or hidden, and react when the user switches tabs or minimises the window, via the Page Visibility API.

Import: @rtorcato/browser-common/visibility

๐Ÿ“– MDN: Page Visibility API ยท ๐Ÿ“Š caniuse: pagevisibility

Browser supportโ€‹

APIChromeEdgeFirefoxSafariiOS SafariBaseline
Document.visibilityStateโœ… 33+โœ… 12+โœ… 18+โœ… 7+โœ… 7+Widely available
Document.visibilitychange_eventโœ… 62+โœ… 18+โœ… 56+โœ… 14.1+โœ… 14.5+Widely available

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

Exampleโ€‹

import {
isPageVisible,
onVisibilityChange,
} from '@rtorcato/browser-common/visibility'

if (isPageVisible()) startPolling()

const off = onVisibilityChange((state) => {
if (state === 'hidden') stopPolling()
else startPolling()
})
// later:
off()

Exportsโ€‹

  • isVisibilityAvailable() โ€” feature check
  • getVisibilityState() โ€” 'visible' or 'hidden', or null where unsupported
  • isPageVisible() โ€” true / false, or null where unsupported
  • onVisibilityChange(callback) โ€” subscribe to visibilitychange; the callback gets the new state; returns an unsubscribe function

See the API reference for full signatures.