File size: 3,899 Bytes
a8fb296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useEffect, useRef, type RefObject } from 'react'

/**
 * Swipe between a story's screens on a touch screen (#1222, owner 2026-11-26: "It should swipe left to toggle"): swipe left
 * = next screen, swipe right = previous. Shared by the Data Center Story and the topic stories.
 *
 * - Horizontal-dominant only: the finger must travel at least `minDistance` px sideways and clearly more sideways than up or
 *   down (`ratio`), within `maxMs`, so a vertical scroll (the phone's screen-by-screen snap) is never taken for a swipe and
 *   nothing calls preventDefault: the page keeps its own scrolling.
 * - Not inside something that pans or swipes on its own: maps, charts, sliders, form fields, dialogs, a horizontally
 *   scrollable strip, or anything marked `data-swipe-ignore` (see `SWIPE_IGNORE`).
 * - Touch only (`touchstart` / `touchend`): a mouse drag or a trackpad does nothing.
 */
export const SWIPE_IGNORE = [
  '[data-swipe-ignore]',
  '.stance-map',
  '.sc-block',
  '.leaflet-container',
  '.stance-place',
  'input',
  'select',
  'textarea',
  '[role="dialog"]',
  '[role="slider"]',
].join(', ')

export interface SwipeOptions {
  /** The element whose touches count (the story root). */
  rootRef: RefObject<HTMLElement>
  onNext: () => void
  onPrev: () => void
  /** False turns swiping off (a dialog is open, reduced layout, a wide screen). */
  enabled?: boolean
  minDistance?: number
  /** How much more sideways than vertical the gesture must be. */
  ratio?: number
  maxMs?: number
}

/** True when `el` (or an ancestor up to `root`) can scroll sideways itself, so a sideways drag belongs to it. */
function scrollsSideways(el: HTMLElement | null, root: HTMLElement): boolean {
  for (let n = el; n && n !== root && n !== document.body; n = n.parentElement) {
    if (n.scrollWidth > n.clientWidth + 1) {
      const ox = getComputedStyle(n).overflowX
      if (ox === 'auto' || ox === 'scroll') return true
    }
  }
  return false
}

/** Pure: which way a finished gesture goes, or null. Exported for the unit test. */
export function swipeDirection(dx: number, dy: number, ms: number, minDistance = 50, ratio = 1.5, maxMs = 800): 'next' | 'prev' | null {
  if (ms > maxMs || Math.abs(dx) < minDistance || Math.abs(dx) < ratio * Math.abs(dy)) return null
  return dx < 0 ? 'next' : 'prev'
}

export function useSwipeNav({ rootRef, onNext, onPrev, enabled = true, minDistance = 50, ratio = 1.5, maxMs = 800 }: SwipeOptions): void {
  const cb = useRef({ onNext, onPrev })
  cb.current = { onNext, onPrev }
  useEffect(() => {
    const root = rootRef.current
    if (!root || !enabled) return
    let start: { x: number; y: number; t: number } | null = null
    const onStart = (e: TouchEvent) => {
      start = null
      if (e.touches.length !== 1) return
      const target = e.target as HTMLElement | null
      if (target?.closest(SWIPE_IGNORE) || scrollsSideways(target, root)) return
      const t = e.touches[0]!
      start = { x: t.clientX, y: t.clientY, t: performance.now() }
    }
    const onEnd = (e: TouchEvent) => {
      if (!start) return
      const t = e.changedTouches[0]
      const s = start
      start = null
      if (!t) return
      const dir = swipeDirection(t.clientX - s.x, t.clientY - s.y, performance.now() - s.t, minDistance, ratio, maxMs)
      if (dir === 'next') cb.current.onNext()
      else if (dir === 'prev') cb.current.onPrev()
    }
    const onCancel = () => (start = null)
    root.addEventListener('touchstart', onStart, { passive: true })
    root.addEventListener('touchend', onEnd, { passive: true })
    root.addEventListener('touchcancel', onCancel, { passive: true })
    return () => {
      root.removeEventListener('touchstart', onStart)
      root.removeEventListener('touchend', onEnd)
      root.removeEventListener('touchcancel', onCancel)
    }
  }, [rootRef, enabled, minDistance, ratio, maxMs])
}