All files / src/composables useLongPress.js

96.93% Statements 95/98
64.28% Branches 9/14
83.33% Functions 5/6
96.93% Lines 95/98

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 1131x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x   1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 777x 777x 777x 777x 777x 777x 777x 777x 777x 777x   777x 398x 3x 3x 3x 398x 777x   777x 7x 7x   7x 7x 7x   7x 4x 4x 4x 7x 777x   777x 1x 1x 1x 1x 777x   777x 777x 777x 777x 777x 777x 777x 777x 4x 2x 2x 2x 2x 2x 777x   777x       777x   777x   777x 777x 777x 777x 777x 777x 777x 777x 777x 777x 777x 777x 777x 777x 777x  
/**
 * Long-press as a secondary gesture on a row whose primary gesture is a tap
 * (SB-1101).
 *
 * The match list gave every match two full-width buttons so that the rare act
 * (open, edit) had somewhere to live. Tap now does the common act, and the rare
 * one moves behind a long press. That only works if the long press cannot
 * possibly fire the tap as well, which is most of what is going on here.
 *
 * Pointer events rather than touch events: one code path covers finger, pen and
 * a held mouse button, and the browser's touch/mouse compatibility events never
 * enter the picture.
 */
 
import { onBeforeUnmount, ref } from 'vue';
 
/** Long enough not to fire while scrolling; short enough not to feel broken. */
const DEFAULT_DELAY_MS = 500;
 
/**
 * Finger travel that means "this is a scroll, not a press". A finger resting on
 * a phone drifts a few pixels; a scroll moves far more.
 */
const MOVE_TOLERANCE_PX = 10;
 
/**
 * @param {(event: PointerEvent) => void} onLongPress  fired once the press holds
 * @param {object} [options]
 * @param {number} [options.delay]
 * @returns handlers to spread onto an element, plus `didLongPress`
 */
export function useLongPress(onLongPress, { delay = DEFAULT_DELAY_MS } = {}) {
  let timer = null;
  let origin = null;
  // Set the moment the press fires and cleared on the next pointerdown — NOT
  // in the click handler, because a long press on touch may produce no click
  // at all. Clearing it on click would leave it stuck true on those platforms
  // and swallow the *following* genuine tap.
  const didLongPress = ref(false);
  // Only touch and pen get the context menu suppressed. A right-click on a
  // desktop row should still open the browser's own menu.
  let lastPointerType = 'mouse';
 
  const cancel = () => {
    if (timer !== null) {
      clearTimeout(timer);
      timer = null;
    }
    origin = null;
  };
 
  const onPointerDown = event => {
    // Ignore secondary mouse buttons: right-click has its own meaning.
    if (event.pointerType === 'mouse' && event.button !== 0) return;
 
    lastPointerType = event.pointerType || 'mouse';
    didLongPress.value = false;
    origin = { x: event.clientX, y: event.clientY };
 
    timer = setTimeout(() => {
      timer = null;
      didLongPress.value = true;
      onLongPress(event);
    }, delay);
  };
 
  const onPointerMove = event => {
    if (timer === null || !origin) return;
    const dx = Math.abs(event.clientX - origin.x);
    const dy = Math.abs(event.clientY - origin.y);
    if (dx > MOVE_TOLERANCE_PX || dy > MOVE_TOLERANCE_PX) cancel();
  };
 
  /**
   * Wrap the row's click handler. Returns a handler that drops the click when
   * it is the tail of a long press.
   *
   * On Android a long press still delivers a click on pointerup; without this
   * the sheet would open and the row would navigate underneath it.
   */
  const guardClick = handler => event => {
    if (didLongPress.value) {
      event.preventDefault();
      event.stopPropagation();
      return;
    }
    handler(event);
  };
 
  const onContextMenu = event => {
    if (lastPointerType === 'touch' || lastPointerType === 'pen') {
      event.preventDefault();
    }
  };
 
  onBeforeUnmount(cancel);
 
  return {
    didLongPress,
    /** Spread onto the pressable element. */
    handlers: {
      pointerdown: onPointerDown,
      pointermove: onPointerMove,
      pointerup: cancel,
      pointercancel: cancel,
      pointerleave: cancel,
      contextmenu: onContextMenu,
    },
    guardClick,
    cancel,
  };
}