-
Notifications
You must be signed in to change notification settings - Fork 47.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[react-interactions] Add Listener API + useEvent/useDelegatedEvent
Update error codes fix lint DCE fix test Add event kinds to help propagation rules Add event kinds to help propagation rules #2 Fix kind bug cleanup
- Loading branch information
Showing
33 changed files
with
1,952 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
packages/react-dom/src/client/ReactDOMEventListenerHooks.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type {RefObject, ReactDOMEventListener} from 'shared/ReactDOMTypes'; | ||
|
||
import React from 'react'; | ||
import invariant from 'shared/invariant'; | ||
import {getEventPriority} from '../events/SimpleEventPlugin'; | ||
|
||
const ReactCurrentDispatcher = | ||
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED | ||
.ReactCurrentDispatcher; | ||
|
||
type EventOptions = {| | ||
bind?: RefObject, | ||
capture?: boolean, | ||
kind?: null | number | Symbol, | ||
passive?: boolean, | ||
priority?: number, | ||
|}; | ||
|
||
function resolveDispatcher() { | ||
const dispatcher = ReactCurrentDispatcher.current; | ||
invariant( | ||
dispatcher !== null, | ||
'Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for' + | ||
' one of the following reasons:\n' + | ||
'1. You might have mismatching versions of React and the renderer (such as React DOM)\n' + | ||
'2. You might be breaking the Rules of Hooks\n' + | ||
'3. You might have more than one copy of React in the same app\n' + | ||
'See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.', | ||
); | ||
return dispatcher; | ||
} | ||
|
||
function createEventListener( | ||
type: string, | ||
callback: Event => void, | ||
options: EventOptions | void, | ||
isDelegated: boolean, | ||
): ReactDOMEventListener { | ||
let bind = null; | ||
let capture = false; | ||
let kind = null; | ||
let passive = false; | ||
let priority = getEventPriority((type: any)); | ||
|
||
if (options != null) { | ||
const optionsBind = options.bind; | ||
const optionsCapture = options && options.capture; | ||
const optionsKind = options && options.kind; | ||
const optionsPassive = options && options.passive; | ||
const optionsPriority = options && options.priority; | ||
|
||
if (!isDelegated) { | ||
if (optionsBind != null) { | ||
invariant( | ||
'current' in optionsBind, | ||
'An object ref (via the useRef hook) is ' + | ||
'required for the "bind" option in useEvent', | ||
); | ||
bind = optionsBind; | ||
} | ||
} | ||
if (typeof optionsCapture === 'boolean') { | ||
capture = optionsCapture; | ||
} | ||
if ( | ||
(!isDelegated && typeof optionsKind === 'number') || | ||
// $FlowFixMe: we are using an older version of Flow that incorrectly recognizes the typeof for "symbol". | ||
typeof optionsKind === 'symbol' | ||
) { | ||
// Because of the Flow bug, we have to do this :( | ||
kind = (optionsKind: any); | ||
} | ||
if (typeof optionsPassive === 'boolean') { | ||
passive = optionsPassive; | ||
} | ||
if (typeof optionsPriority === 'number') { | ||
priority = optionsPriority; | ||
} | ||
} | ||
return { | ||
attached: false, | ||
bind, | ||
callback, | ||
capture, | ||
depth: 0, | ||
delegated: isDelegated, | ||
kind, | ||
passive, | ||
priority, | ||
target: null, | ||
type, | ||
}; | ||
} | ||
|
||
export function useEvent( | ||
type: string, | ||
callback: Event => void, | ||
options?: EventOptions, | ||
): void { | ||
const dispatcher = resolveDispatcher(); | ||
const isDelegated = false; | ||
const reactEventListener = createEventListener( | ||
type, | ||
callback, | ||
options, | ||
isDelegated, | ||
); | ||
dispatcher.useEvent(reactEventListener); | ||
} | ||
|
||
export function useDelegatedEvent( | ||
type: string, | ||
callback: Event => void, | ||
options?: EventOptions, | ||
): [() => void, () => void] { | ||
const dispatcher = resolveDispatcher(); | ||
const isDelegated = true; | ||
const reactDelegatedEventListener = createEventListener( | ||
type, | ||
callback, | ||
options, | ||
isDelegated, | ||
); | ||
return dispatcher.useDelegatedEvent(reactDelegatedEventListener); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.