Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
margin-block: 1em;
text-align: center;
user-select: none;
font-variant-numeric: tabular-nums;
}

/* Debug styles */
Expand Down Expand Up @@ -224,6 +225,7 @@ <h1><code>multi thumb</code></h1>
</div>
</range-slider>

<p><code>orientation="vertical"</code></p>
<range-slider value="10,80" orientation="vertical">
<div data-track></div>
<div data-track-fill></div>
Expand All @@ -232,6 +234,16 @@ <h1><code>multi thumb</code></h1>
<div data-thumb></div>
</div>
</range-slider>

<p><code>min-steps-between-thumbs</code></p>
<range-slider value="10,40" step="1" min-steps-between-thumbs="1" name="price-range">
<div data-track></div>
<div data-track-fill></div>
<div data-runnable-track>
<div data-thumb aria-label="Minimum Price"></div>
<div data-thumb aria-label="Maximum Price"></div>
</div>
</range-slider>
</section>

<section>
Expand Down
31 changes: 29 additions & 2 deletions src/range-slider-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ export class RangeSliderElement extends HTMLElement {
get valuePrecision() {
return this.getAttribute('value-precision') || '';
}
/**
* @returns {number}
*/
get minStepsBetweenThumbs() {
return this.hasAttribute('min-steps-between-thumbs')
? Number(this.getAttribute('min-steps-between-thumbs'))
: 0;
}
get #isVertical() {
return Boolean(this.getAttribute('orientation') === 'vertical');
}
Expand Down Expand Up @@ -148,6 +156,12 @@ export class RangeSliderElement extends HTMLElement {
set valuePrecision(precision) {
this.setAttribute('value-precision', precision);
}
/**
* @param {number} steps
*/
set minStepsBetweenThumbs(steps) {
this.setAttribute('min-steps-between-thumbs', steps);
}

/**
* Form data support
Expand Down Expand Up @@ -366,8 +380,21 @@ export class RangeSliderElement extends HTMLElement {
#updateValue(index, value, dispatchEvents = []) {
const oldValue = this.#value[index];
const valuePrecision = Number(this.valuePrecision) || getPrescision(this.step) || 0;
const thumbMinValue = this.#value[index - 1] || this.min;
const thumbMaxValue = this.#value[index + 1] || this.max;

let thumbMinValue = this.#value[index - 1] || this.min;
let thumbMaxValue = this.#value[index + 1] || this.max;

// Avoid thumbs with equal values
if (this.#isMultiThumb && this.minStepsBetweenThumbs) {
// Skip first thumb
if (index > 0) {
thumbMinValue = thumbMinValue + this.minStepsBetweenThumbs * this.step;
}
// Skip last thumb
if (index < this.#thumbs.length - 1) {
thumbMaxValue = thumbMaxValue - this.minStepsBetweenThumbs * this.step;
}
}

// Thumb min, max constrain
const safeValue = Math.min(Math.max(value, thumbMinValue), thumbMaxValue);
Expand Down
Loading