cogsbox-state 0.5.326 → 0.5.327

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.326",
3
+ "version": "0.5.327",
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
@@ -1939,21 +1939,36 @@ function createProxyHandler<T>(
1939
1939
  passive: true,
1940
1940
  });
1941
1941
 
1942
- // --- THE CORE FIX ---
1942
+ const hasInitiallyLoadedRef = useRef(false);
1943
+ const prevTotalCountRef = useRef(0);
1944
+
1945
+ // In your useLayoutEffect:
1943
1946
  if (stickToBottom) {
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.
1947
+ // Check if this is initial load or new item
1948
+ const isInitialLoad =
1949
+ !hasInitiallyLoadedRef.current && totalCount > 0;
1950
+ const isNewItem =
1951
+ hasInitiallyLoadedRef.current &&
1952
+ totalCount > prevTotalCountRef.current;
1953
+
1946
1954
  scrollTimeoutId = setTimeout(() => {
1947
1955
  console.log("totalHeight", totalHeight);
1948
1956
  if (isLockedToBottomRef.current) {
1949
1957
  container.scrollTo({
1950
1958
  top: 999999999,
1951
- behavior: "auto", // ALWAYS 'auto' for an instant, correct jump.
1959
+ behavior: isNewItem ? "smooth" : "auto", // Only smooth for NEW items after initial load
1952
1960
  });
1953
1961
  }
1954
- }, 1000); // A small 50ms delay is a robust buffer.
1962
+ }, 200);
1963
+
1964
+ // Mark as initially loaded after first run
1965
+ if (isInitialLoad) {
1966
+ hasInitiallyLoadedRef.current = true;
1967
+ }
1955
1968
  }
1956
1969
 
1970
+ // Update ref at the end
1971
+ prevTotalCountRef.current = totalCount;
1957
1972
  updateVirtualRange();
1958
1973
 
1959
1974
  // Cleanup function is vital to prevent memory leaks.