> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# Gestures

Gestures recognize drags, swipes, taps and long presses on an element, and decide which one wins when several could match. Reach for them when a plain `(bindtap)` isn't enough.

:::tip Drag with a mouse or a finger
The live preview below, like the [web preview](/guide/glossary.md#web-preview), recognizes gestures by the same rules as iOS and Android, from a mouse, a pen or a finger. Scan its QR code with [Lynx Explorer](/guide/glossary.md#lynx-explorer) to try them on a phone.
:::


## When to use a gesture

| You want                             | Use                                                                            |
| ------------------------------------ | ------------------------------------------------------------------------------ |
| A button or a tappable row           | `(bindtap)`. See [Handling taps and events](/guide/elements/event-handling.md) |
| To drag something with a finger      | `PanGesture`                                                                   |
| Momentum after a quick drag          | `FlingGesture`, with a `PanGesture` on the same element                        |
| A press and hold with its own timing | `LongPressGesture`                                                             |
| A tap and a drag on the same element | `TapGesture` and `PanGesture`, combined with `Gesture.Exclusive`               |

A gesture adds rules that events don't have: how far a finger must move, how long it must hold, and which gesture gets priority.

## Attach a gesture

```typescript title="src/app/like-button.ts"
import { Component, signal } from '@angular/core';
import {
  LYNX_ELEMENTS,
  LynxGestureDetector,
  TapGesture,
} from '@blotch/angular-lynx';

@Component({
  selector: 'app-like-button',
  imports: [LYNX_ELEMENTS, LynxGestureDetector],
  template: `
    <view [lynxGesture]="tap" class="rounded-lg bg-primary p-4">
      <text class="text-primary-foreground">Likes: {{ likes() }}</text>
    </view>
  `,
})
export class LikeButton {
  readonly likes = signal(0);

  readonly tap = new TapGesture()
    .maxDistance(10)
    .onEnd(() => this.likes.update((n) => n + 1));
}
```

- **Import `LynxGestureDetector`** next to `LYNX_ELEMENTS`. It provides the `[lynxGesture]` directive.
- **Build each gesture with chained calls.** `new TapGesture()` creates it, an option such as `.maxDistance(10)` sets a rule, and a callback such as `.onEnd()` reacts. Each call returns the same gesture.
- **Create each gesture once, as a `readonly` field.** The directive registers its gestures again whenever its input changes.
- **Callbacks run on the [main thread](/guide/glossary.md#main-thread)**, inside the native gesture. Like [event handlers](/guide/concepts/threads.md#event-handlers), they can change signals and add or remove elements.

## Follow the gesture's lifecycle

A gesture calls you back at each stage:

| Callback   | Fires when                                                                  |
| ---------- | --------------------------------------------------------------------------- |
| `onBegin`  | A finger touches down, before the gesture is recognized                     |
| `onStart`  | The gesture is recognized: the finger moved far enough, or held long enough |
| `onUpdate` | The finger moves while the gesture is active. Pan and fling only            |
| `onEnd`    | The gesture finishes                                                        |

- **On a tap or long press, `onEnd` means success.** Lynx also calls `onEnd` when these gestures fail, for example when a tap turns into a drag. AngularLynx drops that call for you.
- **Register only the callbacks you need.** Lynx skips a fling's `onStart` and `onEnd` unless an `onBegin` is registered, so AngularLynx registers an empty one for you.
- **Every callback also gets a state manager** as its second argument. It can end or fail the gesture from code. See [Gesture lifecycle](/api/gestures/lifecycle.md).

## Combine gestures

```typescript
import { Gesture, PanGesture, TapGesture } from '@blotch/angular-lynx';

export class DragOrTap {
  readonly #pan = new PanGesture().minDistance(10).onUpdate(() => {});
  readonly #tap = new TapGesture().onEnd(() => {});

  // Bind this one with [lynxGesture]="gesture".
  // The pan gets the first claim, so a drag never also counts as a tap.
  readonly gesture = Gesture.Exclusive(this.#pan, this.#tap);
}
```

An element can carry several gestures. By default, only one can be active at a time. The `Gesture` helpers set how they share the element:

| Helper                       | Rule                                                                        | Use it for                                                              |
| ---------------------------- | --------------------------------------------------------------------------- | ----------------------------------------------------------------------- |
| `Gesture.Exclusive(a, b, c)` | Priority order. `b` waits until `a` fails, and `c` waits until both fail    | A tap and a drag on one element: list the drag first                    |
| `Gesture.Simultaneous(a, b)` | All of them can be active at once                                           | Gestures that should run together, such as a drag and a long press      |
| `Gesture.Race(a, b)`         | No priority. Whichever is recognized first wins, and the others can't start | Gestures that can't be confused, such as a fling left and a fling right |

- **Order matters in `Exclusive`.** List the gesture that should win first. With the drag before the tap, a finger that moves becomes a drag, never a tap.
- **The helpers change the gestures you pass.** Use each gesture in only one helper call.
- **For finer control**, call `waitFor()`, `simultaneousWith()` or `continueWith()` on a single gesture. See [the gestures API](/api/gestures/index.md).

## Complete example: a sticker you can drag or tap

```typescript title="src/app/sticker.ts"
import { Component, computed, signal } from '@angular/core';
import {
  Gesture,
  LYNX_ELEMENTS,
  LynxGestureDetector,
  PanGesture,
  TapGesture,
} from '@blotch/angular-lynx';

@Component({
  selector: 'app-sticker',
  imports: [LYNX_ELEMENTS, LynxGestureDetector],
  template: `
    <view class="h-full w-full bg-background p-6">
      <view
        [lynxGesture]="gesture"
        [style.transform]="transform()"
        class="flex h-[120px] w-[120px] items-center justify-center rounded-2xl bg-primary"
      >
        <text class="text-primary-foreground">Taps: {{ taps() }}</text>
      </view>
      <text class="mt-4 text-muted-foreground">{{ status() }}</text>
    </view>
  `,
})
export class Sticker {
  readonly taps = signal(0);
  readonly status = signal('Drag or tap the sticker');
  readonly #x = signal(0);
  readonly #y = signal(0);
  readonly transform = computed(
    () => `translate(${this.#x()}px, ${this.#y()}px)`,
  );

  // Where the sticker rests between drags.
  #restX = 0;
  #restY = 0;

  readonly #pan = new PanGesture()
    .minDistance(10)
    .onUpdate((event) => {
      // translationX/Y is the distance from where the finger went down.
      this.#x.set(this.#restX + event.translationX);
      this.#y.set(this.#restY + event.translationY);
      this.status.set('Dragging');
    })
    .onEnd(() => {
      this.#restX = this.#x();
      this.#restY = this.#y();
      this.status.set('Dropped');
    });

  readonly #tap = new TapGesture().onEnd(() => {
    this.taps.update((n) => n + 1);
    this.status.set('Tapped');
  });

  // The pan gets the first claim, so a drag never also counts as a tap.
  readonly gesture = Gesture.Exclusive(this.#pan, this.#tap);
}
```

- **The pan moves the sticker** by updating two signals that feed a `transform`. Restyling the same element on every frame is safe.
- **`onEnd` saves where the sticker stopped**, so the next drag starts from there instead of snapping back.
- **The tap only fires when the pan fails**, which happens when the finger lifts before moving 10 pixels.

## Limitations

- **Pinch and rotation never fire.** `PinchGesture` and `RotationGesture` exist, but no Lynx platform recognizes them yet. For a pinch, build it from touch events instead:
  - Set `"multiTouch": true` in the build target's options in `angular.json`. Without it, Lynx reports one finger at a time.
  - Work out the zoom from the fingers in `touchstart`, `touchmove` and `touchend`. Read each event's `changedTouches`, by `identifier`, with `pageX` and `pageY` (`x` and `y` in the web preview).
  - Taps don't fire while two fingers are down.
  - The [Vitrine example](/examples/museum.md)'s viewer (`src/app/artwork/viewer.ts`) zooms this way.
- **A double tap can't make a single tap wait.** `numberOfTaps(2)` works, but `Gesture.Exclusive(doubleTap, singleTap)` on one element doesn't: Lynx recognizes each tap on its own. See [Double taps](/api/gestures/gesture-types.md#tapgesture).
- **A pan waiting for its `activeOffsetX` already holds the touch.** AngularLynx holds back its callbacks, but Lynx activates the pan at `minDistance`. So the pan fails the element's other gestures before the offset is reached, unless they're simultaneous with it. See [Options AngularLynx enforces](/api/gestures/gesture-types.md#options-angularlynx-enforces).
- **`minPointers`, `maxPointers` and a fling's `numberOfPointers` do nothing.** Lynx follows one finger at a time.
- **A fling has no `onStart`**, and fires reliably only on an element that also has a pan. See [FlingGesture](/api/gestures/gesture-types.md#flinggesture).
- **If no gesture in the app fires on a device**, check that your `lynx.config.ts` doesn't set `enableNewGesture` to `false`. AngularLynx turns it on by default, and Lynx ignores every gesture without it. See [If no gesture fires](/api/gestures/gesture-types.md#if-no-gesture-fires).

## Related

- [Gestures API](/api/gestures/index.md): `[lynxGesture]` and the `Gesture` helpers
- [Gesture types](/api/gestures/gesture-types.md): every gesture's options and event fields
- [Gesture lifecycle](/api/gestures/lifecycle.md): states, callbacks and the state manager
- [Main-thread scripts](/guide/main-thread/index.md): move elements on every frame with no lag
- [Gestures example](/examples/gestures.md)
