Skip to content

Commit be4c912

Browse files
committed
chore: use computed radius size for checkbox ripple during 2018 material spec update transition
1 parent 7c4fa66 commit be4c912

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/lib/checkbox/checkbox.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,7 @@ describe('MatCheckbox', () => {
794794
it('should properly detect native tabindex attribute', fakeAsync(() => {
795795
fixture = createComponent(CheckboxWithTabindexAttr);
796796
fixture.detectChanges();
797+
flush();
797798

798799
const checkbox = fixture.debugElement
799800
.query(By.directive(MatCheckbox)).componentInstance as MatCheckbox;

src/lib/checkbox/checkbox.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ import {MAT_CHECKBOX_CLICK_ACTION, MatCheckboxClickAction} from './checkbox-conf
5050
// Increasing integer for generating unique ids for checkbox components.
5151
let nextUniqueId = 0;
5252

53+
// The radius for the checkbox's ripple, in pixels.
54+
let calculatedRippleRadius = 0;
55+
5356
/**
5457
* Provider Expression that allows mat-checkbox to register as a ControlValueAccessor.
5558
* This allows it to support [(ngModel)].
@@ -208,6 +211,7 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
208211
this._focusMonitor
209212
.monitor(this._inputElement)
210213
.subscribe(focusOrigin => this._onInputFocusChange(focusOrigin));
214+
this._calculateRippleRadius();
211215
}
212216

213217
ngOnDestroy() {
@@ -457,4 +461,20 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
457461

458462
return `mat-checkbox-anim-${animSuffix}`;
459463
}
464+
465+
/**
466+
* Calculate the radius for the ripple based on the ripple elements width. Only calculated once
467+
* for the application.
468+
*/
469+
private _calculateRippleRadius() {
470+
// setTimeout is used to ensure we delay until the checkbox's ripple is placed in the DOM.
471+
setTimeout(() => {
472+
if (!calculatedRippleRadius) {
473+
const rippleWidth = parseInt(getComputedStyle(
474+
this._elementRef.nativeElement.querySelector('.mat-checkbox-ripple')).width!, 10) || 0;
475+
calculatedRippleRadius = rippleWidth / 2;
476+
}
477+
this.ripple.radius = calculatedRippleRadius;
478+
});
479+
}
460480
}

0 commit comments

Comments
 (0)