Skip to content

Commit ef4c3c9

Browse files
tinayuangaojelbourn
authored andcommitted
feat(checkbox): add ripple (#1611)
1 parent 3c79855 commit ef4c3c9

File tree

5 files changed

+53
-8
lines changed

5 files changed

+53
-8
lines changed

src/lib/checkbox/_checkbox-theme.scss

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,16 @@
6868
}
6969
}
7070

71-
// TODO(jelbourn): remove style for temporary ripple once the real ripple is applied.
7271
.md-checkbox-focused {
73-
&.md-primary .md-ink-ripple {
72+
&.md-primary .md-checkbox-ripple .md-ripple-foreground {
7473
background-color: md-color($primary, 0.26);
7574
}
7675

77-
&.md-accent .md-ink-ripple {
76+
&.md-accent .md-checkbox-ripple .md-ripple-foreground {
7877
background-color: md-color($accent, 0.26);
7978
}
8079

81-
&.md-warn .md-ink-ripple {
80+
&.md-warn .md-checkbox-ripple .md-ripple-foreground {
8281
background-color: md-color($warn, 0.26);
8382
}
8483
}

src/lib/checkbox/checkbox.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
(blur)="_onInputBlur()"
1515
(change)="_onInteractionEvent($event)"
1616
(click)="_onInputClick($event)">
17-
<div class="md-ink-ripple"></div>
17+
<div md-ripple *ngIf="!disableRipple" class="md-checkbox-ripple"
18+
[md-ripple-trigger]="getHostElement()"
19+
[md-ripple-centered]="true"
20+
[md-ripple-speed-factor]="0.3"
21+
md-ripple-background-color="rgba(0, 0, 0, 0)"></div>
1822
<div class="md-checkbox-frame"></div>
1923
<div class="md-checkbox-background">
2024
<svg version="1.1"

src/lib/checkbox/checkbox.scss

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ $md-checkbox-item-spacing: $md-toggle-padding;
1919
$_md-checkbox-mark-path-length: 22.910259;
2020
$_md-checkbox-indeterminate-checked-easing-function: cubic-bezier(0.14, 0, 0, 1);
2121

22+
// The ripple size of the checkbox
23+
$md-checkbox-ripple-size: 15px;
24+
2225
// Fades in the background of the checkbox when it goes from unchecked -> {checked,indeterminate}.
2326
@keyframes md-checkbox-fade-in-background {
2427
0% {
@@ -414,4 +417,12 @@ md-checkbox {
414417
left: 50%;
415418
}
416419

417-
@include md-temporary-ink-ripple(checkbox);
420+
.md-checkbox-ripple {
421+
position: absolute;
422+
left: -$md-checkbox-ripple-size;
423+
top: -$md-checkbox-ripple-size;
424+
right: -$md-checkbox-ripple-size;
425+
bottom: -$md-checkbox-ripple-size;
426+
border-radius: 50%;
427+
z-index: 1;
428+
}

src/lib/checkbox/checkbox.spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,20 @@ describe('MdCheckbox', () => {
483483

484484
expect(inputElement.tabIndex).toBe(13);
485485
});
486+
487+
it('should remove ripple if md-ripple-disabled input is set', async(() => {
488+
testComponent.disableRipple = true;
489+
fixture.detectChanges();
490+
491+
expect(checkboxNativeElement.querySelectorAll('[md-ripple]').length)
492+
.toBe(0, 'Expect no [md-ripple] in checkbox');
493+
494+
testComponent.disableRipple = false;
495+
fixture.detectChanges();
496+
497+
expect(checkboxNativeElement.querySelectorAll('[md-ripple]').length)
498+
.toBe(1, 'Expect [md-ripple] in checkbox');
499+
}));
486500
});
487501

488502
describe('with multiple checkboxes', () => {
@@ -598,12 +612,16 @@ class MultipleCheckboxes { }
598612
/** Simple test component with tabIndex */
599613
@Component({
600614
template: `
601-
<md-checkbox [tabindex]="customTabIndex" [disabled]="isDisabled">
615+
<md-checkbox
616+
[tabindex]="customTabIndex"
617+
[disabled]="isDisabled"
618+
[disableRipple]="disableRipple">
602619
</md-checkbox>`,
603620
})
604621
class CheckboxWithTabIndex {
605622
customTabIndex: number = 7;
606623
isDisabled: boolean = false;
624+
disableRipple: boolean = false;
607625
}
608626

609627
/** Simple test component with an aria-label set. */

src/lib/checkbox/checkbox.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import {
1111
NgModule,
1212
ModuleWithProviders,
1313
} from '@angular/core';
14+
import {CommonModule} from '@angular/common';
1415
import {NG_VALUE_ACCESSOR, ControlValueAccessor} from '@angular/forms';
1516
import {coerceBooleanProperty} from '../core/coersion/boolean-property';
16-
17+
import {MdRippleModule} from '../core';
1718

1819
/**
1920
* Monotonically increasing integer used to auto-generate unique ids for checkbox components.
@@ -89,6 +90,13 @@ export class MdCheckbox implements ControlValueAccessor {
8990
/** A unique id for the checkbox. If one is not supplied, it is auto-generated. */
9091
@Input() id: string = `md-checkbox-${++nextId}`;
9192

93+
/** Whether the ripple effect on click should be disabled. */
94+
private _disableRipple: boolean;
95+
96+
@Input()
97+
get disableRipple(): boolean { return this._disableRipple; }
98+
set disableRipple(value) { this._disableRipple = coerceBooleanProperty(value); }
99+
92100
/** ID to be applied to the `input` element */
93101
get inputId(): string {
94102
return `input-${this.id}`;
@@ -338,10 +346,15 @@ export class MdCheckbox implements ControlValueAccessor {
338346

339347
return `md-checkbox-anim-${animSuffix}`;
340348
}
349+
350+
getHostElement() {
351+
return this._elementRef.nativeElement;
352+
}
341353
}
342354

343355

344356
@NgModule({
357+
imports: [CommonModule, MdRippleModule],
345358
exports: [MdCheckbox],
346359
declarations: [MdCheckbox],
347360
})

0 commit comments

Comments
 (0)