TouchEventUtils.js.flow 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. /**
  2. * Copyright (c) 2013-present, Facebook, Inc.
  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. * @providesModule TouchEventUtils
  8. */
  9. const TouchEventUtils = {
  10. /**
  11. * Utility function for common case of extracting out the primary touch from a
  12. * touch event.
  13. * - `touchEnd` events usually do not have the `touches` property.
  14. * http://stackoverflow.com/questions/3666929/
  15. * mobile-sarai-touchend-event-not-firing-when-last-touch-is-removed
  16. *
  17. * @param {Event} nativeEvent Native event that may or may not be a touch.
  18. * @return {TouchesObject?} an object with pageX and pageY or null.
  19. */
  20. extractSingleTouch: function (nativeEvent) {
  21. const touches = nativeEvent.touches;
  22. const changedTouches = nativeEvent.changedTouches;
  23. const hasTouches = touches && touches.length > 0;
  24. const hasChangedTouches = changedTouches && changedTouches.length > 0;
  25. return !hasTouches && hasChangedTouches ? changedTouches[0] : hasTouches ? touches[0] : nativeEvent;
  26. }
  27. };
  28. module.exports = TouchEventUtils;