Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(Slider): horizontal arrow keys follow the mirrored track under RTL (#7461)

Cindy Zhang5f417474b22b
diff --git a/.changeset/slider-rtl-arrow-keys.md b/.changeset/slider-rtl-arrow-keys.md
new file mode 100644
index 0000000000..2c75a20434
--- /dev/null
+++ b/.changeset/slider-rtl-arrow-keys.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `Slider`: on a horizontal track under RTL the arrow keys now follow the mirrored track — ArrowLeft increases the value and ArrowRight decreases it, matching where the thumb moves on screen. ArrowUp/ArrowDown, PageUp/PageDown, Home/End, and vertical sliders are unchanged.
+
+@cixzhang
diff --git a/packages/core/src/Slider/Slider.spec.md b/packages/core/src/Slider/Slider.spec.md
index 2e5087e0c0..4a77aec95e 100644
--- a/packages/core/src/Slider/Slider.spec.md
+++ b/packages/core/src/Slider/Slider.spec.md
@@ -214,14 +214,14 @@ separately listed value tooltip retains Tooltip's target.
## Verification map
-| Contract | Verification | Representative states | Mutation or failure expectation | Audit section |
-| ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------- |
-| FR1 | `Slider.test.tsx` structure, range, and marks suites | Single, range, horizontal, vertical, marks | Removing or misaligning stable parts breaks existing role, position, or mark assertions. | `audit:Slider/anatomy` |
-| AR1 | Slider accessibility contract (`internal/a11y-spec`, pending) bound to Slider, in jsdom and Chromium; `Slider.test.tsx` keyboard suites, with the RTL arrow, Page, Home/End and Up/Down cases to be added with the contract | Single and range; horizontal LTR and RTL; vertical; disabled; formatted value | A thumb loses its role, name or value properties; an arrow, Page, Home or End key stops stepping or steps against the mirrored track; a range thumb crosses the other | `audit:Slider/accessibility` |
-| FR2 | `Slider.test.tsx`, `themingTargets.test.ts`, generated probe theme, and Chromium | Root, horizontal/vertical control, disabled control, track, and thumb | A target class/state is missing, undocumented, or placed outside its owning anatomy. | `audit:Slider/theming` |
-| FR3 | Source and consumer-doc review | Filled range, marks, labels, adjacent text value | A missing target is inaccurately documented as present or intentionally permanent. | `audit:Slider/theming` |
-| FR4 | Source inspection; focused tests cover label, status, and disabled-reason Tooltip only | Label, status, value tooltip, disabled reason | Shared composition or its accessible association disappears; value-tooltip composition still lacks focused coverage. | `audit:Slider/anatomy` |
-| Theming anatomy map | `scripts/check-knowledge.mjs` | Canonical anatomy and four current local targets | Missing, extra, prefixed, stale, or unclaimed mappings fail repository validation. | `audit:Slider/theming` |
+| Contract | Verification | Representative states | Mutation or failure expectation | Audit section |
+| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------- |
+| FR1 | `Slider.test.tsx` structure, range, and marks suites | Single, range, horizontal, vertical, marks | Removing or misaligning stable parts breaks existing role, position, or mark assertions. | `audit:Slider/anatomy` |
+| AR1 | Slider accessibility contract (`internal/a11y-spec`, pending) bound to Slider, in jsdom and Chromium; `Slider.test.tsx` keyboard suites, including "follows the mirrored track under RTL: ArrowLeft increases, ArrowRight decreases, Up/Down unchanged" and "keeps ArrowLeft decreasing on a vertical track under RTL"; the Page, Home/End and Up/Down cases under the contract | Single and range; horizontal LTR and RTL; vertical; disabled; formatted value | A thumb loses its role, name or value properties; an arrow, Page, Home or End key stops stepping or steps against the mirrored track; a range thumb crosses the other | `audit:Slider/accessibility` |
+| FR2 | `Slider.test.tsx`, `themingTargets.test.ts`, generated probe theme, and Chromium | Root, horizontal/vertical control, disabled control, track, and thumb | A target class/state is missing, undocumented, or placed outside its owning anatomy. | `audit:Slider/theming` |
+| FR3 | Source and consumer-doc review | Filled range, marks, labels, adjacent text value | A missing target is inaccurately documented as present or intentionally permanent. | `audit:Slider/theming` |
+| FR4 | Source inspection; focused tests cover label, status, and disabled-reason Tooltip only | Label, status, value tooltip, disabled reason | Shared composition or its accessible association disappears; value-tooltip composition still lacks focused coverage. | `audit:Slider/anatomy` |
+| Theming anatomy map | `scripts/check-knowledge.mjs` | Canonical anatomy and four current local targets | Missing, extra, prefixed, stale, or unclaimed mappings fail repository validation. | `audit:Slider/theming` |
The focused Slider suite asserts the `slider-control` target's orientation and
disabled state. Source/metadata guards cover all four target declarations. The
diff --git a/packages/core/src/Slider/Slider.test.tsx b/packages/core/src/Slider/Slider.test.tsx
index 268baba432..158985dfaf 100644
--- a/packages/core/src/Slider/Slider.test.tsx
+++ b/packages/core/src/Slider/Slider.test.tsx
@@ -708,6 +708,77 @@ describe('Slider', () => {
gcsSpy.mockRestore();
});
+ it('follows the mirrored track under RTL: ArrowLeft increases, ArrowRight decreases, Up/Down unchanged', () => {
+ const handleChange = vi.fn();
+ render(
+ <Slider
+ label="Volume"
+ value={50}
+ min={0}
+ max={100}
+ step={1}
+ valueDisplay="none"
+ onChange={handleChange}
+ />,
+ );
+ const thumb = screen.getByRole('slider');
+ const track = thumb.parentElement!;
+ const realGetComputedStyle = window.getComputedStyle;
+ const gcsSpy = vi
+ .spyOn(window, 'getComputedStyle')
+ .mockImplementation((el: Element, pseudo?: string | null) => {
+ if (el === track) {
+ return {direction: 'rtl'} as CSSStyleDeclaration;
+ }
+ return realGetComputedStyle(el, pseudo ?? undefined);
+ });
+
+ fireEvent.keyDown(thumb, {key: 'ArrowLeft'});
+ expect(handleChange).toHaveBeenLastCalledWith(51);
+ fireEvent.keyDown(thumb, {key: 'ArrowRight'});
+ expect(handleChange).toHaveBeenLastCalledWith(49);
+ fireEvent.keyDown(thumb, {key: 'ArrowUp'});
+ expect(handleChange).toHaveBeenLastCalledWith(51);
+ fireEvent.keyDown(thumb, {key: 'ArrowDown'});
+ expect(handleChange).toHaveBeenLastCalledWith(49);
+
+ gcsSpy.mockRestore();
+ });
+
+ it('keeps ArrowLeft decreasing on a vertical track under RTL', () => {
+ const handleChange = vi.fn();
+ render(
+ <Slider
+ label="Volume"
+ value={50}
+ min={0}
+ max={100}
+ step={1}
+ orientation="vertical"
+ valueDisplay="none"
+ onChange={handleChange}
+ />,
+ );
+ const thumb = screen.getByRole('slider');
+ const track = thumb.parentElement!;
+ const realGetComputedStyle = window.getComputedStyle;
+ const gcsSpy = vi
+ .spyOn(window, 'getComputedStyle')
+ .mockImplementation((el: Element, pseudo?: string | null) => {
+ if (el === track) {
+ return {direction: 'rtl'} as CSSStyleDeclaration;
+ }
+ return realGetComputedStyle(el, pseudo ?? undefined);
+ });
+
+ fireEvent.keyDown(thumb, {key: 'ArrowLeft'});
+ expect(handleChange).toHaveBeenLastCalledWith(49);
+ fireEvent.keyDown(thumb, {key: 'ArrowRight'});
+ expect(handleChange).toHaveBeenLastCalledWith(51);
+
+ gcsSpy.mockRestore();
+ });
+
// --- Mark label click snapping ---
it('clicking a mark label snaps to that mark value, not pointer position', () => {
diff --git a/packages/core/src/Slider/Slider.tsx b/packages/core/src/Slider/Slider.tsx
index 0373c33efa..2b30a9adcb 100644
--- a/packages/core/src/Slider/Slider.tsx
+++ b/packages/core/src/Slider/Slider.tsx
@@ -827,12 +827,22 @@ export function Slider({ref, ...props}: SliderProps) {
const currentVal = values[thumbIndex];
let newVal: number;
+ // On a horizontal track the keyboard follows the track's direction:
+ // under RTL the value grows toward the left, so ArrowLeft increases and
+ // ArrowRight decreases (APG Slider). Up/Down and a vertical track are
+ // direction-free.
+ const towardMax =
+ isHorizontal && isRtlElement(trackRef.current)
+ ? 'ArrowLeft'
+ : 'ArrowRight';
+ const towardMin = towardMax === 'ArrowLeft' ? 'ArrowRight' : 'ArrowLeft';
+
switch (e.key) {
- case 'ArrowRight':
+ case towardMax:
case 'ArrowUp':
newVal = currentVal + step;
break;
- case 'ArrowLeft':
+ case towardMin:
case 'ArrowDown':
newVal = currentVal - step;
break;
@@ -876,6 +886,7 @@ export function Slider({ref, ...props}: SliderProps) {
},
[
isDisabled,
+ isHorizontal,
isRange,
values,
step,