@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
77 lines (71 loc) • 2.58 kB
JavaScript
import { splitProps } from "reakit-utils";
import { warn } from "@chakra-ui/utils";
// Null Assertion
export var isNull = value => value == null;
export function clamp(value) {
var min = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : -Infinity;
var max = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : Infinity;
return Math.min(Math.max(value, min), max);
}
/**
* Clamps a value to ensure it stays within the min and max range.
*
* @param value the value to clamp
* @param min the minimum value
* @param max the maximum value
*
* @see https://github.com/chakra-ui/chakra-ui/blob/c38892760257b9bbf1b63c05f7f9ccf1684a90b0/packages/utils/src/number.ts
*/
export function clampValue(value, min, max) {
if (isNull(value)) return value;
warn({
condition: max < min,
message: "clamp: max cannot be less than min"
});
return Math.min(Math.max(value, min), max);
}
/**
* 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
*/
export function getOptimumValue(min, max) {
return max < min ? min : min + (max - min) / 2;
}
/**
* 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
*/
export function valueToPercent(value, min, max) {
return (value - min) * 100 / (max - min);
} // Function assertions
export function isFunction(value) {
return typeof value === "function";
}
export function isTouch() {
return Boolean("ontouchstart" in window || window.navigator.maxTouchPoints > 0 || // @ts-ignore
window.navigator.msMaxTouchPoints > 0);
}
export var dataAttr = condition => condition ? "" : undefined;
export var ariaAttr = condition => condition ? true : undefined;
export var cx = function cx() {
for (var _len = arguments.length, classNames = new Array(_len), _key = 0; _key < _len; _key++) {
classNames[_key] = arguments[_key];
}
return classNames.filter(Boolean).join(" ");
};
export function kebabCase(string) {
return string.toLowerCase().replace(/[^a-z0-9]/g, "-");
} // Split state props
export function splitStateProps(props, keys) {
var [stateProps, otherProps] = splitProps(props, keys);
return [stateProps, otherProps];
}
export * from "./date";
export * from "./useControllableState";
export * from "./useControlledState";
//# sourceMappingURL=index.js.map