mirror of
https://github.com/hanzoai/sentinel.git
synced 2026-09-29 22:34:33 +00:00
25 lines
772 B
TypeScript
25 lines
772 B
TypeScript
function isReactEvent(
|
|
maybe: MouseEvent | TouchEvent | React.MouseEvent | React.TouchEvent
|
|
): maybe is React.MouseEvent | React.TouchEvent {
|
|
return 'nativeEvent' in maybe;
|
|
}
|
|
|
|
/**
|
|
* Handle getting position out of both React and Raw DOM events
|
|
* as both are handled here due to mousedown/mousemove events
|
|
* working differently.
|
|
*/
|
|
export function getPointerPosition(
|
|
event: MouseEvent | TouchEvent | React.MouseEvent | React.TouchEvent,
|
|
property: 'pageX' | 'pageY' | 'clientX' | 'clientY'
|
|
): number {
|
|
const actual = isReactEvent(event) ? event.nativeEvent : event;
|
|
if (window.TouchEvent && actual instanceof TouchEvent) {
|
|
return actual.targetTouches[0][property];
|
|
}
|
|
if (actual instanceof MouseEvent) {
|
|
return actual[property];
|
|
}
|
|
return 0;
|
|
}
|