cogsbox-state 0.5.422 → 0.5.423

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.422",
3
+ "version": "0.5.423",
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
@@ -1930,12 +1930,8 @@ function createProxyHandler<T>(
1930
1930
  setRange(newRange);
1931
1931
 
1932
1932
  const timeoutId = setTimeout(() => {
1933
- if (containerRef.current) {
1934
- containerRef.current.scrollTop =
1935
- containerRef.current.scrollHeight;
1936
- }
1933
+ scrollToIndex(totalCount - 1, "smooth");
1937
1934
  }, 50);
1938
-
1939
1935
  return () => clearTimeout(timeoutId);
1940
1936
  }
1941
1937
 
@@ -2023,32 +2019,48 @@ function createProxyHandler<T>(
2023
2019
  containerRef.current.scrollHeight;
2024
2020
  }
2025
2021
  }, [scrollToLastItem]);
2026
-
2027
2022
  const scrollToIndex = useCallback(
2028
2023
  (index: number, behavior: ScrollBehavior = "smooth") => {
2024
+ const container = containerRef.current;
2025
+ if (!container) return;
2026
+
2027
+ const isLastItem = index === totalCount - 1;
2028
+
2029
+ // --- Special Case: The Last Item ---
2030
+ if (isLastItem) {
2031
+ // For the last item, scrollIntoView can fail. The most reliable method
2032
+ // is to scroll the parent container to its maximum scroll height.
2033
+ container.scrollTo({
2034
+ top: container.scrollHeight,
2035
+ behavior: behavior,
2036
+ });
2037
+ return; // We're done.
2038
+ }
2039
+
2040
+ // --- Standard Case: All Other Items ---
2041
+ // For all other items, we find the ref and use scrollIntoView.
2029
2042
  const shadowArray =
2030
2043
  getGlobalStore
2031
2044
  .getState()
2032
2045
  .getShadowMetadata(stateKey, path) || [];
2033
2046
  const itemData = shadowArray[index];
2047
+ const element = itemData?.virtualizer?.domRef;
2034
2048
 
2035
- if (itemData?.virtualizer?.domRef) {
2036
- const element = itemData.virtualizer.domRef;
2037
- if (element && element.scrollIntoView) {
2038
- element.scrollIntoView({ behavior, block: "end" });
2039
- return;
2040
- }
2041
- }
2042
-
2043
- // Fallback to position-based scrolling
2044
- if (containerRef.current && positions[index] !== undefined) {
2045
- containerRef.current.scrollTo({
2049
+ if (element) {
2050
+ // 'center' gives a better user experience for items in the middle of the list.
2051
+ element.scrollIntoView({
2052
+ behavior: behavior,
2053
+ block: "center",
2054
+ });
2055
+ } else if (positions[index] !== undefined) {
2056
+ // Fallback if the ref isn't available for some reason.
2057
+ container.scrollTo({
2046
2058
  top: positions[index],
2047
2059
  behavior,
2048
2060
  });
2049
2061
  }
2050
2062
  },
2051
- [positions, stateKey, path]
2063
+ [positions, stateKey, path, totalCount] // Add totalCount to the dependencies
2052
2064
  );
2053
2065
 
2054
2066
  const virtualizerProps = {