Skip to content

Commit

Permalink
React events: extract common helper functions (#15449)
Browse files Browse the repository at this point in the history
  • Loading branch information
necolas authored Apr 19, 2019
1 parent 0b50fb2 commit 5857c89
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions packages/react-events/src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* 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 {
ReactResponderEvent,
ReactResponderContext,
} from 'shared/ReactTypes';

export function getEventCurrentTarget(
event: ReactResponderEvent,
context: ReactResponderContext,
) {
const target: any = event.target;
let currentTarget = target;
while (
currentTarget.parentNode &&
currentTarget.parentNode.nodeType === Node.ELEMENT_NODE &&
context.isTargetWithinEventComponent(currentTarget.parentNode)
) {
currentTarget = currentTarget.parentNode;
}
return currentTarget;
}

export function getEventPointerType(event: ReactResponderEvent) {
const nativeEvent: any = event.nativeEvent;
const {type, pointerType} = nativeEvent;
if (pointerType != null) {
return pointerType;
}
if (type.indexOf('mouse') === 0) {
return 'mouse';
}
if (type.indexOf('touch') === 0) {
return 'touch';
}
if (type.indexOf('key') === 0) {
return 'keyboard';
}
return '';
}

export function isEventPositionWithinTouchHitTarget(
event: ReactResponderEvent,
context: ReactResponderContext,
) {
const nativeEvent: any = event.nativeEvent;
const target: any = event.target;
return context.isPositionWithinTouchHitTarget(
target.ownerDocument,
nativeEvent.x,
nativeEvent.y,
);
}

0 comments on commit 5857c89

Please sign in to comment.