cogsbox-state 0.5.305 → 0.5.306

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.305",
3
+ "version": "0.5.306",
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,10 +1814,20 @@ function createProxyHandler<T>(
1814
1814
  endIndex: 10,
1815
1815
  });
1816
1816
 
1817
+ // Force re-render when heights change
1818
+ const [, forceUpdate] = useState({});
1819
+
1817
1820
  const isAtBottomRef = useRef(stickToBottom);
1818
1821
  const previousTotalCountRef = useRef(0);
1819
1822
  const isInitialMountRef = useRef(true);
1820
- const hasMeasurementsRef = useRef(false);
1823
+
1824
+ // Subscribe to shadow state changes
1825
+ useEffect(() => {
1826
+ const unsubscribe = getGlobalStore
1827
+ .getState()
1828
+ .subscribeToShadowState(stateKey, () => forceUpdate({}));
1829
+ return unsubscribe;
1830
+ }, [stateKey]);
1821
1831
 
1822
1832
  const sourceArray = getGlobalStore().getNestedState(
1823
1833
  stateKey,
@@ -1832,17 +1842,14 @@ function createProxyHandler<T>(
1832
1842
  [];
1833
1843
  let height = 0;
1834
1844
  const pos: number[] = [];
1835
- let hasMeasurements = false;
1836
1845
 
1837
1846
  for (let i = 0; i < totalCount; i++) {
1838
1847
  pos[i] = height;
1839
1848
  const measuredHeight =
1840
1849
  shadowArray[i]?.virtualizer?.itemHeight;
1841
- if (measuredHeight) hasMeasurements = true;
1842
1850
  height += measuredHeight || itemHeight;
1843
1851
  }
1844
1852
 
1845
- hasMeasurementsRef.current = hasMeasurements;
1846
1853
  return { totalHeight: height, positions: pos };
1847
1854
  }, [totalCount, stateKey, path.join("."), itemHeight]);
1848
1855
 
@@ -1913,9 +1920,26 @@ function createProxyHandler<T>(
1913
1920
  });
1914
1921
 
1915
1922
  // Handle stick to bottom
1916
- if (stickToBottom && !isInitialMountRef.current) {
1917
- // Only auto-scroll for new items after initial mount
1918
- if (wasAtBottom && listGrew) {
1923
+ if (stickToBottom) {
1924
+ if (isInitialMountRef.current && totalCount > 0) {
1925
+ // Double rAF to ensure everything is rendered and measured
1926
+ requestAnimationFrame(() => {
1927
+ requestAnimationFrame(() => {
1928
+ if (containerRef.current) {
1929
+ containerRef.current.scrollTo({
1930
+ top: containerRef.current.scrollHeight,
1931
+ behavior: "auto",
1932
+ });
1933
+ isInitialMountRef.current = false;
1934
+ }
1935
+ });
1936
+ });
1937
+ } else if (
1938
+ !isInitialMountRef.current &&
1939
+ wasAtBottom &&
1940
+ listGrew
1941
+ ) {
1942
+ // New items added and we were at bottom - stay at bottom
1919
1943
  requestAnimationFrame(() => {
1920
1944
  container.scrollTo({
1921
1945
  top: container.scrollHeight,
@@ -1932,30 +1956,6 @@ function createProxyHandler<T>(
1932
1956
  container.removeEventListener("scroll", handleScroll);
1933
1957
  }, [totalCount, positions, overscan, stickToBottom]);
1934
1958
 
1935
- // Separate effect for initial scroll to bottom
1936
- useEffect(() => {
1937
- if (
1938
- stickToBottom &&
1939
- isInitialMountRef.current &&
1940
- totalCount > 0 &&
1941
- hasMeasurementsRef.current
1942
- ) {
1943
- const container = containerRef.current;
1944
- if (container) {
1945
- // Use rAF to ensure DOM is updated
1946
- requestAnimationFrame(() => {
1947
- requestAnimationFrame(() => {
1948
- container.scrollTo({
1949
- top: container.scrollHeight,
1950
- behavior: "auto",
1951
- });
1952
- isInitialMountRef.current = false;
1953
- });
1954
- });
1955
- }
1956
- }
1957
- }, [stickToBottom, totalCount, positions]); // positions change triggers this when measurements come in
1958
-
1959
1959
  const scrollToBottom = useCallback(
1960
1960
  (behavior: ScrollBehavior = "smooth") => {
1961
1961
  if (containerRef.current) {