main
js 42 lines 1.1 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @flow
8 */
9
10 import {canUseDOM} from 'shared/ExecutionEnvironment';
11
12 /**
13 * Checks if an event is supported in the current execution environment.
14 *
15 * NOTE: This will not work correctly for non-generic events such as `change`,
16 * `reset`, `load`, `error`, and `select`.
17 *
18 * Borrows from Modernizr.
19 *
20 * @param {string} eventNameSuffix Event name, e.g. "click".
21 * @return {boolean} True if the event is supported.
22 * @internal
23 * @license Modernizr 3.0.0pre (Custom Build) | MIT
24 */
25 function isEventSupported(eventNameSuffix: string): boolean {
26 if (!canUseDOM) {
27 return false;
28 }
29
30 const eventName = 'on' + eventNameSuffix;
31 let isSupported = eventName in document;
32
33 if (!isSupported) {
34 const element = document.createElement('div');
35 element.setAttribute(eventName, 'return;');
36 isSupported = typeof (element as any)[eventName] === 'function';
37 }
38
39 return isSupported;
40 }
41
42 export default isEventSupported;