Skip to content

Commit

Permalink
feat: readyStateChanged for VirtuosoGrid
Browse files Browse the repository at this point in the history
  • Loading branch information
petyosi committed Aug 13, 2024
1 parent 14e10f5 commit 9f124f1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
21 changes: 21 additions & 0 deletions examples/grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,24 @@ export function Example() {
</>
)
}

export function ReadyStateChanged() {
const ref = React.createRef<VirtuosoGridHandle>()
const [gridVisible, setGridVisible] = React.useState(false)

return (
<>
<VirtuosoGrid
ref={ref}
readyStateChanged={setGridVisible}
components={{
Item: ItemContainer,
List: ListContainer,
}}
totalCount={100}
itemContent={(index) => <ItemWrapper>Item {index}</ItemWrapper>}
style={{ height: 300, width: 1200, visibility: gridVisible ? 'visible' : 'hidden' }}
/>
</>
)
}
12 changes: 11 additions & 1 deletion src/VirtuosoGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Log, LogLevel } from './loggerSystem'
import { VirtuosoGridHandle, VirtuosoGridProps } from './component-interfaces/VirtuosoGrid'
import { correctItemSize } from './utils/correctItemSize'
import { VirtuosoGridMockContext } from './utils/context'
import useIsomorphicLayoutEffect from './hooks/useIsomorphicLayoutEffect'

const gridComponentPropsSystem = /*#__PURE__*/ u.system(() => {
const itemContent = u.statefulStream<GridItemContent<any, any>>((index) => `Item ${index}`)
Expand All @@ -33,7 +34,10 @@ const gridComponentPropsSystem = /*#__PURE__*/ u.system(() => {
)
}

const readyStateChanged = u.statefulStream(false)

return {
readyStateChanged,
context,
itemContent,
components,
Expand Down Expand Up @@ -71,6 +75,7 @@ const GridItems: React.FC = /*#__PURE__*/ React.memo(function GridItems() {
const gridGap = usePublisher('gap')
const log = useEmitterValue('log')
const stateRestoreInProgress = useEmitterValue('stateRestoreInProgress')
const reportReadyState = usePublisher('readyStateChanged')

const listRef = useSize(
React.useMemo(
Expand All @@ -97,7 +102,11 @@ const GridItems: React.FC = /*#__PURE__*/ React.memo(function GridItems() {
return null
}

// console.log('rendering items', gridState.items)
useIsomorphicLayoutEffect(() => {
if (gridState.itemHeight > 0 && gridState.itemWidth > 0) {
reportReadyState(true)
}
}, [gridState])

return React.createElement(
ListComponent,
Expand Down Expand Up @@ -266,6 +275,7 @@ const {
atBottomStateChange: 'atBottomStateChange',
atTopStateChange: 'atTopStateChange',
stateChanged: 'stateChanged',
readyStateChanged: 'readyStateChanged',
},
},
GridRoot
Expand Down
5 changes: 5 additions & 0 deletions src/component-interfaces/VirtuosoGrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ export interface VirtuosoGridProps<D, C = unknown> extends GridRootProps {
* Pass in an object to achieve additional effects similar to `scrollToIndex`.
*/
initialTopMostItemIndex?: GridIndexLocation

/**
* invoked with true after the grid has done the initial render and the items have been measured.
*/
readyStateChanged?: (ready: boolean) => void
}

export interface VirtuosoGridHandle {
Expand Down

0 comments on commit 9f124f1

Please sign in to comment.