Last active
February 19, 2023 20:20
-
-
Save khromov/d02de25f38d63ef8aeb8f8891103c0d2 to your computer and use it in GitHub Desktop.
Svelte window focused action
This file contains 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
// Example: <div use:focused={(e) => console.log('Focused, event:', e)}> | |
export default function focused(node: HTMLElement, callback: Function) { | |
const handleFocusEvents = (event: Event) => { | |
callback(event) | |
}; | |
window.addEventListener('online', handleFocusEvents); | |
window.addEventListener('focus', handleFocusEvents); | |
return { | |
destroy() { | |
window.removeEventListener('online', handleFocusEvents); | |
window.removeEventListener('focus', handleFocusEvents); | |
}, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment