mirror of
https://github.com/GiriNeko/YesPlayMusic.git
synced 2025-12-16 21:28:06 +00:00
26 lines
618 B
TypeScript
26 lines
618 B
TypeScript
import { useState, useEffect } from 'react'
|
|
|
|
const hasFocus = () => typeof document !== 'undefined' && document.hasFocus()
|
|
|
|
const useWindowFocus = () => {
|
|
const [focused, setFocused] = useState(hasFocus)
|
|
|
|
useEffect(() => {
|
|
setFocused(hasFocus())
|
|
|
|
const onFocus = () => setFocused(true)
|
|
const onBlur = () => setFocused(false)
|
|
|
|
window.addEventListener('focus', onFocus)
|
|
window.addEventListener('blur', onBlur)
|
|
|
|
return () => {
|
|
window.removeEventListener('focus', onFocus)
|
|
window.removeEventListener('blur', onBlur)
|
|
}
|
|
}, [])
|
|
|
|
return focused
|
|
}
|
|
|
|
export default useWindowFocus
|