@gitlab/ui
Version:
GitLab UI Components
37 lines (34 loc) • 1.71 kB
JavaScript
const HAS_WINDOW_SUPPORT = typeof window !== 'undefined';
const HAS_DOCUMENT_SUPPORT = typeof document !== 'undefined';
const HAS_NAVIGATOR_SUPPORT = typeof navigator !== 'undefined';
/* istanbul ignore next: JSDOM always returns false */
const HAS_MUTATION_OBSERVER_SUPPORT = typeof MutationObserver !== 'undefined' || typeof WebKitMutationObserver !== 'undefined' || typeof MozMutationObserver !== 'undefined';
const IS_BROWSER = HAS_WINDOW_SUPPORT && HAS_DOCUMENT_SUPPORT && HAS_NAVIGATOR_SUPPORT;
const WINDOW = HAS_WINDOW_SUPPORT ? window : {};
const DOCUMENT = HAS_DOCUMENT_SUPPORT ? document : {};
const NAVIGATOR = HAS_NAVIGATOR_SUPPORT ? navigator : {};
const USER_AGENT = (NAVIGATOR.userAgent || '').toLowerCase();
const IS_JSDOM = USER_AGENT.indexOf('jsdom') > 0;
// Determine if the browser supports the option passive for events
const HAS_PASSIVE_EVENT_SUPPORT = (() => {
let passiveEventSupported = false;
if (IS_BROWSER) {
try {
const options = {
// This function will be called when the browser
// attempts to access the passive property
get passive() {
/* istanbul ignore next: will never be called in JSDOM */
passiveEventSupported = true;
}
};
WINDOW.addEventListener('test', options, options);
WINDOW.removeEventListener('test', options, options);
} catch {
/* istanbul ignore next: will never be called in JSDOM */
passiveEventSupported = false;
}
}
return passiveEventSupported;
})();
export { DOCUMENT, HAS_DOCUMENT_SUPPORT, HAS_MUTATION_OBSERVER_SUPPORT, HAS_NAVIGATOR_SUPPORT, HAS_PASSIVE_EVENT_SUPPORT, HAS_WINDOW_SUPPORT, IS_BROWSER, IS_JSDOM, NAVIGATOR, USER_AGENT, WINDOW };