Skip to content

Commit d93e16f

Browse files
authored
fix(material/slider): some screen readers announcing long decimal values (#20721)
It looks like some screen readers announce the value of a slider by calculating the percentage themselves using the `aria-valuemin`, `aria-valuemax` and `aria-valuenow`. The problem is that they don't round down the decimals so for a slider between 0 and 1 with a step of 0.1, they end up reading out values like 0.20000068. These changes work around the issue by setting `aria-valuetext` to the same value that we shown in the thumb which we truncate ourselves. Fixes #20719.
1 parent 1c83423 commit d93e16f

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/material/slider/slider.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,16 @@ describe('MatSlider', () => {
507507
expect(sliderInstance.value).toBe(0.3);
508508
});
509509

510+
it('should set the truncated value to the aria-valuetext', () => {
511+
fixture.componentInstance.step = 0.1;
512+
fixture.detectChanges();
513+
514+
dispatchSlideEventSequence(sliderNativeElement, 0, 0.333333);
515+
fixture.detectChanges();
516+
517+
expect(sliderNativeElement.getAttribute('aria-valuetext')).toBe('33');
518+
});
519+
510520
});
511521

512522
describe('slider with auto ticks', () => {

src/material/slider/slider.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ const _MatSliderMixinBase:
134134
'[attr.aria-valuemax]': 'max',
135135
'[attr.aria-valuemin]': 'min',
136136
'[attr.aria-valuenow]': 'value',
137+
138+
// NVDA and Jaws appear to announce the `aria-valuenow` by calculating its percentage based
139+
// on its value between `aria-valuemin` and `aria-valuemax`. Due to how decimals are handled,
140+
// it can cause the slider to read out a very long value like 0.20000068 if the current value
141+
// is 0.2 with a min of 0 and max of 1. We work around the issue by setting `aria-valuetext`
142+
// to the same value that we set on the slider's thumb which will be truncated.
143+
'[attr.aria-valuetext]': 'displayValue',
137144
'[attr.aria-orientation]': 'vertical ? "vertical" : "horizontal"',
138145
'[class.mat-slider-disabled]': 'disabled',
139146
'[class.mat-slider-has-ticks]': 'tickInterval',

0 commit comments

Comments
 (0)