Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ripple): don't create background div until ripple becomes enabled #1849

Merged
merged 2 commits into from
Nov 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/lib/core/ripple/ripple-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,20 @@ export class RippleRenderer {

constructor(_elementRef: ElementRef, private _eventHandlers: Map<string, (e: Event) => void>) {
this._rippleElement = _elementRef.nativeElement;
// It might be nice to delay creating the background until it's needed, but doing this in
// fadeInRippleBackground causes the first click event to not be handled reliably.
this._backgroundDiv = document.createElement('div');
this._backgroundDiv.classList.add('md-ripple-background');
this._rippleElement.appendChild(this._backgroundDiv);
// The background div is created in createBackgroundIfNeeded when the ripple becomes enabled.
// This avoids creating unneeded divs when the ripple is always disabled.
this._backgroundDiv = null;
}

/**
* Creates the div for the ripple background, if it doesn't already exist.
*/
createBackgroundIfNeeded() {
if (!this._backgroundDiv) {
this._backgroundDiv = document.createElement('div');
this._backgroundDiv.classList.add('md-ripple-background');
this._rippleElement.appendChild(this._backgroundDiv);
}
}

/**
Expand Down
39 changes: 39 additions & 0 deletions src/lib/core/ripple/ripple.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,45 @@ describe('MdRipple', () => {
expect(pxStringToFloat(ripple.style.height)).toBeCloseTo(2 * customRadius, 1);
});
});

describe('initially disabled ripple', () => {
let controller: RippleContainerWithInputBindings;
let rippleComponent: MdRipple;

beforeEach(() => {
fixture = TestBed.createComponent(RippleContainerWithInputBindings);
controller = fixture.debugElement.componentInstance;
controller.disabled = true;
fixture.detectChanges();

rippleComponent = controller.ripple;
rippleElement = fixture.debugElement.nativeElement.querySelector('[md-ripple]');
});

it('initially does not create background', () => {
rippleBackground = rippleElement.querySelector('.md-ripple-background');
expect(rippleBackground).toBeNull();
});

it('creates background when enabled', () => {
rippleBackground = rippleElement.querySelector('.md-ripple-background');
expect(rippleBackground).toBeNull();

controller.disabled = false;
fixture.detectChanges();
rippleBackground = rippleElement.querySelector('.md-ripple-background');
expect(rippleBackground).toBeTruthy();
});

it('creates background when manually activated', () => {
rippleBackground = rippleElement.querySelector('.md-ripple-background');
expect(rippleBackground).toBeNull();

rippleComponent.start();
rippleBackground = rippleElement.querySelector('.md-ripple-background');
expect(rippleBackground).toBeTruthy();
});
});
});

@Component({
Expand Down
7 changes: 7 additions & 0 deletions src/lib/core/ripple/ripple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ export class MdRipple implements OnInit, OnDestroy, OnChanges {
if (!this.trigger) {
this._rippleRenderer.setTriggerElementToHost();
}
if (!this.disabled) {
this._rippleRenderer.createBackgroundIfNeeded();
}
}

/** TODO: internal */
Expand All @@ -91,12 +94,16 @@ export class MdRipple implements OnInit, OnDestroy, OnChanges {
if (changedInputs.indexOf('trigger') !== -1) {
this._rippleRenderer.setTriggerElement(this.trigger);
}
if (!this.disabled) {
this._rippleRenderer.createBackgroundIfNeeded();
}
}

/**
* Responds to the start of a ripple animation trigger by fading the background in.
*/
start() {
this._rippleRenderer.createBackgroundIfNeeded();
this._rippleRenderer.fadeInRippleBackground(this.backgroundColor);
}

Expand Down