Skip to content

Commit d010e11

Browse files
crisbetoalxhub
authored andcommitted
feat(core): add event listener options to renderer (#59092)
Updates the `Renderer2.listen` signature to accept event options, as well as all adjacent types to it. PR Close #59092
1 parent 46f00f9 commit d010e11

File tree

19 files changed

+143
-36
lines changed

19 files changed

+143
-36
lines changed

goldens/public-api/core/index.api.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,16 @@ export function linkedSignal<S, D>(options: {
11391139
equal?: ValueEqualityFn<NoInfer<D>>;
11401140
}): WritableSignal<D>;
11411141

1142+
// @public
1143+
export interface ListenerOptions {
1144+
// (undocumented)
1145+
capture?: boolean;
1146+
// (undocumented)
1147+
once?: boolean;
1148+
// (undocumented)
1149+
passive?: boolean;
1150+
}
1151+
11421152
// @public
11431153
export const LOCALE_ID: InjectionToken<string>;
11441154

@@ -1524,7 +1534,7 @@ export abstract class Renderer2 {
15241534
abstract destroy(): void;
15251535
destroyNode: ((node: any) => void) | null;
15261536
abstract insertBefore(parent: any, newChild: any, refChild: any, isMove?: boolean): void;
1527-
abstract listen(target: 'window' | 'document' | 'body' | any, eventName: string, callback: (event: any) => boolean | void): () => void;
1537+
abstract listen(target: 'window' | 'document' | 'body' | any, eventName: string, callback: (event: any) => boolean | void, options?: ListenerOptions): () => void;
15281538
abstract nextSibling(node: any): any;
15291539
abstract parentNode(node: any): any;
15301540
abstract removeAttribute(el: any, name: string, namespace?: string | null): void;

goldens/public-api/platform-browser/index.api.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { HttpTransferCacheOptions } from '@angular/common/http';
1414
import * as i0 from '@angular/core';
1515
import * as i1 from '@angular/common';
1616
import { InjectionToken } from '@angular/core';
17+
import { ListenerOptions } from '@angular/core';
1718
import { NgZone } from '@angular/core';
1819
import { PlatformRef } from '@angular/core';
1920
import { Predicate } from '@angular/core';
@@ -77,7 +78,7 @@ export const EVENT_MANAGER_PLUGINS: InjectionToken<EventManagerPlugin[]>;
7778
// @public
7879
export class EventManager {
7980
constructor(plugins: EventManagerPlugin[], _zone: NgZone);
80-
addEventListener(element: HTMLElement, eventName: string, handler: Function): Function;
81+
addEventListener(element: HTMLElement, eventName: string, handler: Function, options?: ListenerOptions): Function;
8182
getZone(): NgZone;
8283
// (undocumented)
8384
static ɵfac: i0.ɵɵFactoryDeclaration<EventManager, never>;
@@ -88,7 +89,7 @@ export class EventManager {
8889
// @public
8990
export abstract class EventManagerPlugin {
9091
constructor(_doc: any);
91-
abstract addEventListener(element: HTMLElement, eventName: string, handler: Function): Function;
92+
abstract addEventListener(element: HTMLElement, eventName: string, handler: Function, options?: ListenerOptions): Function;
9293
// (undocumented)
9394
manager: EventManager;
9495
abstract supports(eventName: string): boolean;

packages/animations/browser/src/render/renderer.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
RendererFactory2,
1515
RendererStyleFlags2,
1616
ɵAnimationRendererType as AnimationRendererType,
17+
type ListenerOptions,
1718
} from '@angular/core';
1819
import type {AnimationEngine} from './animation_engine_next';
1920

@@ -135,8 +136,13 @@ export class BaseAnimationRenderer implements Renderer2 {
135136
this.delegate.setValue(node, value);
136137
}
137138

138-
listen(target: any, eventName: string, callback: (event: any) => boolean | void): () => void {
139-
return this.delegate.listen(target, eventName, callback);
139+
listen(
140+
target: any,
141+
eventName: string,
142+
callback: (event: any) => boolean | void,
143+
options?: ListenerOptions,
144+
): () => void {
145+
return this.delegate.listen(target, eventName, callback, options);
140146
}
141147

142148
protected disableAnimations(element: any, value: boolean) {
@@ -173,6 +179,7 @@ export class AnimationRenderer extends BaseAnimationRenderer implements Renderer
173179
target: 'window' | 'document' | 'body' | any,
174180
eventName: string,
175181
callback: (event: any) => any,
182+
options?: ListenerOptions,
176183
): () => void {
177184
if (eventName.charAt(0) == ANIMATION_PREFIX) {
178185
const element = resolveElementFromTarget(target);
@@ -188,7 +195,7 @@ export class AnimationRenderer extends BaseAnimationRenderer implements Renderer
188195
this.factory.scheduleListenerCallback(countId, callback, event);
189196
});
190197
}
191-
return this.delegate.listen(target, eventName, callback);
198+
return this.delegate.listen(target, eventName, callback, options);
192199
}
193200
}
194201

packages/common/src/dom_adapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export abstract class DomAdapter {
4141
abstract isShadowRoot(node: any): boolean;
4242

4343
// Used by KeyEventsPlugin
44-
abstract onAndCancel(el: any, evt: any, listener: any): Function;
44+
abstract onAndCancel(el: any, evt: any, listener: any, options?: any): Function;
4545

4646
// Used by PlatformLocation and ServerEventManagerPlugin
4747
abstract getGlobalEventTarget(doc: Document, target: string): any;

packages/core/src/render.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
*/
88

99
// Public API for render
10-
export {Renderer2, RendererFactory2} from './render/api';
10+
export {Renderer2, RendererFactory2, ListenerOptions} from './render/api';
1111
export {RendererStyleFlags2, RendererType2} from './render/api_flags';

packages/core/src/render/api.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,14 @@ export abstract class Renderer2 {
217217
* DOM element.
218218
* @param eventName The event to listen for.
219219
* @param callback A handler function to invoke when the event occurs.
220+
* @param options Options that configure how the event listener is bound.
220221
* @returns An "unlisten" function for disposing of this handler.
221222
*/
222223
abstract listen(
223224
target: 'window' | 'document' | 'body' | any,
224225
eventName: string,
225226
callback: (event: any) => boolean | void,
227+
options?: ListenerOptions,
226228
): () => void;
227229

228230
/**
@@ -252,3 +254,13 @@ export const enum AnimationRendererType {
252254
Regular = 0,
253255
Delegated = 1,
254256
}
257+
258+
/**
259+
* Options that can be used to configure an event listener.
260+
* @publicApi
261+
*/
262+
export interface ListenerOptions {
263+
capture?: boolean;
264+
once?: boolean;
265+
passive?: boolean;
266+
}

packages/core/src/render3/interfaces/renderer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import {RendererStyleFlags2, RendererType2} from '../../render/api_flags';
10+
import type {ListenerOptions} from '../../render/api';
1011
import {TrustedHTML, TrustedScript, TrustedScriptURL} from '../../util/security/trusted_type_defs';
1112

1213
import {RComment, RElement, RNode, RText} from './renderer_dom';
@@ -68,6 +69,7 @@ export interface Renderer {
6869
target: GlobalTargetName | RNode,
6970
eventName: string,
7071
callback: (event: any) => boolean | void,
72+
options?: ListenerOptions,
7173
): () => void;
7274
}
7375

packages/core/test/bundling/animations-standalone/bundle.golden_symbols.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@
164164
{
165165
"name": "DomAdapter"
166166
},
167+
{
168+
"name": "DomEventsPlugin"
169+
},
167170
{
168171
"name": "DomRendererFactory2"
169172
},
@@ -263,6 +266,9 @@
263266
{
264267
"name": "InputFlags"
265268
},
269+
{
270+
"name": "KeyEventsPlugin"
271+
},
266272
{
267273
"name": "LContainerFlags"
268274
},

packages/core/test/bundling/hydration/bundle.golden_symbols.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@
104104
{
105105
"name": "DomAdapter"
106106
},
107+
{
108+
"name": "DomEventsPlugin"
109+
},
107110
{
108111
"name": "DomRendererFactory2"
109112
},
@@ -212,6 +215,9 @@
212215
{
213216
"name": "InputFlags"
214217
},
218+
{
219+
"name": "KeyEventsPlugin"
220+
},
215221
{
216222
"name": "LContainerFlags"
217223
},

packages/core/test/bundling/router/bundle.golden_symbols.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@
176176
{
177177
"name": "DomAdapter"
178178
},
179+
{
180+
"name": "DomEventsPlugin"
181+
},
179182
{
180183
"name": "DomRendererFactory2"
181184
},
@@ -290,6 +293,9 @@
290293
{
291294
"name": "ItemComponent"
292295
},
296+
{
297+
"name": "KeyEventsPlugin"
298+
},
293299
{
294300
"name": "LContainerFlags"
295301
},

0 commit comments

Comments
 (0)