Skip to content

Commit fa9dd31

Browse files
committed
Fix window-scroll SSR viewport layout
1 parent db67907 commit fa9dd31

6 files changed

Lines changed: 85 additions & 7 deletions

File tree

‎.changeset/curvy-brooms-smoke.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-virtuoso': patch
3+
---
4+
5+
Fix `useWindowScroll` SSR layout collapse by rendering the window viewport in normal flow while preserving sticky top items.

‎packages/react-virtuoso/src/TableVirtuoso.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
identity,
1717
itemPropIfNotDomElement,
1818
viewportStyle,
19+
windowViewportStyle,
1920
} from './Virtuoso'
2021

2122
import type {
@@ -274,6 +275,7 @@ const WindowViewport: React.FC<React.PropsWithChildren> = ({ children }) => {
274275
const windowViewportRect = usePublisher('windowViewportRect')
275276
const fixedItemHeight = usePublisher('fixedItemHeight')
276277
const customScrollParent = useEmitterValue('customScrollParent')
278+
const useWindowScroll = useEmitterValue('useWindowScroll')
277279
const viewportRef = useWindowViewportRectRef(
278280
windowViewportRect,
279281
customScrollParent,
@@ -288,7 +290,7 @@ const WindowViewport: React.FC<React.PropsWithChildren> = ({ children }) => {
288290
}, [ctx, windowViewportRect, fixedItemHeight])
289291

290292
return (
291-
<div data-viewport-type="window" ref={viewportRef} style={viewportStyle(false)}>
293+
<div data-viewport-type="window" ref={viewportRef} style={windowViewportStyle(false, useWindowScroll)}>
292294
{children}
293295
</div>
294296
)

‎packages/react-virtuoso/src/Virtuoso.tsx‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ export const viewportStyle: (alignToBottom: boolean) => React.CSSProperties = (a
227227
...(alignToBottom ? { display: 'flex', flexDirection: 'column' } : undefined),
228228
})
229229

230+
export const windowViewportStyle = (alignToBottom: boolean, useWindowScroll: boolean, topOffset = 0): React.CSSProperties => ({
231+
...viewportStyle(alignToBottom),
232+
position: useWindowScroll ? 'relative' : 'absolute',
233+
top: useWindowScroll ? -topOffset : 0,
234+
})
235+
230236
const topItemListStyle: React.CSSProperties = {
231237
position: positionStickyCssValue(),
232238
top: 0,
@@ -407,6 +413,8 @@ const WindowViewport: React.FC<React.PropsWithChildren> = ({ children }) => {
407413
const windowViewportRect = usePublisher('windowViewportRect')
408414
const fixedItemHeight = usePublisher('fixedItemHeight')
409415
const customScrollParent = useEmitterValue('customScrollParent')
416+
const useWindowScroll = useEmitterValue('useWindowScroll')
417+
const topListHeight = useEmitterValue('topListHeight')
410418
const viewportRef = useWindowViewportRectRef(
411419
windowViewportRect,
412420
customScrollParent,
@@ -422,7 +430,7 @@ const WindowViewport: React.FC<React.PropsWithChildren> = ({ children }) => {
422430
}, [ctx, windowViewportRect, fixedItemHeight])
423431

424432
return (
425-
<div data-viewport-type="window" ref={viewportRef} style={viewportStyle(alignToBottom)}>
433+
<div data-viewport-type="window" ref={viewportRef} style={windowViewportStyle(alignToBottom, useWindowScroll, topListHeight)}>
426434
{children}
427435
</div>
428436
)

‎packages/react-virtuoso/src/VirtuosoGrid.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { systemToComponent } from './react-urx'
99
import * as u from './urx'
1010
import { VirtuosoGridMockContext } from './utils/context'
1111
import { correctItemSize } from './utils/correctItemSize'
12-
import { buildScroller, buildWindowScroller, contextPropIfNotDomElement, identity, viewportStyle } from './Virtuoso'
12+
import { buildScroller, buildWindowScroller, contextPropIfNotDomElement, identity, viewportStyle, windowViewportStyle } from './Virtuoso'
1313

1414
import type { VirtuosoGridHandle, VirtuosoGridProps } from './component-interfaces/VirtuosoGrid'
1515
import type { GridComponents, GridComputeItemKey, GridItemContent, GridRootProps } from './interfaces'
@@ -229,6 +229,7 @@ const WindowViewport: React.FC<React.PropsWithChildren> = ({ children }) => {
229229
const windowViewportRect = usePublisher('windowViewportRect')
230230
const itemDimensions = usePublisher('itemDimensions')
231231
const customScrollParent = useEmitterValue('customScrollParent')
232+
const useWindowScroll = useEmitterValue('useWindowScroll')
232233
const viewportRef = useWindowViewportRectRef(windowViewportRect, customScrollParent, false)
233234

234235
React.useEffect(() => {
@@ -239,7 +240,7 @@ const WindowViewport: React.FC<React.PropsWithChildren> = ({ children }) => {
239240
}, [ctx, windowViewportRect, itemDimensions])
240241

241242
return (
242-
<div ref={viewportRef} style={viewportStyle(false)}>
243+
<div ref={viewportRef} style={windowViewportStyle(false, useWindowScroll)}>
243244
{children}
244245
</div>
245246
)

‎packages/react-virtuoso/test/VirtuosoMockContext.test.tsx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ describe('VirtuosoMockContext', () => {
3939

4040
expect(container).toMatchSnapshot()
4141
})
42+
43+
it('offsets the window viewport by the sticky top items height', () => {
44+
const { container } = render(<Virtuoso data={data} topItemCount={2} useWindowScroll />, {
45+
wrapper: ({ children }) => (
46+
<VirtuosoMockContext.Provider value={{ itemHeight: 100, viewportHeight: 300 }}>{children}</VirtuosoMockContext.Provider>
47+
),
48+
})
49+
50+
expect(container).toMatchSnapshot()
51+
})
4252
})
4353

4454
describe('Table', () => {

‎packages/react-virtuoso/test/__snapshots__/VirtuosoMockContext.test.tsx.snap‎

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ exports[`VirtuosoMockContext > Grid > correctly renders items with useWindowScro
6565
style="position: relative; height: 300px;"
6666
>
6767
<div
68-
style="height: 100%; position: absolute; top: 0px; width: 100%;"
68+
style="height: 100%; position: relative; top: 0px; width: 100%;"
6969
>
7070
<div
7171
class="virtuoso-grid-list"
@@ -168,7 +168,7 @@ exports[`VirtuosoMockContext > List > correctly renders items with useWindowScro
168168
>
169169
<div
170170
data-viewport-type="window"
171-
style="height: 100%; position: absolute; top: 0px; width: 100%;"
171+
style="height: 100%; position: relative; top: 0px; width: 100%;"
172172
>
173173
<div
174174
data-testid="virtuoso-item-list"
@@ -204,6 +204,58 @@ exports[`VirtuosoMockContext > List > correctly renders items with useWindowScro
204204
</div>
205205
`;
206206

207+
exports[`VirtuosoMockContext > List > offsets the window viewport by the sticky top items height 1`] = `
208+
<div>
209+
<div
210+
data-virtuoso-scroller="true"
211+
style="position: relative; height: 800px;"
212+
>
213+
<div
214+
style="position: -webkit-sticky; top: 0px; width: 100%; z-index: 1; margin-top: 0px;"
215+
>
216+
<div
217+
data-testid="virtuoso-top-item-list"
218+
>
219+
<div
220+
data-index="0"
221+
data-item-index="0"
222+
data-known-size="100"
223+
style="overflow-anchor: none;"
224+
>
225+
Item 0
226+
</div>
227+
<div
228+
data-index="1"
229+
data-item-index="1"
230+
data-known-size="100"
231+
style="overflow-anchor: none;"
232+
>
233+
Item 1
234+
</div>
235+
</div>
236+
</div>
237+
<div
238+
data-viewport-type="window"
239+
style="height: 100%; position: relative; top: -200px; width: 100%;"
240+
>
241+
<div
242+
data-testid="virtuoso-item-list"
243+
style="box-sizing: border-box; margin-top: 0px; padding-bottom: 500px; padding-top: 200px;"
244+
>
245+
<div
246+
data-index="2"
247+
data-item-index="2"
248+
data-known-size="100"
249+
style="overflow-anchor: none;"
250+
>
251+
Item 2
252+
</div>
253+
</div>
254+
</div>
255+
</div>
256+
</div>
257+
`;
258+
207259
exports[`VirtuosoMockContext > Table > correctly renders items 1`] = `
208260
<div>
209261
<div
@@ -275,7 +327,7 @@ exports[`VirtuosoMockContext > Table > correctly renders items with useWindowScr
275327
>
276328
<div
277329
data-viewport-type="window"
278-
style="height: 100%; position: absolute; top: 0px; width: 100%;"
330+
style="height: 100%; position: relative; top: 0px; width: 100%;"
279331
>
280332
<table
281333
style="border-spacing: 0px; overflow-anchor: none;"

0 commit comments

Comments
 (0)