Skip to content

Commit e244e69

Browse files
committed
fix: normalize min/max bounds when min > max
- Normalize range bounds before clamping values - Fix incorrect clamping when min is greater than max - Ensure negative and reversed ranges behave correctly
1 parent f9bcd2e commit e244e69

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ <h1>default attributes</h1>
175175

176176
<section>
177177
<h1>Negative attributes</h1>
178-
<range-slider min="-10" max="0"></range-slider>
178+
<range-slider min="-10" max="-80" step="5" value="-30"></range-slider>
179179
</section>
180180

181181
<section>

src/range-slider-element.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,13 @@ export class RangeSliderElement extends HTMLElement {
385385
#updateValue(index, value, dispatchEvents = []) {
386386
const oldValue = this.#value[index];
387387
const valuePrecision = Number(this.valuePrecision) || getPrescision(this.step) || 0;
388-
const thumbMinValue = this.#value[index - 1] || this.min;
389-
const thumbMaxValue = this.#value[index + 1] || this.max;
388+
389+
// Normalize bounds for negative attributes
390+
const rangeMin = Math.min(this.min, this.max);
391+
const rangeMax = Math.max(this.min, this.max);
392+
393+
const thumbMinValue = this.#value[index - 1] ?? rangeMin;
394+
const thumbMaxValue = this.#value[index + 1] ?? rangeMax;
390395

391396
// Thumb min, max constrain
392397
const safeValue = Math.min(Math.max(value, thumbMinValue), thumbMaxValue);

test/range-slider.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ test('custom attributes', async () => {
5454
expect(handleEvent).not.toHaveBeenCalled();
5555
});
5656

57+
test('negative attributes', async () => {
58+
render('<range-slider min="-10" max="-80" step="10" value="-30"></range-slider>');
59+
60+
const element = document.querySelector('range-slider');
61+
expect(element).toHaveValue('-30');
62+
});
63+
5764
test('programmatic value property changes', async () => {
5865
render('<range-slider></range-slider>');
5966

0 commit comments

Comments
 (0)