Skip to content

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

TypeScript

fix(slider): round snapped values to the min/step decimal precision (#3897)

Shahrier Islam Arham63b0660add21
diff --git a/.changeset/slider-step-precision.md b/.changeset/slider-step-precision.md
new file mode 100644
index 0000000000..1386111472
--- /dev/null
+++ b/.changeset/slider-step-precision.md
@@ -0,0 +1,8 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Slider: round snapped values to the min/step decimal precision
+
+With fractional steps, `min + steps * step` accumulated binary floating-point error, so a keyboard nudge on a `step={0.1}` slider emitted `0.30000000000000004` through `onChange`/`onChangeEnd` (and into `aria-valuenow`/the value tooltip once the consumer echoed it back). Snapped values are now rounded to the combined decimal precision of `min` and `step`, which removes only the error — exact steps are unaffected.
+@arham766
diff --git a/packages/core/src/Slider/Slider.test.tsx b/packages/core/src/Slider/Slider.test.tsx
index b54991ed6a..2175e635bb 100644
--- a/packages/core/src/Slider/Slider.test.tsx
+++ b/packages/core/src/Slider/Slider.test.tsx
@@ -394,6 +394,58 @@ describe('Slider', () => {
expect(handleChangeEnd).toHaveBeenCalledWith(55);
});
+ // --- Fractional step precision ---
+
+ it('emits exact decimal values for fractional steps on keyboard', async () => {
+ const user = userEvent.setup();
+ const handleChange = vi.fn();
+ const handleChangeEnd = vi.fn();
+ render(
+ <Slider
+ label="Opacity"
+ value={0.2}
+ min={0}
+ max={1}
+ step={0.1}
+ onChange={handleChange}
+ onChangeEnd={handleChangeEnd}
+ />,
+ );
+ const slider = screen.getByRole('slider');
+ act(() => {
+ slider.focus();
+ });
+ await user.keyboard('{ArrowRight}');
+ // 0.2 + 0.1 must not surface binary float error (0.30000000000000004)
+ expect(handleChange).toHaveBeenCalledWith(0.3);
+ expect(handleChangeEnd).toHaveBeenCalledWith(0.3);
+ });
+
+ it('emits exact decimal values for fractional steps in range mode', async () => {
+ const user = userEvent.setup();
+ const handleChange = vi.fn();
+ const handleChangeEnd = vi.fn();
+ render(
+ <Slider
+ label="Range"
+ value={[0.2, 0.6] as [number, number]}
+ min={0}
+ max={1}
+ step={0.1}
+ onChange={handleChange}
+ onChangeEnd={handleChangeEnd}
+ />,
+ );
+ const sliders = screen.getAllByRole('slider');
+ act(() => {
+ sliders[1].focus();
+ });
+ await user.keyboard('{ArrowRight}');
+ // 0.6 + 0.1 snaps to 7 * 0.1, which is 0.7000000000000001 without rounding
+ expect(handleChange).toHaveBeenCalledWith([0.2, 0.7]);
+ expect(handleChangeEnd).toHaveBeenCalledWith([0.2, 0.7]);
+ });
+
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 2051c27153..26e49a3036 100644
--- a/packages/core/src/Slider/Slider.tsx
+++ b/packages/core/src/Slider/Slider.tsx
@@ -341,12 +341,39 @@ function clamp(val: number, min: number, max: number): number {
return Math.min(Math.max(val, min), max);
}
+/**
+ * Number of decimal places a value carries, including values in exponent
+ * notation (e.g. 1e-7 → 7). Used to round away binary floating-point error
+ * after step arithmetic.
+ */
+function getDecimalPrecision(num: number): number {
+ if (Math.abs(num) < 1) {
+ const parts = num.toExponential().split('e-');
+ if (parts.length === 2) {
+ const mantissaDecimals = parts[0].split('.')[1]?.length ?? 0;
+ return mantissaDecimals + parseInt(parts[1], 10);
+ }
+ }
+ const decimalPart = String(num).split('.')[1];
+ return decimalPart ? decimalPart.length : 0;
+}
+
function snapToStep(val: number, min: number, step: number): number {
if (step <= 0) {
return val;
}
const steps = Math.round((val - min) / step);
- return min + steps * step;
+ const snapped = min + steps * step;
+ // `min + steps * step` accumulates binary floating-point error with
+ // fractional steps (0 + 3 * 0.1 → 0.30000000000000004), which leaks into
+ // onChange/onChangeEnd payloads, aria-valuenow, and the value tooltip.
+ // Snapped values can never carry more decimals than min/step combined, so
+ // rounding to that precision removes only the error.
+ const precision = Math.min(
+ Math.max(getDecimalPrecision(min), getDecimalPrecision(step)),
+ 20, // toFixed() throws past 20 digits
+ );
+ return Number(snapped.toFixed(precision));
}
function getPercent(val: number, min: number, max: number): number {