Skip to content

Commit 732b6a0

Browse files
committed
demo upd with stats
1 parent c465caf commit 732b6a0

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

build_examples/demo.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const itemsGrid = Array.from({ length: 300 }).map((_, iy) => Array.from({ length
3939
function ListWithHookExample() {
4040
const items = itemsLine;
4141
const containerRef = (0, react_1.useRef)(undefined);
42+
const infoRef = (0, react_1.useRef)(undefined);
4243
const itemHeight = 40;
4344
const { renderedItems, updateViewRect } = (0, __1.useVirtualOverflowY)({
4445
containerRef,
@@ -49,9 +50,14 @@ function ListWithHookExample() {
4950
renderItem: (itemIndex, offsetTop, item = items[itemIndex]) => ((0, jsx_runtime_1.jsx)("div", { style: { position: 'absolute', top: `${offsetTop}px` }, children: (0, jsx_runtime_1.jsx)("div", { style: { height: '40px' }, children: item }) }, item)),
5051
});
5152
(0, react_1.useEffect)(() => {
52-
setInterval(() => updateViewRect(), 8);
53+
// in case of animated containers
54+
// setInterval(() => updateViewRect(), 8);
55+
setInterval(() => {
56+
const visibleRect = (0, __1.virtualOverflowCalcVisibleRect)(containerRef.current);
57+
infoRef.current.innerText = `Visible rect of content:\n\n${JSON.stringify(visibleRect, null, 2)}`;
58+
}, 24);
5359
}, []);
54-
return ((0, jsx_runtime_1.jsx)("div", { style: { overflowY: 'scroll', height: '300px', background: 'lightgreen' }, children: (0, jsx_runtime_1.jsx)("div", { ref: containerRef, style: { position: 'relative', height: `${itemHeight * items.length}px` }, children: renderedItems }) }));
60+
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { ref: infoRef, style: { position: 'fixed', top: 0, right: 0, paddingRight: '40px', width: '200px' } }), (0, jsx_runtime_1.jsx)("div", { style: { overflowY: 'scroll', height: '300px', background: 'lightgreen' }, children: (0, jsx_runtime_1.jsx)("div", { ref: containerRef, style: { position: 'relative', height: `${itemHeight * items.length}px` }, children: renderedItems }) })] }));
5561
}
5662
function VerticalListExample() {
5763
const items = itemsLine;
@@ -68,7 +74,7 @@ function App() {
6874
}, children: "Scroll me down" }), (0, jsx_runtime_1.jsxs)("div", { style: { overflowY: 'scroll', height: '600px', background: 'blue' }, children: [(0, jsx_runtime_1.jsx)("div", { style: {
6975
height: '700px', fontSize: '60px',
7076
color: 'white'
71-
}, children: "Scroll me down" }), (0, jsx_runtime_1.jsx)(VerticalListExample, {})] }), (0, jsx_runtime_1.jsx)("div", { style: { height: '700px' } })] }));
77+
}, children: "Scroll me down" }), (0, jsx_runtime_1.jsx)(ListWithHookExample, {})] }), (0, jsx_runtime_1.jsx)("div", { style: { height: '700px' } })] }));
7278
}
7379
const rootElement = document.getElementById("demo");
7480
const root = client_1.default.createRoot(rootElement);

src/examples/demo.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useEffect, useRef } from "react";
22
import ReactDOM from "react-dom/client";
3-
import { useVirtualOverflowY } from "..";
3+
import { useVirtualOverflowY, virtualOverflowCalcVisibleRect } from "..";
44
import { virtualOverflowUtils } from "../utils";
55
import { VirtualListY } from "../fixed-list-y";
66
import { VirtualGrid } from "../fixed-grid";
@@ -11,6 +11,7 @@ const itemsGrid = Array.from({ length: 300 }).map((_, iy) => Array.from({ length
1111
function ListWithHookExample() {
1212
const items = itemsLine;
1313
const containerRef = useRef<HTMLDivElement>(undefined!);
14+
const infoRef = useRef<HTMLDivElement>(undefined!);
1415

1516
const itemHeight = 40;
1617

@@ -28,15 +29,24 @@ function ListWithHookExample() {
2829
});
2930

3031
useEffect(() => {
31-
setInterval(() => updateViewRect(), 8);
32+
// in case of animated containers
33+
// setInterval(() => updateViewRect(), 8);
34+
35+
setInterval(() => {
36+
const visibleRect = virtualOverflowCalcVisibleRect(containerRef.current);
37+
infoRef.current.innerText = `Visible rect of content:\n\n${JSON.stringify(visibleRect, null, 2)}`;
38+
}, 24);
3239
}, []);
3340

3441
return (
35-
<div style={{ overflowY: 'scroll', height: '300px', background: 'lightgreen' }}>
36-
<div ref={containerRef} style={{ position: 'relative', height: `${itemHeight * items.length}px` }}>
37-
{renderedItems}
42+
<>
43+
<div ref={infoRef} style={{ position: 'fixed', top: 0, right: 0, paddingRight: '40px', width: '200px' }}></div>
44+
<div style={{ overflowY: 'scroll', height: '300px', background: 'lightgreen' }}>
45+
<div ref={containerRef} style={{ position: 'relative', height: `${itemHeight * items.length}px` }}>
46+
{renderedItems}
47+
</div>
3848
</div>
39-
</div>
49+
</>
4050
);
4151
}
4252

@@ -87,7 +97,7 @@ function App() {
8797
}}>
8898
Scroll me down
8999
</div>
90-
<VerticalListExample />
100+
<ListWithHookExample />
91101
</div>
92102
<div style={{ height: '700px' }}></div>
93103
</div>

0 commit comments

Comments
 (0)