Skip to content

Commit

Permalink
fix: initialOffset forces layout in window virtualizer (#705)
Browse files Browse the repository at this point in the history
  • Loading branch information
piecyk committed Apr 11, 2024
1 parent 3bf3ae1 commit 44fb184
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/api/virtualizer.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ The padding to apply to the end of the virtualizer in pixels when scrolling to a
### `initialOffset`

```tsx
initialOffset?: number
initialOffset?: number | (() => number)
```

The initial offset to apply to the virtualizer. This is usually only useful if you are rendering the virtualizer in a SSR environment.
Expand Down
2 changes: 1 addition & 1 deletion packages/react-virtual/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function useWindowVirtualizer<TItemElement extends Element>(
observeElementRect: observeWindowRect,
observeElementOffset: observeWindowOffset,
scrollToFn: windowScroll,
initialOffset: typeof document !== 'undefined' ? window.scrollY : undefined,
initialOffset: () => (typeof document !== 'undefined' ? window.scrollY : 0),
...options,
})
}
4 changes: 2 additions & 2 deletions packages/solid-virtual/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ export function createWindowVirtualizer<TItemElement extends Element>(
observeElementRect: observeWindowRect,
observeElementOffset: observeWindowOffset,
scrollToFn: windowScroll,
initialOffset:
typeof document !== 'undefined' ? window.scrollY : undefined,
initialOffset: () =>
typeof document !== 'undefined' ? window.scrollY : 0,
},
options,
),
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte-virtual/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export function createWindowVirtualizer<TItemElement extends Element>(
observeElementRect: observeWindowRect,
observeElementOffset: observeWindowOffset,
scrollToFn: windowScroll,
initialOffset: typeof document !== 'undefined' ? window.scrollY : undefined,
initialOffset: () => (typeof document !== 'undefined' ? window.scrollY : 0),
...options,
})
}
7 changes: 5 additions & 2 deletions packages/virtual-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ export interface VirtualizerOptions<
paddingEnd?: number
scrollPaddingStart?: number
scrollPaddingEnd?: number
initialOffset?: number
initialOffset?: number | (() => number)
getItemKey?: (index: number) => Key
rangeExtractor?: (range: Range) => number[]
scrollMargin?: number
Expand Down Expand Up @@ -320,7 +320,10 @@ export class Virtualizer<
constructor(opts: VirtualizerOptions<TScrollElement, TItemElement>) {
this.setOptions(opts)
this.scrollRect = this.options.initialRect
this.scrollOffset = this.options.initialOffset
this.scrollOffset =
typeof this.options.initialOffset === 'function'
? this.options.initialOffset()
: this.options.initialOffset
this.measurementsCache = this.options.initialMeasurementsCache
this.measurementsCache.forEach((item) => {
this.itemSizeCache.set(item.key, item.size)
Expand Down
4 changes: 2 additions & 2 deletions packages/vue-virtual/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ export function useWindowVirtualizer<TItemElement extends Element>(
observeElementRect: observeWindowRect,
observeElementOffset: observeWindowOffset,
scrollToFn: windowScroll,
initialOffset:
typeof document !== 'undefined' ? window.scrollY : undefined,
initialOffset: () =>
typeof document !== 'undefined' ? window.scrollY : 0,
...unref(options),
})),
)
Expand Down

0 comments on commit 44fb184

Please sign in to comment.