Skip to content

Commit 6595ad8

Browse files
devversionmmalerba
authored andcommitted
feat(ripple): expose ripple directive in template (#3165)
* When programmatically calling `launch` on the ripple element the developer can specifiy a configuration object. Since the `launch` method is part of the directive and all the associated `@Input`s. The given configuration should be used as default. * Exposes the ripple component instance to the Angular template.
1 parent b939cd8 commit 6595ad8

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ describe('MdRipple', () => {
7575
}
7676

7777
describe('basic ripple', () => {
78+
let rippleDirective: MdRipple;
7879

7980
const TARGET_HEIGHT = 200;
8081
const TARGET_WIDTH = 300;
@@ -83,7 +84,8 @@ describe('MdRipple', () => {
8384
fixture = TestBed.createComponent(BasicRippleContainer);
8485
fixture.detectChanges();
8586

86-
rippleTarget = fixture.debugElement.nativeElement.querySelector('[mat-ripple]');
87+
rippleTarget = fixture.nativeElement.querySelector('[mat-ripple]');
88+
rippleDirective = fixture.componentInstance.ripple;
8789
});
8890

8991
it('creates ripple on mousedown', () => {
@@ -111,15 +113,29 @@ describe('MdRipple', () => {
111113
}));
112114

113115
it('creates ripples when manually triggered', () => {
114-
let rippleComponent = fixture.debugElement.componentInstance.ripple as MdRipple;
115-
116116
expect(rippleTarget.querySelectorAll('.mat-ripple-element').length).toBe(0);
117117

118-
rippleComponent.launch(0, 0);
118+
rippleDirective.launch(0, 0);
119119

120120
expect(rippleTarget.querySelectorAll('.mat-ripple-element').length).toBe(1);
121121
});
122122

123+
it('creates manual ripples with the default ripple config', () => {
124+
expect(rippleTarget.querySelectorAll('.mat-ripple-element').length).toBe(0);
125+
126+
// Calculate the diagonal distance and divide it by two for the center radius.
127+
let radius = Math.sqrt(TARGET_HEIGHT * TARGET_HEIGHT + TARGET_WIDTH * TARGET_WIDTH) / 2;
128+
129+
rippleDirective.centered = true;
130+
rippleDirective.launch(0, 0);
131+
132+
let rippleElement = rippleTarget.querySelector('.mat-ripple-element') as HTMLElement;
133+
134+
expect(rippleElement).toBeTruthy();
135+
expect(parseFloat(rippleElement.style.left)).toBeCloseTo(TARGET_WIDTH / 2 - radius, 1);
136+
expect(parseFloat(rippleElement.style.top)).toBeCloseTo(TARGET_HEIGHT / 2 - radius, 1);
137+
});
138+
123139
it('sizes ripple to cover element', () => {
124140
let elementRect = rippleTarget.getBoundingClientRect();
125141

@@ -382,13 +398,13 @@ describe('MdRipple', () => {
382398

383399
@Component({
384400
template: `
385-
<div id="container" mat-ripple [mdRippleSpeedFactor]="0"
401+
<div id="container" #ripple="mdRipple" mat-ripple [mdRippleSpeedFactor]="0"
386402
style="position: relative; width:300px; height:200px;">
387403
</div>
388404
`,
389405
})
390406
class BasicRippleContainer {
391-
@ViewChild(MdRipple) ripple: MdRipple;
407+
@ViewChild('ripple') ripple: MdRipple;
392408
}
393409

394410
@Component({

src/lib/core/ripple/ripple.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {SCROLL_DISPATCHER_PROVIDER} from '../overlay/scroll/scroll-dispatcher';
1717

1818
@Directive({
1919
selector: '[md-ripple], [mat-ripple]',
20+
exportAs: 'mdRipple',
2021
host: {
2122
'[class.mat-ripple]': 'true',
2223
'[class.mat-ripple-unbounded]': 'unbounded'
@@ -77,7 +78,7 @@ export class MdRipple implements OnChanges, OnDestroy {
7778
}
7879

7980
this._rippleRenderer.rippleDisabled = this.disabled;
80-
this._updateRippleConfig();
81+
this._rippleRenderer.rippleConfig = this.rippleConfig;
8182
}
8283

8384
ngOnDestroy() {
@@ -86,13 +87,13 @@ export class MdRipple implements OnChanges, OnDestroy {
8687
}
8788

8889
/** Launches a manual ripple at the specified position. */
89-
launch(pageX: number, pageY: number, config?: RippleConfig) {
90+
launch(pageX: number, pageY: number, config = this.rippleConfig) {
9091
this._rippleRenderer.fadeInRipple(pageX, pageY, config);
9192
}
9293

93-
/** Updates the ripple configuration with the input values. */
94-
private _updateRippleConfig() {
95-
this._rippleRenderer.rippleConfig = {
94+
/** Ripple configuration from the directive's input values. */
95+
get rippleConfig(): RippleConfig {
96+
return {
9697
centered: this.centered,
9798
speedFactor: this.speedFactor,
9899
radius: this.radius,

0 commit comments

Comments
 (0)