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
(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'srouterLinknavigates 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
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
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
catchstops the event for all of them. If any of them usescatch, 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
$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:orwindow:listener doesn't hear it. - Listeners that ran before it have heard it already. Those are an ancestor's
capture-handler or a capturingdocument:one. A capturingdocument: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 adocument:orwindow:one, from hearing the pointer. A capturingdocument: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()setsdefaultPrevented, with nothing to prevent.- In the web preview, pointer listeners get the browser's own pointer events. Their
stopPropagation()andpreventDefault()work as in a browser.
Lynx also has its own way, in the event's name. The prefix decides how the event travels:
catchstops 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.catchleavesdocument:andwindow: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
Every event has the same outer shape, which AngularLynx calls LynxEvent:
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:
- Tap, long-press and touch events are Lynx's
TouchEvent. Import it withimport type { TouchEvent } from '@lynx-js/types', as above: without the import, TypeScript uses the DOM's. - Every other event is a
LynxEventwith the event's detail type, such asLynxEvent<InputDetail>for<input>'sbindinput. Element events lists each event's type. - Don't take a DOM
Event.$eventisn't one, soonInput(event: Event)fails the build. Code that tookEventand cast it toCustomEventneeds the event's type instead, with no cast.
Common events
Each element's page under API → Elements lists all its events.
bindtapandbindlongpressexclude each other. With both on one element, a long press fires onlybindlongpress.- 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
tapbut notouchstartortouchend. If you build press feedback from touch events, also bindbindmousedownandbindmouseup.
Choose taps, gestures or touch events
Related
- Gestures
- Threads: where your code runs
- Forms:
bindinputbehind Angular's form APIs - Element events: the type of each event's
$event - Call element methods
- Testing event reference: firing these events in tests
