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

feat(ripple): expose ripple directive in template #3165

Merged
merged 1 commit into from
Feb 21, 2017
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
28 changes: 22 additions & 6 deletions src/lib/core/ripple/ripple.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ describe('MdRipple', () => {
}

describe('basic ripple', () => {
let rippleDirective: MdRipple;

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

rippleTarget = fixture.debugElement.nativeElement.querySelector('[mat-ripple]');
rippleTarget = fixture.nativeElement.querySelector('[mat-ripple]');
rippleDirective = fixture.componentInstance.ripple;
});

it('creates ripple on mousedown', () => {
Expand Down Expand Up @@ -111,15 +113,29 @@ describe('MdRipple', () => {
}));

it('creates ripples when manually triggered', () => {
let rippleComponent = fixture.debugElement.componentInstance.ripple as MdRipple;

expect(rippleTarget.querySelectorAll('.mat-ripple-element').length).toBe(0);

rippleComponent.launch(0, 0);
rippleDirective.launch(0, 0);

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

it('creates manual ripples with the default ripple config', () => {
expect(rippleTarget.querySelectorAll('.mat-ripple-element').length).toBe(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: might be fractionally faster to do expect(rippleTarget.querySelector('.mat-ripple-element')).toBeFalsy();.

Copy link
Member Author

@devversion devversion Feb 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it might be fractionally faster, but I think when checking for ripple elements we want to explicitly check for an amount. Therefore the falsy check should be consistent / similar here.


// Calculate the diagonal distance and divide it by two for the center radius.
let radius = Math.sqrt(TARGET_HEIGHT * TARGET_HEIGHT + TARGET_WIDTH * TARGET_WIDTH) / 2;

rippleDirective.centered = true;
rippleDirective.launch(0, 0);

let rippleElement = rippleTarget.querySelector('.mat-ripple-element') as HTMLElement;

expect(rippleElement).toBeTruthy();
expect(parseFloat(rippleElement.style.left)).toBeCloseTo(TARGET_WIDTH / 2 - radius, 1);
expect(parseFloat(rippleElement.style.top)).toBeCloseTo(TARGET_HEIGHT / 2 - radius, 1);
});

it('sizes ripple to cover element', () => {
let elementRect = rippleTarget.getBoundingClientRect();

Expand Down Expand Up @@ -382,13 +398,13 @@ describe('MdRipple', () => {

@Component({
template: `
<div id="container" mat-ripple [mdRippleSpeedFactor]="0"
<div id="container" #ripple="mdRipple" mat-ripple [mdRippleSpeedFactor]="0"
style="position: relative; width:300px; height:200px;">
</div>
`,
})
class BasicRippleContainer {
@ViewChild(MdRipple) ripple: MdRipple;
@ViewChild('ripple') ripple: MdRipple;
}

@Component({
Expand Down
11 changes: 6 additions & 5 deletions src/lib/core/ripple/ripple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {SCROLL_DISPATCHER_PROVIDER} from '../overlay/scroll/scroll-dispatcher';

@Directive({
selector: '[md-ripple], [mat-ripple]',
exportAs: 'mdRipple',
host: {
'[class.mat-ripple]': 'true',
'[class.mat-ripple-unbounded]': 'unbounded'
Expand Down Expand Up @@ -77,7 +78,7 @@ export class MdRipple implements OnChanges, OnDestroy {
}

this._rippleRenderer.rippleDisabled = this.disabled;
this._updateRippleConfig();
this._rippleRenderer.rippleConfig = this.rippleConfig;
}

ngOnDestroy() {
Expand All @@ -86,13 +87,13 @@ export class MdRipple implements OnChanges, OnDestroy {
}

/** Launches a manual ripple at the specified position. */
launch(pageX: number, pageY: number, config?: RippleConfig) {
launch(pageX: number, pageY: number, config = this.rippleConfig) {
this._rippleRenderer.fadeInRipple(pageX, pageY, config);
}

/** Updates the ripple configuration with the input values. */
private _updateRippleConfig() {
this._rippleRenderer.rippleConfig = {
/** Ripple configuration from the directive's input values. */
get rippleConfig(): RippleConfig {
return {
centered: this.centered,
speedFactor: this.speedFactor,
radius: this.radius,
Expand Down