Skip to Content
ohooks

useDocumentVisibility

Tracks the current document.visibilityState ('visible' | 'hidden') using useSyncExternalStore.

Loading preview...

The useDocumentVisibility hook subscribes to the visibilitychange event via useSyncExternalStore and returns the current DocumentVisibilityState ('visible' or 'hidden').

Installation

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

Usage

Examples

Pausing Media or Polling When Tab is Hidden

Check whether visibility === "hidden" to pause video playback, animations, or background polling while the user is away from the tab.

Refreshing Data When Returning to the Tab

Run an effect when visibility transitions back to "visible" to fetch fresh data.