|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file RefreshPullRecognizer.ts |
| 5 | + * @input Touches from the gesture arena |
| 6 | + * (@astryxdesign/core/gestures/experimental) |
| 7 | + * @output Exports RefreshPullRecognizer: the downward pan that reads a pull |
| 8 | + * to refresh without taking the touch from the browser |
| 9 | + * @position The gesture of RefreshControl, on the foundation's |
| 10 | + * PanGestureRecognizer |
| 11 | + * |
| 12 | + * UIKit's refresh pull is the scroll view's own drag, not a second gesture: |
| 13 | + * pulling past the top reveals the control, and pushing back keeps scrolling |
| 14 | + * the same content in the same gesture. So this pan never claims its touch. |
| 15 | + * The browser keeps the finger for native scrolling, and the pan reads the |
| 16 | + * finger to draw the overscroll (the scroll owner's `overscroll-behavior-y` |
| 17 | + * is `none` near the top, so the browser itself has nothing to draw there): |
| 18 | + * |
| 19 | + * - it begins once the finger pulls down past the pan's hysteresis while |
| 20 | + * nothing under it can scroll that way (`scrollEdge`), or, when the drag |
| 21 | + * began as a scroll, once the content runs out (`scrollHandoff`); |
| 22 | + * - when the browser takes the touch to scroll, a pull already under way |
| 23 | + * keeps reading it instead of being cancelled; |
| 24 | + * - one finger: a second finger is the browser's (pinch-zoom). |
| 25 | + * |
| 26 | + * `onTouchDown` reports a first finger landing, so a settle in flight can |
| 27 | + * stop where it is (UIScrollView brakes on touch-down). |
| 28 | + * |
| 29 | + * SYNC: When modified, update: |
| 30 | + * - /packages/lab/src/RefreshControl/useRefreshControl.ts |
| 31 | + */ |
| 32 | + |
| 33 | +import { |
| 34 | + PanGestureRecognizer, |
| 35 | + type GestureEventContext, |
| 36 | + type GestureTouch, |
| 37 | + type PanGestureRecognizerOptions, |
| 38 | +} from '@astryxdesign/core/gestures/experimental'; |
| 39 | + |
| 40 | +export class RefreshPullRecognizer extends PanGestureRecognizer { |
| 41 | + /** Called when the first finger of a gesture lands, with its timestamp. */ |
| 42 | + onTouchDown: ((time: number) => void) | null = null; |
| 43 | + |
| 44 | + constructor(options: PanGestureRecognizerOptions = {}) { |
| 45 | + super({ |
| 46 | + name: 'RefreshPull', |
| 47 | + direction: 'down', |
| 48 | + scrollEdge: true, |
| 49 | + scrollHandoff: true, |
| 50 | + maximumNumberOfTouches: 1, |
| 51 | + ...options, |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + /** Native scrolling keeps the finger: the pull only reads it. */ |
| 56 | + override get claimsTouches(): boolean { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + protected override touchesBegan( |
| 61 | + touches: ReadonlyArray<GestureTouch>, |
| 62 | + event: GestureEventContext, |
| 63 | + ): void { |
| 64 | + if (this.numberOfTouches === touches.length) { |
| 65 | + this.onTouchDown?.(event.time); |
| 66 | + } |
| 67 | + super.touchesBegan(touches, event); |
| 68 | + } |
| 69 | + |
| 70 | + protected override touchesTakenByBrowser( |
| 71 | + touches: ReadonlyArray<GestureTouch>, |
| 72 | + event: GestureEventContext, |
| 73 | + ): boolean { |
| 74 | + if (this.state === 'began' || this.state === 'changed') { |
| 75 | + return true; |
| 76 | + } |
| 77 | + return super.touchesTakenByBrowser(touches, event); |
| 78 | + } |
| 79 | +} |
0 commit comments