Skip to content

Commit d7a54ef

Browse files
dozingcatkara
authored andcommitted
fix(ripple): don't create background div until ripple becomes enabled (#1849)
1 parent 607de8f commit d7a54ef

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,20 @@ export class RippleRenderer {
4545

4646
constructor(_elementRef: ElementRef, private _eventHandlers: Map<string, (e: Event) => void>) {
4747
this._rippleElement = _elementRef.nativeElement;
48-
// It might be nice to delay creating the background until it's needed, but doing this in
49-
// fadeInRippleBackground causes the first click event to not be handled reliably.
50-
this._backgroundDiv = document.createElement('div');
51-
this._backgroundDiv.classList.add('md-ripple-background');
52-
this._rippleElement.appendChild(this._backgroundDiv);
48+
// The background div is created in createBackgroundIfNeeded when the ripple becomes enabled.
49+
// This avoids creating unneeded divs when the ripple is always disabled.
50+
this._backgroundDiv = null;
51+
}
52+
53+
/**
54+
* Creates the div for the ripple background, if it doesn't already exist.
55+
*/
56+
createBackgroundIfNeeded() {
57+
if (!this._backgroundDiv) {
58+
this._backgroundDiv = document.createElement('div');
59+
this._backgroundDiv.classList.add('md-ripple-background');
60+
this._rippleElement.appendChild(this._backgroundDiv);
61+
}
5362
}
5463

5564
/**

src/lib/core/ripple/ripple.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,45 @@ describe('MdRipple', () => {
293293
expect(pxStringToFloat(ripple.style.height)).toBeCloseTo(2 * customRadius, 1);
294294
});
295295
});
296+
297+
describe('initially disabled ripple', () => {
298+
let controller: RippleContainerWithInputBindings;
299+
let rippleComponent: MdRipple;
300+
301+
beforeEach(() => {
302+
fixture = TestBed.createComponent(RippleContainerWithInputBindings);
303+
controller = fixture.debugElement.componentInstance;
304+
controller.disabled = true;
305+
fixture.detectChanges();
306+
307+
rippleComponent = controller.ripple;
308+
rippleElement = fixture.debugElement.nativeElement.querySelector('[md-ripple]');
309+
});
310+
311+
it('initially does not create background', () => {
312+
rippleBackground = rippleElement.querySelector('.md-ripple-background');
313+
expect(rippleBackground).toBeNull();
314+
});
315+
316+
it('creates background when enabled', () => {
317+
rippleBackground = rippleElement.querySelector('.md-ripple-background');
318+
expect(rippleBackground).toBeNull();
319+
320+
controller.disabled = false;
321+
fixture.detectChanges();
322+
rippleBackground = rippleElement.querySelector('.md-ripple-background');
323+
expect(rippleBackground).toBeTruthy();
324+
});
325+
326+
it('creates background when manually activated', () => {
327+
rippleBackground = rippleElement.querySelector('.md-ripple-background');
328+
expect(rippleBackground).toBeNull();
329+
330+
rippleComponent.start();
331+
rippleBackground = rippleElement.querySelector('.md-ripple-background');
332+
expect(rippleBackground).toBeTruthy();
333+
});
334+
});
296335
});
297336

298337
@Component({

src/lib/core/ripple/ripple.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ export class MdRipple implements OnInit, OnDestroy, OnChanges {
7676
if (!this.trigger) {
7777
this._rippleRenderer.setTriggerElementToHost();
7878
}
79+
if (!this.disabled) {
80+
this._rippleRenderer.createBackgroundIfNeeded();
81+
}
7982
}
8083

8184
/** TODO: internal */
@@ -91,12 +94,16 @@ export class MdRipple implements OnInit, OnDestroy, OnChanges {
9194
if (changedInputs.indexOf('trigger') !== -1) {
9295
this._rippleRenderer.setTriggerElement(this.trigger);
9396
}
97+
if (!this.disabled) {
98+
this._rippleRenderer.createBackgroundIfNeeded();
99+
}
94100
}
95101

96102
/**
97103
* Responds to the start of a ripple animation trigger by fading the background in.
98104
*/
99105
start() {
106+
this._rippleRenderer.createBackgroundIfNeeded();
100107
this._rippleRenderer.fadeInRippleBackground(this.backgroundColor);
101108
}
102109

0 commit comments

Comments
 (0)