cogsbox-state 0.5.302 → 0.5.304

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.302",
3
+ "version": "0.5.304",
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
@@ -1814,48 +1814,33 @@ function createProxyHandler<T>(
1814
1814
  endIndex: 10,
1815
1815
  });
1816
1816
 
1817
- const [heightsVersion, setHeightsVersion] = useState(0);
1818
- const forceRecalculate = useCallback(
1819
- () => setHeightsVersion((v) => v + 1),
1820
- []
1821
- );
1822
-
1823
- // Track scroll position
1824
1817
  const isAtBottomRef = useRef(stickToBottom);
1825
1818
  const previousTotalCountRef = useRef(0);
1826
1819
  const isInitialMountRef = useRef(true);
1827
1820
 
1828
- useEffect(() => {
1829
- const unsubscribe = getGlobalStore
1830
- .getState()
1831
- .subscribeToShadowState(stateKey, forceRecalculate);
1832
- const timer = setTimeout(forceRecalculate, 50);
1833
- return () => {
1834
- unsubscribe();
1835
- clearTimeout(timer);
1836
- };
1837
- }, [stateKey, forceRecalculate]);
1838
-
1839
1821
  const sourceArray = getGlobalStore().getNestedState(
1840
1822
  stateKey,
1841
1823
  path
1842
1824
  ) as any[];
1843
1825
  const totalCount = sourceArray.length;
1844
1826
 
1827
+ // Calculate heights from shadow state
1845
1828
  const { totalHeight, positions } = useMemo(() => {
1846
1829
  const shadowArray =
1847
1830
  getGlobalStore.getState().getShadowMetadata(stateKey, path) ||
1848
1831
  [];
1849
1832
  let height = 0;
1850
1833
  const pos: number[] = [];
1834
+
1851
1835
  for (let i = 0; i < totalCount; i++) {
1852
1836
  pos[i] = height;
1853
1837
  const measuredHeight =
1854
1838
  shadowArray[i]?.virtualizer?.itemHeight;
1855
1839
  height += measuredHeight || itemHeight;
1856
1840
  }
1841
+
1857
1842
  return { totalHeight: height, positions: pos };
1858
- }, [totalCount, stateKey, path, itemHeight, heightsVersion]);
1843
+ }, [totalCount, stateKey, path.join("."), itemHeight]);
1859
1844
 
1860
1845
  const virtualState = useMemo(() => {
1861
1846
  const start = Math.max(0, range.startIndex);
@@ -1881,7 +1866,6 @@ function createProxyHandler<T>(
1881
1866
 
1882
1867
  const handleScroll = () => {
1883
1868
  const { scrollTop, clientHeight, scrollHeight } = container;
1884
- // Consider "at bottom" if within 10px
1885
1869
  isAtBottomRef.current =
1886
1870
  scrollHeight - scrollTop - clientHeight < 10;
1887
1871
 
@@ -1926,12 +1910,17 @@ function createProxyHandler<T>(
1926
1910
 
1927
1911
  // Handle stick to bottom
1928
1912
  if (stickToBottom) {
1929
- if (isInitialMountRef.current) {
1930
- // First render - go to bottom instantly
1931
- container.scrollTo({
1932
- top: container.scrollHeight,
1933
- behavior: "auto",
1934
- });
1913
+ if (isInitialMountRef.current && totalCount > 0) {
1914
+ // Delay initial scroll to ensure items are rendered
1915
+ setTimeout(() => {
1916
+ if (containerRef.current) {
1917
+ containerRef.current.scrollTo({
1918
+ top: containerRef.current.scrollHeight,
1919
+ behavior: "auto",
1920
+ });
1921
+ }
1922
+ }, 0);
1923
+ isInitialMountRef.current = false;
1935
1924
  } else if (wasAtBottom && listGrew) {
1936
1925
  // New items added and we were at bottom - stay at bottom
1937
1926
  requestAnimationFrame(() => {
@@ -1941,11 +1930,10 @@ function createProxyHandler<T>(
1941
1930
  });
1942
1931
  });
1943
1932
  }
1933
+ } else {
1934
+ isInitialMountRef.current = false;
1944
1935
  }
1945
1936
 
1946
- // Mark as no longer initial mount after first render
1947
- isInitialMountRef.current = false;
1948
-
1949
1937
  // Run handleScroll once to set initial range
1950
1938
  handleScroll();
1951
1939