This is a library for optimizing performance sensitive events and declarative preventDefault and stopPropagation
npm install @taiga-ui/event-plugins



@taiga-ui/event-plugins is a tiny (1KB gzip) library for optimizing change detection cycles for performance
sensitive events (such as _touchmove_, _scroll_, _drag_ etc.) and declarative _preventDefault()_ and
_stopPropagation()_.
1. Add NG_EVENT_PLUGINS to your app providers:
``typescript`
bootstrapApplication(AppComponent, {
providers: [NG_EVENT_PLUGINS],
});
2. Use new modifiers for events in templates and in @HostListener:.stop
- to call stopPropagation() on event.prevent
- to call preventDefault() on event.self
- to skip bubbled events.zoneless
- to call event handler outside Angular's NgZone.capture
- to listen to events in.passive
capture phase
- to add.once
passive event listener
- to remove event listener after first callbackresize
- to watch for elements changing dimensions with ResizeObserver.debounce~
- to delay the execution of the event handler for a specified time (ms or s).throttle~
- to limits how often the event handler can run for a specified time (ms or s)
For example:
`html`
Clicking on this DIV will not move focus
`html`
Clicks on this DIV will not bubble up
`html`
Callbacks to mousemove will not trigger change detection
`html`
Clicks will be stopped before reaching this DIV
`html`
`html`
3. You can also re-enter NgZone and trigger change detection, using @shouldCall decorator that takes a predicate
function as argument:
`html`
Scrolling this DIV will only trigger change detection and onScroll callback if it is scrolled to bottom
`typescript
import {shouldCall} from '@taiga-ui/event-plugins';
export function scrollFilter({
scrollTop, scrollHeight, clientHeight
}: HTMLElement): boolean {
return scrollTop === scrollHeight - clientHeight;
}
// ...
@shouldCall(scrollFilter)
onScroll(_element: HTMLElement): void {
this.someService.requestMoreData();
}
`
4. Angular global events only support body, window and document. You can listen to events on any global object:
with these plugins by replacing with > symbol, for example:
`ts`
@HostListener('visualViewport>resize', ['$event.target'])
onPinchZoom({ scale }: VisualViewport) {
console.log(scale)
}
5. iOS currently doesn't support the contextmenu event. Instead, you can use a custom longtap event. This eventcontextmenu
captures the event on non-iOS devices and simulates similar behavior on iOS devices.
`html`Div with context menu
> All examples above work the same when used with @HostListener and CustomEvent
- Predicate is called with the same arguments as the decorated method and in the context of class instance (has access
to this)
- Decorated method will be called and change detection triggered if predicate returns true.
- .zoneless modifier will not work with built-in keyboard pseudo-events, such as keydown.enter orkeydown.arrowDown
since Angular re-enters NgZone` inside internal handlers.
You can try this
interactive demo
You can also read this detailed article
explaining how this library works
@taiga-ui/event-plugins is a part of Taiga UI libraries family which is
backed and used by a large enterprise. This means you can rely on timely support and continuous development.
š Feel free to use our library in your commercial and private applications
All @taiga-ui/event-plugins packages are covered by Apache 2.0
Read more about this license here