-
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 Address feedback Fix flow Major refactor and re-design
- Loading branch information
Showing
30 changed files
with
1,719 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
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
74 changes: 74 additions & 0 deletions
74
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,74 @@ | ||
/** | ||
* 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 { | ||
ReactDOMListenerEvent, | ||
ReactDOMListenerHook, | ||
} 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 = {| | ||
capture?: boolean, | ||
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; | ||
} | ||
|
||
export function useEvent( | ||
type: string, | ||
options?: EventOptions, | ||
): ReactDOMListenerHook { | ||
const dispatcher = resolveDispatcher(); | ||
let capture = false; | ||
let passive = false; | ||
let priority = getEventPriority((type: any)); | ||
|
||
if (options != null) { | ||
const optionsCapture = options && options.capture; | ||
const optionsPassive = options && options.passive; | ||
const optionsPriority = options && options.priority; | ||
|
||
if (typeof optionsCapture === 'boolean') { | ||
capture = optionsCapture; | ||
} | ||
if (typeof optionsPassive === 'boolean') { | ||
passive = optionsPassive; | ||
} | ||
if (typeof optionsPriority === 'number') { | ||
priority = optionsPriority; | ||
} | ||
} | ||
const event: ReactDOMListenerEvent = { | ||
capture, | ||
passive, | ||
priority, | ||
type, | ||
}; | ||
return dispatcher.useEvent(event); | ||
} |
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.