@aiszlab/relax 1.2.38 → 1.2.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/dist/_virtual/_rollupPluginBabelHelpers.js +153 -0
  2. package/dist/dom/contains.js +16 -16
  3. package/dist/dom/scroll-to.js +59 -43
  4. package/dist/hooks/use-boolean.js +23 -11
  5. package/dist/hooks/use-controlled-state.js +26 -23
  6. package/dist/hooks/use-counter.js +48 -21
  7. package/dist/hooks/use-debounce-callback.js +57 -33
  8. package/dist/hooks/use-default.js +2 -2
  9. package/dist/hooks/use-event.js +7 -5
  10. package/dist/hooks/use-focus.js +25 -10
  11. package/dist/hooks/use-force-update.js +12 -6
  12. package/dist/hooks/use-hover.js +19 -10
  13. package/dist/hooks/use-image-loader.js +42 -29
  14. package/dist/hooks/use-memorable.js +9 -9
  15. package/dist/hooks/use-mount.js +5 -5
  16. package/dist/hooks/use-mounted.js +5 -5
  17. package/dist/hooks/use-refs.js +18 -16
  18. package/dist/hooks/use-scroll-locker.js +75 -62
  19. package/dist/hooks/use-scrollable.js +33 -31
  20. package/dist/hooks/use-throttle-callback.d.ts +3 -3
  21. package/dist/hooks/use-throttle-callback.js +61 -35
  22. package/dist/hooks/use-timeout.js +8 -9
  23. package/dist/hooks/use-toggleable.js +165 -124
  24. package/dist/hooks/use-update-effect.js +13 -14
  25. package/dist/index.d.ts +1 -0
  26. package/dist/index.js +7 -6
  27. package/dist/is/is-array.js +2 -2
  28. package/dist/is/is-complex.js +4 -2
  29. package/dist/is/is-dom-usable.js +2 -2
  30. package/dist/is/is-empty.js +13 -13
  31. package/dist/is/is-function.js +2 -2
  32. package/dist/is/is-mobile.js +6 -9
  33. package/dist/is/is-null.js +2 -2
  34. package/dist/is/is-overflow.js +7 -7
  35. package/dist/is/is-refable.js +20 -19
  36. package/dist/is/is-state-getter.js +2 -2
  37. package/dist/is/is-style-element.js +2 -2
  38. package/dist/is/is-thenable/index.js +9 -0
  39. package/dist/is/is-thenable/index.test.d.ts +1 -0
  40. package/dist/is/is-undefined.js +2 -2
  41. package/dist/is/is-void.js +2 -2
  42. package/dist/utils/{chain.d.ts → chain/index.d.ts} +1 -1
  43. package/dist/utils/chain/index.js +15 -0
  44. package/dist/utils/chain/index.test.d.ts +1 -0
  45. package/dist/utils/{clamp.js → clamp/index.js} +2 -2
  46. package/dist/utils/clamp/index.test.d.ts +1 -0
  47. package/dist/utils/clone/index.test.d.ts +1 -0
  48. package/dist/utils/debounce/index.d.ts +23 -0
  49. package/dist/utils/debounce/index.js +32 -0
  50. package/dist/utils/debounce/index.test.d.ts +1 -0
  51. package/dist/utils/{effect.d.ts → effect/index.d.ts} +1 -1
  52. package/dist/utils/effect/index.js +16 -0
  53. package/dist/utils/effect/index.test.d.ts +1 -0
  54. package/dist/utils/exclude/index.d.ts +7 -0
  55. package/dist/utils/exclude/index.js +14 -0
  56. package/dist/utils/exclude/index.test.d.ts +1 -0
  57. package/dist/utils/range.js +7 -3
  58. package/dist/utils/set-style.js +14 -10
  59. package/dist/utils/{throttle.d.ts → throttle/index.d.ts} +3 -3
  60. package/dist/utils/throttle/index.js +32 -0
  61. package/dist/utils/throttle/index.test.d.ts +1 -0
  62. package/dist/utils/to-array/index.d.ts +18 -0
  63. package/dist/utils/to-array/index.js +25 -0
  64. package/dist/utils/to-array/index.test.d.ts +1 -0
  65. package/dist/utils/to-function.js +8 -6
  66. package/dist/utils/unique.js +4 -4
  67. package/dist/utils/waitable/index.d.ts +36 -0
  68. package/dist/utils/waitable/index.js +90 -0
  69. package/package.json +7 -4
  70. package/dist/is/is-thenable.js +0 -9
  71. package/dist/utils/chain.js +0 -9
  72. package/dist/utils/debounce.d.ts +0 -56
  73. package/dist/utils/debounce.js +0 -77
  74. package/dist/utils/effect.js +0 -18
  75. package/dist/utils/throttle.js +0 -17
  76. package/dist/utils/to-array.d.ts +0 -5
  77. package/dist/utils/to-array.js +0 -12
  78. /package/dist/is/{is-thenable.d.ts → is-thenable/index.d.ts} +0 -0
  79. /package/dist/utils/{clamp.d.ts → clamp/index.d.ts} +0 -0
  80. /package/dist/utils/{clone.d.ts → clone/index.d.ts} +0 -0
