|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its 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 type {RefObject, ReactDOMEventListener} from 'shared/ReactDOMTypes'; |
| 11 | + |
| 12 | +import React from 'react'; |
| 13 | +import invariant from 'shared/invariant'; |
| 14 | +import {getEventPriority} from '../events/SimpleEventPlugin'; |
| 15 | + |
| 16 | +const ReactCurrentDispatcher = |
| 17 | + React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED |
| 18 | + .ReactCurrentDispatcher; |
| 19 | + |
| 20 | +type EventOptions = {| |
| 21 | + bind?: RefObject, |
| 22 | + capture?: boolean, |
| 23 | + kind?: null | number | Symbol, |
| 24 | + passive?: boolean, |
| 25 | + priority?: number, |
| 26 | +|}; |
| 27 | + |
| 28 | +function resolveDispatcher() { |
| 29 | + const dispatcher = ReactCurrentDispatcher.current; |
| 30 | + invariant( |
| 31 | + dispatcher !== null, |
| 32 | + 'Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for' + |
| 33 | + ' one of the following reasons:\n' + |
| 34 | + '1. You might have mismatching versions of React and the renderer (such as React DOM)\n' + |
| 35 | + '2. You might be breaking the Rules of Hooks\n' + |
| 36 | + '3. You might have more than one copy of React in the same app\n' + |
| 37 | + 'See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.', |
| 38 | + ); |
| 39 | + return dispatcher; |
| 40 | +} |
| 41 | + |
| 42 | +function createEventListener( |
| 43 | + type: string, |
| 44 | + callback: Event => void, |
| 45 | + options: EventOptions | void, |
| 46 | + isDelegated: boolean, |
| 47 | +): ReactDOMEventListener { |
| 48 | + let bind = null; |
| 49 | + let capture = false; |
| 50 | + let kind = null; |
| 51 | + let passive = false; |
| 52 | + let priority = getEventPriority((type: any)); |
| 53 | + |
| 54 | + if (options != null) { |
| 55 | + const optionsBind = options.bind; |
| 56 | + const optionsCapture = options && options.capture; |
| 57 | + const optionsKind = options && options.kind; |
| 58 | + const optionsPassive = options && options.passive; |
| 59 | + const optionsPriority = options && options.priority; |
| 60 | + |
| 61 | + if (!isDelegated) { |
| 62 | + if (optionsBind != null) { |
| 63 | + invariant( |
| 64 | + 'current' in optionsBind, |
| 65 | + 'An object ref (via the useRef hook) is ' + |
| 66 | + 'required for the "bind" option in useEvent', |
| 67 | + ); |
| 68 | + bind = optionsBind; |
| 69 | + } |
| 70 | + } |
| 71 | + if (typeof optionsCapture === 'boolean') { |
| 72 | + capture = optionsCapture; |
| 73 | + } |
| 74 | + if ( |
| 75 | + (!isDelegated && typeof optionsKind === 'number') || |
| 76 | + // $FlowFixMe: we are using an older version of Flow that incorrectly recognizes the typeof for "symbol". |
| 77 | + typeof optionsKind === 'symbol' |
| 78 | + ) { |
| 79 | + // Because of the Flow bug, we have to do this :( |
| 80 | + kind = (optionsKind: any); |
| 81 | + } |
| 82 | + if (typeof optionsPassive === 'boolean') { |
| 83 | + passive = optionsPassive; |
| 84 | + } |
| 85 | + if (typeof optionsPriority === 'number') { |
| 86 | + priority = optionsPriority; |
| 87 | + } |
| 88 | + } |
| 89 | + return { |
| 90 | + attached: false, |
| 91 | + bind, |
| 92 | + callback, |
| 93 | + capture, |
| 94 | + depth: 0, |
| 95 | + delegated: isDelegated, |
| 96 | + kind, |
| 97 | + passive, |
| 98 | + priority, |
| 99 | + target: null, |
| 100 | + type, |
| 101 | + }; |
| 102 | +} |
| 103 | + |
| 104 | +export function useEvent( |
| 105 | + type: string, |
| 106 | + callback: Event => void, |
| 107 | + options?: EventOptions, |
| 108 | +): void { |
| 109 | + const dispatcher = resolveDispatcher(); |
| 110 | + const isDelegated = false; |
| 111 | + const reactEventListener = createEventListener( |
| 112 | + type, |
| 113 | + callback, |
| 114 | + options, |
| 115 | + isDelegated, |
| 116 | + ); |
| 117 | + dispatcher.useEvent(reactEventListener); |
| 118 | +} |
| 119 | + |
| 120 | +export function useDelegatedEvent( |
| 121 | + type: string, |
| 122 | + callback: Event => void, |
| 123 | + options?: EventOptions, |
| 124 | +): [() => void, () => void] { |
| 125 | + const dispatcher = resolveDispatcher(); |
| 126 | + const isDelegated = true; |
| 127 | + const reactDelegatedEventListener = createEventListener( |
| 128 | + type, |
| 129 | + callback, |
| 130 | + options, |
| 131 | + isDelegated, |
| 132 | + ); |
| 133 | + return dispatcher.useDelegatedEvent(reactDelegatedEventListener); |
| 134 | +} |
0 commit comments