Skip to content

feat(virtual-core): add startItem option #1042

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions docs/api/virtualizer.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ getItemKey?: (index: number) => Key

This function is passed the index of each item and should return a unique key for that item. The default functionality of this function is to return the index of the item, but you should override this when possible to return a unique identifier for each item across the entire set. This function should be memoized to prevent unnecessary re-renders.

### `startItem`

```ts
startItem?: (index: number) => number
```

This optional function is called when you need specials gaps between your items. This measurement should return the left or top of the item depending on `horizontal`. Useful when you know your layouts in advance.

### `rangeExtractor`

```tsx
Expand Down
10 changes: 7 additions & 3 deletions packages/virtual-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ export interface VirtualizerOptions<
scrollPaddingEnd?: number
initialOffset?: number | (() => number)
getItemKey?: (index: number) => Key
startItem?: ((index: number) => number) | null
rangeExtractor?: (range: Range) => Array<number>
scrollMargin?: number
gap?: number
Expand Down Expand Up @@ -431,6 +432,7 @@ export class Virtualizer<
getItemKey: defaultKeyExtractor,
rangeExtractor: defaultRangeExtractor,
onChange: () => {},
startItem: null,
measureElement,
initialRect: { width: 0, height: 0 },
scrollMargin: 0,
Expand Down Expand Up @@ -668,9 +670,11 @@ export class Virtualizer<
? measurements[i - 1]
: this.getFurthestMeasurement(measurements, i)

const start = furthestMeasurement
? furthestMeasurement.end + this.options.gap
: paddingStart + scrollMargin
const start =
this.options.startItem?.(i) ??
(furthestMeasurement
? furthestMeasurement.end + this.options.gap
: paddingStart + scrollMargin)

const measuredSize = itemSizeCache.get(key)
const size =
Expand Down