Skip to content

Commit

Permalink
fix: props race condition with initial topmost
Browse files Browse the repository at this point in the history
  • Loading branch information
petyosi committed Mar 19, 2024
1 parent c5c6dbd commit f4add10
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
92 changes: 91 additions & 1 deletion examples/initial-topmost-item-default-height.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as React from 'react'
import { Virtuoso } from '../src'
import { LogLevel, Virtuoso } from '../src'

//
const itemContent = (index: number) => <div style={{ height: index % 2 ? 30 : 20, background: 'white' }}>Item {index}</div>

export function Example() {
return (
<div>
Expand All @@ -11,3 +12,92 @@ export function Example() {
</div>
)
}

function useSuppressResizeObserverError() {
React.useEffect(() => {
const cb = (e: ErrorEvent) => {
if (e.message === 'ResizeObserver loop completed with undelivered notifications.') {
e.preventDefault()
}
}
window.addEventListener('error', cb)
return () => {
window.removeEventListener('error', cb)
}
}, [])
}

export function BombProps() {
// const [channelId, setChannelId] = React.useState(1);
useSuppressResizeObserverError()
const [bogus, setBogus] = React.useState(0)

React.useEffect(() => {
setTimeout(() => {
setBogus((prev) => prev + 1)
setTimeout(() => {
setBogus((prev) => prev + 1)
setTimeout(() => {
setBogus((prev) => prev + 1)
setTimeout(() => {
setBogus((prev) => prev + 1)
}, 2)
}, 2)
}, 2)
}, 2)
}, [])

return (
<div className="App">
{/*
<button
onClick={() => {
setChannelId(1);
}}
>
Channel 1
</button>
<button
onClick={() => {
setChannelId(2);
}}
>
Channel 2
</button>
<button
onClick={() => {
setChannelId(3);
}}
>
Channel 3
</button>
*/}

<Virtuoso
logLevel={LogLevel.DEBUG}
totalCount={15}
context={{ bogus }}
defaultItemHeight={50}
itemContent={(index: number) => {
return (
<div
style={{
height: index % 2 ? 200 : 100,
background: index % 2 ? 'red' : 'blue',
}}
>
Item {index}
</div>
)
}}
initialTopMostItemIndex={14}
alignToBottom={true}
followOutput="auto"
increaseViewportBy={100}
style={{ height: 800 }}
/>
</div>
)
}
5 changes: 4 additions & 1 deletion src/followOutputSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ export const followOutputSystem = u.system(
u.withLatestFrom(followOutput, totalCount)
),
([, followOutput]) => {
trapNextSizeIncrease(followOutput !== false)
// activate adjustment only if the initial item is already scrolled to
if (u.getValue(scrolledToInitialItem)) {
trapNextSizeIncrease(followOutput !== false)
}
}
)

Expand Down

0 comments on commit f4add10

Please sign in to comment.