Skip to content

Commit

Permalink
fix: account for headerHeight when restoring state (#1110)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Segal <[email protected]>
  • Loading branch information
phazei and Adam Segal authored Jul 20, 2024
1 parent e27936a commit 370023b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
7 changes: 7 additions & 0 deletions examples/state.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as React from 'react'
import { StateSnapshot, Virtuoso, VirtuosoHandle } from '../src'

function Header() {
return <div>Header</div>
}

export function Example() {
const ref = React.useRef<VirtuosoHandle>(null)
const state = React.useRef<StateSnapshot | undefined>(undefined)
Expand All @@ -25,6 +29,9 @@ export function Example() {
ref={ref}
restoreStateFrom={state.current}
computeItemKey={(key: number) => `item-${key.toString()}`}
components={{
Header,
}}
totalCount={100}
itemContent={(index) => <div style={{ height: index % 2 ? 30 : 20 }}>Item {index}</div>}
style={{ height: 300 }}
Expand Down
10 changes: 7 additions & 3 deletions src/stateLoadSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { windowScrollerSystem } from './windowScrollerSystem'
export const stateLoadSystem = u.system(
([
{ sizes, sizeRanges },
{ scrollTop },
{ scrollTop, headerHeight },
{ initialTopMostItemIndex },
{ didMount },
{ useWindowScroll, windowScrollContainerState, windowViewportRect },
Expand All @@ -24,12 +24,16 @@ export const stateLoadSystem = u.system(
u.connect(windowViewportRect, statefulWindowViewportRect)

u.subscribe(
u.pipe(getState, u.withLatestFrom(sizes, scrollTop, useWindowScroll, statefulWindowScrollContainerState, statefulWindowViewportRect)),
([callback, sizes, scrollTop, useWindowScroll, windowScrollContainerState, windowViewportRect]) => {
u.pipe(
getState,
u.withLatestFrom(sizes, scrollTop, useWindowScroll, statefulWindowScrollContainerState, statefulWindowViewportRect, headerHeight)
),
([callback, sizes, scrollTop, useWindowScroll, windowScrollContainerState, windowViewportRect, headerHeight]) => {
const ranges = sizeTreeToRanges(sizes.sizeTree)
if (useWindowScroll && windowScrollContainerState !== null && windowViewportRect !== null) {
scrollTop = windowScrollContainerState.scrollTop - windowViewportRect.offsetTop
}
scrollTop -= headerHeight
callback({ ranges, scrollTop })
}
)
Expand Down

0 comments on commit 370023b

Please sign in to comment.