Slider (Multi-Thumb)
A slider with two thumbs that allows users to select a range of values within a given range.
Demo
Accessibility Features
WAI-ARIA Roles
| Role | Target Element | Description |
|---|---|---|
slider | Lower thumb element | Identifies the element as a slider for selecting the lower bound of the range. |
slider | Upper thumb element | Identifies the element as a slider for selecting the upper bound of the range. |
group | Container element | Groups the two sliders together and associates them with a common label. |
Each thumb is an independent slider element with its own ARIA attributes. The group role establishes the semantic relationship between the two sliders.
WAI-ARIA Properties
aria-valuenow (Required)
Indicates the current numeric value of each thumb. Updated dynamically as the user changes the value.
| Type | Number |
| Required | Yes |
| Range |
Must be between aria-valuemin and aria-valuemax |
aria-valuemin (Required)
Dynamic bounds: Per the APG specification, when the range of one slider depends on the value of another, these attributes must update dynamically:
| Thumb | aria-valuemin | aria-valuemax |
|---|---|---|
| Lower thumb | Static (absolute min) | Dynamic (upper value - minDistance) |
| Upper thumb | Dynamic (lower value + minDistance) | Static (absolute max) |
This approach correctly communicates to assistive technology users the actual allowed range for each thumb at any given moment, which enables better predictability of Home and End key behaviors.
aria-valuemax (Required)
aria-valuetext
Provides a human-readable text alternative for the current value. Use when the numeric value alone doesn't convey sufficient meaning.
| Type | String |
| Required | No (recommended when value needs context) |
| Example | "$20", "$80", "20% - 80%" |
aria-orientation
Specifies the orientation of the slider. Only set to "vertical" for vertical sliders; omit for horizontal (default).
| Type | "horizontal" | "vertical" |
| Required | No |
| Default | horizontal (implicit) |
aria-disabled
Indicates that the slider is disabled and not interactive.
| Type | true | undefined |
| Required | No |
Keyboard Support
| Key | Action |
|---|---|
| Tab | Moves focus between thumbs (lower to upper) |
| Shift + Tab | Moves focus between thumbs (upper to lower) |
| Right Arrow | Increases the value by one step |
| Up Arrow | Increases the value by one step |
| Left Arrow | Decreases the value by one step |
| Down Arrow | Decreases the value by one step |
| Home | Sets the thumb to its minimum allowed value (dynamic for upper thumb) |
| End | Sets the thumb to its maximum allowed value (dynamic for lower thumb) |
| Page Up | Increases the value by a large step (default: step * 10) |
| Page Down | Decreases the value by a large step (default: step * 10) |
Collision Prevention
The multi-thumb slider ensures that the thumbs cannot cross each other:
- Lower thumb - Cannot exceed (upper value - minDistance)
- Upper thumb - Cannot go below (lower value + minDistance)
- minDistance - Configurable minimum gap between thumbs (default: 0)
Accessible Naming
Multi-thumb sliders require careful labeling to distinguish between the two thumbs. This implementation supports several approaches:
- Visible group label - Using the
labelprop to display a visible label for the slider group -
aria-label(tuple) - Provides individual labels for each thumb, e.g., ["Minimum Price", "Maximum Price"] -
aria-labelledby(tuple) - References external elements as labels for each thumb -
getAriaLabelfunction - Dynamic label generation based on thumb index
Focus Management
Focus behavior in this implementation:
- Tab order - Both thumbs are in the tab order (tabindex="0")
- Constant order - Lower thumb always comes first in tab order, regardless of values
- Track click - Clicking the track moves the nearest thumb and focuses it
Pointer Interaction
This implementation supports mouse and touch interaction:
- Click on track: Moves the nearest thumb to the clicked position
- Drag thumb: Allows continuous adjustment while dragging
- Pointer capture: Maintains interaction even when pointer moves outside the slider
Visual Design
This implementation follows WCAG guidelines for accessible visual design:
- Focus indicator: Visible focus ring on each thumb element
- Range indicator: Visual representation of the selected range between thumbs
- Hover states: Visual feedback on hover
- Disabled state: Clear visual indication when slider is disabled
- Forced colors mode: Uses system colors for accessibility in Windows High Contrast Mode
References
Source Code
<script lang="ts">
interface MultiThumbSliderProps {
defaultValue?: [number, number];
min?: number;
max?: number;
step?: number;
largeStep?: number;
minDistance?: number;
orientation?: 'horizontal' | 'vertical';
disabled?: boolean;
showValues?: boolean;
format?: string;
label?: string;
ariaLabel?: [string, string];
ariaLabelledby?: [string, string];
ariaDescribedby?: string | [string, string];
getAriaValueText?: (value: number, index: number) => string;
getAriaLabel?: (index: number) => string;
onvaluechange?: (values: [number, number], activeThumbIndex: number) => void;
onvaluecommit?: (values: [number, number]) => void;
[key: string]: unknown;
}
let {
defaultValue,
min = 0,
max = 100,
step = 1,
largeStep,
minDistance = 0,
orientation = 'horizontal',
disabled = false,
showValues = true,
format,
label,
ariaLabel,
ariaLabelledby,
ariaDescribedby,
getAriaValueText,
getAriaLabel,
onvaluechange,
onvaluecommit,
...restProps
}: MultiThumbSliderProps = $props();
// Utility functions
function clamp(val: number, minVal: number, maxVal: number): number {
return Math.min(maxVal, Math.max(minVal, val));
}
function roundToStep(val: number, stepVal: number, minVal: number): number {
const steps = Math.round((val - minVal) / stepVal);
const result = minVal + steps * stepVal;
const decimalPlaces = (stepVal.toString().split('.')[1] || '').length;
return Number(result.toFixed(decimalPlaces));
}
function getPercent(val: number, minVal: number, maxVal: number): number {
if (maxVal === minVal) return 0;
return ((val - minVal) / (maxVal - minVal)) * 100;
}
function formatValue(
val: number,
formatStr: string | undefined,
minVal: number,
maxVal: number
): string {
if (!formatStr) return String(val);
return formatStr
.replace('{value}', String(val))
.replace('{min}', String(minVal))
.replace('{max}', String(maxVal));
}
// Get dynamic bounds for a thumb
function getThumbBounds(
index: number,
currentValues: [number, number],
minVal: number,
maxVal: number,
minDist: number
): { min: number; max: number } {
const effectiveMinDistance = Math.min(minDist, maxVal - minVal);
if (index === 0) {
return { min: minVal, max: currentValues[1] - effectiveMinDistance };
} else {
return { min: currentValues[0] + effectiveMinDistance, max: maxVal };
}
}
// Normalize values
function normalizeValues(
inputValues: [number, number],
minVal: number,
maxVal: number,
stepVal: number,
minDist: number
): [number, number] {
let [lower, upper] = inputValues;
const effectiveMinDistance = Math.min(minDist, maxVal - minVal);
lower = roundToStep(lower, stepVal, minVal);
upper = roundToStep(upper, stepVal, minVal);
lower = clamp(lower, minVal, maxVal - effectiveMinDistance);
upper = clamp(upper, minVal + effectiveMinDistance, maxVal);
if (lower > upper - effectiveMinDistance) {
lower = upper - effectiveMinDistance;
}
return [lower, upper];
}
// Generate unique ID for group label
const groupLabelId = `slider-multithumb-label-${Math.random().toString(36).slice(2, 9)}`;
// Refs
let lowerThumbEl: HTMLDivElement | null = null;
let upperThumbEl: HTMLDivElement | null = null;
let trackEl: HTMLDivElement | null = null;
let activeThumbIndex = $state<number | null>(null);
// State
const initialValues = normalizeValues(defaultValue ?? [min, max], min, max, step, minDistance);
let values = $state<[number, number]>(initialValues);
// Computed
const isVertical = $derived(orientation === 'vertical');
const effectiveLargeStep = $derived(largeStep ?? step * 10);
const lowerPercent = $derived(getPercent(values[0], min, max));
const upperPercent = $derived(getPercent(values[1], min, max));
// Bounds helpers
const lowerBoundsMax = $derived(getThumbBounds(0, values, min, max, minDistance).max);
const upperBoundsMin = $derived(getThumbBounds(1, values, min, max, minDistance).min);
// Update values and dispatch event
function updateValues(newValues: [number, number], activeIndex: number) {
values = newValues;
onvaluechange?.(newValues, activeIndex);
}
function updateThumbValue(index: number, newValue: number) {
const bounds = getThumbBounds(index, values, min, max, minDistance);
const rounded = roundToStep(newValue, step, min);
const clamped = clamp(rounded, bounds.min, bounds.max);
if (clamped === values[index]) return;
const newValues: [number, number] = [...values];
newValues[index] = clamped;
updateValues(newValues, index);
}
// Calculate value from pointer position
function getValueFromPointer(clientX: number, clientY: number): number {
if (!trackEl) return values[0];
const rect = trackEl.getBoundingClientRect();
if (rect.width === 0 && rect.height === 0) {
return values[0];
}
let pct: number;
if (isVertical) {
if (rect.height === 0) return values[0];
pct = 1 - (clientY - rect.top) / rect.height;
} else {
if (rect.width === 0) return values[0];
pct = (clientX - rect.left) / rect.width;
}
const rawValue = min + pct * (max - min);
return roundToStep(rawValue, step, min);
}
// Keyboard handler
function handleKeyDown(index: number, event: KeyboardEvent) {
if (disabled) return;
const bounds = getThumbBounds(index, values, min, max, minDistance);
let newValue = values[index];
switch (event.key) {
case 'ArrowRight':
case 'ArrowUp':
newValue = values[index] + step;
break;
case 'ArrowLeft':
case 'ArrowDown':
newValue = values[index] - step;
break;
case 'Home':
newValue = bounds.min;
break;
case 'End':
newValue = bounds.max;
break;
case 'PageUp':
newValue = values[index] + effectiveLargeStep;
break;
case 'PageDown':
newValue = values[index] - effectiveLargeStep;
break;
default:
return;
}
event.preventDefault();
updateThumbValue(index, newValue);
}
// Pointer handlers
function getThumbEl(index: number): HTMLDivElement | null {
return index === 0 ? lowerThumbEl : upperThumbEl;
}
function handleThumbPointerDown(index: number, event: PointerEvent) {
if (disabled) return;
event.preventDefault();
const thumb = getThumbEl(index);
if (!thumb) return;
if (typeof thumb.setPointerCapture === 'function') {
thumb.setPointerCapture(event.pointerId);
}
activeThumbIndex = index;
thumb.focus();
}
function handleThumbPointerMove(index: number, event: PointerEvent) {
const thumb = getThumbEl(index);
if (!thumb) return;
const hasCapture =
typeof thumb.hasPointerCapture === 'function'
? thumb.hasPointerCapture(event.pointerId)
: activeThumbIndex === index;
if (!hasCapture) return;
const newValue = getValueFromPointer(event.clientX, event.clientY);
updateThumbValue(index, newValue);
}
function handleThumbPointerUp(index: number, event: PointerEvent) {
const thumb = getThumbEl(index);
if (thumb && typeof thumb.releasePointerCapture === 'function') {
try {
thumb.releasePointerCapture(event.pointerId);
} catch {
// Ignore
}
}
activeThumbIndex = null;
onvaluecommit?.(values);
}
// Track click handler
function handleTrackClick(event: MouseEvent) {
if (disabled) return;
if (event.target === lowerThumbEl || event.target === upperThumbEl) return;
const clickValue = getValueFromPointer(event.clientX, event.clientY);
const distToLower = Math.abs(clickValue - values[0]);
const distToUpper = Math.abs(clickValue - values[1]);
const targetIndex = distToLower <= distToUpper ? 0 : 1;
updateThumbValue(targetIndex, clickValue);
getThumbEl(targetIndex)?.focus();
}
// ARIA helpers
function getThumbAriaLabelValue(index: number): string | undefined {
if (ariaLabel) {
return ariaLabel[index];
}
if (getAriaLabel) {
return getAriaLabel(index);
}
return undefined;
}
function getThumbAriaLabelledbyValue(index: number): string | undefined {
if (ariaLabelledby) {
return ariaLabelledby[index];
}
return undefined;
}
function getThumbAriaDescribedbyValue(index: number): string | undefined {
if (!ariaDescribedby) return undefined;
if (Array.isArray(ariaDescribedby)) {
return ariaDescribedby[index];
}
return ariaDescribedby;
}
function getThumbAriaValueTextValue(index: number): string | undefined {
const value = values[index];
if (getAriaValueText) {
return getAriaValueText(value, index);
}
if (format) {
return formatValue(value, format, min, max);
}
return undefined;
}
function getDisplayText(index: number): string {
return formatValue(values[index], format, min, max);
}
</script>
<div
role={label ? 'group' : undefined}
aria-labelledby={label ? groupLabelId : undefined}
class="apg-slider-multithumb {isVertical ? 'apg-slider-multithumb--vertical' : ''} {disabled
? 'apg-slider-multithumb--disabled'
: ''} {restProps.class || ''}"
id={restProps.id as string | undefined}
data-testid={restProps['data-testid'] as string | undefined}
>
{#if label}
<span id={groupLabelId} class="apg-slider-multithumb-label">
{label}
</span>
{/if}
<!-- svelte-ignore a11y_click_events_have_key_events a11y_no_static_element_interactions -->
<div
bind:this={trackEl}
class="apg-slider-multithumb-track"
style="--slider-lower: {lowerPercent}%; --slider-upper: {upperPercent}%"
onclick={handleTrackClick}
>
<div class="apg-slider-multithumb-range" aria-hidden="true"></div>
<!-- Lower thumb -->
<div
bind:this={lowerThumbEl}
role="slider"
tabindex={disabled ? -1 : 0}
aria-valuenow={values[0]}
aria-valuemin={min}
aria-valuemax={lowerBoundsMax}
aria-valuetext={getThumbAriaValueTextValue(0)}
aria-label={getThumbAriaLabelValue(0)}
aria-labelledby={getThumbAriaLabelledbyValue(0)}
aria-orientation={isVertical ? 'vertical' : undefined}
aria-disabled={disabled ? true : undefined}
aria-describedby={getThumbAriaDescribedbyValue(0)}
class="apg-slider-multithumb-thumb apg-slider-multithumb-thumb--lower"
style={isVertical ? `bottom: ${lowerPercent}%` : `left: ${lowerPercent}%`}
onkeydown={(e) => handleKeyDown(0, e)}
onpointerdown={(e) => handleThumbPointerDown(0, e)}
onpointermove={(e) => handleThumbPointerMove(0, e)}
onpointerup={(e) => handleThumbPointerUp(0, e)}
>
<span class="apg-slider-multithumb-tooltip" aria-hidden="true">
{getThumbAriaLabelValue(0)}
</span>
</div>
<!-- Upper thumb -->
<div
bind:this={upperThumbEl}
role="slider"
tabindex={disabled ? -1 : 0}
aria-valuenow={values[1]}
aria-valuemin={upperBoundsMin}
aria-valuemax={max}
aria-valuetext={getThumbAriaValueTextValue(1)}
aria-label={getThumbAriaLabelValue(1)}
aria-labelledby={getThumbAriaLabelledbyValue(1)}
aria-orientation={isVertical ? 'vertical' : undefined}
aria-disabled={disabled ? true : undefined}
aria-describedby={getThumbAriaDescribedbyValue(1)}
class="apg-slider-multithumb-thumb apg-slider-multithumb-thumb--upper"
style={isVertical ? `bottom: ${upperPercent}%` : `left: ${upperPercent}%`}
onkeydown={(e) => handleKeyDown(1, e)}
onpointerdown={(e) => handleThumbPointerDown(1, e)}
onpointermove={(e) => handleThumbPointerMove(1, e)}
onpointerup={(e) => handleThumbPointerUp(1, e)}
>
<span class="apg-slider-multithumb-tooltip" aria-hidden="true">
{getThumbAriaLabelValue(1)}
</span>
</div>
</div>
{#if showValues}
<div class="apg-slider-multithumb-values" aria-hidden="true">
<span class="apg-slider-multithumb-value apg-slider-multithumb-value--lower">
{getDisplayText(0)}
</span>
<span class="apg-slider-multithumb-value-separator"> - </span>
<span class="apg-slider-multithumb-value apg-slider-multithumb-value--upper">
{getDisplayText(1)}
</span>
</div>
{/if}
</div> Usage
<script>
import MultiThumbSlider from './MultiThumbSlider.svelte';
function handleValueChange(values, activeIndex) {
console.log('Changed:', values, 'Active thumb:', activeIndex);
}
function handleValueCommit(values) {
console.log('Committed:', values);
}
</script>
<!-- Basic usage with visible label and ariaLabel tuple -->
<MultiThumbSlider
defaultValue={[20, 80]}
label="Price Range"
ariaLabel={['Minimum Price', 'Maximum Price']}
/>
<!-- With format for display and aria-valuetext -->
<MultiThumbSlider
defaultValue={[25, 75]}
label="Temperature"
format="{value}°C"
ariaLabel={['Min Temp', 'Max Temp']}
/>
<!-- With minDistance to prevent thumbs from getting too close -->
<MultiThumbSlider
defaultValue={[30, 70]}
minDistance={10}
label="Budget"
format="${value}"
ariaLabel={['Min Budget', 'Max Budget']}
/>
<!-- With callbacks -->
<MultiThumbSlider
defaultValue={[20, 80]}
label="Range"
ariaLabel={['Lower', 'Upper']}
onvaluechange={handleValueChange}
onvaluecommit={handleValueCommit}
/> API
Props
| Prop | Type | Default | Description |
|---|---|---|---|
defaultValue | [number, number] | [min, max] | Initial values for the two thumbs |
min | number | 0 | Minimum value |
max | number | 100 | Maximum value |
step | number | 1 | Step increment |
minDistance | number | 0 | Minimum distance between thumbs |
disabled | boolean | false | Whether the slider is disabled |
label | string | - | Visible label for the slider group |
format | string | - | Format pattern for display |
ariaLabel | [string, string] | - | Accessible labels for each thumb |
ariaLabelledby | [string, string] | - | IDs of external label elements |
onvaluechange | (values, activeIndex) => void | - | Callback when any value changes |
onvaluecommit | (values) => void | - | Callback when interaction ends |
Testing
Tests verify APG compliance for ARIA attributes, keyboard interactions, collision prevention, and accessibility requirements for multi-thumb sliders.
Test Categories
High Priority: ARIA Structure
| Test | Description |
|---|---|
two slider elements | Container has exactly two elements with role="slider" |
role="group" | Sliders are contained in a group with aria-labelledby |
aria-valuenow | Both thumbs have correct initial values |
static aria-valuemin | Lower thumb has static min (absolute minimum) |
static aria-valuemax | Upper thumb has static max (absolute maximum) |
dynamic aria-valuemax | Lower thumb's max depends on upper thumb's value |
dynamic aria-valuemin | Upper thumb's min depends on lower thumb's value |
High Priority: Dynamic Bounds Update
| Test | Description |
|---|---|
lower -> upper bound | Moving lower thumb updates upper thumb's aria-valuemin |
upper -> lower bound | Moving upper thumb updates lower thumb's aria-valuemax |
High Priority: Keyboard Interaction
| Test | Description |
|---|---|
Arrow Right/Up | Increases value by one step |
Arrow Left/Down | Decreases value by one step |
Home (lower) | Sets lower thumb to absolute minimum |
End (lower) | Sets lower thumb to dynamic maximum (upper - minDistance) |
Home (upper) | Sets upper thumb to dynamic minimum (lower + minDistance) |
End (upper) | Sets upper thumb to absolute maximum |
Page Up/Down | Increases/decreases value by large step |
High Priority: Collision Prevention
| Test | Description |
|---|---|
lower cannot exceed upper | Lower thumb stops at (upper - minDistance) |
upper cannot go below lower | Upper thumb stops at (lower + minDistance) |
rapid key presses | Thumbs do not cross when rapidly pressing arrow keys |
High Priority: Focus Management
| Test | Description |
|---|---|
Tab order | Tab moves from lower to upper thumb |
Shift+Tab order | Shift+Tab moves from upper to lower thumb |
tabindex="0" | Both thumbs have tabindex="0" (always in tab order) |
Medium Priority: aria-valuetext Updates
| Test | Description |
|---|---|
lower thumb update | aria-valuetext updates when lower thumb value changes |
upper thumb update | aria-valuetext updates when upper thumb value changes |
Medium Priority: Accessibility
| Test | Description |
|---|---|
axe violations (container) | No accessibility violations on the container |
axe violations (sliders) | No accessibility violations on each slider element |
Low Priority: Cross-framework Consistency
| Test | Description |
|---|---|
render two sliders | All frameworks render exactly two slider elements |
consistent initial values | All frameworks have identical initial aria-valuenow values |
keyboard navigation | All frameworks support identical keyboard navigation |
collision prevention | All frameworks prevent thumb crossing |
Example Test Code
The following is the actual E2E test file (e2e/slider-multithumb.spec.ts).
import { test, expect } from '@playwright/test';
import AxeBuilder from '@axe-core/playwright';
/**
* E2E Tests for Multi-Thumb Slider Pattern
*
* A slider with two thumbs that allows users to select a range of values.
* Each thumb uses role="slider" with dynamic aria-valuemin/aria-valuemax
* based on the other thumb's position.
*
* APG Reference: https://www.w3.org/WAI/ARIA/apg/patterns/slider-multithumb/
*/
const frameworks = ['react', 'vue', 'svelte', 'astro'] as const;
// ============================================
// Helper Functions
// ============================================
const getBasicSliderContainer = (page: import('@playwright/test').Page) => {
return page.getByTestId('basic-slider');
};
const getSliders = (page: import('@playwright/test').Page) => {
return getBasicSliderContainer(page).getByRole('slider');
};
const getSliderByLabel = (page: import('@playwright/test').Page, label: string) => {
return getBasicSliderContainer(page).getByRole('slider', { name: label });
};
// ============================================
// Framework-specific Tests
// ============================================
for (const framework of frameworks) {
test.describe(`MultiThumbSlider (${framework})`, () => {
test.beforeEach(async ({ page }) => {
await page.goto(`patterns/slider-multithumb/${framework}/demo/`);
await getSliders(page).first().waitFor();
// Wait for hydration - sliders should have aria-valuenow
const firstSlider = getSliders(page).first();
await expect
.poll(async () => {
const valuenow = await firstSlider.getAttribute('aria-valuenow');
return valuenow !== null;
})
.toBe(true);
});
// ------------------------------------------
// 🔴 High Priority: APG ARIA Structure
// ------------------------------------------
test.describe('APG: ARIA Structure', () => {
test('has two slider elements', async ({ page }) => {
const sliders = getSliders(page);
await expect(sliders).toHaveCount(2);
});
test('lower thumb has role="slider"', async ({ page }) => {
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
await expect(lowerThumb).toHaveRole('slider');
});
test('upper thumb has role="slider"', async ({ page }) => {
const upperThumb = getSliderByLabel(page, 'Maximum Price');
await expect(upperThumb).toHaveRole('slider');
});
test('lower thumb has correct initial aria-valuenow', async ({ page }) => {
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
const valuenow = await lowerThumb.getAttribute('aria-valuenow');
expect(valuenow).toBe('20');
});
test('upper thumb has correct initial aria-valuenow', async ({ page }) => {
const upperThumb = getSliderByLabel(page, 'Maximum Price');
const valuenow = await upperThumb.getAttribute('aria-valuenow');
expect(valuenow).toBe('80');
});
test('lower thumb has static aria-valuemin (absolute min)', async ({ page }) => {
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
await expect(lowerThumb).toHaveAttribute('aria-valuemin', '0');
});
test('upper thumb has static aria-valuemax (absolute max)', async ({ page }) => {
const upperThumb = getSliderByLabel(page, 'Maximum Price');
await expect(upperThumb).toHaveAttribute('aria-valuemax', '100');
});
test('lower thumb has dynamic aria-valuemax based on upper thumb', async ({ page }) => {
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
// Upper thumb is at 80, so lower thumb max should be 80 (or 80 - minDistance)
const valuemax = await lowerThumb.getAttribute('aria-valuemax');
expect(Number(valuemax)).toBeLessThanOrEqual(80);
});
test('upper thumb has dynamic aria-valuemin based on lower thumb', async ({ page }) => {
const upperThumb = getSliderByLabel(page, 'Maximum Price');
// Lower thumb is at 20, so upper thumb min should be 20 (or 20 + minDistance)
const valuemin = await upperThumb.getAttribute('aria-valuemin');
expect(Number(valuemin)).toBeGreaterThanOrEqual(20);
});
test('sliders are contained in group with label', async ({ page }) => {
const group = page.getByRole('group', { name: 'Price Range' });
await expect(group).toBeVisible();
});
});
// ------------------------------------------
// 🔴 High Priority: Dynamic Bounds Update
// ------------------------------------------
test.describe('APG: Dynamic Bounds Update', () => {
test('moving lower thumb updates upper thumb aria-valuemin', async ({ page }) => {
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
const upperThumb = getSliderByLabel(page, 'Maximum Price');
await lowerThumb.click();
await page.keyboard.press('ArrowRight');
// Lower thumb moved from 20 to 21
await expect(lowerThumb).toHaveAttribute('aria-valuenow', '21');
// Upper thumb's min should have increased
const valuemin = await upperThumb.getAttribute('aria-valuemin');
expect(Number(valuemin)).toBeGreaterThanOrEqual(21);
});
test('moving upper thumb updates lower thumb aria-valuemax', async ({ page }) => {
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
const upperThumb = getSliderByLabel(page, 'Maximum Price');
await upperThumb.click();
await page.keyboard.press('ArrowLeft');
// Upper thumb moved from 80 to 79
await expect(upperThumb).toHaveAttribute('aria-valuenow', '79');
// Lower thumb's max should have decreased
const valuemax = await lowerThumb.getAttribute('aria-valuemax');
expect(Number(valuemax)).toBeLessThanOrEqual(79);
});
});
// ------------------------------------------
// 🔴 High Priority: Keyboard Interaction
// ------------------------------------------
test.describe('APG: Keyboard Interaction', () => {
test('ArrowRight increases lower thumb value by step', async ({ page }) => {
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
await lowerThumb.click();
await expect(lowerThumb).toBeFocused();
const initialValue = await lowerThumb.getAttribute('aria-valuenow');
await page.keyboard.press('ArrowRight');
const newValue = await lowerThumb.getAttribute('aria-valuenow');
expect(Number(newValue)).toBe(Number(initialValue) + 1);
});
test('ArrowLeft decreases upper thumb value by step', async ({ page }) => {
const upperThumb = getSliderByLabel(page, 'Maximum Price');
await upperThumb.click();
const initialValue = await upperThumb.getAttribute('aria-valuenow');
await page.keyboard.press('ArrowLeft');
const newValue = await upperThumb.getAttribute('aria-valuenow');
expect(Number(newValue)).toBe(Number(initialValue) - 1);
});
test('Home sets lower thumb to absolute minimum', async ({ page }) => {
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
await lowerThumb.click();
await page.keyboard.press('Home');
await expect(lowerThumb).toHaveAttribute('aria-valuenow', '0');
});
test('End sets lower thumb to dynamic maximum (not absolute)', async ({ page }) => {
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
const upperThumb = getSliderByLabel(page, 'Maximum Price');
await lowerThumb.click();
// Get upper thumb value to determine expected max
const upperValue = await upperThumb.getAttribute('aria-valuenow');
await page.keyboard.press('End');
// Lower thumb should be at or near upper thumb value (respecting minDistance)
const newValue = await lowerThumb.getAttribute('aria-valuenow');
expect(Number(newValue)).toBeLessThanOrEqual(Number(upperValue));
});
test('Home sets upper thumb to dynamic minimum (not absolute)', async ({ page }) => {
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
const upperThumb = getSliderByLabel(page, 'Maximum Price');
await upperThumb.click();
// Get lower thumb value to determine expected min
const lowerValue = await lowerThumb.getAttribute('aria-valuenow');
await page.keyboard.press('Home');
// Upper thumb should be at or near lower thumb value (respecting minDistance)
const newValue = await upperThumb.getAttribute('aria-valuenow');
expect(Number(newValue)).toBeGreaterThanOrEqual(Number(lowerValue));
});
test('End sets upper thumb to absolute maximum', async ({ page }) => {
const upperThumb = getSliderByLabel(page, 'Maximum Price');
await upperThumb.click();
await page.keyboard.press('End');
await expect(upperThumb).toHaveAttribute('aria-valuenow', '100');
});
test('PageUp increases value by large step', async ({ page }) => {
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
await lowerThumb.click();
const initialValue = await lowerThumb.getAttribute('aria-valuenow');
await page.keyboard.press('PageUp');
const newValue = await lowerThumb.getAttribute('aria-valuenow');
expect(Number(newValue)).toBe(Number(initialValue) + 10);
});
test('PageDown decreases value by large step', async ({ page }) => {
const upperThumb = getSliderByLabel(page, 'Maximum Price');
await upperThumb.click();
const initialValue = await upperThumb.getAttribute('aria-valuenow');
await page.keyboard.press('PageDown');
const newValue = await upperThumb.getAttribute('aria-valuenow');
expect(Number(newValue)).toBe(Number(initialValue) - 10);
});
});
// ------------------------------------------
// 🔴 High Priority: Collision Prevention
// ------------------------------------------
test.describe('APG: Collision Prevention', () => {
test('lower thumb cannot exceed upper thumb', async ({ page }) => {
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
const upperThumb = getSliderByLabel(page, 'Maximum Price');
await lowerThumb.click();
// Get upper thumb's current value
const upperValue = await upperThumb.getAttribute('aria-valuenow');
// Try to move lower thumb to End (dynamic max)
await page.keyboard.press('End');
// Verify lower thumb is at or below upper thumb
const lowerValue = await lowerThumb.getAttribute('aria-valuenow');
expect(Number(lowerValue)).toBeLessThanOrEqual(Number(upperValue));
});
test('upper thumb cannot go below lower thumb', async ({ page }) => {
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
const upperThumb = getSliderByLabel(page, 'Maximum Price');
await upperThumb.click();
// Get lower thumb's current value
const lowerValue = await lowerThumb.getAttribute('aria-valuenow');
// Try to move upper thumb to Home (dynamic min)
await page.keyboard.press('Home');
// Verify upper thumb is at or above lower thumb
const upperValue = await upperThumb.getAttribute('aria-valuenow');
expect(Number(upperValue)).toBeGreaterThanOrEqual(Number(lowerValue));
});
test('thumbs cannot cross when rapidly pressing arrow keys', async ({ page }) => {
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
const upperThumb = getSliderByLabel(page, 'Maximum Price');
// Move lower thumb toward upper thumb
await lowerThumb.click();
for (let i = 0; i < 100; i++) {
await page.keyboard.press('ArrowRight');
}
const lowerValue = await lowerThumb.getAttribute('aria-valuenow');
const upperValue = await upperThumb.getAttribute('aria-valuenow');
expect(Number(lowerValue)).toBeLessThanOrEqual(Number(upperValue));
});
});
// ------------------------------------------
// 🔴 High Priority: Focus Management
// ------------------------------------------
test.describe('APG: Focus Management', () => {
test('Tab moves from lower to upper thumb', async ({ page }) => {
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
const upperThumb = getSliderByLabel(page, 'Maximum Price');
await lowerThumb.focus();
await expect(lowerThumb).toBeFocused();
await page.keyboard.press('Tab');
await expect(upperThumb).toBeFocused();
});
test('Shift+Tab moves from upper to lower thumb', async ({ page }) => {
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
const upperThumb = getSliderByLabel(page, 'Maximum Price');
await upperThumb.focus();
await expect(upperThumb).toBeFocused();
await page.keyboard.press('Shift+Tab');
await expect(lowerThumb).toBeFocused();
});
test('both thumbs have tabindex="0"', async ({ page }) => {
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
const upperThumb = getSliderByLabel(page, 'Maximum Price');
await expect(lowerThumb).toHaveAttribute('tabindex', '0');
await expect(upperThumb).toHaveAttribute('tabindex', '0');
});
});
// ------------------------------------------
// 🟡 Medium Priority: aria-valuetext Updates
// ------------------------------------------
test.describe('aria-valuetext Updates', () => {
test('lower thumb aria-valuetext updates on value change', async ({ page }) => {
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
await lowerThumb.click();
await page.keyboard.press('Home');
await expect(lowerThumb).toHaveAttribute('aria-valuetext', '$0');
await page.keyboard.press('ArrowRight');
await expect(lowerThumb).toHaveAttribute('aria-valuetext', '$1');
});
test('upper thumb aria-valuetext updates on value change', async ({ page }) => {
const upperThumb = getSliderByLabel(page, 'Maximum Price');
await upperThumb.click();
await page.keyboard.press('End');
await expect(upperThumb).toHaveAttribute('aria-valuetext', '$100');
await page.keyboard.press('ArrowLeft');
await expect(upperThumb).toHaveAttribute('aria-valuetext', '$99');
});
});
// ------------------------------------------
// 🟢 Low Priority: Accessibility
// ------------------------------------------
test.describe('Accessibility', () => {
test('has no axe-core violations', async ({ page }) => {
const results = await new AxeBuilder({ page })
.include('[data-testid="basic-slider"]')
.exclude('[aria-hidden="true"]')
.analyze();
expect(results.violations).toEqual([]);
});
test('both sliders pass axe-core', async ({ page }) => {
const results = await new AxeBuilder({ page })
.include('[data-testid="basic-slider"] [role="slider"]')
.analyze();
expect(results.violations).toEqual([]);
});
});
// ------------------------------------------
// 🟡 Medium Priority: Pointer Interactions
// ------------------------------------------
test.describe('Pointer Interactions', () => {
test('track click moves nearest thumb', async ({ page }) => {
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
const track = page.locator('[data-testid="basic-slider"] .apg-slider-multithumb-track');
// Click near the start of the track (should move lower thumb)
const trackBox = await track.boundingBox();
if (trackBox) {
await page.mouse.click(trackBox.x + 10, trackBox.y + trackBox.height / 2);
}
// Lower thumb should have moved toward the click position
const newValue = await lowerThumb.getAttribute('aria-valuenow');
expect(Number(newValue)).toBeLessThan(20); // Was 20, should be lower
});
test('thumb can be dragged', async ({ page }) => {
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
const thumbBox = await lowerThumb.boundingBox();
if (thumbBox) {
// Drag thumb to the right
await page.mouse.move(thumbBox.x + thumbBox.width / 2, thumbBox.y + thumbBox.height / 2);
await page.mouse.down();
await page.mouse.move(thumbBox.x + 100, thumbBox.y + thumbBox.height / 2);
await page.mouse.up();
}
// Value should have increased
const newValue = await lowerThumb.getAttribute('aria-valuenow');
expect(Number(newValue)).toBeGreaterThan(20); // Was 20
});
});
// ------------------------------------------
// 🟡 Medium Priority: Disabled State
// ------------------------------------------
test.describe('Disabled State', () => {
test('disabled slider thumbs have tabindex="-1"', async ({ page }) => {
const disabledSliders = page.locator('[data-testid="disabled-slider"]').getByRole('slider');
await expect(disabledSliders.first()).toHaveAttribute('tabindex', '-1');
await expect(disabledSliders.last()).toHaveAttribute('tabindex', '-1');
});
test('disabled slider thumbs have aria-disabled="true"', async ({ page }) => {
const disabledSliders = page.locator('[data-testid="disabled-slider"]').getByRole('slider');
await expect(disabledSliders.first()).toHaveAttribute('aria-disabled', 'true');
await expect(disabledSliders.last()).toHaveAttribute('aria-disabled', 'true');
});
test('disabled slider ignores keyboard input', async ({ page }) => {
const disabledThumb = page
.locator('[data-testid="disabled-slider"]')
.getByRole('slider')
.first();
const initialValue = await disabledThumb.getAttribute('aria-valuenow');
// Try to click and press arrow key (disabled elements can still receive focus via click)
await disabledThumb.click({ force: true });
await page.keyboard.press('ArrowRight');
// Value should not change
await expect(disabledThumb).toHaveAttribute('aria-valuenow', initialValue!);
});
});
// ------------------------------------------
// 🟡 Medium Priority: Vertical Orientation
// ------------------------------------------
test.describe('Vertical Orientation', () => {
test('vertical slider has aria-orientation="vertical"', async ({ page }) => {
const verticalSliders = page.locator('[data-testid="vertical-slider"]').getByRole('slider');
await expect(verticalSliders.first()).toHaveAttribute('aria-orientation', 'vertical');
await expect(verticalSliders.last()).toHaveAttribute('aria-orientation', 'vertical');
});
test('vertical slider responds to ArrowUp/Down', async ({ page }) => {
const verticalThumb = page
.locator('[data-testid="vertical-slider"]')
.getByRole('slider')
.first();
await verticalThumb.click();
const initialValue = await verticalThumb.getAttribute('aria-valuenow');
await page.keyboard.press('ArrowUp');
const afterUp = await verticalThumb.getAttribute('aria-valuenow');
expect(Number(afterUp)).toBe(Number(initialValue) + 1);
await page.keyboard.press('ArrowDown');
const afterDown = await verticalThumb.getAttribute('aria-valuenow');
expect(Number(afterDown)).toBe(Number(initialValue));
});
});
// ------------------------------------------
// 🟡 Medium Priority: minDistance
// ------------------------------------------
test.describe('minDistance Constraint', () => {
test('thumbs maintain minimum distance', async ({ page }) => {
const minDistanceSliders = page
.locator('[data-testid="min-distance-slider"]')
.getByRole('slider');
const lowerThumb = minDistanceSliders.first();
const upperThumb = minDistanceSliders.last();
// Try to move lower thumb to End
await lowerThumb.click();
await page.keyboard.press('End');
const lowerValue = Number(await lowerThumb.getAttribute('aria-valuenow'));
const upperValue = Number(await upperThumb.getAttribute('aria-valuenow'));
// Should maintain minDistance of 10
expect(upperValue - lowerValue).toBeGreaterThanOrEqual(10);
});
test('lower thumb aria-valuemax respects minDistance', async ({ page }) => {
const minDistanceSliders = page
.locator('[data-testid="min-distance-slider"]')
.getByRole('slider');
const lowerThumb = minDistanceSliders.first();
const upperThumb = minDistanceSliders.last();
const upperValue = Number(await upperThumb.getAttribute('aria-valuenow'));
const lowerMax = Number(await lowerThumb.getAttribute('aria-valuemax'));
// Lower thumb max should be upper value - minDistance
expect(lowerMax).toBeLessThanOrEqual(upperValue - 10);
});
test('upper thumb aria-valuemin respects minDistance', async ({ page }) => {
const minDistanceSliders = page
.locator('[data-testid="min-distance-slider"]')
.getByRole('slider');
const lowerThumb = minDistanceSliders.first();
const upperThumb = minDistanceSliders.last();
const lowerValue = Number(await lowerThumb.getAttribute('aria-valuenow'));
const upperMin = Number(await upperThumb.getAttribute('aria-valuemin'));
// Upper thumb min should be lower value + minDistance
expect(upperMin).toBeGreaterThanOrEqual(lowerValue + 10);
});
});
});
}
// ============================================
// Cross-framework Consistency Tests
// ============================================
test.describe('MultiThumbSlider - Cross-framework Consistency', () => {
test('all frameworks render two sliders', async ({ page }) => {
for (const framework of frameworks) {
await page.goto(`patterns/slider-multithumb/${framework}/demo/`);
await getSliders(page).first().waitFor();
const sliders = getSliders(page);
const count = await sliders.count();
expect(count).toBe(2);
}
});
test('all frameworks have consistent initial values', async ({ page }) => {
test.setTimeout(60000);
for (const framework of frameworks) {
await page.goto(`patterns/slider-multithumb/${framework}/demo/`);
await getSliders(page).first().waitFor();
// Wait for hydration
await expect
.poll(async () => {
const valuenow = await getSliders(page).first().getAttribute('aria-valuenow');
return valuenow !== null;
})
.toBe(true);
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
const upperThumb = getSliderByLabel(page, 'Maximum Price');
await expect(lowerThumb).toHaveAttribute('aria-valuenow', '20');
await expect(upperThumb).toHaveAttribute('aria-valuenow', '80');
}
});
test('all frameworks support keyboard navigation', async ({ page }) => {
test.setTimeout(60000);
for (const framework of frameworks) {
await page.goto(`patterns/slider-multithumb/${framework}/demo/`);
await getSliders(page).first().waitFor();
// Wait for hydration
await expect
.poll(async () => {
const valuenow = await getSliders(page).first().getAttribute('aria-valuenow');
return valuenow !== null;
})
.toBe(true);
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
await lowerThumb.click();
// Test ArrowRight
const initialValue = await lowerThumb.getAttribute('aria-valuenow');
await page.keyboard.press('ArrowRight');
const newValue = await lowerThumb.getAttribute('aria-valuenow');
expect(Number(newValue)).toBe(Number(initialValue) + 1);
}
});
test('all frameworks prevent thumb crossing', async ({ page }) => {
test.setTimeout(60000);
for (const framework of frameworks) {
await page.goto(`patterns/slider-multithumb/${framework}/demo/`);
await getSliders(page).first().waitFor();
// Wait for hydration
await expect
.poll(async () => {
const valuenow = await getSliders(page).first().getAttribute('aria-valuenow');
return valuenow !== null;
})
.toBe(true);
const lowerThumb = getSliderByLabel(page, 'Minimum Price');
const upperThumb = getSliderByLabel(page, 'Maximum Price');
// Try to move lower thumb beyond upper
await lowerThumb.click();
await page.keyboard.press('End');
const lowerValue = Number(await lowerThumb.getAttribute('aria-valuenow'));
const upperValue = Number(await upperThumb.getAttribute('aria-valuenow'));
expect(lowerValue).toBeLessThanOrEqual(upperValue);
}
});
}); Running Tests
# Run unit tests for MultiThumbSlider
npm run test -- MultiThumbSlider
# Run E2E tests for MultiThumbSlider (all frameworks)
npm run test:e2e:pattern --pattern=slider-multithumb
# Run E2E tests for specific framework
npm run test:e2e:react:pattern --pattern=slider-multithumb
npm run test:e2e:vue:pattern --pattern=slider-multithumb
npm run test:e2e:svelte:pattern --pattern=slider-multithumb
npm run test:e2e:astro:pattern --pattern=slider-multithumb Testing Tools
- Vitest (opens in new tab) - Test runner for unit tests
- Testing Library (opens in new tab) - Framework-specific testing utilities
- Playwright (opens in new tab) - Browser automation for E2E tests
- axe-core (opens in new tab) - Accessibility testing engine
Resources
- WAI-ARIA APG: Slider (Multi-Thumb) Pattern (opens in new tab)
- WAI-ARIA APG: Slider Pattern (opens in new tab)
- AI Implementation Guide (llm.md) (opens in new tab) - ARIA specs, keyboard support, test checklist