@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
129 lines (99 loc) • 4.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useSpinner = useSpinner;
var _react = require("react");
var _hooks = require("@chakra-ui/hooks");
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
/**
* When click and hold on a button - the speed of auto changing the value.
*/
var CONTINUOUS_CHANGE_INTERVAL = 50;
/**
* When click and hold on a button - the delay before auto changing the value.
*/
var CONTINUOUS_CHANGE_DELAY = 300;
/**
* React hook used in the number input to spin it's
* value on long press of the spin buttons
*
* @param increment the function to increment
* @param decrement the function to decrement
*/
function useSpinner(increment, decrement) {
/**
* To keep incrementing/decrementing on press, we call that `spinning`
*/
var _useState = (0, _react.useState)(false),
_useState2 = _slicedToArray(_useState, 2),
isSpinning = _useState2[0],
setIsSpinning = _useState2[1]; // This state keeps track of the action ("increment" or "decrement")
var _useState3 = (0, _react.useState)(null),
_useState4 = _slicedToArray(_useState3, 2),
action = _useState4[0],
setAction = _useState4[1]; // To increment the value the first time you mousedown, we call that `runOnce`
var _useState5 = (0, _react.useState)(true),
_useState6 = _slicedToArray(_useState5, 2),
runOnce = _useState6[0],
setRunOnce = _useState6[1]; // Store the timeout instance id in a ref, so we can clear the timeout later
var timeoutRef = (0, _react.useRef)(null); // Clears the timeout from memory
var removeTimeout = function removeTimeout() {
return clearTimeout(timeoutRef.current);
};
/**
* useInterval hook provides a performant way to
* update the state value at specific interval
*/
(0, _hooks.useInterval)(function () {
if (action === "increment") {
increment();
}
if (action === "decrement") {
decrement();
}
}, isSpinning ? CONTINUOUS_CHANGE_INTERVAL : null); // Function to activate the spinning and increment the value
var up = (0, _react.useCallback)(function () {
// increment the first fime
if (runOnce) {
increment();
} // after a delay, keep incrementing at interval ("spinning up")
timeoutRef.current = setTimeout(function () {
setRunOnce(false);
setIsSpinning(true);
setAction("increment");
}, CONTINUOUS_CHANGE_DELAY);
}, [increment, runOnce]); // Function to activate the spinning and increment the value
var down = (0, _react.useCallback)(function () {
// decrement the first fime
if (runOnce) {
decrement();
} // after a delay, keep decrementing at interval ("spinning down")
timeoutRef.current = setTimeout(function () {
setRunOnce(false);
setIsSpinning(true);
setAction("decrement");
}, CONTINUOUS_CHANGE_DELAY);
}, [decrement, runOnce]); // Function to stop spinng (useful for mouseup, keyup handlers)
var stop = (0, _react.useCallback)(function () {
setRunOnce(true);
setIsSpinning(false);
removeTimeout();
}, []);
/**
* If the component unmounts while spinning,
* let's clear the timeout as well
*/
(0, _hooks.useUnmountEffect)(removeTimeout);
return {
up: up,
down: down,
stop: stop
};
}
//# sourceMappingURL=useSpinner.js.map