@@ -0,0 +1,153 @@
1
+ function _assertClassBrand(e, t, n) {
2
+ if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n;
3
+ throw new TypeError("Private element is not present on this object");
4
+ }
5
+ function _classPrivateFieldGet2(s, a) {
6
+ return s.get(_assertClassBrand(s, a));
7
+ }
8
+ function _classPrivateFieldSet2(s, a, r) {
9
+ return s.set(_assertClassBrand(s, a), r), r;
10
+ }
11
+ function _iterableToArrayLimit(r, l) {
12
+ var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
13
+ if (null != t) {
14
+ var e,
15
+ n,
16
+ i,
17
+ u,
18
+ a = [],
19
+ f = !0,
20
+ o = !1;
21
+ try {
22
+ if (i = (t = t.call(r)).next, 0 === l) ; else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
23
+ } catch (r) {
24
+ o = !0, n = r;
25
+ } finally {
26
+ try {
27
+ if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
28
+ } finally {
29
+ if (o) throw n;
30
+ }
31
+ }
32
+ return a;
33
+ }
34
+ }
35
+ function _toPrimitive(t, r) {
36
+ if ("object" != typeof t || !t) return t;
37
+ var e = t[Symbol.toPrimitive];
38
+ if (void 0 !== e) {
39
+ var i = e.call(t, r );
40
+ if ("object" != typeof i) return i;
41
+ throw new TypeError("@@toPrimitive must return a primitive value.");
42
+ }
43
+ return (String )(t);
44
+ }
45
+ function _toPropertyKey(t) {
46
+ var i = _toPrimitive(t, "string");
47
+ return "symbol" == typeof i ? i : i + "";
48
+ }
49
+ function _typeof(o) {
50
+ "@babel/helpers - typeof";
51
+
52
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
53
+ return typeof o;
54
+ } : function (o) {
55
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
56
+ }, _typeof(o);
57
+ }
58
+ function _classCallCheck(instance, Constructor) {
59
+ if (!(instance instanceof Constructor)) {
60
+ throw new TypeError("Cannot call a class as a function");
61
+ }
62
+ }
63
+ function _defineProperties(target, props) {
64
+ for (var i = 0; i < props.length; i++) {
65
+ var descriptor = props[i];
66
+ descriptor.enumerable = descriptor.enumerable || false;
67
+ descriptor.configurable = true;
68
+ if ("value" in descriptor) descriptor.writable = true;
69
+ Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
70
+ }
71
+ }
72
+ function _createClass(Constructor, protoProps, staticProps) {
73
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
74
+ Object.defineProperty(Constructor, "prototype", {
75
+ writable: false
76
+ });
77
+ return Constructor;
78
+ }
79
+ function _objectWithoutPropertiesLoose(source, excluded) {
80
+ if (source == null) return {};
81
+ var target = {};
82
+ for (var key in source) {
83
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
84
+ if (excluded.indexOf(key) >= 0) continue;
85
+ target[key] = source[key];
86
+ }
87
+ }
88
+ return target;
89
+ }
90
+ function _objectWithoutProperties(source, excluded) {
91
+ if (source == null) return {};
92
+ var target = _objectWithoutPropertiesLoose(source, excluded);
93
+ var key, i;
94
+ if (Object.getOwnPropertySymbols) {
95
+ var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
96
+ for (i = 0; i < sourceSymbolKeys.length; i++) {
97
+ key = sourceSymbolKeys[i];
98
+ if (excluded.indexOf(key) >= 0) continue;
99
+ if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
100
+ target[key] = source[key];
101
+ }
102
+ }
103
+ return target;
104
+ }
105
+ function _slicedToArray(arr, i) {
106
+ return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
107
+ }
108
+ function _toConsumableArray(arr) {
109
+ return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
110
+ }
111
+ function _arrayWithoutHoles(arr) {
112
+ if (Array.isArray(arr)) return _arrayLikeToArray(arr);
113
+ }
114
+ function _arrayWithHoles(arr) {
115
+ if (Array.isArray(arr)) return arr;
116
+ }
117
+ function _iterableToArray(iter) {
118
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
119
+ }
120
+ function _unsupportedIterableToArray(o, minLen) {
121
+ if (!o) return;
122
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
123
+ var n = Object.prototype.toString.call(o).slice(8, -1);
124
+ if (n === "Object" && o.constructor) n = o.constructor.name;
125
+ if (n === "Map" || n === "Set") return Array.from(o);
126
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
127
+ }
128
+ function _arrayLikeToArray(arr, len) {
129
+ if (len == null || len > arr.length) len = arr.length;
130
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
131
+ return arr2;
132
+ }
133
+ function _nonIterableSpread() {
134
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
135
+ }
136
+ function _nonIterableRest() {
137
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
138
+ }
139
+ function _checkPrivateRedeclaration(obj, privateCollection) {
140
+ if (privateCollection.has(obj)) {
141
+ throw new TypeError("Cannot initialize the same private elements twice on an object");
142
+ }
143
+ }
144
+ function _classPrivateFieldInitSpec(obj, privateMap, value) {
145
+ _checkPrivateRedeclaration(obj, privateMap);
146
+ privateMap.set(obj, value);
147
+ }
148
+ function _classPrivateMethodInitSpec(obj, privateSet) {
149
+ _checkPrivateRedeclaration(obj, privateSet);
150
+ privateSet.add(obj);
151
+ }
152
+
153
+ export { _arrayLikeToArray as arrayLikeToArray, _arrayWithHoles as arrayWithHoles, _arrayWithoutHoles as arrayWithoutHoles, _assertClassBrand as assertClassBrand, _checkPrivateRedeclaration as checkPrivateRedeclaration, _classCallCheck as classCallCheck, _classPrivateFieldGet2 as classPrivateFieldGet2, _classPrivateFieldInitSpec as classPrivateFieldInitSpec, _classPrivateFieldSet2 as classPrivateFieldSet2, _classPrivateMethodInitSpec as classPrivateMethodInitSpec, _createClass as createClass, _iterableToArray as iterableToArray, _iterableToArrayLimit as iterableToArrayLimit, _nonIterableRest as nonIterableRest, _nonIterableSpread as nonIterableSpread, _objectWithoutProperties as objectWithoutProperties, _objectWithoutPropertiesLoose as objectWithoutPropertiesLoose, _slicedToArray as slicedToArray, _toConsumableArray as toConsumableArray, _toPrimitive as toPrimitive, _toPropertyKey as toPropertyKey, _typeof as typeof, _unsupportedIterableToArray as unsupportedIterableToArray };
@@ -1,20 +1,20 @@
1
- const contains = (root, n) => {
2
- if (!root) {
3
- return false;
4
- }
5
- // Use native if support
6
- if (root.contains) {
7
- return root.contains(n ?? null);
8
- }
9
- // `document.contains` not support with IE11
10
- let node = n;
11
- while (node) {
12
- if (node === root) {
13
- return true;
14
- }
15
- node = node.parentNode;
16
- }
1
+ var contains = function contains(root, n) {
2
+ if (!root) {
17
3
  return false;
4
+ }
5
+ // Use native if support
6
+ if (root.contains) {
7
+ return root.contains(n !== null && n !== void 0 ? n : null);
8
+ }
9
+ // `document.contains` not support with IE11
10
+ var node = n;
11
+ while (node) {
12
+ if (node === root) {
13
+ return true;
14
+ }
15
+ node = node.parentNode;
16
+ }
17
+ return false;
18
18
  };
19
19
 
20
20
  export { contains };
@@ -1,53 +1,69 @@
1
- class Scroller {
2
- #scrolled = new Map();
3
- // singleton mode
4
- static #scroller = null;
5
- constructor() {
6
- return (Scroller.#scroller ??= this);
7
- }
8
- get scrolled() {
9
- return this.#scrolled;
1
+ import { createClass as _createClass, classPrivateFieldGet2 as _classPrivateFieldGet2, classCallCheck as _classCallCheck, classPrivateFieldInitSpec as _classPrivateFieldInitSpec } from '../_virtual/_rollupPluginBabelHelpers.js';
2
+
3
+ var _scrolled = /*#__PURE__*/new WeakMap();
4
+ var Scroller = /*#__PURE__*/function () {
5
+ function Scroller() {
6
+ var _scroller$_;
7
+ _classCallCheck(this, Scroller);
8
+ _classPrivateFieldInitSpec(this, _scrolled, new Map());
9
+ return (_scroller$_ = _scroller._) !== null && _scroller$_ !== void 0 ? _scroller$_ : _scroller._ = this;
10
+ }
11
+ return _createClass(Scroller, [{
12
+ key: "scrolled",
13
+ get: function get() {
14
+ return _classPrivateFieldGet2(_scrolled, this);
10
15
  }
11
- currentAt(direction) {
12
- return direction === 'vertical' ? 'scrollTop' : 'scrollLeft';
16
+ }, {
17
+ key: "currentAt",
18
+ value: function currentAt(direction) {
19
+ return direction === 'vertical' ? 'scrollTop' : 'scrollLeft';
13
20
  }
14
- }
21
+ }]);
22
+ }();
15
23
  /**
16
24
  * @description
17
25
  * scroll to for wrapper element
18
26
  */
19
- const scrollTo = (target, to, { duration = 0, direction = 'vertical' } = {
20
- duration: 0,
21
- direction: 'vertical'
22
- }) => {
23
- const scroller = new Scroller();
24
- const scrolled = scroller.scrolled.get(target);
25
- const currentAtProperty = scroller.currentAt(direction);
26
- if (scrolled) {
27
- cancelAnimationFrame(scrolled);
28
- scroller.scrolled.delete(target);
29
- }
30
- // if duration <= 0, jump immediately
31
- if (duration <= 0) {
32
- scroller.scrolled.set(target, requestAnimationFrame(() => {
33
- target[currentAtProperty] = to;
34
- }));
35
- return;
36
- }
37
- // animate
38
- const currentAt = target[currentAtProperty];
39
- const difference = to - currentAt;
40
- const step = (difference / duration) * 10;
41
- scroller.scrolled.set(target, requestAnimationFrame(() => {
42
- target[currentAtProperty] = currentAt + step;
43
- // over end, stop any animation
44
- if (target[currentAtProperty] === to)
45
- return;
46
- scrollTo(target, to, {
47
- duration: duration - 10,
48
- direction
49
- });
27
+ // singleton mode
28
+ var _scroller = {
29
+ _: null
30
+ };
31
+ var scrollTo = function scrollTo(target, to) {
32
+ var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {
33
+ duration: 0,
34
+ direction: 'vertical'
35
+ },
36
+ _ref$duration = _ref.duration,
37
+ duration = _ref$duration === void 0 ? 0 : _ref$duration,
38
+ _ref$direction = _ref.direction,
39
+ direction = _ref$direction === void 0 ? 'vertical' : _ref$direction;
40
+ var scroller = new Scroller();
41
+ var scrolled = scroller.scrolled.get(target);
42
+ var currentAtProperty = scroller.currentAt(direction);
43
+ if (scrolled) {
44
+ cancelAnimationFrame(scrolled);
45
+ scroller.scrolled["delete"](target);
46
+ }
47
+ // if duration <= 0, jump immediately
48
+ if (duration <= 0) {
49
+ scroller.scrolled.set(target, requestAnimationFrame(function () {
50
+ target[currentAtProperty] = to;
50
51
  }));
52
+ return;
53
+ }
54
+ // animate
55
+ var currentAt = target[currentAtProperty];
56
+ var difference = to - currentAt;
57
+ var step = difference / duration * 10;
58
+ scroller.scrolled.set(target, requestAnimationFrame(function () {
59
+ target[currentAtProperty] = currentAt + step;
60
+ // over end, stop any animation
61
+ if (target[currentAtProperty] === to) return;
62
+ scrollTo(target, to, {
63
+ duration: duration - 10,
64
+ direction: direction
65
+ });
66
+ }));
51
67
  };
52
68
 
53
69
  export { scrollTo };
@@ -1,3 +1,4 @@
1
+ import { slicedToArray as _slicedToArray } from '../_virtual/_rollupPluginBabelHelpers.js';
1
2
  import { useState, useCallback } from 'react';
2
3
 
3
4
  /**
@@ -6,17 +7,28 @@ import { useState, useCallback } from 'react';
6
7
  * @description
7
8
  * boolean state, in relax, we already create some api to easy use
8
9
  */
9
- const useBoolean = (initialState) => {
10
- const [isOn, setIsOn] = useState(initialState ?? false);
11
- const turnOn = useCallback(() => setIsOn(true), []);
12
- const turnOff = useCallback(() => setIsOn(false), []);
13
- const toggle = useCallback(() => setIsOn((_isOn) => !_isOn), []);
14
- return [isOn, {
15
- turnOn,
16
- turnOff,
17
- toggle,
18
- setBoolean: setIsOn
19
- }];
10
+ var useBoolean = function useBoolean(initialState) {
11
+ var _useState = useState(initialState !== null && initialState !== void 0 ? initialState : false),
12
+ _useState2 = _slicedToArray(_useState, 2),
13
+ isOn = _useState2[0],
14
+ setIsOn = _useState2[1];
15
+ var turnOn = useCallback(function () {
16
+ return setIsOn(true);
17
+ }, []);
18
+ var turnOff = useCallback(function () {
19
+ return setIsOn(false);
20
+ }, []);
21
+ var toggle = useCallback(function () {
22
+ return setIsOn(function (_isOn) {
23
+ return !_isOn;
24
+ });
25
+ }, []);
26
+ return [isOn, {
27
+ turnOn: turnOn,
28
+ turnOff: turnOff,
29
+ toggle: toggle,
30
+ setBoolean: setIsOn
31
+ }];
20
32
  };
21
33
 
22
34
  export { useBoolean };
@@ -1,3 +1,4 @@
1
+ import { slicedToArray as _slicedToArray } from '../_virtual/_rollupPluginBabelHelpers.js';
1
2
  import { useState } from 'react';
2
3
  import { isStateGetter } from '../is/is-state-getter.js';
3
4
  import { isUndefined } from '../is/is-undefined.js';
@@ -9,29 +10,31 @@ import { useUpdateEffect } from './use-update-effect.js';
9
10
  * @description
10
11
  * controlled state
11
12
  */
12
- const useControlledState = (controlledState, { defaultState } = {}) => {
13
- /// initialize state
14
- const [_state, _setState] = useState(() => {
15
- // default use controlled state
16
- if (!isUndefined(controlledState)) {
17
- return controlledState;
18
- }
19
- // not controlled use default prop
20
- if (isUndefined(defaultState))
21
- return controlledState;
22
- if (isStateGetter(defaultState))
23
- return defaultState();
24
- return defaultState;
25
- });
26
- /// sync value back to `undefined` when it from control to un-control
27
- useUpdateEffect(() => {
28
- if (!isUndefined(controlledState))
29
- return;
30
- _setState(controlledState ?? defaultState);
31
- }, [controlledState]);
32
- /// use controlled
33
- const state = !isUndefined(controlledState) ? controlledState : _state;
34
- return [state, _setState];
13
+ var useControlledState = function useControlledState(controlledState) {
14
+ var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
15
+ defaultState = _ref.defaultState;
16
+ /// initialize state
17
+ var _useState = useState(function () {
18
+ // default use controlled state
19
+ if (!isUndefined(controlledState)) {
20
+ return controlledState;
21
+ }
22
+ // not controlled use default prop
23
+ if (isUndefined(defaultState)) return controlledState;
24
+ if (isStateGetter(defaultState)) return defaultState();
25
+ return defaultState;
26
+ }),
27
+ _useState2 = _slicedToArray(_useState, 2),
28
+ _state = _useState2[0],
29
+ _setState = _useState2[1];
30
+ /// sync value back to `undefined` when it from control to un-control
31
+ useUpdateEffect(function () {
32
+ if (!isUndefined(controlledState)) return;
33
+ _setState(controlledState !== null && controlledState !== void 0 ? controlledState : defaultState);
34
+ }, [controlledState]);
35
+ /// use controlled
36
+ var state = !isUndefined(controlledState) ? controlledState : _state;
37
+ return [state, _setState];
35
38
  };
36
39
 
37
40
  export { useControlledState };
@@ -1,5 +1,6 @@
1
+ import { slicedToArray as _slicedToArray } from '../_virtual/_rollupPluginBabelHelpers.js';
1
2
  import { useState, useCallback, useMemo } from 'react';
2
- import { clamp } from '../utils/clamp.js';
3
+ import { clamp } from '../utils/clamp/index.js';
3
4
  import { useDefault } from './use-default.js';
4
5
 
5
6
  /**
@@ -8,26 +9,52 @@ import { useDefault } from './use-default.js';
8
9
  * @description
9
10
  * a number counter with some useful apis
10
11
  */
11
- const useCounter = (initialState, { max = Infinity, min = -Infinity } = { max: Infinity, min: -Infinity }) => {
12
- const defaultState = useDefault(initialState ?? 0);
13
- const [_count, _setCount] = useState(defaultState);
14
- const add = useCallback((step = 1) => {
15
- _setCount((prev) => Math.min(max, prev + step));
16
- }, [max]);
17
- const subtract = useCallback((step = 1) => {
18
- _setCount((prev) => Math.max(min, prev - step));
19
- }, [min]);
20
- const first = useCallback(() => {
21
- _setCount(min);
22
- }, [min]);
23
- const last = useCallback(() => {
24
- _setCount(max);
25
- }, [max]);
26
- const reset = useCallback(() => {
27
- _setCount(defaultState);
28
- }, []);
29
- const count = useMemo(() => clamp(_count, min, max), [_count, min, max]);
30
- return [count, { add, subtract, first, last, reset, setCount: _setCount }];
12
+ var useCounter = function useCounter(initialState) {
13
+ var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
14
+ max: Infinity,
15
+ min: -Infinity
16
+ },
17
+ _ref$max = _ref.max,
18
+ max = _ref$max === void 0 ? Infinity : _ref$max,
19
+ _ref$min = _ref.min,
20
+ min = _ref$min === void 0 ? -Infinity : _ref$min;
21
+ var defaultState = useDefault(initialState !== null && initialState !== void 0 ? initialState : 0);
22
+ var _useState = useState(defaultState),
23
+ _useState2 = _slicedToArray(_useState, 2),
24
+ _count = _useState2[0],
25
+ _setCount = _useState2[1];
26
+ var add = useCallback(function () {
27
+ var step = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
28
+ _setCount(function (prev) {
29
+ return Math.min(max, prev + step);
30
+ });
31
+ }, [max]);
32
+ var subtract = useCallback(function () {
33
+ var step = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
34
+ _setCount(function (prev) {
35
+ return Math.max(min, prev - step);
36
+ });
37
+ }, [min]);
38
+ var first = useCallback(function () {
39
+ _setCount(min);
40
+ }, [min]);
41
+ var last = useCallback(function () {
42
+ _setCount(max);
43
+ }, [max]);
44
+ var reset = useCallback(function () {
45
+ _setCount(defaultState);
46
+ }, []);
47
+ var count = useMemo(function () {
48
+ return clamp(_count, min, max);
49
+ }, [_count, min, max]);
50
+ return [count, {
51
+ add: add,
52
+ subtract: subtract,
53
+ first: first,
54
+ last: last,
55
+ reset: reset,
56
+ setCount: _setCount
57
+ }];
31
58
  };
32
59
 
33
60
  export { useCounter };
@@ -1,21 +1,28 @@
1
1
  import { useRef, useEffect, useMemo } from 'react';
2
- import { debounce } from '../utils/debounce.js';
2
+ import { debounce } from '../utils/debounce/index.js';
3
3
  import { useEvent } from './use-event.js';
4
4
  import { isFunction } from '../is/is-function.js';
5
5
  import { useDefault } from './use-default.js';
6
6
 
7
- const useDebouncer = (debouncer) => {
8
- const _debouncer = useMemo(() => {
9
- return isFunction(debouncer) ? { callback: debouncer, pipeable: null } : debouncer;
10
- }, [debouncer]);
11
- return {
12
- callback: useEvent((...args) => {
13
- return _debouncer.callback(...args);
14
- }),
15
- pipeable: useEvent((...args) => {
16
- return _debouncer.pipeable?.(...args) ?? args;
17
- })
18
- };
7
+ var useDebouncer = function useDebouncer(debouncer) {
8
+ var _debouncer = useMemo(function () {
9
+ return isFunction(debouncer) ? {
10
+ callback: debouncer,
11
+ pipe: null
12
+ } : debouncer;
13
+ }, [debouncer]);
14
+ return {
15
+ callback: useEvent(function () {
16
+ return _debouncer.callback.apply(_debouncer, arguments);
17
+ }),
18
+ pipe: useEvent(function () {
19
+ var _debouncer$pipe, _debouncer$pipe2;
20
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
21
+ args[_key] = arguments[_key];
22
+ }
23
+ return (_debouncer$pipe = (_debouncer$pipe2 = _debouncer.pipe) === null || _debouncer$pipe2 === void 0 ? void 0 : _debouncer$pipe2.call.apply(_debouncer$pipe2, [_debouncer].concat(args))) !== null && _debouncer$pipe !== void 0 ? _debouncer$pipe : args;
24
+ })
25
+ };
19
26
  };
20
27
  /**
21
28
  * @author murukal
@@ -32,26 +39,43 @@ const useDebouncer = (debouncer) => {
32
39
  * @example
33
40
  * 1000
34
41
  */
35
- const useDebounceCallback = (debouncer, wait = 1000) => {
36
- const debounced = useRef(null);
37
- const { callback, pipeable } = useDebouncer(debouncer);
38
- useEffect(() => {
39
- const _debounced = debounce({
40
- callback,
41
- pipeable
42
- }, wait);
43
- debounced.current = _debounced;
44
- // dispose
45
- return () => {
46
- _debounced.abort();
47
- debounced.current = null;
48
- };
49
- }, [wait]);
50
- return useDefault(() => ({
51
- next: (...args) => debounced.current?.next(...args),
52
- flush: () => debounced.current?.flush(),
53
- abort: () => debounced.current?.abort()
54
- }));
42
+ var useDebounceCallback = function useDebounceCallback(debouncer) {
43
+ var wait = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1000;
44
+ var debounced = useRef(null);
45
+ var _useDebouncer = useDebouncer(debouncer),
46
+ callback = _useDebouncer.callback,
47
+ pipe = _useDebouncer.pipe;
48
+ useEffect(function () {
49
+ var _debounced = debounce({
50
+ callback: callback,
51
+ pipe: pipe
52
+ }, wait);
53
+ debounced.current = _debounced;
54
+ // dispose
55
+ return function () {
56
+ _debounced.abort();
57
+ debounced.current = null;
58
+ };
59
+ }, [wait]);
60
+ return useDefault(function () {
61
+ return {
62
+ next: function next() {
63
+ var _debounced$current;
64
+ for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
65
+ args[_key2] = arguments[_key2];
66
+ }
67
+ return (_debounced$current = debounced.current) === null || _debounced$current === void 0 ? void 0 : _debounced$current.next.apply(_debounced$current, args);
68
+ },
69
+ flush: function flush() {
70
+ var _debounced$current2;
71
+ return (_debounced$current2 = debounced.current) === null || _debounced$current2 === void 0 ? void 0 : _debounced$current2.flush();
72
+ },
73
+ abort: function abort() {
74
+ var _debounced$current3;
75
+ return (_debounced$current3 = debounced.current) === null || _debounced$current3 === void 0 ? void 0 : _debounced$current3.abort();
76
+ }
77
+ };
78
+ });
55
79
  };
56
80
 
57
81
  export { useDebounceCallback };
@@ -7,8 +7,8 @@ import { toFunction } from '../utils/to-function.js';
7
7
  * @description
8
8
  * state always be same after first render
9
9
  */
10
- const useDefault = (initialState) => {
11
- return useMemo(toFunction(initialState), []);
10
+ var useDefault = function useDefault(initialState) {
11
+ return useMemo(toFunction(initialState), []);
12
12
  };
13
13
 
14
14
  export { useDefault };
@@ -1,10 +1,12 @@
1
1
  import { useRef, useCallback } from 'react';
2
2
 
3
- const useEvent = (callback) => {
4
- const ref = useRef(callback);
5
- ref.current = callback;
6
- // @ts-ignore
7
- return useCallback((...args) => ref.current(...args), []);
3
+ var useEvent = function useEvent(callback) {
4
+ var ref = useRef(callback);
5
+ ref.current = callback;
6
+ // @ts-ignore
7
+ return useCallback(function () {
8
+ return ref.current.apply(ref, arguments);
9
+ }, []);
8
10
  };
9
11
 
10
12
  export { useEvent };