-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cdk/platform): add utility for backwards-compatible event bi…
…ndings Adds a utility that we can use to bind events with options in a backwards-compatible way. It will be removed once we get to v20.
- Loading branch information
Showing
3 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
import {Renderer2, VERSION, ListenerOptions} from '@angular/core'; | ||
|
||
// TODO(crisbeto): remove this function when making breaking changes for v20. | ||
/** | ||
* Binds an event listener with specific options in a backwards-compatible way. | ||
* This function is necessary, because `Renderer2.listen` only supports listener options | ||
* after 19.1 and during the v19 period we support any 19.x version. | ||
* @docs-private | ||
*/ | ||
export function _bindEventWithOptions( | ||
renderer: Renderer2, | ||
target: EventTarget, | ||
eventName: string, | ||
callback: (event: any) => boolean | void, | ||
options: ListenerOptions, | ||
): () => void { | ||
const major = parseInt(VERSION.major); | ||
const minor = parseInt(VERSION.minor); | ||
|
||
// Event options in `listen` are only supported in 19.1 and beyond. | ||
// We also allow 0.0.x, because that indicates a build at HEAD. | ||
if (major > 19 || (major === 19 && minor > 0) || (major === 0 && minor === 0)) { | ||
return renderer.listen(target, eventName, callback, options); | ||
} | ||
|
||
target.addEventListener(eventName, callback, options); | ||
|
||
return () => { | ||
target.removeEventListener(eventName, callback, options); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters