@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
72 lines (62 loc) • 2.93 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useMeterState = useMeterState;
var _react = require("react");
var _utils = require("./__utils");
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; }
/**
* Provides state for the `Meter` components.
* @example
* ```jsx
* const meter = useMeterState();
* <Meter state={meter} />
* ```
*/
function useMeterState() {
var _props$low, _props$high, _props$optimum;
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$value = _ref.value,
defaultValue = _ref$value === void 0 ? 0 : _ref$value,
_ref$min = _ref.min,
min = _ref$min === void 0 ? 0 : _ref$min,
_ref$max = _ref.max,
max = _ref$max === void 0 ? 1 : _ref$max,
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 : (0, _utils.getOptimumValue)(initialLow, initialHigh);
var value = (0, _utils.clamp)(defaultValue, min, max);
var optimum = (0, _utils.clamp)(initialOptimum, min, max);
var low = (0, _utils.clamp)(initialLow, min, max);
var high = (0, _utils.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 = (0, _utils.calculateStatus)({
value: value,
min: min,
max: max,
low: low,
optimum: optimum,
high: high
});
var percent = (0, _utils.valueToPercent)(value, min, max);
var state = (0, _react.useMemo)(function () {
return {
value: value,
min: min,
max: max,
low: low,
optimum: optimum,
high: high,
status: status,
percent: percent
};
}, [value, min, max, low, optimum, high, status, percent]);
return state;
}
//# sourceMappingURL=meter-state.js.map