Skip to content

Commit fa18ce1

Browse files
crisbetommalerba
authored andcommitted
fix(slider): track fill not rendering on ios safari when slide… (#17583)
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 b56bcad commit fa18ce1

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
@@ -373,13 +373,19 @@ export class MatSlider extends _MatSliderMixinBase
373373

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

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

0 commit comments

Comments
 (0)