Skip to content

Commit 5857c89

Browse files
authored
React events: extract common helper functions (#15449)
1 parent 0b50fb2 commit 5857c89

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

packages/react-events/src/utils.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 {
11+
ReactResponderEvent,
12+
ReactResponderContext,
13+
} from 'shared/ReactTypes';
14+
15+
export function getEventCurrentTarget(
16+
event: ReactResponderEvent,
17+
context: ReactResponderContext,
18+
) {
19+
const target: any = event.target;
20+
let currentTarget = target;
21+
while (
22+
currentTarget.parentNode &&
23+
currentTarget.parentNode.nodeType === Node.ELEMENT_NODE &&
24+
context.isTargetWithinEventComponent(currentTarget.parentNode)
25+
) {
26+
currentTarget = currentTarget.parentNode;
27+
}
28+
return currentTarget;
29+
}
30+
31+
export function getEventPointerType(event: ReactResponderEvent) {
32+
const nativeEvent: any = event.nativeEvent;
33+
const {type, pointerType} = nativeEvent;
34+
if (pointerType != null) {
35+
return pointerType;
36+
}
37+
if (type.indexOf('mouse') === 0) {
38+
return 'mouse';
39+
}
40+
if (type.indexOf('touch') === 0) {
41+
return 'touch';
42+
}
43+
if (type.indexOf('key') === 0) {
44+
return 'keyboard';
45+
}
46+
return '';
47+
}
48+
49+
export function isEventPositionWithinTouchHitTarget(
50+
event: ReactResponderEvent,
51+
context: ReactResponderContext,
52+
) {
53+
const nativeEvent: any = event.nativeEvent;
54+
const target: any = event.target;
55+
return context.isPositionWithinTouchHitTarget(
56+
target.ownerDocument,
57+
nativeEvent.x,
58+
nativeEvent.y,
59+
);
60+
}

0 commit comments

Comments
 (0)