@julo-ui/sliders
Version:
A React Slider component that implements input[type='range']
79 lines (76 loc) • 2.68 kB
JavaScript
import {
roundValueToStep
} from "./chunk-Y2VS5NAR.mjs";
// src/range-slider/utils.ts
import { clampValue } from "@julo-ui/number-utils";
function getValueBounds(value, min, max, spacing) {
return value.map((_v, i) => {
const _min = i === 0 ? min : value[i - 1] + spacing;
const _max = i === value.length - 1 ? max : value[i + 1] - spacing;
return { min: _min, max: _max };
});
}
function getIds(id) {
return {
root: `slider-root-${id}`,
getThumb: (i) => `slider-thumb-${id}-${i}`,
getInput: (i) => `slider-input-${id}-${i}`,
track: `slider-track-${id}`,
innerTrack: `slider-filled-track-${id}`,
getMarker: (i) => `slider-marker-${id}-${i}`,
output: `slider-output-${id}`
};
}
function getThumbStateOnChange({
bounds,
index,
values,
pointerValue,
prevValue,
step,
isDisableSwap,
distanceBetweenThumbs
}) {
const prevValueAtIndex = prevValue[index];
const boundsAtIndex = bounds[index];
let valueAtIndex = clampValue(
parseFloat(roundValueToStep(pointerValue, boundsAtIndex.min, step)),
isDisableSwap ? boundsAtIndex.min : bounds[0].min,
isDisableSwap ? boundsAtIndex.max : bounds[bounds.length - 1].max
);
const isDecreasing = pointerValue < prevValueAtIndex;
const isValueExceedBoundedValue = !isDisableSwap && (isDecreasing ? valueAtIndex <= boundsAtIndex.min - distanceBetweenThumbs : valueAtIndex >= boundsAtIndex.max + distanceBetweenThumbs);
if (isValueExceedBoundedValue) {
const totalThumb = prevValue.length;
let isFoundNewThumb = false;
let isNoMoreThumb = false;
values[index] = isDecreasing ? boundsAtIndex.min : boundsAtIndex.max;
for (let i = isDecreasing ? index - 1 : index + 1; !isFoundNewThumb && !isNoMoreThumb; isDecreasing ? i-- : i++) {
if (i >= totalThumb || i < 0) {
isNoMoreThumb = true;
if (isDecreasing ? valueAtIndex < boundsAtIndex.min : valueAtIndex > boundsAtIndex.max) {
valueAtIndex = isDecreasing ? boundsAtIndex.min : boundsAtIndex.max;
}
continue;
}
if (!isDecreasing ? valueAtIndex >= bounds[i].min && valueAtIndex < bounds[i].max + distanceBetweenThumbs : valueAtIndex > bounds[i].min - distanceBetweenThumbs && valueAtIndex <= bounds[i].max) {
isFoundNewThumb = true;
index = i;
}
}
} else {
if (!isDecreasing && pointerValue > boundsAtIndex.max) {
valueAtIndex = boundsAtIndex.max;
}
if (isDecreasing && pointerValue < boundsAtIndex.min) {
valueAtIndex = boundsAtIndex.min;
}
}
values[index] = valueAtIndex;
return { index, value: values };
}
export {
getValueBounds,
getIds,
getThumbStateOnChange
};