Skip to content

Commit

Permalink
Merge pull request #539 from jackhodkinson/feature/get-item-size
Browse files Browse the repository at this point in the history
Add `getItemSize` to `VirtualizerHandle`
  • Loading branch information
inokawa authored Nov 10, 2024
2 parents 231fb7e + 983e725 commit 2bf55a3
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/react/Virtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ export interface VirtualizerHandle {
* @param index index of item
*/
getItemOffset(index: number): number;
/**
* Get item size.
* @param index index of item
*/
getItemSize(index: number): number;
/**
* Scroll to the item specified by index.
* @param index index of item
Expand Down Expand Up @@ -324,6 +329,7 @@ export const Virtualizer = forwardRef<VirtualizerHandle, VirtualizerProps>(
return store._getViewportSize();
},
getItemOffset: store._getItemOffset,
getItemSize: store._getItemSize,
scrollToIndex: scroller._scrollToIndex,
scrollTo: scroller._scrollTo,
scrollBy: scroller._scrollBy,
Expand Down
6 changes: 6 additions & 0 deletions src/solid/Virtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ export interface VirtualizerHandle {
* @param index index of item
*/
getItemOffset(index: number): number;
/**
* Get item size.
* @param index index of item
*/
getItemSize(index: number): number;
/**
* Scroll to the item specified by index.
* @param index index of item
Expand Down Expand Up @@ -223,6 +228,7 @@ export const Virtualizer = <T,>(props: VirtualizerProps<T>): JSX.Element => {
return store._getViewportSize();
},
getItemOffset: store._getItemOffset,
getItemSize: store._getItemSize,
scrollToIndex: scroller._scrollToIndex,
scrollTo: scroller._scrollTo,
scrollBy: scroller._scrollBy,
Expand Down
8 changes: 8 additions & 0 deletions src/svelte/VList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
ref.getScrollSize()) satisfies VListHandle["getScrollSize"] as VListHandle["getScrollSize"];
export const getViewportSize = (() =>
ref.getViewportSize()) satisfies VListHandle["getViewportSize"] as VListHandle["getViewportSize"];
export const getItemOffset = ((...args) =>
ref.getItemOffset(
...args
)) satisfies VListHandle["getItemOffset"] as VListHandle["getItemOffset"];
export const getItemSize = ((...args) =>
ref.getItemSize(
...args
)) satisfies VListHandle["getItemSize"] as VListHandle["getItemSize"];
export const scrollToIndex = ((...args) =>
ref.scrollToIndex(
...args
Expand Down
7 changes: 7 additions & 0 deletions src/svelte/Virtualizer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
CHANGE_START_MARGIN,
GET_START_SPACER_SIZE,
GET_ITEMS_LENGTH,
GET_ITEM_SIZE,
} from "./core";
import { defaultGetKey, styleToString } from "./utils";
import ListItem from "./ListItem.svelte";
Expand Down Expand Up @@ -131,6 +132,12 @@
export const getViewportSize = virtualizer[
GET_VIEWPORT_SIZE
] satisfies VirtualizerHandle["getViewportSize"] as VirtualizerHandle["getViewportSize"];
export const getItemOffset = virtualizer[
GET_ITEM_OFFSET
] satisfies VirtualizerHandle["getItemOffset"] as VirtualizerHandle["getItemOffset"];
export const getItemSize = virtualizer[
GET_ITEM_SIZE
] satisfies VirtualizerHandle["getItemSize"] as VirtualizerHandle["getItemSize"];
export const scrollToIndex = virtualizer[
SCROLL_TO_INDEX
] satisfies VirtualizerHandle["scrollToIndex"] as VirtualizerHandle["scrollToIndex"];
Expand Down
10 changes: 10 additions & 0 deletions src/svelte/Virtualizer.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ export interface VirtualizerHandle {
* Get current offsetHeight, or offsetWidth if horizontal: true.
*/
getViewportSize: () => number;
/**
* Get item offset from start.
* @param index index of item
*/
getItemOffset(index: number): number;
/**
* Get item size.
* @param index index of item
*/
getItemSize(index: number): number;
/**
* Scroll to the item specified by index.
* @param index index of item
Expand Down
24 changes: 13 additions & 11 deletions src/svelte/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ export const GET_SCROLL_OFFSET = 5;
export const GET_SCROLL_DIRECTION = 6;
export const GET_JUMP_COUNT = 7;
export const GET_ITEM_OFFSET = 8;
export const IS_ITEM_HIDDEN = 9;
export const GET_ITEMS_LENGTH = 10;
export const GET_START_SPACER_SIZE = 11;
export const OBSERVE_ITEM_RESIZE = 12;
export const FIX_SCROLL_JUMP = 13;
export const CHANGE_ITEM_LENGTH = 14;
export const CHANGE_START_MARGIN = 15;
export const GET_SCROLL_SIZE = 16;
export const SCROLL_TO = 17;
export const SCROLL_BY = 18;
export const SCROLL_TO_INDEX = 19;
export const GET_ITEM_SIZE = 9;
export const IS_ITEM_HIDDEN = 10;
export const GET_ITEMS_LENGTH = 11;
export const GET_START_SPACER_SIZE = 12;
export const OBSERVE_ITEM_RESIZE = 13;
export const FIX_SCROLL_JUMP = 14;
export const CHANGE_ITEM_LENGTH = 15;
export const CHANGE_START_MARGIN = 16;
export const GET_SCROLL_SIZE = 17;
export const SCROLL_TO = 18;
export const SCROLL_BY = 19;
export const SCROLL_TO_INDEX = 20;

/**
* This function is workaround for terser minification.
Expand Down Expand Up @@ -95,6 +96,7 @@ export const createVirtualizer = (
[GET_SCROLL_DIRECTION]: store._getScrollDirection,
[GET_JUMP_COUNT]: store._getJumpCount,
[GET_ITEM_OFFSET]: store._getItemOffset,
[GET_ITEM_SIZE]: store._getItemSize,
[IS_ITEM_HIDDEN]: store._isUnmeasuredItem,
[GET_ITEMS_LENGTH]: store._getItemsLength,
[GET_START_SPACER_SIZE]: store._getStartSpacerSize,
Expand Down
1 change: 1 addition & 0 deletions src/vue/VList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const VList = /*#__PURE__*/ defineComponent({
return handle.value!.viewportSize;
},
getItemOffset: (...args) => handle.value!.getItemOffset(...args),
getItemSize: (...args) => handle.value!.getItemSize(...args),
scrollToIndex: (...args) => handle.value!.scrollToIndex(...args),
scrollTo: (...args) => handle.value!.scrollTo(...args),
scrollBy: (...args) => handle.value!.scrollBy(...args),
Expand Down
6 changes: 6 additions & 0 deletions src/vue/Virtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export interface VirtualizerHandle {
* @param index index of item
*/
getItemOffset(index: number): number;
/**
* Get item size.
* @param index index of item
*/
getItemSize(index: number): number;
/**
* Scroll to the item specified by index.
* @param index index of item
Expand Down Expand Up @@ -218,6 +223,7 @@ export const Virtualizer = /*#__PURE__*/ defineComponent({
return store._getViewportSize();
},
getItemOffset: store._getItemOffset,
getItemSize: store._getItemSize,
scrollToIndex: scroller._scrollToIndex,
scrollTo: scroller._scrollTo,
scrollBy: scroller._scrollBy,
Expand Down

0 comments on commit 2bf55a3

Please sign in to comment.