Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initialScroll prop #455

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/core/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { isIOSWebKit } from "./environment";
import type {
CacheSnapshot,
InitialScrollProp,
InternalCacheSnapshot,
ItemResize,
ItemsRange,
Expand Down Expand Up @@ -141,6 +142,7 @@ export const createVirtualStore = (
ssrCount: number = 0,
cacheSnapshot?: CacheSnapshot | undefined,
shouldAutoEstimateItemSize: boolean = false,
initialScroll?: InitialScrollProp | undefined,
startSpacerSize: number = 0
): VirtualStore => {
let isSSR = !!ssrCount;
Expand Down Expand Up @@ -189,6 +191,11 @@ export const createVirtualStore = (
}
};

if (initialScroll) {
scrollOffset =
initialScroll.offset ?? getItemOffset(initialScroll.index || 0);
}

return {
_getStateVersion() {
return stateVersion;
Expand Down
8 changes: 8 additions & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,11 @@ export interface ScrollToIndexOpts {
*/
offset?: number;
}

export interface InitialScrollProp extends ScrollToIndexOpts {
/**
* Index of item.
* @defaultValue 0
*/
index?: number;
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export type {
CacheSnapshot,
ScrollToIndexAlign,
ScrollToIndexOpts,
InitialScrollProp,
} from "./core/types";
export * from "./react";
3 changes: 3 additions & 0 deletions src/react/VList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface VListProps
| "itemSize"
| "shift"
| "horizontal"
| "initialScroll"
| "cache"
| "ssrCount"
| "item"
Expand Down Expand Up @@ -52,6 +53,7 @@ export const VList = forwardRef<VListHandle, VListProps>(
shift,
horizontal,
reverse,
initialScroll,
cache,
ssrCount,
item,
Expand All @@ -76,6 +78,7 @@ export const VList = forwardRef<VListHandle, VListProps>(
itemSize={itemSize}
shift={shift}
horizontal={horizontal}
initialScroll={initialScroll}
cache={cache}
ssrCount={ssrCount}
item={item}
Expand Down
15 changes: 14 additions & 1 deletion src/react/Virtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
import { useLatestRef } from "./useLatestRef";
import { createResizer } from "../core/resizer";
import { ListItem } from "./ListItem";
import { CacheSnapshot, ScrollToIndexOpts } from "../core/types";
import {
CacheSnapshot,
InitialScrollProp,
ScrollToIndexOpts,
} from "../core/types";
import { flushSync } from "react-dom";
import { useRerender } from "./useRerender";
import { useChildren } from "./useChildren";
Expand Down Expand Up @@ -112,6 +116,10 @@
* If true, rendered as a horizontally scrollable list. Otherwise rendered as a vertically scrollable list.
*/
horizontal?: boolean;
/**
* TODO
*/
initialScroll?: InitialScrollProp;
/**
* You can restore cache by passing a {@link CacheSnapshot} on mount. This is useful when you want to restore scroll position after navigation. The snapshot can be obtained from {@link VirtualizerHandle.cache}.
*
Expand Down Expand Up @@ -177,6 +185,7 @@
itemSize,
shift,
horizontal: horizontalProp,
initialScroll,
cache,
startMargin,
ssrCount,
Expand Down Expand Up @@ -208,6 +217,7 @@
ssrCount,
cache,
!itemSize,
initialScroll,
startMargin
);
return [
Expand Down Expand Up @@ -284,6 +294,9 @@
const assignScrollableElement = (e: HTMLElement) => {
resizer._observeRoot(e);
scroller._observe(e);
if (initialScroll) {
scroller._scrollToIndex(initialScroll.index || 0, initialScroll);
}
};
if (scrollRef) {
// parent's ref doesn't exist when useLayoutEffect is called
Expand All @@ -309,7 +322,7 @@
if (!onRangeChangeProp) return;

onRangeChangeProp(startIndex, endIndex);
}, [startIndex, endIndex]);

Check warning on line 325 in src/react/Virtualizer.tsx

View workflow job for this annotation

GitHub Actions / check

React Hook useEffect has a missing dependency: 'onRangeChangeProp'. Either include it or remove the dependency array. If 'onRangeChangeProp' changes too often, find the parent component that defines it and wrap that definition in useCallback

useImperativeHandle(
ref,
Expand All @@ -333,7 +346,7 @@
scrollBy: scroller._scrollBy,
};
},
[]

Check warning on line 349 in src/react/Virtualizer.tsx

View workflow job for this annotation

GitHub Actions / check

React Hook useImperativeHandle has missing dependencies: 'scroller._scrollBy', 'scroller._scrollTo', 'scroller._scrollToIndex', and 'store'. Either include them or remove the dependency array
);

for (let i = overscanedRangeStart, j = overscanedRangeEnd; i <= j; i++) {
Expand Down
1 change: 1 addition & 0 deletions src/vue/Virtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export const Virtualizer = /*#__PURE__*/ defineComponent({
props.ssrCount,
undefined,
!props.itemSize,
undefined,
props.startMargin
);
const resizer = createResizer(store, isHorizontal);
Expand Down
7 changes: 1 addition & 6 deletions stories/react/advanced/Keep offscreen items.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,6 @@ export const SelectedIndex: StoryObj = {
isPrepend.current = false;
});

useEffect(() => {
if (!ref.current) return;

ref.current.scrollToIndex(items.length - 1, { align: "end" });
}, []);

const toggleEditing = useCallback((itemId: number) => {
setEditingItemId((currentValue) =>
itemId === currentValue ? null : itemId
Expand Down Expand Up @@ -185,6 +179,7 @@ export const SelectedIndex: StoryObj = {
ref={ref}
style={{ flex: 1 }}
reverse
initialScroll={{ index: items.length - 1, align: "end" }}
keepMounted={
editingItemId
? [items.findIndex((item) => item.id === editingItemId)]
Expand Down
17 changes: 7 additions & 10 deletions stories/react/basics/VList.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,8 @@ export const PaddingAndMargin: StoryObj = {

export const Reverse: StoryObj = {
render: () => {
const ref = useRef<VListHandle>(null);
useEffect(() => {
ref.current?.scrollToIndex(999);
}, []);
return (
<VList ref={ref} style={{ height: "100vh" }} reverse>
<VList style={{ height: "100vh" }} reverse initialScroll={{ index: 999 }}>
{createRows(1000)}
</VList>
);
Expand Down Expand Up @@ -402,10 +398,6 @@ const RestorableList = ({ id }: { id: string }) => {
if (!ref.current) return;
const handle = ref.current;

if (offset) {
handle.scrollTo(offset);
}

return () => {
sessionStorage.setItem(
cacheKey,
Expand All @@ -415,7 +407,12 @@ const RestorableList = ({ id }: { id: string }) => {
}, []);

return (
<VList ref={ref} cache={cache} style={{ height: "100vh" }}>
<VList
ref={ref}
cache={cache}
initialScroll={{ offset }}
style={{ height: "100vh" }}
>
{createRows(1000)}
</VList>
);
Expand Down
Loading