Skip to content

Commit 5c5f3b6

Browse files
crisbetojelbourn
authored andcommitted
perf(ripple): cache ClientRect between overlapping ripples (#10206)
Avoids doing extra `getBoundingClientRect` calls when there are multiple active ripples. The cache is cleared once the last ripple is removed.
1 parent ccf7a4d commit 5c5f3b6

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

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

Lines changed: 16 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) {
@@ -173,8 +179,15 @@ export class RippleRenderer {
173179

174180
/** Fades out a ripple reference. */
175181
fadeOutRipple(rippleRef: RippleRef) {
182+
const wasActive = this._activeRipples.delete(rippleRef);
183+
184+
// Clear out the cached bounding rect if we have no more ripples.
185+
if (!this._activeRipples.size) {
186+
this._containerRect = null;
187+
}
188+
176189
// For ripples that are not active anymore, don't re-un the fade-out animation.
177-
if (!this._activeRipples.delete(rippleRef)) {
190+
if (!wasActive) {
178191
return;
179192
}
180193

@@ -183,7 +196,6 @@ export class RippleRenderer {
183196

184197
rippleEl.style.transitionDuration = `${animationConfig.exitDuration}ms`;
185198
rippleEl.style.opacity = '0';
186-
187199
rippleRef.state = RippleState.FADING_OUT;
188200

189201
// Once the ripple faded out, the ripple can be safely removed from the DOM.

0 commit comments

Comments
 (0)