-
Notifications
You must be signed in to change notification settings - Fork 46.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Press event prop types and fix a check in Safari #15288
Merged
trueadm
merged 6 commits into
facebook:master
from
necolas:react-events/misc-press-fixes
Apr 2, 2019
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d1f5846
Add PressProps type to event module
necolas c2f16d7
Move default Press event delays to constants
necolas 20df2bf
Fix right-click press check for Safari
necolas 4c86723
Prettier and Linter
necolas 8dc9352
Use event.key in press responder
necolas 77f2284
Remove unused props from Press event module
necolas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,11 @@ | |
import type {EventResponderContext} from 'events/EventTypes'; | ||
import {REACT_EVENT_COMPONENT_TYPE} from 'shared/ReactSymbols'; | ||
|
||
// const DEFAULT_PRESS_DELAY_MS = 0; | ||
// const DEFAULT_PRESS_END_DELAY_MS = 0; | ||
// const DEFAULT_PRESS_START_DELAY_MS = 0; | ||
const DEFAULT_LONG_PRESS_DELAY_MS = 1000; | ||
|
||
const targetEventTypes = [ | ||
{name: 'click', passive: false}, | ||
{name: 'keydown', passive: false}, | ||
|
@@ -26,6 +31,23 @@ if (typeof window !== 'undefined' && window.PointerEvent === undefined) { | |
rootEventTypes.push({name: 'mouseup', passive: false}); | ||
} | ||
|
||
type PressProps = { | ||
disabled: boolean, | ||
delayLongPress: number, | ||
delayPress: number, | ||
delayPressEnd: number, | ||
delayPressStart: number, | ||
hitSlop: Object, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this prop for now #15261 |
||
onLongPress: (e: Object) => void, | ||
onLongPressChange: boolean => void, | ||
onLongPressShouldCancelPress: () => boolean, | ||
onPress: (e: Object) => void, | ||
onPressChange: boolean => void, | ||
onPressEnd: (e: Object) => void, | ||
onPressStart: (e: Object) => void, | ||
pressRententionOffset: Object, | ||
}; | ||
|
||
type PressState = { | ||
defaultPrevented: boolean, | ||
isAnchorTouched: boolean, | ||
|
@@ -47,7 +69,7 @@ function dispatchPressEvent( | |
|
||
function dispatchPressStartEvents( | ||
context: EventResponderContext, | ||
props: Object, | ||
props: PressProps, | ||
state: PressState, | ||
): void { | ||
function dispatchPressChangeEvent(bool) { | ||
|
@@ -64,7 +86,11 @@ function dispatchPressStartEvents( | |
dispatchPressChangeEvent(true); | ||
} | ||
if ((props.onLongPress || props.onLongPressChange) && !state.isLongPressed) { | ||
const delayLongPress = calculateDelayMS(props.delayLongPress, 0, 1000); | ||
const delayLongPress = calculateDelayMS( | ||
props.delayLongPress, | ||
0, | ||
DEFAULT_LONG_PRESS_DELAY_MS, | ||
); | ||
|
||
state.longPressTimeout = setTimeout(() => { | ||
state.isLongPressed = true; | ||
|
@@ -105,7 +131,7 @@ function dispatchPressStartEvents( | |
|
||
function dispatchPressEndEvents( | ||
context: EventResponderContext, | ||
props: Object, | ||
props: PressProps, | ||
state: PressState, | ||
): void { | ||
if (state.longPressTimeout !== null) { | ||
|
@@ -138,6 +164,11 @@ function isAnchorTagElement(eventTarget: EventTarget): boolean { | |
return (eventTarget: any).nodeName === 'A'; | ||
} | ||
|
||
function isValidKeyPress(key: string): boolean { | ||
// Accessibility for keyboards. Space and Enter only. | ||
return key === ' ' || key === 'Enter'; | ||
} | ||
|
||
function calculateDelayMS(delay: ?number, min = 0, fallback = 0) { | ||
const maybeNumber = delay == null ? null : delay; | ||
return Math.max(min, maybeNumber != null ? maybeNumber : fallback); | ||
|
@@ -158,22 +189,18 @@ const PressResponder = { | |
}, | ||
handleEvent( | ||
context: EventResponderContext, | ||
props: Object, | ||
props: PressProps, | ||
state: PressState, | ||
): void { | ||
const {eventTarget, eventType, event} = context; | ||
|
||
switch (eventType) { | ||
case 'keydown': { | ||
if (!props.onPress || context.isTargetOwned(eventTarget)) { | ||
return; | ||
} | ||
const isValidKeyPress = | ||
(event: any).which === 13 || | ||
(event: any).which === 32 || | ||
(event: any).keyCode === 13; | ||
|
||
if (!isValidKeyPress) { | ||
if ( | ||
!props.onPress || | ||
context.isTargetOwned(eventTarget) || | ||
!isValidKeyPress((event: any).key) | ||
) { | ||
return; | ||
} | ||
let keyPressEventListener = props.onPress; | ||
|
@@ -196,8 +223,12 @@ const PressResponder = { | |
dispatchPressEvent(context, state, 'press', keyPressEventListener); | ||
break; | ||
} | ||
|
||
/** | ||
* Touch event implementations are only needed for Safari, which lacks | ||
* support for pointer events. | ||
*/ | ||
case 'touchstart': | ||
// Touch events are for Safari, which lack pointer event support. | ||
if (!state.isPressed && !context.isTargetOwned(eventTarget)) { | ||
// We bail out of polyfilling anchor tags, given the same heuristics | ||
// explained above in regards to needing to use click events. | ||
|
@@ -213,7 +244,6 @@ const PressResponder = { | |
|
||
break; | ||
case 'touchend': { | ||
// Touch events are for Safari, which lack pointer event support | ||
if (state.isAnchorTouched) { | ||
return; | ||
} | ||
|
@@ -253,14 +283,21 @@ const PressResponder = { | |
} | ||
break; | ||
} | ||
|
||
/** | ||
* Respond to pointer events and fall back to mouse. | ||
*/ | ||
case 'pointerdown': | ||
case 'mousedown': { | ||
if ( | ||
!state.isPressed && | ||
!context.isTargetOwned(eventTarget) && | ||
!state.shouldSkipMouseAfterTouch | ||
) { | ||
if ((event: any).pointerType === 'mouse') { | ||
if ( | ||
(event: any).pointerType === 'mouse' || | ||
eventType === 'mousedown' | ||
) { | ||
// Ignore if we are pressing on hit slop area with mouse | ||
if ( | ||
context.isPositionWithinTouchHitTarget( | ||
|
@@ -282,8 +319,8 @@ const PressResponder = { | |
} | ||
break; | ||
} | ||
case 'mouseup': | ||
case 'pointerup': { | ||
case 'pointerup': | ||
case 'mouseup': { | ||
if (state.isPressed) { | ||
if (state.shouldSkipMouseAfterTouch) { | ||
state.shouldSkipMouseAfterTouch = false; | ||
|
@@ -320,6 +357,7 @@ const PressResponder = { | |
state.isAnchorTouched = false; | ||
break; | ||
} | ||
|
||
case 'scroll': | ||
case 'touchcancel': | ||
case 'contextmenu': | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this prop for now. There's no equivalent that we're currently using (only delay for start/end). And if
onPress
is not cancelled it is always a "completion" event that occurs immediately afteronPressEnd
.