cogsbox-state 0.5.307 → 0.5.309

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.307",
3
+ "version": "0.5.309",
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,19 +1814,21 @@ function createProxyHandler<T>(
1814
1814
  endIndex: 10,
1815
1815
  });
1816
1816
 
1817
- // Force re-render when heights change
1818
- const [, forceUpdate] = useState({});
1819
-
1820
1817
  const isAtBottomRef = useRef(stickToBottom);
1821
1818
  const previousTotalCountRef = useRef(0);
1822
1819
  const isInitialMountRef = useRef(true);
1823
- const previousTotalHeightRef = useRef(0);
1824
-
1825
1820
  // Subscribe to shadow state changes
1821
+ const [shadowUpdateTrigger, setShadowUpdateTrigger] = useState(0);
1822
+
1826
1823
  useEffect(() => {
1824
+ // Subscribe to shadow state updates for this stateKey
1827
1825
  const unsubscribe = getGlobalStore
1828
1826
  .getState()
1829
- .subscribeToShadowState(stateKey, () => forceUpdate({}));
1827
+ .subscribeToShadowState(stateKey, () => {
1828
+ // Force recalculation when shadow state updates
1829
+ setShadowUpdateTrigger((prev) => prev + 1);
1830
+ });
1831
+
1830
1832
  return unsubscribe;
1831
1833
  }, [stateKey]);
1832
1834
 
@@ -1852,30 +1854,15 @@ function createProxyHandler<T>(
1852
1854
  }
1853
1855
 
1854
1856
  return { totalHeight: height, positions: pos };
1855
- }, [totalCount, stateKey, path.join("."), itemHeight]);
1856
-
1857
- // Adjust scroll when height changes while at bottom
1858
- useLayoutEffect(() => {
1859
- const container = containerRef.current;
1860
- if (!container) return;
1861
-
1862
- const heightChanged =
1863
- totalHeight !== previousTotalHeightRef.current;
1864
- previousTotalHeightRef.current = totalHeight;
1865
-
1866
- // If we're at bottom and height changed, maintain bottom position
1867
- if (
1868
- heightChanged &&
1869
- isAtBottomRef.current &&
1870
- !isInitialMountRef.current
1871
- ) {
1872
- container.scrollTo({
1873
- top: container.scrollHeight,
1874
- behavior: "auto",
1875
- });
1876
- }
1877
- }, [totalHeight]);
1857
+ }, [
1858
+ totalCount,
1859
+ stateKey,
1860
+ path.join("."),
1861
+ itemHeight,
1862
+ shadowUpdateTrigger,
1863
+ ]);
1878
1864
 
1865
+ console.log("height", totalHeight);
1879
1866
  const virtualState = useMemo(() => {
1880
1867
  const start = Math.max(0, range.startIndex);
1881
1868
  const end = Math.min(totalCount, range.endIndex);
@@ -1942,38 +1929,32 @@ function createProxyHandler<T>(
1942
1929
  passive: true,
1943
1930
  });
1944
1931
 
1945
- // Handle stick to bottom for initial mount only
1946
- if (
1947
- stickToBottom &&
1948
- isInitialMountRef.current &&
1949
- totalCount > 0
1950
- ) {
1951
- // Set flag first to prevent height adjustment from interfering
1952
- isAtBottomRef.current = true;
1953
- // Wait for next frame to ensure everything is rendered
1954
- requestAnimationFrame(() => {
1955
- if (containerRef.current) {
1956
- containerRef.current.scrollTo({
1957
- top: containerRef.current.scrollHeight,
1958
- behavior: "auto",
1959
- });
1960
- isInitialMountRef.current = false;
1961
- }
1962
- });
1963
- } else if (
1964
- !isInitialMountRef.current &&
1965
- wasAtBottom &&
1966
- listGrew
1967
- ) {
1968
- // New items added and we were at bottom - stay at bottom
1969
- requestAnimationFrame(() => {
1932
+ // Handle stick to bottom
1933
+ if (stickToBottom) {
1934
+ if (isInitialMountRef.current) {
1935
+ console.log(
1936
+ "stickToBottom initial mount",
1937
+ container.scrollHeight
1938
+ );
1970
1939
  container.scrollTo({
1971
1940
  top: container.scrollHeight,
1972
- behavior: "smooth",
1941
+ behavior: "auto",
1973
1942
  });
1974
- });
1943
+ isInitialMountRef.current = false;
1944
+ } else if (wasAtBottom && listGrew) {
1945
+ console.log(
1946
+ "stickToBottom wasAtBottom && listGrew",
1947
+ container.scrollHeight
1948
+ );
1949
+ requestAnimationFrame(() => {
1950
+ container.scrollTo({
1951
+ top: container.scrollHeight,
1952
+ behavior: "smooth",
1953
+ });
1954
+ });
1955
+ }
1975
1956
  }
1976
-
1957
+ console.log("wasAtBottom && listGrew", wasAtBottom, listGrew);
1977
1958
  // Run handleScroll once to set initial range
1978
1959
  handleScroll();
1979
1960