UNPKG

@wordpress/components

Version:
65 lines (64 loc) • 2.04 kB
import { useCallback } from "@wordpress/element"; import { useControlledState } from "../utils/hooks/index.mjs"; import { clamp } from "../utils/math.mjs"; // 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(`${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] = useControlledState(floatClamp(valueProp, min, max), { initial: floatClamp(initial ?? null, min, max), fallback: null }); return [state, 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; } export { floatClamp, getCSSWideKeyword, useControlledRangeValue }; //# sourceMappingURL=utils.mjs.map