Command palette

@yasinsensoy/native

React Native · Components

GestureView

Gesture region: a drag inside it does not conflict with scrolling.

18 exports

Import

import {
  GestureView,
  toDirections,
  directionOf,
  gestureArbiter,
  hasNativeGesture,
  usePanGesture,
} from "@yasinsensoy/native";

GestureView

component

Gesture region: a drag inside it does not conflict with scrolling.

When a finger lands on this view, the ScrollView/FlatList above waits until the direction is known (slop, 8 dp). If the move is in a direction the region wants, the scroll does not start at all for that touch; otherwise the scroll starts at once with the accumulated movement. The decision is made on the native side (iOS UIGestureRecognizer, the Android touch intercept chain) in the same frame as the touch; it behaves the same on both platforms. It also registers with the arbiter in JS: outer JS drags (the navigator's back gesture, Sheet) yield to this region's directions.

Without the native view (web, an old build) a plain View is drawn; the behavior continues with the JS arbiter.

GestureView({ ref, gesture, claim, claimOnStart, claimPinch, slop, enabled, onTouchStart, onTouchEnd, onTouchCancel, children, ...rest }: GestureViewProps): JSX.Element

Examples

// With usePanGesture
const pan = usePanGesture({ claim: "horizontal", onUpdate: (_, g) => x.setValue(g.dx) });
<GestureView gesture={pan}>…</GestureView>

// With your own PanResponder
<GestureView claim="horizontal" {...responder.panHandlers}>…</GestureView>

toDirections

function

Turns a GestureClaim value into a set of directions.

toDirections(claim: GestureClaim | undefined): GestureDirections

directionOf

function

Dominant direction of the movement (dx, dy the finger's displacement).

directionOf(dx: number, dy: number): GestureDirection

The arbiter. Components mostly use it indirectly (GestureView, usePanGesture, ScrollView); an outer drag that writes its own PanResponder (e.g. a drawer pulled from the edge) should ask with yields before taking a move.

NameTypeDescription
enter(region: GestureRegion, event: ArbiterTouchEvent): voidA region registers when the finger lands on it (onTouchStart). A new touch (one finger, a different event object) clears the previous list.
leave(region: GestureRegion): voidThe region leaves the touch (the finger lifted or the touch was cancelled).
regions(): readonly GestureRegion[]Regions under the touch (innermost first).
claimant(direction: GestureDirection, query?: GestureClaimQuery): GestureRegion | undefinedThe innermost region that wants a move in this direction. If a region is found, an outer handler must not take the move.
yields(direction: GestureDirection, query?: GestureClaimQuery): booleanWhether a region inside wants this direction (gestureArbiter.claimant).
begin(region: GestureRegion): voidThe region took the move (a drag or scroll started).
end(region: GestureRegion): voidThe region's move ended.
isActive(kind?: GestureRegionKind): booleanWhether some region is handling a move right now (optionally by kind).
subscribe(listener: (event: GestureArbiterEvent) => void): () => voidListens to begin/end events; returns a function that stops listening.

Examples

onMoveShouldSetPanResponderCapture: (_, g) =>
  Math.abs(g.dx) > 10 && !gestureArbiter.yields(directionOf(g.dx, g.dy)),

Whether the native gesture view (YasinGestureView) exists in this build. If not (web, an app whose native code was not rebuilt), regions are drawn as plain Views; the JS arbiter still works, and the scroll above is disabled from JS when a drag starts (one frame late).

hasNativeGesture(): boolean

A drag that does not conflict with scrolling. Built on top of PanResponder and used with GestureView:

  • Starts only for moves in the claim directions; a horizontal drag on a vertically scrolling page does not move the page, and a vertical move does not start the drag.
  • On the native side (the same on iOS and Android) the decision is made on the UI thread: the scroll does not "escape" because of JS latency.
  • Yields to an inner region that wants the same direction (a nested drag or a scrollable horizontal list).
  • Once started it does not hand over to another handler (the navigator's back gesture, Sheet).
usePanGesture(options: PanGestureOptions): PanGesture

Examples

const x = useRef(new Animated.Value(0)).current;
const pan = usePanGesture({
  claim: "horizontal",
  onUpdate: (_, g) => x.setValue(g.dx),
  onEnd: () => Animated.spring(x, { toValue: 0, useNativeDriver: true }).start(),
});
return (
  <GestureView gesture={pan}>
    <Animated.View style={{ transform: [{ translateX: x }] }} />
  </GestureView>
);

Direction of the finger's movement.

type GestureDirection = "left" | "right" | "up" | "down"

Directions a region takes: "horizontal" (left + right), "vertical" (up + down), "all", "none" or a list of directions (e.g. ["right"] for a "reply" that is only pulled to the right).

type GestureClaim = "none" | "horizontal" | "vertical" | "all" | readonly GestureDirection[]

On/off state of the directions.

NameTypeDescription
leftbooleanCan move left.
rightbooleanCan move right.
upbooleanCan move up.
downbooleanCan move down.

Kind of region: a gesture handling a drag, or a scroll view.

type GestureRegionKind = "gesture" | "scroll"

GestureRegion

interface

A region registered with the arbiter.

NameTypeDescription
idnumberUnique id.
kindGestureRegionKindKind of the region.
claims(direction: GestureDirection): booleanWhether it wants a move in this direction right now (e.g. if the scroll is not at the edge).

Event emitted by the arbiter.

NameTypeDescription
type"begin" | "end"begin: the region took the move (a drag or scroll started); end: it finished.
regionGestureRegionRegion the event belongs to.

Options of gestureArbiter.claimant.

NameTypeDescription
below?GestureRegionOnly regions INSIDE this region are considered (a region yields to what is inside it, not outside). If omitted, all regions under the touch.
kinds?readonly GestureRegionKind[]Only regions of this kind. If omitted, all.

The part of a touch event the arbiter uses.

NameTypeDescription
nativeEvent{ readonly touches?: ArrayLike<unknown>; }Native touch event; only the touch count is read.

Props of GestureView.

Extends: ViewProps

NameTypeDefaultDescription
ref?Ref<ViewRef>Ref of the underlying native view.
gesture?PanGestureA usePanGesture result. When given, the directions and handlers are taken from it; claim, claimOnStart, claimPinch, slop, enabled are ignored.
claim?GestureClaim"none"For components using their own PanResponder: directions this region takes. A drag in those directions does not start the scroll above.
claimOnStart?booleanfalseTakes the touch as soon as it lands. If it becomes true while a touch is in progress (e.g. a drag starting with a long press), scroll views above release that touch at once.
claimPinch?booleanfalseTakes it when a second finger lands.
slop?number8Distance at which the direction is decided (dp).
enabled?booleantrueWhen disabled it takes no direction.

State of the drag (React Native PanResponderGestureState).

type PanGestureState = PanResponderGestureState

Options of usePanGesture.

NameTypeDefaultDescription
claim?GestureClaim"all"Directions the drag takes. A move in other directions stays with the scroll above (or the outer gesture): a horizontal slider takes "horizontal" and the page keeps scrolling vertically. Directions can change with state (e.g. a carousel on its first slide does not give "right"; then a pull to the right stays with the navigator's "back" gesture).
claimOnStart?booleanfalseStarts as soon as touched; the scroll above does not start at all for this touch (a signature pad, a slider thumb).
claimPinch?booleanfalseStarts when a second finger lands (pinch to zoom).
enabled?booleantrueWhen disabled it takes no touches.
slop?number8Distance at which the direction is decided (dp).
shouldBegin?(state: PanGestureState, event: GestureResponderEvent) => booleanExtra condition: return false to not start even when the direction matches (e.g. only pulls that start at the edge). Do not put the direction condition here but in claim: the native side decides based on claim.
onTap?(event: GestureResponderEvent) => voidA tap and release without dragging (e.g. jumping to the tapped spot on a slider). When given, the touch is tracked from the start but a drag still starts only in the claim direction; if the finger goes another way the scroll takes over and it does not count as a tap.
onBegin?(event: GestureResponderEvent, state: PanGestureState) => voidThe drag started.
onUpdate?(event: GestureResponderEvent, state: PanGestureState) => voidThe finger moved.
onEnd?(event: GestureResponderEvent, state: PanGestureState, info: { readonly cancelled: boolean; }) => voidThe drag ended. cancelled: it moved to another handler before the finger lifted, or the system cancelled it (the value must not be applied; revert to the old state).

PanGesture

interface

The usePanGesture result; passed to GestureView as gesture.

NameTypeDescription
regionGestureRegionIts region in the arbiter.
panHandlersGestureResponderHandlersReact Native responder handlers (GestureView binds them itself).
isActive(): booleanWhether a drag is in progress right now.