mirror of
https://github.com/GiriNeko/YesPlayMusic.git
synced 2025-12-18 06:07:48 +00:00
feat: updates
This commit is contained in:
parent
9a52681687
commit
840a5b8e9b
104 changed files with 1645 additions and 13494 deletions
26
packages/web/hooks/useWindowFocus.ts
Normal file
26
packages/web/hooks/useWindowFocus.ts
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue