Skip to content

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

TypeScript

fix(Slider): End reaches max even when the step grid skips it (#7484)

Cindy Zhangb2633d1184c2
diff --git a/.changeset/slider-end-max.md b/.changeset/slider-end-max.md
new file mode 100644
index 0000000000..3e8ebb9efd
--- /dev/null
+++ b/.changeset/slider-end-max.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `Slider`: End now reaches `max` even when the step grid does not land on it (for `max={100}` with `step={3}`, End stopped at 99). Arrow and Page key results still snap to the nearest step and then clamp.
+
+@cixzhang
diff --git a/packages/core/src/Slider/Slider.spec.md b/packages/core/src/Slider/Slider.spec.md
index 4a77aec95e..44b417df93 100644
--- a/packages/core/src/Slider/Slider.spec.md
+++ b/packages/core/src/Slider/Slider.spec.md
@@ -128,7 +128,7 @@ value-tooltip behavior; AR1 records the keyboard model and its one correction.
ArrowUp and ArrowDown step by `step`; on a horizontal track the horizontal
arrows follow the track's direction — ArrowRight increases and ArrowLeft
decreases under LTR, and under RTL ArrowLeft increases and ArrowRight
- decreases; PageUp and PageDown step by ten steps; Home moves to `min` and End to `max`. Implementation gap: End snaps to the nearest step and then clamps to the bounds, so it stops short of `max` when `max` lies less than half a step above the step below it (99 for `max=100, step=3`), while ArrowRight and PageUp reach `max`. In a range, a thumb never crosses the other past the minimum
+ decreases; PageUp and PageDown step by ten steps; Home moves to `min` and End to `max`. In a range, a thumb never crosses the other past the minimum
separation.
## Design relationships
diff --git a/packages/core/src/Slider/Slider.test.tsx b/packages/core/src/Slider/Slider.test.tsx
index 158985dfaf..961428c78c 100644
--- a/packages/core/src/Slider/Slider.test.tsx
+++ b/packages/core/src/Slider/Slider.test.tsx
@@ -510,6 +510,69 @@ describe('Slider', () => {
expect(handleChangeEnd).toHaveBeenCalledWith([0.2, 0.7]);
});
+ it('End reaches an off-grid max from a non-zero min, and Home still reaches min', async () => {
+ const user = userEvent.setup();
+ const onChange = vi.fn();
+ const onChangeEnd = vi.fn();
+ // From min 1 with step 3 the grid is 1, 4, …, 100; max 101 is off it.
+ render(
+ <Slider
+ label="Zoom"
+ min={1}
+ max={101}
+ step={3}
+ value={49}
+ onChange={onChange}
+ onChangeEnd={onChangeEnd}
+ />,
+ );
+ const thumb = screen.getByRole('slider');
+ thumb.focus();
+
+ await user.keyboard('{End}');
+ expect(onChange).toHaveBeenLastCalledWith(101);
+ expect(onChangeEnd).toHaveBeenLastCalledWith(101);
+
+ await user.keyboard('{Home}');
+ expect(onChange).toHaveBeenLastCalledWith(1);
+ expect(onChangeEnd).toHaveBeenLastCalledWith(1);
+ });
+
+ it('End lands on max when max is off the step grid, while arrows still snap', async () => {
+ const user = userEvent.setup();
+ const onChange = vi.fn();
+ function Owner() {
+ const [value, setValue] = useState(48);
+ return (
+ <Slider
+ label="Zoom"
+ min={0}
+ max={100}
+ step={3}
+ value={value}
+ onChange={(next: number) => {
+ onChange(next);
+ setValue(next);
+ }}
+ />
+ );
+ }
+ render(<Owner />);
+ const thumb = screen.getByRole('slider');
+ thumb.focus();
+
+ await user.keyboard('{End}');
+ expect(onChange).toHaveBeenLastCalledWith(100);
+
+ // ORD1 for arrows and Page keys is unchanged: from max (100, off the
+ // grid) ArrowLeft targets 97 and snaps to 96; from 96 PageUp targets 126
+ // and clamps to 100, the bound.
+ await user.keyboard('{ArrowLeft}');
+ expect(onChange).toHaveBeenLastCalledWith(96);
+ await user.keyboard('{PageUp}');
+ expect(onChange).toHaveBeenLastCalledWith(100);
+ });
+
it('fires onChangeEnd on keyboard Home/End with correct value', async () => {
const user = userEvent.setup();
const handleChangeEnd = vi.fn();
diff --git a/packages/core/src/Slider/Slider.tsx b/packages/core/src/Slider/Slider.tsx
index 2b30a9adcb..7cd2125f4a 100644
--- a/packages/core/src/Slider/Slider.tsx
+++ b/packages/core/src/Slider/Slider.tsx
@@ -668,11 +668,18 @@ export function Slider({ref, ...props}: SliderProps) {
);
const updateValue = useCallback(
- (thumbIndex: number, newVal: number) => {
+ (thumbIndex: number, newVal: number, snap = true) => {
if (isDisabled) {
return;
}
- const clamped = clamp(snapToStep(newVal, min, step), min, max);
+ // Home and End pass the bound itself (`snap: false`). The step grid is
+ // anchored at `min`, so Home always landed there; `max` may lie off the
+ // grid (max 100 with step 3 snapped End to 99, not 100).
+ const clamped = clamp(
+ snap ? snapToStep(newVal, min, step) : newVal,
+ min,
+ max,
+ );
if (isRange) {
const currentValues = [...values] as [number, number];
@@ -863,8 +870,13 @@ export function Slider({ref, ...props}: SliderProps) {
}
e.preventDefault();
- const clamped = clamp(snapToStep(newVal, min, step), min, max);
- updateValue(thumbIndex, newVal);
+ const toBound = e.key === 'Home' || e.key === 'End';
+ const clamped = clamp(
+ toBound ? newVal : snapToStep(newVal, min, step),
+ min,
+ max,
+ );
+ updateValue(thumbIndex, newVal, !toBound);
// Compute exact post-update values so onChangeEnd reports the correct
// value even before React batches the state update.
diff --git a/packages/core/src/Slider/__tests__/Slider.a11y.known-failures.ts b/packages/core/src/Slider/__tests__/Slider.a11y.known-failures.ts
index 5512e87b64..be5ab70a85 100644
--- a/packages/core/src/Slider/__tests__/Slider.a11y.known-failures.ts
+++ b/packages/core/src/Slider/__tests__/Slider.a11y.known-failures.ts
@@ -9,18 +9,4 @@
import type {KnownFailure} from '@astryxdesign/a11y-spec';
-export const SLIDER_KNOWN_FAILURES: ReadonlyArray<KnownFailure> = [
- {
- expectation: 'slider.keys.home-end',
- binding: 'Slider',
- state: 'step-three',
- evidenceLayer: 'real-browser',
- failureEquals: 'End left aria-valuenow at 99, not 100',
- standardsReference:
- 'APG Slider keyboard interaction — End sets the slider to the last allowed value in its range; Astryx component:Slider AR1 (End moves to max)',
- userImpact:
- 'With a step that does not divide the range, End stops one step short of the maximum while ArrowRight and PageUp can still reach it.',
- reason:
- 'This test-only binding records the gap named in the Slider record; the endpoint fix is its own change.',
- },
-];
+export const SLIDER_KNOWN_FAILURES: ReadonlyArray<KnownFailure> = [];