Skip to content

Commit 2bc20bc

Browse files
committed
feat(ripple): option to specify global ripple options
* Adds a new DI token that can be used to specify global ripple options. * The `RippleRef` options are not related to the `MD_DISABLE_RIPPLES` DI token (for API convenience) Closes angular#3454
1 parent 8b2ae0d commit 2bc20bc

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

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

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import {TestBed, ComponentFixture, fakeAsync, tick, inject} from '@angular/core/
22
import {Component, ViewChild} from '@angular/core';
33
import {MdRipple, MdRippleModule, MD_DISABLE_RIPPLES, RippleState} from './index';
44
import {ViewportRuler} from '../overlay/position/viewport-ruler';
5-
import {RIPPLE_FADE_OUT_DURATION, RIPPLE_FADE_IN_DURATION} from './ripple-renderer';
5+
import {RIPPLE_FADE_OUT_DURATION, RIPPLE_FADE_IN_DURATION, RippleConfig} from './ripple-renderer';
66
import {dispatchMouseEvent} from '../testing/dispatch-events';
7+
import {MD_RIPPLES_CONFIG} from './ripple';
78

89
/** Extracts the numeric value of a pixel size string like '123px'. */
910
const pxStringToFloat = (s: string) => {
@@ -390,6 +391,54 @@ describe('MdRipple', () => {
390391

391392
});
392393

394+
describe('with a global config', () => {
395+
let rippleDirective: MdRipple;
396+
397+
function createTestComponent(rippleConfig: RippleConfig) {
398+
// Reset the previously configured testing module to be able set new providers.
399+
// The testing module has been initialized in the root describe group for the ripples.
400+
TestBed.resetTestingModule();
401+
TestBed.configureTestingModule({
402+
imports: [MdRippleModule],
403+
declarations: [BasicRippleContainer],
404+
providers: [{ provide: MD_RIPPLES_CONFIG, useValue: rippleConfig }]
405+
});
406+
407+
fixture = TestBed.createComponent(BasicRippleContainer);
408+
fixture.detectChanges();
409+
410+
rippleTarget = fixture.nativeElement.querySelector('[mat-ripple]');
411+
rippleDirective = fixture.componentInstance.ripple;
412+
}
413+
414+
it('should be able to overwrite the speedFactor', fakeAsync(() => {
415+
createTestComponent({ speedFactor: 0.5 });
416+
417+
dispatchMouseEvent(rippleTarget, 'mousedown');
418+
dispatchMouseEvent(rippleTarget, 'mouseup');
419+
420+
expect(rippleTarget.querySelectorAll('.mat-ripple-element').length).toBe(1);
421+
422+
// Calculates the duration for fading-in and fading-out the ripple.
423+
tick(RIPPLE_FADE_IN_DURATION * 2 + RIPPLE_FADE_OUT_DURATION);
424+
425+
expect(rippleTarget.querySelectorAll('.mat-ripple-element').length).toBe(0);
426+
}));
427+
428+
it('should be able to overwrite the color', () => {
429+
createTestComponent({ color: 'red' });
430+
431+
dispatchMouseEvent(rippleTarget, 'mousedown');
432+
dispatchMouseEvent(rippleTarget, 'mouseup');
433+
434+
let rippleElement = rippleTarget.querySelector('.mat-ripple-element') as HTMLElement;
435+
436+
expect(rippleElement).toBeTruthy();
437+
expect(rippleElement.style.backgroundColor).toBe('red');
438+
});
439+
440+
});
441+
393442
describe('configuring behavior', () => {
394443
let controller: RippleContainerWithInputBindings;
395444
let rippleComponent: MdRipple;

src/lib/core/ripple/ripple.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ import {
1313
import {RippleConfig, RippleRenderer} from './ripple-renderer';
1414
import {ViewportRuler} from '../overlay/position/viewport-ruler';
1515
import {RippleRef} from './ripple-ref';
16+
import {extendObject} from '../util/object-extend';
1617

1718
/** OpaqueToken that can be used to globally disable all ripples. Except programmatic ones. */
1819
export const MD_DISABLE_RIPPLES = new OpaqueToken('md-disable-ripples');
1920

21+
/** OpaqueToken that can be used to specify a global ripple configuration. */
22+
export const MD_RIPPLES_CONFIG = new OpaqueToken('md-ripples-config');
23+
2024
@Directive({
2125
selector: '[md-ripple], [mat-ripple]',
2226
exportAs: 'mdRipple',
@@ -71,7 +75,8 @@ export class MdRipple implements OnChanges, OnDestroy {
7175
private _rippleRenderer: RippleRenderer;
7276

7377
constructor(elementRef: ElementRef, ngZone: NgZone, ruler: ViewportRuler,
74-
@Optional() @Inject(MD_DISABLE_RIPPLES) private _forceDisableRipples: boolean) {
78+
@Optional() @Inject(MD_DISABLE_RIPPLES) private _forceDisableRipples: boolean,
79+
@Optional() @Inject(MD_RIPPLES_CONFIG) private _globalRippleConfig: RippleConfig) {
7580

7681
this._rippleRenderer = new RippleRenderer(elementRef, ngZone, ruler);
7782
}
@@ -102,11 +107,11 @@ export class MdRipple implements OnChanges, OnDestroy {
102107

103108
/** Ripple configuration from the directive's input values. */
104109
get rippleConfig(): RippleConfig {
105-
return {
110+
return extendObject({
106111
centered: this.centered,
107112
speedFactor: this.speedFactor,
108113
radius: this.radius,
109114
color: this.color
110-
};
115+
}, this._globalRippleConfig);
111116
}
112117
}

0 commit comments

Comments
 (0)