@renderlesskit/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
52 lines (45 loc) • 2.27 kB
JavaScript
var _excluded = ["value", "min", "max"];
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import { getOptimumValue, valueToPercent } from "../utils";
import { calculateStatus, clamp } from "./helpers";
export var useMeterState = function useMeterState() {
var _sealed$low, _sealed$high, _sealed$optimum;
var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var {
value: defaultValue = 0,
min = 0,
max = 1
} = props,
sealed = _objectWithoutProperties(props, _excluded);
var initialLow = (_sealed$low = sealed.low) !== null && _sealed$low !== void 0 ? _sealed$low : min;
var initialHigh = (_sealed$high = sealed.high) !== null && _sealed$high !== void 0 ? _sealed$high : max;
var initialOptimum = (_sealed$optimum = sealed.optimum) !== null && _sealed$optimum !== void 0 ? _sealed$optimum : getOptimumValue(initialLow, initialHigh);
var value = clamp(defaultValue, min, max);
var optimum = clamp(initialOptimum, min, max);
var low = clamp(initialLow, min, max);
var high = clamp(initialHigh, min, max); // More inequalities handled
// low ≤ high (if both low and high are specified)
if (low >= high) low = high;
if (high <= low) high = low;
var status = calculateStatus({
value,
min,
max,
low,
optimum,
high
});
var percent = valueToPercent(value, min, max);
return {
value,
min,
max,
low,
optimum,
high,
status,
percent
};
};
//# sourceMappingURL=MeterState.js.map