1.1.4 • Published 1 year ago

@artikool5/use-document-visibility v1.1.4

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

useDocumentVisibility hook

React hook for easy access to Page Visibility API.

!CAUTION For learning purposes only!

useDocumentVisibility() returns three values:

  • isVisible: boolean - whether tab is visible in browser or not.
  • count: number - how many times tab has been hidden.
  • onVisibilityChange: (listener: VisibilityListener) => () => void - function to subscribe to the tab visibility changes.

Usage examples

function App() {
  const { isVisible, count, onVisibilityChange } = useDocumentVisibility();

  useEffect(() => {
    onVisibilityChange((isVisible) => {
      console.log("first handler", isVisible);
    });

    const unsubscribeSecondHandler = onVisibilityChange((isVisible) => {
      console.log("second handler", isVisible);
    });

    setTimeout(() => unsubscribeSecondHandler(), 5000);
  }, [onVisibilityChange]);

  return (
    <>
      <p>This tab is {isVisible ? "visible" : "not visible"}</p>
      <p>This tab was hidden {count} times</p>
    </>
  );
}
1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago