Skip to content

Commit 0cc5488

Browse files
committed
fix(slider): track fill not rendering on ios safari when slider starts at 0
Fixes the track fill element not being rendered by iOS Safari if the slider starts off at zero. We work around the issue by forcing the element to be recalculated when the value goes from 0 to something else.
1 parent 0f6cd05 commit 0cc5488

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/material/slider/slider.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ describe('MatSlider', () => {
107107
expect(trackFillElement.style.transform).toContain('scale3d(0.39, 1, 1)');
108108
});
109109

110+
it('should hide the fill element at zero percent', () => {
111+
expect(trackFillElement.style.display).toBe('none');
112+
113+
dispatchMousedownEventSequence(sliderNativeElement, 0.39);
114+
fixture.detectChanges();
115+
116+
expect(trackFillElement.style.display).toBeFalsy();
117+
});
118+
110119
it('should update the track fill on slide', () => {
111120
expect(trackFillElement.style.transform).toContain('scale3d(0, 1, 1)');
112121

src/material/slider/slider.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,13 +374,19 @@ export class MatSlider extends _MatSliderMixinBase
374374

375375
/** CSS styles for the track fill element. */
376376
get _trackFillStyles(): { [key: string]: string } {
377+
const percent = this.percent;
377378
const axis = this.vertical ? 'Y' : 'X';
378-
const scale = this.vertical ? `1, ${this.percent}, 1` : `${this.percent}, 1, 1`;
379+
const scale = this.vertical ? `1, ${percent}, 1` : `${percent}, 1, 1`;
379380
const sign = this._shouldInvertMouseCoords() ? '' : '-';
380381

381382
return {
382383
// scale3d avoids some rendering issues in Chrome. See #12071.
383-
transform: `translate${axis}(${sign}${this._thumbGap}px) scale3d(${scale})`
384+
transform: `translate${axis}(${sign}${this._thumbGap}px) scale3d(${scale})`,
385+
// iOS Safari has a bug where it won't re-render elements which start of as `scale(0)` until
386+
// something forces a style recalculation on it. Since we'll end up with `scale(0)` when
387+
// the value of the slider is 0, we can end up in this situation. We force a recalculation
388+
// by changing the element's `display` when it goes from 0 to any other value.
389+
display: percent === 0 ? 'none' : ''
384390
};
385391
}
386392

0 commit comments

Comments
 (0)