Skip to content

Commit 06e6be2

Browse files
committed
perf(ripple): cache ClientRect between overlapping ripples
Avoids doing extra `getBoundingClientRect` calls when there are multiple active ripples. The cache is cleared once the last ripple is removed.
1 parent e51fc4e commit 06e6be2

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ const ignoreMouseEventsTimeout = 800;
6969
* @docs-private
7070
*/
7171
export class RippleRenderer {
72-
7372
/** Element where the ripples are being added to. */
7473
private _containerElement: HTMLElement;
7574

@@ -91,6 +90,12 @@ export class RippleRenderer {
9190
/** Options that apply to all the event listeners that are bound by the renderer. */
9291
private _eventOptions = supportsPassiveEventListeners() ? ({passive: true} as any) : false;
9392

93+
/**
94+
* Cached dimensions of the ripple container. Set when the first
95+
* ripple is shown and cleared once no more ripples are visible.
96+
*/
97+
private _containerRect: ClientRect | null;
98+
9499
constructor(private _target: RippleTarget,
95100
private _ngZone: NgZone,
96101
elementRef: ElementRef,
@@ -117,7 +122,8 @@ export class RippleRenderer {
117122
* @param config Extra ripple options.
118123
*/
119124
fadeInRipple(x: number, y: number, config: RippleConfig = {}): RippleRef {
120-
const containerRect = this._containerElement.getBoundingClientRect();
125+
const containerRect = this._containerRect =
126+
this._containerRect || this._containerElement.getBoundingClientRect();
121127
const animationConfig = {...defaultRippleAnimationConfig, ...config.animation};
122128

123129
if (config.centered) {
@@ -174,7 +180,7 @@ export class RippleRenderer {
174180
/** Fades out a ripple reference. */
175181
fadeOutRipple(rippleRef: RippleRef) {
176182
// For ripples that are not active anymore, don't re-un the fade-out animation.
177-
if (!this._activeRipples.delete(rippleRef)) {
183+
if (!this._activeRipples.has(rippleRef)) {
178184
return;
179185
}
180186

@@ -183,13 +189,18 @@ export class RippleRenderer {
183189

184190
rippleEl.style.transitionDuration = `${animationConfig.exitDuration}ms`;
185191
rippleEl.style.opacity = '0';
186-
187192
rippleRef.state = RippleState.FADING_OUT;
188193

189194
// Once the ripple faded out, the ripple can be safely removed from the DOM.
190195
this.runTimeoutOutsideZone(() => {
191196
rippleRef.state = RippleState.HIDDEN;
192197
rippleEl.parentNode!.removeChild(rippleEl);
198+
this._activeRipples.delete(rippleRef);
199+
200+
// Clear out the cached bounding rect if we have no more ripples.
201+
if (!this._activeRipples.size) {
202+
this._containerRect = null;
203+
}
193204
}, animationConfig.exitDuration);
194205
}
195206

0 commit comments

Comments
 (0)