@wordpress/components
Version:
UI components for WordPress.
72 lines (71 loc) • 2.33 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _wordpress_element = require("@wordpress/element");
let ___utils_hooks_index_ts = require("../utils/hooks/index.cjs");
let ___utils_math_ts = require("../utils/math.cjs");
// packages/components/src/range-control/utils.ts
/**
* A float supported clamp function for a specific value.
*
* @param value The value to clamp.
* @param min The minimum value.
* @param max The maximum value.
*
* @return A (float) number
*/
function floatClamp(value, min, max) {
if (typeof value !== "number") return null;
return parseFloat(`${(0, ___utils_math_ts.clamp)(value, min, max)}`);
}
/**
* Hook to store a clamped value, derived from props.
*
* @param settings
* @return The controlled value and the value setter.
*/
function useControlledRangeValue(settings) {
const { min, max, value: valueProp, initial } = settings;
const [state, setInternalState] = (0, ___utils_hooks_index_ts.useControlledState)(floatClamp(valueProp, min, max), {
initial: floatClamp(initial ?? null, min, max),
fallback: null
});
return [state, (0, _wordpress_element.useCallback)((nextValue) => {
if (nextValue === null) setInternalState(null);
else setInternalState(floatClamp(nextValue, min, max));
}, [
min,
max,
setInternalState
])];
}
const CSS_WIDE_KEYWORDS = /* @__PURE__ */ new Set([
"inherit",
"initial",
"unset",
"revert",
"revert-layer"
]);
/**
* Resolve CSS-wide keyword spellings for the private color classes without
* changing the original color value passed to the stylesheet.
*
* @param value The CSS color value.
*/
function getCSSWideKeyword(value) {
if (typeof value !== "string") return;
const keyword = value.replace(/\\([a-f\d]{1,6})[ \t\n\r\f]?|\\([^\n\r\f])|\/\*[\s\S]*?\*\//gi, (_match, hex, character) => {
if (hex) {
const codePoint = parseInt(hex, 16);
return String.fromCodePoint(codePoint > 0 && codePoint <= 1114111 ? codePoint : 65533);
}
return character ?? "";
}).replace(/^[ \t\n\r\f]+|[ \t\n\r\f]+$/g, "").toLowerCase();
return CSS_WIDE_KEYWORDS.has(keyword) ? keyword : void 0;
}
exports.floatClamp = floatClamp;
exports.getCSSWideKeyword = getCSSWideKeyword;
exports.useControlledRangeValue = useControlledRangeValue;
//# sourceMappingURL=utils.cjs.map