-
Notifications
You must be signed in to change notification settings - Fork 0
/
onWindowFocus.ts
33 lines (29 loc) · 1.09 KB
/
onWindowFocus.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { debounce } from './vendor'
/**
* The `onWindowFocus` function adds an event listener when the window gains focus or becomes visible after being hidden.
* @param callback - The `callback` parameter is a function that will be executed when the window gains
* focus or becomes visible.
* @returns The function `onWindowFocus` returns a function that can be used to remove the event
* listeners that were added.
* @example
* ```ts
* const removeListener = onWindowFocus(() => {console.log('Window is focused')})
*
* // Remove the event listeners
* removeListener()
* ```
*
*/
export function onWindowFocus(callback: (...args: any[]) => any): () => void {
const listener = debounce(100, callback)
const visibilitychangeListener = () => {
if (document.visibilityState === 'visible')
listener()
}
window.addEventListener('focus', listener, false)
window.addEventListener('visibilitychange', visibilitychangeListener, false)
return () => {
window.removeEventListener('focus', listener)
window.removeEventListener('visibilitychange', visibilitychangeListener)
}
}