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

# Handling taps and events

[Lynx](/guide/glossary.md#lynx) elements fire native events: taps, touches, scrolls and text input. You bind them with Angular's `(event)` syntax: a DOM name such as `(click)` where Lynx sends that event, or a Lynx name such as `(bindtap)`.


## Handle a tap

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

@Component({
  selector: 'app-color-picker',
  imports: [LYNX_ELEMENTS],
  template: `
    @for (color of colors; track color) {
      <view class="rounded-lg bg-card p-4" (click)="selected.set(color)">
        <text class="text-foreground">{{ color }}</text>
      </view>
    }
    <text class="text-muted-foreground">Selected: {{ selected() }}</text>
  `,
})
export class ColorPicker {
  readonly colors = ['Red', 'Green', 'Blue'];
  readonly selected = signal('none');
}
```

- **`(click)` runs on a tap.** It fires when a finger presses and lifts without moving far, and bubbles as on the web. In the [web preview](/guide/glossary.md#web-preview), a mouse click fires it too. That's also how Angular's `routerLink` navigates on a tap.
- **`(bindtap)` is Lynx's own name for it.** The two are the same event, as Lynx for Web maps them, so use either. Other DOM event names work where Lynx sends the same event: see [DOM event names](#dom-event-names).
- **Pass values as arguments.** `(click)="select(item)"` is simpler than reading the item back from the event.
- **Handlers run on the [main thread](/guide/glossary.md#main-thread)**, inside the native event. Do anything there you'd do on the web: change signals, navigate, open overlays, start animations. See [Threads](/guide/concepts/threads.md).

## DOM event names

```angular-html
<input (input)="name.set($any($event.target).value)" (keyup.enter)="search()" />
```

A template written for the browser keeps working where Lynx sends the same event. These names run as their `bind` form does:

- **`(click)`** runs on a tap (`bindtap`).
- **`(input)`, `(focus)` and `(blur)`** run on an `<input>` or `<textarea>`. As on the web, the field's text is in `$event.target.value`.
- **`(keyup.enter)` and `(keydown.enter)`** run when the keyboard's return key is pressed (`bindconfirm`).
- **`(scroll)`, `(scrollend)`, `(load)` and `(error)`** run on the elements that send them, such as a `<scroll-view>` or an `<image>`.
- **`(touchstart)`, `(touchmove)`, `(touchend)` and `(touchcancel)`**, and the animation and transition events, such as `(transitionend)`, run on any element.
- **Pointer events, such as `(pointerdown)`, and `(mouseover)`, `(mouseenter)`, `(mouseout)` and `(mouseleave)`** come from touches on any element. A finger is over the element from the moment it touches until it lifts. See [Pointer events](/api/angular-lynx/element-events.md#pointer-events).

Other DOM names, such as `(mousedown)`, `(keydown)` or `(submit)`, never run, and AngularLynx doesn't warn you. iOS and Android don't send them.

`$event` is still Lynx's event, though Angular types it as the DOM's. Apart from `target.value`, read what it sends from `$event.detail`, as for the `bind` names.

## Several handlers on one element

```angular-html
<view routerLink="/orders/1042" (click)="track('order')">
  <text>Order #1042</text>
</view>
```

An element can have as many handlers for an event as you like, as on the web. Here `routerLink` navigates and `track()` runs too, in the order Angular added them.

- **Lynx keeps one handler per event on an element.** So AngularLynx registers one that runs them all, [main-thread scripts](/guide/main-thread/index.md) included.
- **One `catch` stops the event for all of them.** If any of them uses `catch`, the event stops at the element once they've all run.
- **That one handler has one phase.** A `capture-` listener beside a bubbling one, or beside a main-thread script, runs as the event bubbles. That's after the element's children have handled it.

## Stop an event from bubbling

```angular-html
<view class="flex-row items-center p-4" (click)="open(item)">
  <text class="flex-1">{{ item.name }}</text>
  <!-- Deleting the row doesn't also open it -->
  <view class="p-2" (click)="remove(item); $event.stopPropagation()">
    <text>Delete</text>
  </view>
</view>
```

**`$event.stopPropagation()` works as on the web** for a tap, a touch event and the other events Lynx sends.

- **The element's other handlers all run,** and the event propagates no further. No ancestor's handler runs after it, and a bubbling `document:` or `window:` listener doesn't hear it.
- **Listeners that ran before it have heard it already.** Those are an ancestor's `capture-` handler or a capturing `document:` one. A capturing `document:` listener that calls it keeps the event from the elements.
- **`$event.stopImmediatePropagation()`** also skips the element's handlers that come after it.
- **Call them while the handler runs,** not after an `await`.
- **`preventDefault()` does nothing** on these events, since Lynx has no default action to prevent.

**Pointer events are the exception on iOS and Android.** Lynx has no pointer events. So each element with a `(pointerdown)` or another pointer listener makes its own from its own touches, and so do `document:` and `window:`.

- **Their `stopPropagation()` reaches no other listener's pointer event.** A child's doesn't keep its parent's pointer listener, or a `document:` or `window:` one, from hearing the pointer. A capturing `document:` pointer listener's doesn't keep it from the elements.
- **To stop a touch from reaching other elements,** stop its touch event or tap instead.
- **`stopImmediatePropagation()` still skips** the element's own later pointer listeners.
- **`preventDefault()` sets `defaultPrevented`,** with nothing to prevent.
- **In the web preview,** pointer listeners get the browser's own pointer events. Their `stopPropagation()` and `preventDefault()` work as in a browser.

Lynx also has its own way, in the event's name. The prefix decides how the event travels:

| Prefix          | Behavior                                 | Example                        |
| --------------- | ---------------------------------------- | ------------------------------ |
| `bind`          | Handles the event and lets it bubble up  | `(bindtap)="onTap()"`          |
| `catch`         | Handles the event and stops it there     | `(catchtap)="onTap()"`         |
| `capture-bind`  | Handles it on the way down, then bubbles | `(capture-bindtap)="onTap()"`  |
| `capture-catch` | Handles it on the way down, then stops   | `(capture-catchtap)="onTap()"` |

```angular-html
<view class="h-full w-full bg-black/50" (click)="close()">
  <!-- catchtap keeps taps on the panel from reaching the backdrop -->
  <view class="rounded-lg bg-background p-6" (catchtap)="noop()">
    <text>Tapping here doesn't close the dialog.</text>
  </view>
</view>
```

- **`catch` stops every time, without code.** The prefix alone stops the event, so `(catchtap)="noop()"` with an empty method is a common pattern for a panel over a backdrop. `stopPropagation()` decides as the handler runs, so it can stop some taps and not others.
- **`catch` leaves `document:` and `window:` listeners hearing it,** on the next timer, so a click-outside listener still sees a tap inside a panel. `stopPropagation()` hides the event from their bubbling listeners, as a browser does.
- **Both stop it natively,** so a handler Lynx runs for an ancestor, such as a [main-thread script](/guide/main-thread/index.md), doesn't run either. On iOS and Android, Lynx skips the other elements' handlers when one returns its stop result. AngularLynx returns that result for a handler that called `stopPropagation()`. In the [web preview](/guide/glossary.md#web-preview), AngularLynx skips them itself.

## Read the event object

```typescript title="src/app/touch-pad.ts"
import { Component, signal } from '@angular/core';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';
import type { TouchEvent } from '@lynx-js/types';

@Component({
  selector: 'app-touch-pad',
  imports: [LYNX_ELEMENTS],
  template: `
    <view class="h-[200px] bg-muted" (bindtouchmove)="onMove($event)">
      <text>{{ x() }}, {{ y() }}</text>
    </view>
  `,
})
export class TouchPad {
  readonly x = signal(0);
  readonly y = signal(0);

  onMove(event: TouchEvent): void {
    const touch = event.touches[0];
    if (!touch) return;
    this.x.set(Math.round(touch.x));
    this.y.set(Math.round(touch.y));
  }
}
```

Every event has the same outer shape, which AngularLynx calls `LynxEvent`:

| Field            | Type                   | What it holds                                                                                                                 |
| ---------------- | ---------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
| `type`           | `string`               | The event name without its prefix, such as `'tap'`                                                                            |
| `timestamp`      | `number`               | When the event fired                                                                                                          |
| `target`         | `{ id, uid, dataset }` | The element the event started on: its `id` attribute, an internal number, its `data-*` attributes, and a text field's `value` |
| `currentTarget`  | `{ id, uid, dataset }` | The element whose handler is running                                                                                          |
| `detail`         | object                 | The event's own data. See [Common events](#common-events)                                                                     |
| `touches`        | `Touch[]`              | Touch and tap events only: the fingers on the screen                                                                          |
| `changedTouches` | `Touch[]`              | Touch and tap events only: the fingers that changed in this event                                                             |

Each `Touch` has `x` and `y` relative to the element, `pageX` and `pageY` relative to the page, `clientX` and `clientY` relative to the screen, and an `identifier`.

`target` and `currentTarget` are plain data, not elements, so you can't call methods on them. To call an element method, such as scrolling a `<scroll-view>`, see [Call element methods](/guide/selector-query.md).

### Type the parameter

In a template, `$event` already has its type. Angular knows each element's events, so `(bindinput)="name.set($event.detail.value)"` type-checks, and a misspelt `detail` field fails the build.

A handler declared in the class needs its parameter typed:

```typescript
import { type LynxEvent, type ScrollDetail } from '@blotch/angular-lynx';

// <scroll-view (bindscroll)="onScroll($event)">
onScroll({ detail }: LynxEvent<ScrollDetail>): void {
  this.offset.set(detail.scrollTop);
}
```

- **Tap, long-press and touch events** are Lynx's `TouchEvent`. Import it with `import type { TouchEvent } from '@lynx-js/types'`, as above: without the import, TypeScript uses the DOM's.
- **Every other event** is a `LynxEvent` with the event's detail type, such as `LynxEvent<InputDetail>` for `<input>`'s `bindinput`. [Element events](/api/angular-lynx/element-events.md) lists each event's type.
- **Don't take a DOM `Event`.** `$event` isn't one, so `onInput(event: Event)` fails the build. Code that took `Event` and cast it to `CustomEvent` needs the event's type instead, with no cast.

## Common events

| Event                                                                | Elements                  | Fires when                                                              | `detail`                                                               |
| -------------------------------------------------------------------- | ------------------------- | ----------------------------------------------------------------------- | ---------------------------------------------------------------------- |
| `bindtap`                                                            | All                       | A finger presses and lifts                                              | `{ x, y }`, page position                                              |
| `bindlongpress`                                                      | All                       | A finger is held down                                                   | `{ x, y }`                                                             |
| `bindtouchstart`, `bindtouchmove`, `bindtouchend`, `bindtouchcancel` | All                       | A finger goes down, moves, lifts or is interrupted                      | `{ x, y }`, plus `touches`                                             |
| `bindlayoutchange`                                                   | All                       | The element's size or position changes                                  | `{ id, width, height, top, right, bottom, left, dataset }`             |
| `bindscroll`                                                         | `<scroll-view>`, `<list>` | The content scrolls                                                     | `{ scrollTop, scrollLeft, scrollHeight, scrollWidth, deltaX, deltaY }` |
| `bindscrolltolower`                                                  | `<scroll-view>`, `<list>` | Scrolling nears the end                                                 | Same as `bindscroll`                                                   |
| `bindinput`                                                          | `<input>`, `<textarea>`   | The text changes                                                        | `{ value, selectionStart, selectionEnd, isComposing }`                 |
| `bindfocus`, `bindblur`, `bindconfirm`                               | `<input>`, `<textarea>`   | The field gains or loses focus, or the keyboard's action key is pressed | `{ value }`                                                            |
| `bindload`                                                           | `<image>`                 | The image finishes loading                                              | `{ width, height }`                                                    |

Each element's page under [API → Elements](/api/elements/view.md) lists all its events.

- **`bindtap` and `bindlongpress` exclude each other.** With both on one element, a long press fires only `bindlongpress`.
- **A long press is half a second.** In the web preview, hold the mouse button down as long. The runtime recognizes it there, as Lynx for Web doesn't.
- **Touch events don't fire from a mouse.** In the web preview, a mouse fires `tap` but no `touchstart` or `touchend`. If you build press feedback from touch events, also bind `bindmousedown` and `bindmouseup`.

## Choose taps, gestures or touch events

| Use                                                    | For                                                                                                                        |
| ------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------- |
| `(click)` and `bindlongpress`                          | Buttons, list rows, anything you press. Start here.                                                                        |
| [Gestures](/guide/gestures/index.md) (`[lynxGesture]`) | Drags and swipes, or several interactions on one element that must not clash, such as a drag that shouldn't count as a tap |
| Touch events (`bindtouchstart`…)                       | Raw finger positions, such as a pressed state or a custom drawing pad                                                      |
| [Main-thread scripts](/guide/main-thread/index.md)     | Moving an element on every frame with no lag                                                                               |

## Related

- [Gestures](/guide/gestures/index.md)
- [Threads: where your code runs](/guide/concepts/threads.md)
- [Forms](/guide/forms.md): `bindinput` behind Angular's form APIs
- [Element events](/api/angular-lynx/element-events.md): the type of each event's `$event`
- [Call element methods](/guide/selector-query.md)
- [Testing event reference](/api/testing-library/event-reference.md): firing these events in tests
