UNPKG

@wordpress/components

Version:
35 lines (34 loc) 955 B
import { useCallback } from "@wordpress/element"; import { useControlledState } from "../utils/hooks"; import { clamp } from "../utils/math"; function floatClamp(value, min, max) { if (typeof value !== "number") { return null; } return parseFloat(`${clamp(value, min, max)}`); } function useControlledRangeValue(settings) { const { min, max, value: valueProp, initial } = settings; const [state, setInternalState] = useControlledState(floatClamp(valueProp, min, max), { initial: floatClamp(initial !== null && initial !== void 0 ? initial : null, min, max), fallback: null }); const setState = useCallback((nextValue) => { if (nextValue === null) { setInternalState(null); } else { setInternalState(floatClamp(nextValue, min, max)); } }, [min, max, setInternalState]); return [state, setState]; } export { floatClamp, useControlledRangeValue }; //# sourceMappingURL=utils.js.map