For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /guide/elements/event-handling.md.

Handling taps and events

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

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, 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.
  • Pass values as arguments. (click)="select(item)" is simpler than reading the item back from the event.
  • Handlers run on the main thread, inside the native event. Do anything there you'd do on the web: change signals, navigate, open overlays, start animations. See Threads.

DOM event names

<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.

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

<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 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

<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:

PrefixBehaviorExample
bindHandles the event and lets it bubble up(bindtap)="onTap()"
catchHandles the event and stops it there(catchtap)="onTap()"
capture-bindHandles it on the way down, then bubbles(capture-bindtap)="onTap()"
capture-catchHandles it on the way down, then stops(capture-catchtap)="onTap()"
<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, 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, AngularLynx skips them itself.

Read the event object

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:

FieldTypeWhat it holds
typestringThe event name without its prefix, such as 'tap'
timestampnumberWhen 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
detailobjectThe event's own data. See Common events
touchesTouch[]Touch and tap events only: the fingers on the screen
changedTouchesTouch[]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.

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:

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 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

EventElementsFires whendetail
bindtapAllA finger presses and lifts{ x, y }, page position
bindlongpressAllA finger is held down{ x, y }
bindtouchstart, bindtouchmove, bindtouchend, bindtouchcancelAllA finger goes down, moves, lifts or is interrupted{ x, y }, plus touches
bindlayoutchangeAllThe 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 endSame 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 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

UseFor
(click) and bindlongpressButtons, list rows, anything you press. Start here.
Gestures ([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 scriptsMoving an element on every frame with no lag