refactor(core): use has instead of get to test for existence in ExecEnv (#7763)

refactor(core): use has instead of get to test for existence
This commit is contained in:
Joshua Chen 2022-07-11 19:19:17 +08:00 committed by GitHub
parent f21dadf621
commit 636d47060e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,23 +5,22 @@
* LICENSE file in the root directory of this source tree.
*/
const canUseDOM = !!(
const canUseDOM =
typeof window !== 'undefined' &&
window.document &&
window.document.createElement
);
'document' in window &&
'createElement' in window.document;
const ExecutionEnvironment = {
canUseDOM,
// window.attachEvent is IE-specific; it's very likely Docusaurus won't work
// on IE anyway.
canUseEventListeners:
// @ts-expect-error: window.attachEvent is IE specific.
// See https://github.com/Microsoft/TypeScript/issues/3953#issuecomment-123396830
canUseDOM && !!(window.addEventListener || window.attachEvent),
canUseDOM && ('addEventListener' in window || 'attachEvent' in window),
canUseIntersectionObserver: canUseDOM && 'IntersectionObserver' in window,
canUseViewport: canUseDOM && !!window.screen,
canUseViewport: canUseDOM && 'screen' in window,
};
export default ExecutionEnvironment;