@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
61 lines (54 loc) • 2.47 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 { useMemo } from "react";
import { calculateStatus, clamp, getOptimumValue, valueToPercent } from "./__utils";
/**
* Provides state for the `Meter` components.
* @example
* ```jsx
* const meter = useMeterState();
* <Meter state={meter} />
* ```
*/
export function useMeterState() {
var _props$low, _props$high, _props$optimum;
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
{
value: defaultValue = 0,
min = 0,
max = 1
} = _ref,
props = _objectWithoutProperties(_ref, _excluded);
var initialLow = (_props$low = props.low) !== null && _props$low !== void 0 ? _props$low : min;
var initialHigh = (_props$high = props.high) !== null && _props$high !== void 0 ? _props$high : max;
var initialOptimum = (_props$optimum = props.optimum) !== null && _props$optimum !== void 0 ? _props$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);
var state = useMemo(() => ({
value,
min,
max,
low,
optimum,
high,
status,
percent
}), [value, min, max, low, optimum, high, status, percent]);
return state;
}
//# sourceMappingURL=meter-state.js.map