Skip to content

Commit 620b260

Browse files
committed
changeset + example
1 parent b04c3e6 commit 620b260

2 files changed

Lines changed: 134 additions & 0 deletions

File tree

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 `atBottomStateChange` in `useWindowScroll` lists when the Virtuoso instance is rendered below other page content.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import { useRef, useState } from "react";
2+
3+
import { Virtuoso } from "../src";
4+
5+
import type { VirtuosoHandle } from "../src";
6+
7+
const INITIAL_COUNT = 60;
8+
9+
function itemHeight(index: number) {
10+
return 56 + (index % 4) * 20;
11+
}
12+
13+
export function Example() {
14+
const virtuosoRef = useRef<VirtuosoHandle>(null);
15+
const [count, setCount] = useState(INITIAL_COUNT);
16+
const [atBottom, setAtBottom] = useState(false);
17+
const [bottomHits, setBottomHits] = useState(0);
18+
19+
return (
20+
<div
21+
style={{
22+
minHeight: "100vh",
23+
padding: "0 24px 48px",
24+
}}
25+
>
26+
<div
27+
style={{
28+
margin: "0 auto",
29+
maxWidth: 720,
30+
paddingTop: 24,
31+
position: "sticky",
32+
top: 16,
33+
zIndex: 1,
34+
}}
35+
>
36+
<div
37+
style={{
38+
background: "#e2e8f0",
39+
border: "1px solid #94a3b8",
40+
color: "#0f172a",
41+
display: "flex",
42+
flexWrap: "wrap",
43+
gap: 12,
44+
padding: 16,
45+
}}
46+
>
47+
<strong>Window scroll + atBottomStateChange</strong>
48+
<span>state: {atBottom ? "at bottom" : "away from bottom"}</span>
49+
<span>bottom hits: {bottomHits}</span>
50+
<button
51+
onClick={() => {
52+
virtuosoRef.current?.scrollToIndex({
53+
align: "end",
54+
index: count - 1,
55+
behavior: "auto",
56+
});
57+
}}
58+
style={{ padding: "6px 10px" }}
59+
>
60+
Scroll to bottom
61+
</button>
62+
<button
63+
onClick={() => {
64+
setCount((current) => current + 5);
65+
}}
66+
style={{ padding: "6px 10px" }}
67+
>
68+
Append 5 items
69+
</button>
70+
</div>
71+
</div>
72+
73+
<div
74+
style={{
75+
background: "#f8fafc",
76+
border: "1px solid #bfdbfe",
77+
margin: "32px auto 24px",
78+
maxWidth: 720,
79+
minHeight: 280,
80+
padding: "32px 24px",
81+
}}
82+
>
83+
<h2 style={{ margin: 0 }}>Offset content above the list</h2>
84+
<p style={{ lineHeight: 1.5, margin: "12px 0 0" }}>
85+
Scroll through this intro block first, then continue to the end of the
86+
list. The state badge should switch to at bottom once the last item
87+
reaches the viewport bottom.
88+
</p>
89+
</div>
90+
91+
<div style={{ margin: "0 auto", maxWidth: 720 }}>
92+
<Virtuoso
93+
atBottomStateChange={(next) => {
94+
setAtBottom(next);
95+
if (next) {
96+
setBottomHits((current) => current + 1);
97+
}
98+
}}
99+
followOutput={"smooth"}
100+
initialItemCount={8}
101+
itemContent={(index) => {
102+
const height = itemHeight(index);
103+
104+
return (
105+
<div
106+
style={{
107+
background: index % 2 === 0 ? "#ffffff" : "#f1f5f9",
108+
borderBottom: "1px solid #cbd5e1",
109+
boxSizing: "border-box",
110+
height,
111+
padding: "16px 20px",
112+
}}
113+
>
114+
<strong>Row {index + 1}</strong>
115+
<div style={{ color: "#475569" }}>
116+
Variable height: {height}px
117+
</div>
118+
</div>
119+
);
120+
}}
121+
ref={virtuosoRef}
122+
style={{ border: "1px solid #cbd5e1", height: 520 }}
123+
totalCount={count}
124+
useWindowScroll
125+
/>
126+
</div>
127+
</div>
128+
);
129+
}

0 commit comments

Comments
 (0)