Skip to content

Commit c841289

Browse files
crisbetojelbourn
authored andcommitted
refactor(core): allow ElementRef in RippleRenderer.setupTriggerEvents (#16953)
The way the `RippleRenderer` is set up at the moment is inconsistent from an API standpoint, because the constructor only accepts an `ElementRef`, whereas `setupTriggerEvents` only accepts an `HTMLElement`, and in most cases they're referring to the same DOM node. These changes make the two code paths consistent with each other by allowing `HTMLElement | ElementRef<HTMLElement>` in both of them.
1 parent bfa1853 commit c841289

File tree

5 files changed

+11
-8
lines changed

5 files changed

+11
-8
lines changed

src/material-experimental/mdc-chips/chip.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
380380

381381
this._rippleRenderer =
382382
new RippleRenderer(this, this._ngZone, this._elementRef, this._platform);
383-
this._rippleRenderer.setupTriggerEvents(this._elementRef.nativeElement);
383+
this._rippleRenderer.setupTriggerEvents(this._elementRef);
384384
}
385385

386386
/** Forwards interaction events to the MDC chip foundation. */

src/material/chips/chip.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
244244
this._addHostClassName();
245245

246246
this._chipRipple = new RippleRenderer(this, _ngZone, _elementRef, platform);
247-
this._chipRipple.setupTriggerEvents(_elementRef.nativeElement);
247+
this._chipRipple.setupTriggerEvents(_elementRef);
248248
this.rippleConfig = globalRippleOptions || {};
249249
this._animationsDisabled = animationMode === 'NoopAnimations';
250250
}

src/material/core/ripple/ripple-renderer.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import {ElementRef, NgZone} from '@angular/core';
99
import {Platform, normalizePassiveListenerOptions} from '@angular/cdk/platform';
1010
import {isFakeMousedownFromScreenReader} from '@angular/cdk/a11y';
11+
import {coerceElement} from '@angular/cdk/coercion';
1112
import {RippleRef, RippleState} from './ripple-ref';
1213

1314
export type RippleConfig = {
@@ -97,12 +98,12 @@ export class RippleRenderer {
9798

9899
constructor(private _target: RippleTarget,
99100
private _ngZone: NgZone,
100-
elementRef: ElementRef<HTMLElement>,
101+
elementOrElementRef: HTMLElement | ElementRef<HTMLElement>,
101102
platform: Platform) {
102103

103104
// Only do anything if we're on the browser.
104105
if (platform.isBrowser) {
105-
this._containerElement = elementRef.nativeElement;
106+
this._containerElement = coerceElement(elementOrElementRef);
106107

107108
// Specify events which need to be registered on the trigger.
108109
this._triggerEvents
@@ -226,7 +227,9 @@ export class RippleRenderer {
226227
}
227228

228229
/** Sets up the trigger event listeners */
229-
setupTriggerEvents(element: HTMLElement) {
230+
setupTriggerEvents(elementOrElementRef: HTMLElement | ElementRef<HTMLElement>) {
231+
const element = coerceElement(elementOrElementRef);
232+
230233
if (!element || element === this._triggerElement) {
231234
return;
232235
}

src/material/tabs/tab-nav-bar/tab-nav-bar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ export class MatTabLink extends _MatTabLinkMixinBase implements OnDestroy, CanDi
226226
super();
227227

228228
this._tabLinkRipple = new RippleRenderer(this, ngZone, elementRef, platform);
229-
this._tabLinkRipple.setupTriggerEvents(elementRef.nativeElement);
229+
this._tabLinkRipple.setupTriggerEvents(elementRef);
230230
this.rippleConfig = globalRippleOptions || {};
231231
this.tabIndex = parseInt(tabIndex) || 0;
232232

tools/public_api_guard/material/core.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,12 @@ export declare class RippleRef {
392392
}
393393

394394
export declare class RippleRenderer {
395-
constructor(_target: RippleTarget, _ngZone: NgZone, elementRef: ElementRef<HTMLElement>, platform: Platform);
395+
constructor(_target: RippleTarget, _ngZone: NgZone, elementOrElementRef: HTMLElement | ElementRef<HTMLElement>, platform: Platform);
396396
_removeTriggerEvents(): void;
397397
fadeInRipple(x: number, y: number, config?: RippleConfig): RippleRef;
398398
fadeOutAll(): void;
399399
fadeOutRipple(rippleRef: RippleRef): void;
400-
setupTriggerEvents(element: HTMLElement): void;
400+
setupTriggerEvents(elementOrElementRef: HTMLElement | ElementRef<HTMLElement>): void;
401401
}
402402

403403
export declare enum RippleState {

0 commit comments

Comments
 (0)