touch.tsx 772 B

123456789101112131415161718192021222324
  1. function isReactEvent(
  2. maybe: MouseEvent | TouchEvent | React.MouseEvent | React.TouchEvent
  3. ): maybe is React.MouseEvent | React.TouchEvent {
  4. return 'nativeEvent' in maybe;
  5. }
  6. /**
  7. * Handle getting position out of both React and Raw DOM events
  8. * as both are handled here due to mousedown/mousemove events
  9. * working differently.
  10. */
  11. export function getPointerPosition(
  12. event: MouseEvent | TouchEvent | React.MouseEvent | React.TouchEvent,
  13. property: 'pageX' | 'pageY' | 'clientX' | 'clientY'
  14. ): number {
  15. const actual = isReactEvent(event) ? event.nativeEvent : event;
  16. if (window.TouchEvent && actual instanceof TouchEvent) {
  17. return actual.targetTouches[0][property];
  18. }
  19. if (actual instanceof MouseEvent) {
  20. return actual[property];
  21. }
  22. return 0;
  23. }