|
1 | | -/** |
2 | | - * Grid virtualisation example |
3 | | - * |
4 | | - * A 2D grid virtualised in both axes simultaneously. |
5 | | - * Achieved by composing two <virtualizer> tags sharing the same scroll element: |
6 | | - * - outer: vertical (rows) |
7 | | - * - inner: horizontal (columns, horizontal=true) |
8 | | - * |
9 | | - * Only the cells visible in the current viewport are in the DOM. |
10 | | - */ |
11 | | - |
12 | | -static const ROW_COUNT = 1000 |
13 | | -static const COL_COUNT = 1000 |
14 | | - |
15 | | -static const rowSizes = Array.from({ length: ROW_COUNT }, () => |
16 | | - 30 + Math.round(Math.random() * 20) |
17 | | -) |
18 | | -static const colSizes = Array.from({ length: COL_COUNT }, () => |
19 | | - 80 + Math.round(Math.random() * 80) |
20 | | -) |
21 | | - |
22 | | -<let/mounted = false/> |
23 | | -<script() { mounted = true }/> |
24 | | - |
25 | | -<div class="page"> |
26 | | - <h1>Grid Virtualisation</h1> |
27 | | - <p> |
28 | | - A ${ROW_COUNT} × ${COL_COUNT} grid — ${(ROW_COUNT * COL_COUNT).toLocaleString()} cells total. |
29 | | - Only those in the current viewport are rendered. |
30 | | - </p> |
31 | | - <p class="note"> |
32 | | - Pattern: compose two <code><virtualizer></code> tags sharing |
33 | | - the same scroll element — one vertical, one horizontal. |
34 | | - </p> |
35 | | - |
36 | | - <if=mounted> |
37 | | - <div/$gridScroll class="scroll-container" style="height: 500px; width: 100%; overflow: auto"> |
38 | | - <virtualizer|{ virtualItems: rowItems, totalSize: rowTotal }| |
39 | | - count=ROW_COUNT |
40 | | - estimateSize=(i) => rowSizes[i]! |
41 | | - getScrollElement=gridScroll |
42 | | - > |
43 | | - <virtualizer|{ virtualItems: colItems, totalSize: colTotal }| |
44 | | - count=COL_COUNT |
45 | | - estimateSize=(i) => colSizes[i]! |
46 | | - horizontal=true |
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8"/> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"/> |
| 6 | + <title>Grid Virtualisation — @tanstack/marko-virtual</title> |
| 7 | + <style> |
| 8 | + * { box-sizing: border-box; margin: 0; padding: 0; } |
| 9 | + body { font-family: system-ui, sans-serif; padding: 24px; } |
| 10 | + h1 { font-size: 24px; margin-bottom: 8px; } |
| 11 | + p { color: #555; margin-bottom: 16px; } |
| 12 | + .scroll-grid { height: 500px; width: 100%; max-width: 700px; overflow: auto; border: 1px solid #e5e7eb; border-radius: 6px; } |
| 13 | + .cell { font-size: 11px; font-family: monospace; box-sizing: border-box; border-right: 1px solid #e5e7eb; border-bottom: 1px solid #e5e7eb; display: flex; align-items: center; justify-content: center; } |
| 14 | + .cell-even { background: #f9fafb; } |
| 15 | + .cell-odd { background: #ffffff; } |
| 16 | + </style> |
| 17 | + </head> |
| 18 | + <body> |
| 19 | + <h1>Grid Virtualisation</h1> |
| 20 | + <p>A 1000 × 1000 grid — two <virtualizer> tags sharing the same scroll element.</p> |
| 21 | + |
| 22 | + static const ROW_COUNT = 1000 |
| 23 | + static const COL_COUNT = 1000 |
| 24 | + static const rowSizes = Array.from({ length: ROW_COUNT }, () => 30 + Math.round(Math.random() * 20)) |
| 25 | + static const colSizes = Array.from({ length: COL_COUNT }, () => 80 + Math.round(Math.random() * 80)) |
| 26 | + |
| 27 | + <let/mounted = false/> |
| 28 | + <script() { mounted = true }/> |
| 29 | + |
| 30 | + <div/gridScroll class="scroll-grid"> |
| 31 | + <if=mounted> |
| 32 | + <virtualizer|{ virtualItems: rowItems, totalSize: rowTotal }| |
| 33 | + count=ROW_COUNT |
| 34 | + estimateSize=(i) => rowSizes[i]! |
47 | 35 | getScrollElement=gridScroll |
48 | 36 | > |
49 | | - <div style=`height: ${rowTotal}px; width: ${colTotal}px; position: relative`> |
50 | | - <for|row| of=rowItems> |
51 | | - <for|col| of=colItems> |
52 | | - <div |
53 | | - class=(col.index + row.index) % 2 === 0 ? "cell cell-even" : "cell cell-odd" |
54 | | - style=` |
55 | | - position: absolute; |
56 | | - top: 0; |
57 | | - left: 0; |
58 | | - display: flex; |
59 | | - align-items: center; |
60 | | - justify-content: center; |
61 | | - width: ${col.size}px; |
62 | | - height: ${row.size}px; |
63 | | - transform: translateX(${col.start}px) translateY(${row.start}px); |
64 | | - font-size: 11px; |
65 | | - box-sizing: border-box; |
66 | | - border-right: 1px solid #e5e7eb; |
67 | | - border-bottom: 1px solid #e5e7eb; |
68 | | - ` |
69 | | - > |
70 | | - ${row.index},${col.index} |
71 | | - </div> |
| 37 | + <virtualizer|{ virtualItems: colItems, totalSize: colTotal }| |
| 38 | + count=COL_COUNT |
| 39 | + estimateSize=(i) => colSizes[i]! |
| 40 | + horizontal=true |
| 41 | + getScrollElement=gridScroll |
| 42 | + > |
| 43 | + <div style=`height: ${rowTotal}px; width: ${colTotal}px; position: relative`> |
| 44 | + <for|row| of=rowItems> |
| 45 | + <for|col| of=colItems> |
| 46 | + <div |
| 47 | + class=(col.index + row.index) % 2 === 0 ? "cell cell-even" : "cell cell-odd" |
| 48 | + style=`position: absolute; top: 0; left: 0; width: ${col.size}px; height: ${row.size}px; transform: translateX(${col.start}px) translateY(${row.start}px)` |
| 49 | + > |
| 50 | + ${row.index},${col.index} |
| 51 | + </div> |
| 52 | + </for> |
72 | 53 | </for> |
73 | | - </for> |
74 | | - </div> |
| 54 | + </div> |
| 55 | + </virtualizer> |
75 | 56 | </virtualizer> |
76 | | - </virtualizer> |
| 57 | + </if> |
77 | 58 | </div> |
78 | | - </if> |
79 | | -</div> |
80 | | - |
81 | | -<style> |
82 | | - .page { |
83 | | - font-family: system-ui, sans-serif; |
84 | | - max-width: 800px; |
85 | | - margin: 0 auto; |
86 | | - padding: 24px; |
87 | | - } |
88 | | -
|
89 | | - h1 { font-size: 24px; margin-bottom: 8px; } |
90 | | - p { color: #555; margin-bottom: 12px; } |
91 | | - .note { font-size: 13px; color: #888; font-style: italic; } |
92 | | - code { background: #f3f4f6; padding: 1px 4px; border-radius: 3px; } |
93 | | -
|
94 | | - .scroll-container { |
95 | | - border: 1px solid #e5e7eb; |
96 | | - border-radius: 6px; |
97 | | - overflow: auto; |
98 | | - } |
99 | | -
|
100 | | - .cell { |
101 | | - font-family: monospace; |
102 | | - color: #374151; |
103 | | - } |
104 | | -
|
105 | | - .cell-even { background: #f9fafb; } |
106 | | - .cell-odd { background: #ffffff; } |
107 | | -</style> |
| 59 | + </body> |
| 60 | +</html> |
0 commit comments