@vitus-labs/hooks 2.7.0 → 2.7.2

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.
package/README.md CHANGED
@@ -1,3 +1,19 @@
1
+ <!-- LOGO:BEGIN -->
2
+ <div align="center">
3
+ <a href="https://github.com/vitus-labs/ui-system">
4
+ <picture>
5
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/vitus-labs/ui-system/main/.github/assets/vitus-labs-mark-dark.svg">
6
+ <img alt="vitus·labs" src="https://raw.githubusercontent.com/vitus-labs/ui-system/main/.github/assets/vitus-labs-mark-light.svg" height="64">
7
+ </picture>
8
+ </a>
9
+ &nbsp;&nbsp;&nbsp;&nbsp;
10
+ <picture>
11
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/vitus-labs/ui-system/main/packages/hooks/assets/logo-dark.svg">
12
+ <img alt="@vitus-labs/hooks" src="https://raw.githubusercontent.com/vitus-labs/ui-system/main/packages/hooks/assets/logo-light.svg" height="64">
13
+ </picture>
14
+ </div>
15
+ <!-- LOGO:END -->
16
+
1
17
  # @vitus-labs/hooks
2
18
 
3
19
  28 React hooks for UI interactions, state management, DOM observation, accessibility, and theming. ~3.4 KB gzipped.
package/lib/index.js CHANGED
@@ -120,11 +120,14 @@ const useControllableState = ({ value, defaultValue, onChange }) => {
120
120
  onChangeRef.current = onChange;
121
121
  const isControlled = value !== void 0;
122
122
  const current = isControlled ? value : internal;
123
+ const currentRef = useRef(current);
124
+ currentRef.current = current;
123
125
  return [current, useCallback((next) => {
124
- const nextValue = typeof next === "function" ? next(current) : next;
125
- if (!isControlled) setInternal(nextValue);
126
+ const nextValue = typeof next === "function" ? next(currentRef.current) : next;
127
+ currentRef.current = nextValue;
128
+ if (!isControlled) setInternal(next);
126
129
  onChangeRef.current?.(nextValue);
127
- }, [current, isControlled])];
130
+ }, [isControlled])];
128
131
  };
129
132
 
130
133
  //#endregion
@@ -861,6 +864,10 @@ const useWindowResize = ({ throttleDelay = 200, onChange } = {}, { width = 0, he
861
864
  width,
862
865
  height
863
866
  });
867
+ const lastSizesRef = useRef({
868
+ width,
869
+ height
870
+ });
864
871
  const onChangeRef = useRef(onChange);
865
872
  onChangeRef.current = onChange;
866
873
  const updateSizes = useCallback(() => {
@@ -868,8 +875,12 @@ const useWindowResize = ({ throttleDelay = 200, onChange } = {}, { width = 0, he
868
875
  width: window.innerWidth,
869
876
  height: window.innerHeight
870
877
  };
871
- setWindowSize(sizes);
872
- if (onChangeRef.current) onChangeRef.current(sizes);
878
+ setWindowSize((prev) => prev.width === sizes.width && prev.height === sizes.height ? prev : sizes);
879
+ const last = lastSizesRef.current;
880
+ if (last.width !== sizes.width || last.height !== sizes.height) {
881
+ lastSizesRef.current = sizes;
882
+ if (onChangeRef.current) onChangeRef.current(sizes);
883
+ }
873
884
  }, []);
874
885
  useEffect(() => {
875
886
  updateSizes();
@@ -71,11 +71,14 @@ const useControllableState = ({ value, defaultValue, onChange }) => {
71
71
  onChangeRef.current = onChange;
72
72
  const isControlled = value !== void 0;
73
73
  const current = isControlled ? value : internal;
74
+ const currentRef = useRef(current);
75
+ currentRef.current = current;
74
76
  return [current, useCallback((next) => {
75
- const nextValue = typeof next === "function" ? next(current) : next;
76
- if (!isControlled) setInternal(nextValue);
77
+ const nextValue = typeof next === "function" ? next(currentRef.current) : next;
78
+ currentRef.current = nextValue;
79
+ if (!isControlled) setInternal(next);
77
80
  onChangeRef.current?.(nextValue);
78
- }, [current, isControlled])];
81
+ }, [isControlled])];
79
82
  };
80
83
 
81
84
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitus-labs/hooks",
3
- "version": "2.7.0",
3
+ "version": "2.7.2",
4
4
  "license": "MIT",
5
5
  "author": "Vit Bokisch <vit@bokisch.cz>",
6
6
  "maintainers": [
@@ -51,7 +51,7 @@
51
51
  "node": ">= 18"
52
52
  },
53
53
  "peerDependencies": {
54
- "@vitus-labs/core": "^2.7.0",
54
+ "@vitus-labs/core": "^2.7.2",
55
55
  "react": ">= 19",
56
56
  "react-native": ">= 0.76"
57
57
  },