Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stevewithington/151e963aa2c67dae6d35cc0921415ea5 to your computer and use it in GitHub Desktop.
Save stevewithington/151e963aa2c67dae6d35cc0921415ea5 to your computer and use it in GitHub Desktop.
Check if `passive` is supported via TypeScript
/** 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