Skip to content

Commit

Permalink
Implement scrollToIndex to other WindowVirtualizers (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
inokawa authored Nov 21, 2024
1 parent 3f0881e commit 32f9f6b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/solid/WindowVirtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { createWindowScroller } from "../core/scroller";
import { ListItem } from "./ListItem";
import { RangedFor } from "./RangedFor";
import { isSameRange } from "./utils";
import { ItemsRange } from "../core/types";
import { ItemsRange, ScrollToIndexOpts } from "../core/types";

/**
* Methods of {@link WindowVirtualizer}.
Expand All @@ -37,6 +37,12 @@ export interface WindowVirtualizerHandle {
* Find the end index of visible range of items.
*/
findEndIndex: () => number;
/**
* Scroll to the item specified by index.
* @param index index of item
* @param opts options
*/
scrollToIndex(index: number, opts?: ScrollToIndexOpts): void;
}

/**
Expand Down Expand Up @@ -150,6 +156,7 @@ export const WindowVirtualizer = <T,>(
props.ref({
findStartIndex: store._findStartIndex,
findEndIndex: store._findEndIndex,
scrollToIndex: scroller._scrollToIndex,
});
}

Expand Down
4 changes: 4 additions & 0 deletions src/svelte/WindowVirtualizer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
GET_ITEMS_LENGTH,
FIND_START_INDEX,
FIND_END_INDEX,
SCROLL_TO_INDEX,
} from "./core";
import { defaultGetKey, iterRange, styleToString } from "./utils";
import ListItem from "./ListItem.svelte";
Expand Down Expand Up @@ -90,6 +91,9 @@
export const findEndIndex = virtualizer[
FIND_END_INDEX
] satisfies WindowVirtualizerHandle["findEndIndex"] as WindowVirtualizerHandle["findEndIndex"];
export const scrollToIndex = virtualizer[
SCROLL_TO_INDEX
] satisfies WindowVirtualizerHandle["scrollToIndex"] as WindowVirtualizerHandle["scrollToIndex"];
let containerStyle = $derived(
styleToString({
Expand Down
7 changes: 7 additions & 0 deletions src/svelte/WindowVirtualizer.type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type Snippet } from "svelte";
import { type ScrollToIndexOpts } from "../core/types";

/**
* Props of {@link WindowVirtualizer}.
Expand Down Expand Up @@ -60,4 +61,10 @@ export interface WindowVirtualizerHandle {
* Find the end index of visible range of items.
*/
findEndIndex: () => number;
/**
* Scroll to the item specified by index.
* @param index index of item
* @param opts options
*/
scrollToIndex(index: number, opts?: ScrollToIndexOpts): void;
}
1 change: 1 addition & 0 deletions src/svelte/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export const createWindowVirtualizer = (
[CHANGE_ITEM_LENGTH]: (len: number, shift?: boolean) => {
store._update(ACTION_ITEMS_LENGTH_CHANGE, [len, shift]);
},
[SCROLL_TO_INDEX]: scroller._scrollToIndex,
[FIND_START_INDEX]: store._findStartIndex,
[FIND_END_INDEX]: store._findEndIndex,
};
Expand Down
9 changes: 8 additions & 1 deletion src/vue/WindowVirtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { createWindowResizer } from "../core/resizer";
import { createWindowScroller } from "../core/scroller";
import { ListItem } from "./ListItem";
import { getKey, isSameRange } from "./utils";
import { ItemsRange } from "../core/types";
import { ItemsRange, ScrollToIndexOpts } from "../core/types";

export interface WindowVirtualizerHandle {
/**
Expand All @@ -36,6 +36,12 @@ export interface WindowVirtualizerHandle {
* Find the end index of visible range of items.
*/
findEndIndex: () => number;
/**
* Scroll to the item specified by index.
* @param index index of item
* @param opts options
*/
scrollToIndex(index: number, opts?: ScrollToIndexOpts): void;
}

const props = {
Expand Down Expand Up @@ -151,6 +157,7 @@ export const WindowVirtualizer = /*#__PURE__*/ defineComponent({
expose({
findStartIndex: store._findStartIndex,
findEndIndex: store._findEndIndex,
scrollToIndex: scroller._scrollToIndex,
} satisfies WindowVirtualizerHandle);

return () => {
Expand Down

0 comments on commit 32f9f6b

Please sign in to comment.