cogsbox-state 0.5.337 → 0.5.338

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.337",
3
+ "version": "0.5.338",
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
@@ -1899,6 +1899,14 @@ function createProxyHandler<T>(
1899
1899
  const container = containerRef.current;
1900
1900
  if (!container) return;
1901
1901
 
1902
+ // --- STEP 1: Remember if we were scrolled to the bottom BEFORE this render ---
1903
+ // We check this now, before the new items might have pushed the scrollbar up.
1904
+ const wasScrolledToBottom =
1905
+ container.scrollHeight -
1906
+ container.scrollTop -
1907
+ container.clientHeight <
1908
+ 1;
1909
+
1902
1910
  // This function determines what's visible in the viewport.
1903
1911
  const updateVirtualRange = () => {
1904
1912
  if (!container) return;
@@ -1925,19 +1933,19 @@ function createProxyHandler<T>(
1925
1933
  }
1926
1934
  endIndex = Math.min(totalCount, endIndex + overscan);
1927
1935
 
1936
+ // Update the state to render the correct slice
1928
1937
  setRange({ startIndex, endIndex });
1929
1938
  };
1930
1939
 
1931
1940
  // This function handles ONLY user-initiated scrolls.
1932
1941
  const handleUserScroll = () => {
1933
- // The key: check if we are scrolled to the bottom.
1942
+ // When the user scrolls, update the ref so we know their intent for the *next* update.
1934
1943
  isLockedToBottomRef.current =
1935
1944
  container.scrollHeight -
1936
1945
  container.scrollTop -
1937
1946
  container.clientHeight <
1938
1947
  1;
1939
-
1940
- // After any scroll, update what's visible.
1948
+ // Then, just render what's visible at the new position.
1941
1949
  updateVirtualRange();
1942
1950
  };
1943
1951
 
@@ -1946,15 +1954,17 @@ function createProxyHandler<T>(
1946
1954
  passive: true,
1947
1955
  });
1948
1956
 
1949
- // --- THE AUTO-SCROLL LOGIC ---
1950
- // This checks if we should auto-scroll *after* a render has occurred.
1951
- if (stickToBottom && isLockedToBottomRef.current) {
1952
- // If we were at the bottom before this new item was added,
1953
- // scroll to the new bottom.
1954
- container.scrollTop = container.scrollHeight + 999999999;
1957
+ // --- STEP 2: Apply the scroll AFTER the render, based on what we remembered ---
1958
+ if (
1959
+ stickToBottom &&
1960
+ (isLockedToBottomRef.current || wasScrolledToBottom)
1961
+ ) {
1962
+ // If we are "locked" OR if we were at the bottom just before this render,
1963
+ // then scroll to the new bottom. This handles both initial load and new items.
1964
+ container.scrollTop = container.scrollHeight;
1955
1965
  }
1956
1966
 
1957
- // Always calculate the initial visible range.
1967
+ // Always calculate the visible range after any potential scroll changes.
1958
1968
  updateVirtualRange();
1959
1969
 
1960
1970
  // Cleanup function is vital to prevent memory leaks.