Created
October 13, 2025 13:01
-
-
Save Wigny/51e93f9bf7b88da196611a1a0fc443c6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useSyncExternalStore, useMemo } from 'react'; | |
| export const useDocumentVisibility = (): boolean => { | |
| const subscribe = useMemo(() => { | |
| return (callback: () => void) => { | |
| document.addEventListener('visibilitychange', callback); | |
| return () => document.removeEventListener('visibilitychange', callback); | |
| }; | |
| }, []); | |
| const getSnapshot = useMemo(() => { | |
| return () => document.visibilityState === 'visible'; | |
| }, []); | |
| return useSyncExternalStore(subscribe, getSnapshot); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment