Handling taps and events
Lynx elements fire native events: taps, touches, scrolls and text input. You bind them with Angular's (event) syntax, using Lynx names such as (bindtap) in place of (click).
Handle a tap
bindtapis Lynx's click. It fires when a finger presses and lifts without moving far. In the web preview, a mouse click fires it too.(click)does nothing. AngularLynx ignores event names that don't start with a Lynx prefix such asbindorcatch, and it doesn't warn you.- Pass values as arguments.
(bindtap)="select(item)"is simpler than reading the item back from the event. - Handlers run on the main thread, inside the native event. Changing signals there is always safe. See Threads.
Defer navigation and new views
Wrap router navigation, LynxPortal.open() and element.animate() in setTimeout when you call them from a handler. A handler runs inside the native event, and creating or destroying views there can crash the app or leave it blank. See the setTimeout rule.
Stop an event from bubbling
The prefix on the event name decides how the event travels:
- Use
catch, not$event.stopPropagation(). Lynx sets propagation by the prefix. AngularLynx turnsstopPropagation()andpreventDefault()into harmless no-ops so web-style code doesn't crash, but they don't stop anything. - A
catchhandler can be empty. The prefix alone stops the event, so(catchtap)="noop()"with an empty method is a common pattern.
Read the event object
Every event has the same outer shape. ng add installs @lynx-js/types, which has a type for each one.
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
- Tap, long-press and touch events:
$eventis Lynx'sTouchEvent. Import it withimport type { TouchEvent } from '@lynx-js/types', as above. - Every other event: Angular types
$eventas the DOMEvent. AcceptEvent, then cast it to readdetail:(event as CustomEvent<{ value: string }>).detail.value.
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.- 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 - Call element methods
- Testing event reference: firing these events in tests
