Skip to content

Instantly share code, notes, and snippets.

@khromov
Last active February 19, 2023 20:20
Show Gist options
  • Save khromov/d02de25f38d63ef8aeb8f8891103c0d2 to your computer and use it in GitHub Desktop.
Save khromov/d02de25f38d63ef8aeb8f8891103c0d2 to your computer and use it in GitHub Desktop.
Svelte window focused action
// 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