Skip to content

Commit ee157d5

Browse files
Jason GillValentinH
Jason Gill
authored andcommitted
fix(options): add labelling options for a11y
Provides the option to label slider(s) for screen readers using either a direct label via `aria-label` or as a reference using `aria-labelledby`. fixes issue #492
1 parent 3ba812d commit ee157d5

File tree

8 files changed

+143
-12
lines changed

8 files changed

+143
-12
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,11 @@ The default options are:
249249
logScale: false,
250250
customValueToPosition: null,
251251
customPositionToValue: null,
252-
selectionBarGradient: null
252+
selectionBarGradient: null,
253+
ariaLabel: null,
254+
ariaLabelledBy: null,
255+
ariaLabelHigh: null,
256+
ariaLabelledByHigh: null
253257
}
254258
````
255259

@@ -404,6 +408,10 @@ For custom scales:
404408

405409
**selectionBarGradient** - _Object (default to null)_: Use to display the selection bar as a gradient. The given object must contain `from` and `to` properties which are colors.
406410

411+
**ariaLabel and ariaLabelHigh** - _String (default to null)_: Use to add a label directly to the slider(s) for accessibility. Adds the `aria-label` attribute.
412+
413+
**ariaLabelledBy and ariaLabelledByHigh** - _String (default to null)_: Use instead of ariaLabel and ariaLabelHigh to reference the id of an element which will be used to label the slider(s). Adds the `aria-labelledby` attribute.
414+
407415
## Change default options
408416
If you want the change the default options for all the sliders displayed in your application, you can set them using the `RzSliderOptions.options()` method:
409417
```js

dist/rzslider.css

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/rzslider.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*! angularjs-slider - v6.0.2 -
22
(c) Rafal Zajac <[email protected]>, Valentin Hervieu <[email protected]>, Jussi Saarivirta <[email protected]>, Angelin Sirbu <[email protected]> -
33
https://github.com/angular-slider/angularjs-slider -
4-
2017-03-02 */
4+
2017-03-05 */
55
/*jslint unparam: true */
66
/*global angular: false, console: false, define, module */
77
(function(root, factory) {
@@ -81,7 +81,11 @@
8181
logScale: false,
8282
customValueToPosition: null,
8383
customPositionToValue: null,
84-
selectionBarGradient: null
84+
selectionBarGradient: null,
85+
ariaLabel: null,
86+
ariaLabelledBy: null,
87+
ariaLabelHigh: null,
88+
ariaLabelledByHigh: null
8589
};
8690
var globalOptions = {};
8791

@@ -893,6 +897,10 @@
893897
this.minH.attr('tabindex', '');
894898
if (this.options.vertical)
895899
this.minH.attr('aria-orientation', 'vertical');
900+
if (this.options.ariaLabel)
901+
this.minH.attr('aria-label', this.options.ariaLabel);
902+
else if (this.options.ariaLabelledBy)
903+
this.minH.attr('aria-labelledby', this.options.ariaLabelledBy);
896904

897905
if (this.range) {
898906
this.maxH.attr('role', 'slider');
@@ -902,6 +910,10 @@
902910
this.maxH.attr('tabindex', '');
903911
if (this.options.vertical)
904912
this.maxH.attr('aria-orientation', 'vertical');
913+
if (this.options.ariaLabelHigh)
914+
this.maxH.attr('aria-label', this.options.ariaLabelHigh);
915+
else if (this.options.ariaLabelledByHigh)
916+
this.maxH.attr('aria-labelledby', this.options.ariaLabelledByHigh);
905917
}
906918
},
907919

dist/rzslider.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/rzslider.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/rzslider.scss

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

src/rzslider.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@
8585
logScale: false,
8686
customValueToPosition: null,
8787
customPositionToValue: null,
88-
selectionBarGradient: null
88+
selectionBarGradient: null,
89+
ariaLabel: null,
90+
ariaLabelledBy: null,
91+
ariaLabelHigh: null,
92+
ariaLabelledByHigh: null
8993
};
9094
var globalOptions = {};
9195

@@ -897,6 +901,10 @@
897901
this.minH.attr('tabindex', '');
898902
if (this.options.vertical)
899903
this.minH.attr('aria-orientation', 'vertical');
904+
if (this.options.ariaLabel)
905+
this.minH.attr('aria-label', this.options.ariaLabel);
906+
else if (this.options.ariaLabelledBy)
907+
this.minH.attr('aria-labelledby', this.options.ariaLabelledBy);
900908

901909
if (this.range) {
902910
this.maxH.attr('role', 'slider');
@@ -906,6 +914,10 @@
906914
this.maxH.attr('tabindex', '');
907915
if (this.options.vertical)
908916
this.maxH.attr('aria-orientation', 'vertical');
917+
if (this.options.ariaLabelHigh)
918+
this.maxH.attr('aria-label', this.options.ariaLabelHigh);
919+
else if (this.options.ariaLabelledByHigh)
920+
this.maxH.attr('aria-labelledby', this.options.ariaLabelledByHigh);
909921
}
910922
},
911923

tests/specs/accessibility-test.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,105 @@
243243
expect(helper.slider.minH.attr('aria-valuenow')).to.equal('2');
244244
expect(helper.slider.minH.attr('aria-valuetext')).to.equal('C');
245245
});
246+
247+
it('should have labelled single slider when option is set', function() {
248+
var sliderConf = {
249+
value: 10,
250+
options: {
251+
floor: 0,
252+
ceil: 100,
253+
step: 10,
254+
ariaLabel: "test label"
255+
}
256+
};
257+
helper.createSlider(sliderConf);
258+
expect(helper.slider.minH.attr('aria-label')).to.equal('test label');
259+
});
260+
261+
it('should have labelled range slider when option is set', function() {
262+
var sliderConf = {
263+
min: 10,
264+
max: 90,
265+
options: {
266+
floor: 0,
267+
ceil: 100,
268+
step: 10,
269+
ariaLabel: "test label",
270+
ariaLabelHigh: "test label high"
271+
}
272+
};
273+
helper.createRangeSlider(sliderConf);
274+
expect(helper.slider.minH.attr('aria-label')).to.equal('test label');
275+
expect(helper.slider.maxH.attr('aria-label')).to.equal('test label high');
276+
});
277+
278+
it('should have labelled by id on single slider when option is set', function() {
279+
var sliderConf = {
280+
value: 10,
281+
options: {
282+
floor: 0,
283+
ceil: 100,
284+
step: 10,
285+
ariaLabelledBy: "testId"
286+
}
287+
};
288+
helper.createSlider(sliderConf);
289+
expect(helper.slider.minH.attr('aria-labelledby')).to.equal('testId');
290+
});
291+
292+
it('should have labelled by id on range slider when option is set', function() {
293+
var sliderConf = {
294+
min: 10,
295+
max: 90,
296+
options: {
297+
floor: 0,
298+
ceil: 100,
299+
step: 10,
300+
ariaLabelledBy: "testId",
301+
ariaLabelledByHigh: "testIdHigh"
302+
}
303+
};
304+
helper.createRangeSlider(sliderConf);
305+
expect(helper.slider.minH.attr('aria-labelledby')).to.equal('testId');
306+
expect(helper.slider.maxH.attr('aria-labelledby')).to.equal('testIdHigh');
307+
});
308+
309+
it('should not have labelled by id on single slider when both options set', function() {
310+
var sliderConf = {
311+
value: 10,
312+
options: {
313+
floor: 0,
314+
ceil: 100,
315+
step: 10,
316+
ariaLabel: "test label",
317+
ariaLabelledBy: "testId"
318+
}
319+
};
320+
helper.createSlider(sliderConf);
321+
expect(helper.slider.minH.attr('aria-label')).to.equal('test label');
322+
expect(helper.slider.minH.attr('aria-labelledby')).to.equal(undefined);
323+
});
324+
325+
it('should not have labelled by id on range slider when both options set', function() {
326+
var sliderConf = {
327+
min: 10,
328+
max: 90,
329+
options: {
330+
floor: 0,
331+
ceil: 100,
332+
step: 10,
333+
ariaLabel: "test label",
334+
ariaLabelHigh: "test label high",
335+
ariaLabelledBy: "testId",
336+
ariaLabelledByHigh: "testIdHigh"
337+
}
338+
};
339+
helper.createRangeSlider(sliderConf);
340+
expect(helper.slider.minH.attr('aria-label')).to.equal('test label');
341+
expect(helper.slider.maxH.attr('aria-label')).to.equal('test label high');
342+
expect(helper.slider.minH.attr('aria-labelledby')).to.equal(undefined);
343+
expect(helper.slider.maxH.attr('aria-labelledby')).to.equal(undefined);
344+
});
246345
});
247346
}());
248347

0 commit comments

Comments
 (0)