Last active
August 3, 2020 16:59
-
-
Save stevewithington/151e963aa2c67dae6d35cc0921415ea5 to your computer and use it in GitHub Desktop.
Check if `passive` is supported via TypeScript
This file contains hidden or 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
/** In case your linter gives you a problem attempting to register listeners to `testEvent` */ | |
declare global { | |
interface WindowEventMap { | |
testEvent: CustomEvent<{ event: any, options: any }>; | |
} | |
} | |
/** Inspired by: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Safely_detecting_option_support */ | |
export const isPassiveSupported = () => { | |
let passiveSupported = false; | |
const fakeHandler = () => { | |
return false; | |
}; | |
try { | |
const options = { | |
get passive() { | |
passiveSupported = true; | |
return false; | |
}, | |
}; | |
window.addEventListener('testEvent', fakeHandler, options); | |
window.removeEventListener('testEvent', fakeHandler, false); | |
} catch (err) { | |
passiveSupported = false; | |
} | |
return passiveSupported; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment