cogsbox-state 0.5.330 → 0.5.332

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-state",
3
- "version": "0.5.330",
3
+ "version": "0.5.332",
4
4
  "description": "React state management library with form controls and server sync",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
package/src/CogsState.tsx CHANGED
@@ -1816,7 +1816,7 @@ function createProxyHandler<T>(
1816
1816
 
1817
1817
  // This ref tracks if the user is locked to the bottom.
1818
1818
  const isLockedToBottomRef = useRef(stickToBottom);
1819
- const lastScrollPositionRef = useRef(0);
1819
+
1820
1820
  // This state triggers a re-render when item heights change.
1821
1821
  const [shadowUpdateTrigger, setShadowUpdateTrigger] = useState(0);
1822
1822
 
@@ -1939,31 +1939,26 @@ function createProxyHandler<T>(
1939
1939
  passive: true,
1940
1940
  });
1941
1941
 
1942
+ // --- THE CORE FIX ---
1942
1943
  if (stickToBottom && isLockedToBottomRef.current) {
1943
- const currentScrollHeight = container.scrollHeight;
1944
-
1945
- // Only scroll if the height actually changed (new content rendered)
1946
- if (currentScrollHeight > lastScrollPositionRef.current) {
1947
- // Check if we're far from bottom (initial loads or large batches)
1948
- const distanceFromBottom =
1949
- currentScrollHeight -
1950
- container.scrollTop -
1951
- container.clientHeight;
1952
- const isLargeGap =
1953
- distanceFromBottom > container.clientHeight * 2;
1954
-
1955
- container.scrollTo({
1956
- top: 999999999,
1957
- behavior: isLargeGap ? "auto" : "smooth", // Instant for big jumps, smooth for small
1958
- });
1959
-
1960
- lastScrollPositionRef.current = currentScrollHeight;
1961
- }
1944
+ // We use a timeout to wait for React to render AND for useMeasure to update heights.
1945
+ // This is the CRUCIAL part that fixes the race condition.
1946
+ scrollTimeoutId = setTimeout(() => {
1947
+ console.log("totalHeight", totalHeight);
1948
+ if (isLockedToBottomRef.current) {
1949
+ container.scrollTo({
1950
+ top: 999999999,
1951
+ behavior: "smooth", // ALWAYS 'auto' for an instant, correct jump.
1952
+ });
1953
+ }
1954
+ }, 200); // A small 50ms delay is a robust buffer.
1962
1955
  }
1956
+
1963
1957
  updateVirtualRange();
1964
1958
 
1965
1959
  // Cleanup function is vital to prevent memory leaks.
1966
1960
  return () => {
1961
+ clearTimeout(scrollTimeoutId);
1967
1962
  container.removeEventListener("scroll", handleUserScroll);
1968
1963
  };
1969
1964
  // This effect re-runs whenever the list size or item heights change.