Skip to content

Commit 6530136

Browse files
committed
add code to enable aria labels, and default them if needed
1 parent 6f4de6e commit 6530136

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/RangeSlider.svelte

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
export let suffix = "";
3636
export let formatter = (v,i,p) => v;
3737
export let handleFormatter = formatter;
38+
export let ariaLabels = [];
3839
3940
// stylistic props
4041
export let precision = 2;
@@ -86,8 +87,27 @@
8687
}
8788
// set the valueLength for the next check
8889
valueLength = values.length;
90+
8991
};
9092
93+
$: {
94+
// set default aria labels if none provided,
95+
// and also update the aria labels if the values change
96+
if ( !ariaLabels.length ) {
97+
if ( range === true && values.length === 2 ) {
98+
ariaLabels[0] = `Minimum value in range from ${min} to ${max}`;
99+
ariaLabels[1] = `Maximum value in range from ${min} to ${max}`;
100+
} else {
101+
ariaLabels = values.map((v, i) => `Value ${i + 1} in range from ${min} to ${max}`);
102+
}
103+
} else if ( ariaLabels.length !== values.length ) {
104+
for ( let i = ariaLabels.length; i < values.length; i++ ) {
105+
ariaLabels[i] = `Value ${i + 1} in range from ${min} to ${max}`;
106+
}
107+
}
108+
109+
}
110+
91111
/**
92112
* take in a value, and then calculate that value's percentage
93113
* of the overall range (min-max);
@@ -810,6 +830,7 @@
810830
on:focus={sliderFocusHandle}
811831
on:keydown={sliderKeydown}
812832
style="{orientationStart}: {$springPositions[index]}%; z-index: {activeHandle === index ? 3 : 2};"
833+
aria-label={ariaLabels[index]}
813834
aria-valuemin={range === true && index === 1 ? values[0] : min}
814835
aria-valuemax={range === true && index === 0 ? values[1] : max}
815836
aria-valuenow={value}

test/src/App.svelte

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,12 @@
254254
<RangeSlider bind:values={decimals2} float pips all="label" min={-0.01} max={0.01} step={0.0005} precision={5} {reversed} {hoverable} {disabled} />
255255
{decimals2}<br>
256256

257+
<h2>Aria Labels</h2>
258+
<RangeSlider ariaLabels={["a"]} {reversed} {hoverable} {disabled} />
259+
<RangeSlider ariaLabels={["a", "b"]} values={[5,20]} {reversed} {hoverable} {disabled} />
260+
<RangeSlider ariaLabels={["a", "b"]} values={[5,20,40]} {reversed} {hoverable} {disabled} />
261+
<RangeSlider ariaLabels={["", "b"]} values={[5,20]} range {reversed} {hoverable} {disabled} />
262+
257263
</div>
258264

259265

0 commit comments

Comments
 (0)