Skip to content

Commit e9519c3

Browse files
committed
fix(material/slider): fix tick mark precision (#28193)
* fix(material/slider): fix tick mark precision * Fixes issue #26459 * Fixes a bug where the number of tick marks can be miscalculated when the step is a decimal (e.g. 1.5) * fixup! fix(material/slider): fix tick mark precision (cherry picked from commit f3b0616)
1 parent ee79340 commit e9519c3

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

src/material/slider/slider-input.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,8 @@ export class MatSliderThumb implements _MatSliderThumb, OnDestroy, ControlValueA
487487
if (this._slider._isRtl) {
488488
return (1 - this.percentage) * this._slider._cachedWidth;
489489
}
490-
return this.percentage * this._slider._cachedWidth;
490+
const tickMarkOffset = 3; // The spaces before & after the start & end tick marks.
491+
return this.percentage * (this._slider._cachedWidth - tickMarkOffset * 2) + tickMarkOffset;
491492
}
492493

493494
_calcTranslateXByPointerEvent(event: PointerEvent): number {

src/material/slider/slider.spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,16 @@ describe('MDC-based MatSlider', () => {
5959
expect(input.min).withContext('min').toBe(min);
6060
expect(input.max).withContext('max').toBe(max);
6161
expect(input.value).withContext('value').toBe(value);
62-
expect(input.translateX).withContext('translateX').toBeCloseTo(translateX, 0.1);
62+
63+
// Note: This ±6 is here to account for the slight shift of the slider
64+
// thumb caused by the tick marks being 3px away from the track start
65+
// and end.
66+
//
67+
// This check is meant to ensure the "ideal" estimate is within 3px of the
68+
// actual slider thumb position.
69+
expect(input.translateX - 6 < translateX && input.translateX + 6 > translateX)
70+
.withContext(`translateX: ${input.translateX} should be close to ${translateX}`)
71+
.toBeTrue();
6372
if (step !== undefined) {
6473
expect(input.step).withContext('step').toBe(step);
6574
}

src/material/slider/slider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -891,8 +891,8 @@ export class MatSlider
891891

892892
private _updateTickMarkUINonRange(step: number): void {
893893
const value = this._getValue();
894-
let numActive = Math.max(Math.round((value - this.min) / step), 0);
895-
let numInactive = Math.max(Math.round((this.max - value) / step), 0);
894+
let numActive = Math.max(Math.floor((value - this.min) / step), 0);
895+
let numInactive = Math.max(Math.floor((this.max - value) / step), 0);
896896
this._isRtl ? numActive++ : numInactive++;
897897

898898
this._tickMarks = Array(numActive)

0 commit comments

Comments
 (0)