82 lines
2.7 KiB
JavaScript
82 lines
2.7 KiB
JavaScript
'use client'
|
|
"use strict";
|
|
var __defProp = Object.defineProperty;
|
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
var __export = (target, all) => {
|
|
for (var name in all)
|
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
};
|
|
var __copyProps = (to, from, except, desc) => {
|
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
for (let key of __getOwnPropNames(from))
|
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
}
|
|
return to;
|
|
};
|
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
|
|
// src/use-spinner.ts
|
|
var use_spinner_exports = {};
|
|
__export(use_spinner_exports, {
|
|
useSpinner: () => useSpinner
|
|
});
|
|
module.exports = __toCommonJS(use_spinner_exports);
|
|
var import_react_use_interval = require("@chakra-ui/react-use-interval");
|
|
var import_react = require("react");
|
|
var CONTINUOUS_CHANGE_INTERVAL = 50;
|
|
var CONTINUOUS_CHANGE_DELAY = 300;
|
|
function useSpinner(increment, decrement) {
|
|
const [isSpinning, setIsSpinning] = (0, import_react.useState)(false);
|
|
const [action, setAction] = (0, import_react.useState)(null);
|
|
const [runOnce, setRunOnce] = (0, import_react.useState)(true);
|
|
const timeoutRef = (0, import_react.useRef)(null);
|
|
const removeTimeout = () => clearTimeout(timeoutRef.current);
|
|
(0, import_react_use_interval.useInterval)(
|
|
() => {
|
|
if (action === "increment") {
|
|
increment();
|
|
}
|
|
if (action === "decrement") {
|
|
decrement();
|
|
}
|
|
},
|
|
isSpinning ? CONTINUOUS_CHANGE_INTERVAL : null
|
|
);
|
|
const up = (0, import_react.useCallback)(() => {
|
|
if (runOnce) {
|
|
increment();
|
|
}
|
|
timeoutRef.current = setTimeout(() => {
|
|
setRunOnce(false);
|
|
setIsSpinning(true);
|
|
setAction("increment");
|
|
}, CONTINUOUS_CHANGE_DELAY);
|
|
}, [increment, runOnce]);
|
|
const down = (0, import_react.useCallback)(() => {
|
|
if (runOnce) {
|
|
decrement();
|
|
}
|
|
timeoutRef.current = setTimeout(() => {
|
|
setRunOnce(false);
|
|
setIsSpinning(true);
|
|
setAction("decrement");
|
|
}, CONTINUOUS_CHANGE_DELAY);
|
|
}, [decrement, runOnce]);
|
|
const stop = (0, import_react.useCallback)(() => {
|
|
setRunOnce(true);
|
|
setIsSpinning(false);
|
|
removeTimeout();
|
|
}, []);
|
|
(0, import_react.useEffect)(() => {
|
|
return () => removeTimeout();
|
|
}, []);
|
|
return { up, down, stop, isSpinning };
|
|
}
|
|
// Annotate the CommonJS export names for ESM import in node:
|
|
0 && (module.exports = {
|
|
useSpinner
|
|
});
|
|
//# sourceMappingURL=use-spinner.js.map
|