Skip to content

Commit eae0730

Browse files
committedJan 16, 2025·
fix(material/slider): tick mark positioning (#30329)
(cherry picked from commit b5076f7)
1 parent 0df6e03 commit eae0730

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed
 

‎src/material/slider/slider.spec.ts

+31-5
Original file line numberDiff line numberDiff line change
@@ -1119,9 +1119,11 @@ describe('MatSlider', () => {
11191119
describe('slider with direction', () => {
11201120
let slider: MatSlider;
11211121
let input: MatSliderThumb;
1122+
let sliderEl: HTMLElement;
1123+
let fixture: ComponentFixture<StandardRangeSlider>;
11221124

11231125
beforeEach(waitForAsync(() => {
1124-
const fixture = createComponent(StandardSlider, [
1126+
fixture = createComponent(StandardSlider, [
11251127
{
11261128
provide: Directionality,
11271129
useValue: {value: 'rtl', change: of()},
@@ -1130,13 +1132,31 @@ describe('MatSlider', () => {
11301132
fixture.detectChanges();
11311133
const sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider));
11321134
slider = sliderDebugElement.componentInstance;
1135+
sliderEl = sliderDebugElement.nativeElement;
11331136
input = slider._getInput(_MatThumb.END) as MatSliderThumb;
11341137
}));
11351138

11361139
it('works in RTL languages', fakeAsync(() => {
11371140
setValueByClick(slider, input, 25, true);
11381141
checkInput(input, {min: 0, max: 100, value: 75, translateX: 75});
11391142
}));
1143+
1144+
it('should position the tick marks correctly with a misaligned step (rtl)', () => {
1145+
slider.showTickMarks = true;
1146+
slider.min = 0;
1147+
slider.max = 10;
1148+
slider.step = 9;
1149+
1150+
fixture.detectChanges();
1151+
1152+
const activeClass = '.mdc-slider__tick-mark--active';
1153+
const inactiveClass = '.mdc-slider__tick-mark--inactive';
1154+
const ticks = sliderEl.querySelectorAll(`${activeClass},${inactiveClass}`);
1155+
1156+
expect(ticks.length).toBe(2);
1157+
expect(ticks[0].getBoundingClientRect().x).toBe(312);
1158+
expect(ticks[1].getBoundingClientRect().x).toBeCloseTo(47.4, 2);
1159+
});
11401160
});
11411161

11421162
describe('range slider with direction', () => {
@@ -1622,11 +1642,17 @@ describe('MatSlider', () => {
16221642
expect(tickEls.inactive.length).toBe(0);
16231643
});
16241644

1625-
// TODO(wagnermaciel): Add this test once this is fixed.
1626-
// it('should position the tick marks correctly with a misaligned step', () => {});
1645+
it('should position the tick marks correctly with a misaligned step', () => {
1646+
slider.max = 10;
1647+
slider.step = 9;
1648+
fixture.detectChanges();
1649+
1650+
const {ticks} = getTickMarkEls();
1651+
expect(ticks.length).toBe(2);
16271652

1628-
// TODO(wagnermaciel): Add this test once this is fixed.
1629-
// it('should position the tick marks correctly with a misaligned step (rtl)', () => {});
1653+
expect(ticks[0].getBoundingClientRect().x).toBe(18);
1654+
expect(ticks[1].getBoundingClientRect().x).toBeCloseTo(282.6, 2);
1655+
});
16301656
});
16311657

16321658
describe('range slider with tick marks', () => {

‎src/material/slider/slider.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,8 @@ export class MatSlider implements AfterViewInit, OnDestroy, _MatSlider {
590590
/** Returns the translateX positioning for a tick mark based on it's index. */
591591
_calcTickMarkTransform(index: number): string {
592592
// TODO(wagnermaciel): See if we can avoid doing this and just using flex to position these.
593-
const translateX = index * (this._tickMarkTrackWidth / (this._tickMarks.length - 1));
593+
const offset = index * (this._tickMarkTrackWidth / (this._tickMarks.length - 1));
594+
const translateX = this._isRtl ? this._cachedWidth - 6 - offset : offset;
594595
return `translateX(${translateX}px`;
595596
}
596597

@@ -788,7 +789,7 @@ export class MatSlider implements AfterViewInit, OnDestroy, _MatSlider {
788789
const step = this._step && this._step > 0 ? this._step : 1;
789790
const maxValue = Math.floor(this.max / step) * step;
790791
const percentage = (maxValue - this.min) / (this.max - this.min);
791-
this._tickMarkTrackWidth = this._cachedWidth * percentage - 6;
792+
this._tickMarkTrackWidth = (this._cachedWidth - 6) * percentage;
792793
}
793794

794795
// Track active update conditions
@@ -877,10 +878,6 @@ export class MatSlider implements AfterViewInit, OnDestroy, _MatSlider {
877878
}
878879
const step = this.step > 0 ? this.step : 1;
879880
this._isRange ? this._updateTickMarkUIRange(step) : this._updateTickMarkUINonRange(step);
880-
881-
if (this._isRtl) {
882-
this._tickMarks.reverse();
883-
}
884881
}
885882

886883
private _updateTickMarkUINonRange(step: number): void {

0 commit comments

Comments
 (0)
Please sign in to comment.