@julo-ui/sliders
Version:
A React Slider component that implements input[type='range']
111 lines (107 loc) • 4.12 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/range-slider/utils.ts
var utils_exports = {};
__export(utils_exports, {
getIds: () => getIds,
getThumbStateOnChange: () => getThumbStateOnChange,
getValueBounds: () => getValueBounds
});
module.exports = __toCommonJS(utils_exports);
var import_number_utils2 = require("@julo-ui/number-utils");
// src/utils.ts
var import_number_utils = require("@julo-ui/number-utils");
function roundValueToStep(value, from, step) {
const nextValue = Math.round((value - from) / step) * step + from;
const precision = (0, import_number_utils.countDecimalPlaces)(step);
return (0, import_number_utils.toPreciseDecimal)(nextValue, precision);
}
// src/range-slider/utils.ts
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 = (0, import_number_utils2.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 };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getIds,
getThumbStateOnChange,
getValueBounds
});