cogsbox-state 0.5.326 → 0.5.328

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