From fecde2042fdafa134a106402185703e94864c351 Mon Sep 17 00:00:00 2001 From: Himanshu Aggarwal Date: Sun, 22 Dec 2024 14:20:07 -0500 Subject: [PATCH 1/5] Fix smooth scroll issue on SSR hydration --- src/core/store.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/core/store.ts b/src/core/store.ts index d765be26..0e10fc02 100644 --- a/src/core/store.ts +++ b/src/core/store.ts @@ -332,17 +332,13 @@ export const createVirtualStore = ( if ( // Keep distance from end during shifting _scrollMode === SCROLL_BY_SHIFT || - (_frozenRange - ? // https://github.com/inokawa/virtua/issues/380 - index < _frozenRange[0] - : // Otherwise we should maintain visible position getItemOffset(index) + // https://github.com/inokawa/virtua/issues/385 (_scrollDirection === SCROLL_IDLE && _scrollMode === SCROLL_BY_NATIVE ? getItemSize(index) : 0) < - getRelativeScrollOffset()) + getRelativeScrollOffset() ) { acc += size - getItemSize(index); } From 1481e4f6b13c615d124562e4b472d474d54fc832 Mon Sep 17 00:00:00 2001 From: Himanshu Aggarwal Date: Sun, 22 Dec 2024 14:21:51 -0500 Subject: [PATCH 2/5] Update SSR story to include scroll on hydration --- stories/react/advanced/SSR.stories.tsx | 85 +++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 7 deletions(-) diff --git a/stories/react/advanced/SSR.stories.tsx b/stories/react/advanced/SSR.stories.tsx index 4444e4e9..0da6e8b5 100644 --- a/stories/react/advanced/SSR.stories.tsx +++ b/stories/react/advanced/SSR.stories.tsx @@ -1,6 +1,6 @@ import { Meta, StoryObj } from "@storybook/react"; -import React, { useLayoutEffect, useRef, useState } from "react"; -import { VList } from "../../../src"; +import React, { useEffect, useLayoutEffect, useRef, useState } from "react"; +import { VList, type VListHandle } from "../../../src"; import { hydrateRoot } from "react-dom/client"; import { renderToString } from "react-dom/server"; @@ -26,14 +26,40 @@ const createRows = (num: number) => { }); }; -const App = () => { - const COUNT = 1000; - return {createRows(COUNT)}; +const App = ({ + scrollOnMount, + scrollToIndex, + smooth, +}: { + scrollOnMount?: boolean; + scrollToIndex?: number; + smooth?: boolean; +}) => { + const ref = useRef(null); + useEffect(() => { + if (!ref.current || !scrollOnMount || !scrollToIndex) return; + + ref.current.scrollToIndex(scrollToIndex, { + smooth: smooth, + }); + }, []); + + const COUNT = 10000; + return ( + <> + + {createRows(COUNT)} + + + ); }; export const Default: StoryObj = { name: "SSR", render: () => { + const [scrollOnMount, setScrollOnMount] = useState(false); + const [scrollIndex, setScrollIndex] = useState(100); + const [smooth, setSmooth] = useState(true); const [hydrated, setHydrated] = useState(false); const ref = useRef(null); @@ -43,7 +69,14 @@ export const Default: StoryObj = { if (!hydrated) { ref.current.innerHTML = renderToString(); } else { - hydrateRoot(ref.current, ); + hydrateRoot( + ref.current, + + ); } }, [hydrated]); @@ -51,7 +84,13 @@ export const Default: StoryObj = {
-
+
+
+ + + { + setScrollIndex(Number(e.target.value)); + }} + /> + +
From 73db915ff43f3c8732ed9ef1abae1851c743c74a Mon Sep 17 00:00:00 2001 From: Himanshu Aggarwal Date: Sun, 22 Dec 2024 16:00:53 -0500 Subject: [PATCH 3/5] Simplify hydration options spacing with flex --- stories/react/advanced/SSR.stories.tsx | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/stories/react/advanced/SSR.stories.tsx b/stories/react/advanced/SSR.stories.tsx index 0da6e8b5..daf054bd 100644 --- a/stories/react/advanced/SSR.stories.tsx +++ b/stories/react/advanced/SSR.stories.tsx @@ -99,12 +99,11 @@ export const Default: StoryObj = { > hydrate -
+
-