cogsbox-state 0.5.337 → 0.5.339
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/dist/CogsState.jsx +334 -349
- package/dist/CogsState.jsx.map +1 -1
- package/package.json +1 -1
- package/src/CogsState.tsx +43 -33
package/package.json
CHANGED
package/src/CogsState.tsx
CHANGED
|
@@ -1871,34 +1871,42 @@ function createProxyHandler<T>(
|
|
|
1871
1871
|
validIndices,
|
|
1872
1872
|
});
|
|
1873
1873
|
}, [range.startIndex, range.endIndex, sourceArray, totalCount]);
|
|
1874
|
-
useEffect(() => {
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
}, [totalCount]);
|
|
1874
|
+
// useEffect(() => {
|
|
1875
|
+
// if (stickToBottom && totalCount > 0 && containerRef.current) {
|
|
1876
|
+
// // When count increases, immediately adjust range to show bottom
|
|
1877
|
+
// const container = containerRef.current;
|
|
1878
|
+
// const visibleCount = Math.ceil(
|
|
1879
|
+
// container.clientHeight / itemHeight
|
|
1880
|
+
// );
|
|
1881
|
+
|
|
1882
|
+
// // Set range to show the last items including the new one
|
|
1883
|
+
// setRange({
|
|
1884
|
+
// startIndex: Math.max(
|
|
1885
|
+
// 0,
|
|
1886
|
+
// totalCount - visibleCount - overscan
|
|
1887
|
+
// ),
|
|
1888
|
+
// endIndex: totalCount,
|
|
1889
|
+
// });
|
|
1890
|
+
|
|
1891
|
+
// // Then scroll to bottom after a short delay
|
|
1892
|
+
// setTimeout(() => {
|
|
1893
|
+
// container.scrollTop = container.scrollHeight + 9999;
|
|
1894
|
+
// }, 200);
|
|
1895
|
+
// }
|
|
1896
|
+
// }, [totalCount]);
|
|
1897
1897
|
// This is the main effect that handles all scrolling and updates.
|
|
1898
1898
|
useLayoutEffect(() => {
|
|
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
|
+
itemHeight;
|
|
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
|
-
//
|
|
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
|
-
// ---
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
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
|
|
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.
|