Skip to content

Commit 789da38

Browse files
committed
chore: update examples
1 parent 2fb37d1 commit 789da38

File tree

2 files changed

+28
-47
lines changed

2 files changed

+28
-47
lines changed

examples/sandbox/src/Dynamic.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,23 +147,24 @@ function GridVirtualizerDynamic({ rows, columns }) {
147147
const rowVirtualizer = useVirtual({
148148
size: rows.length,
149149
parentRef,
150-
estimateSize: React.useCallback(() => 35, []), // This is just a best guess
151-
overscan: 5
150+
overscan: 0
152151
});
153152

154153
const columnVirtualizer = useVirtual({
155154
horizontal: true,
156155
size: columns.length,
157156
parentRef,
158-
estimateSize: React.useCallback(() => 100, []), // This is just a best guess
159157
overscan: 5
160158
});
161159

162160
const [show, setShow] = React.useState(true);
163161

164162
return (
165163
<>
166-
{/* <button onClick={() => setShow(old => !old)}>Toggle</button> */}
164+
<button onClick={() => setShow(old => !old)}>Toggle</button>
165+
<button onClick={() => rowVirtualizer.scrollToIndex(5000)}>
166+
Scroll to 5000
167+
</button>
167168
{show ? (
168169
<div
169170
ref={parentRef}

examples/sandbox/src/SmoothScroll.js

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,32 @@ function easeInOutQuint(t) {
66
return t < 0.5 ? 16 * t * t * t * t * t : 1 + 16 * --t * t * t * t * t;
77
}
88

9-
const timeout = fn => setTimeout(fn, 16);
10-
11-
const raf = fn => requestAnimationFrame(fn);
12-
139
export default function() {
1410
const parentRef = React.useRef();
1511
const scrollingRef = React.useRef();
1612

17-
const [animationType, setAnimationType] = React.useState("setTimeout");
18-
19-
const animationFn = animationType === "setTimeout" ? timeout : raf;
20-
21-
const scrollToFn = React.useCallback(
22-
offset => {
23-
const duration = 1000;
24-
const start = parentRef.current.scrollTop;
25-
const startTime = (scrollingRef.current = Date.now());
26-
27-
const run = () => {
28-
if (scrollingRef.current !== startTime) return;
29-
const now = Date.now();
30-
const elapsed = now - startTime;
31-
const progress = easeInOutQuint(Math.min(elapsed / duration, 1));
32-
const interpolated = start + (offset - start) * progress;
33-
34-
if (elapsed < duration) {
35-
parentRef.current.scrollTop = interpolated;
36-
animationFn(run);
37-
} else {
38-
parentRef.current.scrollTop = interpolated;
39-
}
40-
};
41-
42-
animationFn(run);
43-
},
44-
[animationFn]
45-
);
13+
const scrollToFn = React.useCallback((offset, defaultScrollTo) => {
14+
const duration = 1000;
15+
const start = parentRef.current.scrollTop;
16+
const startTime = (scrollingRef.current = Date.now());
17+
18+
const run = () => {
19+
if (scrollingRef.current !== startTime) return;
20+
const now = Date.now();
21+
const elapsed = now - startTime;
22+
const progress = easeInOutQuint(Math.min(elapsed / duration, 1));
23+
const interpolated = start + (offset - start) * progress;
24+
25+
if (elapsed < duration) {
26+
defaultScrollTo(interpolated);
27+
requestAnimationFrame(run);
28+
} else {
29+
defaultScrollTo(interpolated);
30+
}
31+
};
32+
33+
requestAnimationFrame(run);
34+
}, []);
4635

4736
const rowVirtualizer = useVirtual({
4837
size: 10000,
@@ -59,19 +48,10 @@ export default function() {
5948
implement a custom scrolling function for the methods like{" "}
6049
<code>`scrollToIndex`</code> and <code>`scrollToOffset`</code>
6150
</p>
51+
6252
<br />
6353
<br />
6454

65-
<label>
66-
Animation Type:
67-
<select
68-
value={animationType}
69-
onChange={e => setAnimationType(e.target.value)}
70-
>
71-
<option value="setTimeout">setTimeout</option>
72-
<option value="raf">requestAnimationFram</option>
73-
</select>
74-
</label>
7555
<div>
7656
<button
7757
onClick={() =>

0 commit comments

Comments
 (0)