@adaptui/react
Version:
Collection of headless components/hooks that are accessible, composable, customizable from low level to build your own UI & Design System powered by Reakit
86 lines (71 loc) • 2.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.calculateStatus = void 0;
exports.clamp = clamp;
exports.getOptimumValue = getOptimumValue;
exports.isInRange = void 0;
exports.valueToPercent = valueToPercent;
/**
* Handle Inequalities with received values
*
* minimum ≤ value ≤ maximum
* minimum ≤ low ≤ maximum (if low is specified)
* minimum ≤ high ≤ maximum (if high is specified)
* minimum ≤ optimum ≤ maximum (if optimum is specified)
*
* @see https://html.spec.whatwg.org/multipage/form-elements.html#the-meter-element:attr-meter-max-3:~:text=following%20inequalities%20must%20hold
*/
var calculateStatus = function calculateStatus(props) {
var value = props.value,
optimum = props.optimum,
min = props.min,
max = props.max,
low = props.low,
high = props.high; // This check always comes first
if (isInRange(optimum, low, high)) {
if (isInRange(value, low, high)) return "safe";
return "caution";
}
if (isInRange(optimum, min, low)) {
if (isInRange(value, min, low)) return "safe";
if (value > low && value <= high) return "caution";
return "danger";
}
if (isInRange(optimum, high, max)) {
if (isInRange(value, high, max)) return "safe";
if (value < high && value >= low) return "caution";
return "danger";
} // A safe return
return "safe";
};
exports.calculateStatus = calculateStatus;
var isInRange = function isInRange(value, min, max) {
return value >= min && value <= max;
};
exports.isInRange = isInRange;
function clamp(value, min, max) {
if (value == null) return 0;
return Math.min(Math.max(value, min), max);
}
/**
* Convert a value to percentage based on lower and upper bound values
*
* @param value the value in number
* @param min the minimum value
* @param max the maximum value
*/
function valueToPercent(value, min, max) {
return (value - min) * 100 / (max - min);
}
/**
* The candidate optimum point is the midpoint between the minimum value and
* the maximum value.
*
* @see https://html.spec.whatwg.org/multipage/form-elements.html#the-meter-element:attr-meter-high-8:~:text=boundary.-,The%20optimum%20point
*/
function getOptimumValue(min, max) {
return max < min ? min : min + (max - min) / 2;
}
//# sourceMappingURL=__utils.js.map