@mantine/hooks
Version:
A collection of 50+ hooks for state and UI management
38 lines (37 loc) • 989 B
JavaScript
"use client";
import { clamp } from "../utils/clamp/clamp.mjs";
import { useCallback, useState } from "react";
//#region packages/@mantine/hooks/src/use-counter/use-counter.ts
const DEFAULT_OPTIONS = {
min: -Infinity,
max: Infinity
};
function useCounter(initialValue = 0, options) {
const { min, max, step: _step = 1 } = {
...DEFAULT_OPTIONS,
...options
};
const step = Math.abs(_step);
const [count, setCount] = useState(clamp(initialValue, min, max));
return [count, {
increment: useCallback(() => setCount((current) => clamp(current + step, min, max)), [
min,
max,
step
]),
decrement: useCallback(() => setCount((current) => clamp(current - step, min, max)), [
min,
max,
step
]),
set: useCallback((value) => setCount(clamp(value, min, max)), [min, max]),
reset: useCallback(() => setCount(clamp(initialValue, min, max)), [
initialValue,
min,
max
])
}];
}
//#endregion
export { useCounter };
//# sourceMappingURL=use-counter.mjs.map