|
1 | | -/** |
2 | | - * Fixed-size virtualisation example |
3 | | - * |
4 | | - * Demonstrates row, column, and grid virtualisation using the |
5 | | - * <virtualizer> tag from @tanstack/marko-virtual. |
6 | | - * |
7 | | - * "Fixed" means every item is the same size and never changes. |
8 | | - * This is the simplest and most performant case. |
9 | | - */ |
10 | | - |
11 | | -<let/mounted = false/> |
12 | | -<script() { mounted = true }/> |
13 | | - |
14 | | -<div class="page"> |
15 | | - <h1>Fixed Virtualisation</h1> |
16 | | - <p> |
17 | | - These examples use <strong>fixed</strong> sizes — every item's |
18 | | - dimensions are hard-coded to the same value and never change. |
19 | | - </p> |
20 | | - |
21 | | - <if=mounted> |
22 | | - |
23 | | - <!-- ── Rows ─────────────────────────────────────────────────────── --> |
| 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>Fixed 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 | + h2 { font-size: 18px; margin: 24px 0 12px; } |
| 12 | + p { color: #555; margin-bottom: 16px; } |
| 13 | + section { margin-bottom: 32px; } |
| 14 | + .scroll-row-container { height: 200px; overflow-y: auto; border: 1px solid #e5e7eb; border-radius: 6px; position: relative; } |
| 15 | + .scroll-col-container { width: 400px; height: 80px; overflow-x: auto; border: 1px solid #e5e7eb; border-radius: 6px; position: relative; } |
| 16 | + .scroll-grid-container { height: 400px; width: 400px; overflow: auto; border: 1px solid #e5e7eb; border-radius: 6px; } |
| 17 | + .item { display: flex; align-items: center; padding: 0 12px; font-size: 13px; border-bottom: 1px solid #f3f4f6; box-sizing: border-box; } |
| 18 | + .item-even { background: #f9fafb; } |
| 19 | + .item-odd { background: #ffffff; } |
| 20 | + .cell { font-size: 11px; font-family: monospace; box-sizing: border-box; border-right: 1px solid #e5e7eb; border-bottom: 1px solid #e5e7eb; } |
| 21 | + .cell-even { background: #f9fafb; } |
| 22 | + .cell-odd { background: #ffffff; } |
| 23 | + </style> |
| 24 | + </head> |
| 25 | + <body> |
| 26 | + <h1>Fixed Virtualisation</h1> |
| 27 | + <p>Fixed sizes — every item's dimensions are hard-coded to the same value and never change.</p> |
| 28 | + |
| 29 | + <let/mounted = false/> |
| 30 | + <script() { mounted = true }/> |
| 31 | + <p>debug: mounted=${String(mounted)}</p> |
24 | 32 |
|
25 | 33 | <section> |
26 | 34 | <h2>Rows</h2> |
27 | | - <div/$rowScroll class="scroll-container" style="height: 200px; overflow-y: auto"> |
28 | | - <virtualizer|{ virtualItems, totalSize }| |
29 | | - count=10000 |
30 | | - estimateSize=() => 35 |
31 | | - getScrollElement=rowScroll |
32 | | - > |
33 | | - <div style=`height: ${totalSize}px; width: 100%; position: relative`> |
34 | | - <for|item| of=virtualItems> |
35 | | - <div |
36 | | - class=item.index % 2 === 0 ? "item item-even" : "item item-odd" |
37 | | - style=` |
38 | | - position: absolute; |
39 | | - top: 0; |
40 | | - left: 0; |
41 | | - width: 100%; |
42 | | - height: ${item.size}px; |
43 | | - transform: translateY(${item.start}px); |
44 | | - ` |
45 | | - > |
46 | | - Row ${item.index} |
47 | | - </div> |
48 | | - </for> |
49 | | - </div> |
50 | | - </virtualizer> |
| 35 | + <div/rowScroll class="scroll-row-container"> |
| 36 | + <if=mounted> |
| 37 | + <virtualizer|{ virtualItems, totalSize }| |
| 38 | + count=10000 |
| 39 | + estimateSize=() => 35 |
| 40 | + getScrollElement=rowScroll |
| 41 | + > |
| 42 | + <div style=`height: ${totalSize}px; width: 100%; position: relative`> |
| 43 | + <for|item| of=virtualItems> |
| 44 | + <div |
| 45 | + class=item.index % 2 === 0 ? "item item-even" : "item item-odd" |
| 46 | + style=`position: absolute; top: 0; left: 0; width: 100%; height: ${item.size}px; transform: translateY(${item.start}px)` |
| 47 | + > |
| 48 | + Row ${item.index} |
| 49 | + </div> |
| 50 | + </for> |
| 51 | + </div> |
| 52 | + </virtualizer> |
| 53 | + </if> |
51 | 54 | </div> |
52 | 55 | </section> |
53 | 56 |
|
54 | | - <!-- ── Columns ──────────────────────────────────────────────────── --> |
55 | | - |
56 | 57 | <section> |
57 | 58 | <h2>Columns</h2> |
58 | | - <div/$colScroll class="scroll-container" style="width: 400px; height: 80px; overflow-x: auto"> |
59 | | - <virtualizer|{ virtualItems, totalSize }| |
60 | | - count=10000 |
61 | | - estimateSize=() => 100 |
62 | | - horizontal=true |
63 | | - getScrollElement=colScroll |
64 | | - > |
65 | | - <div style=`width: ${totalSize}px; height: 100%; position: relative`> |
66 | | - <for|item| of=virtualItems> |
67 | | - <div |
68 | | - class=item.index % 2 === 0 ? "item item-even" : "item item-odd" |
69 | | - style=` |
70 | | - position: absolute; |
71 | | - top: 0; |
72 | | - left: 0; |
73 | | - height: 100%; |
74 | | - width: ${item.size}px; |
75 | | - transform: translateX(${item.start}px); |
76 | | - ` |
77 | | - > |
78 | | - Col ${item.index} |
79 | | - </div> |
80 | | - </for> |
81 | | - </div> |
82 | | - </virtualizer> |
| 59 | + <div/colScroll class="scroll-col-container"> |
| 60 | + <if=mounted> |
| 61 | + <virtualizer|{ virtualItems, totalSize }| |
| 62 | + count=10000 |
| 63 | + estimateSize=() => 100 |
| 64 | + horizontal=true |
| 65 | + getScrollElement=colScroll |
| 66 | + > |
| 67 | + <div style=`width: ${totalSize}px; height: 100%; position: relative`> |
| 68 | + <for|item| of=virtualItems> |
| 69 | + <div |
| 70 | + class=item.index % 2 === 0 ? "item item-even" : "item item-odd" |
| 71 | + style=`position: absolute; top: 0; left: 0; height: 100%; width: ${item.size}px; transform: translateX(${item.start}px)` |
| 72 | + > |
| 73 | + Col ${item.index} |
| 74 | + </div> |
| 75 | + </for> |
| 76 | + </div> |
| 77 | + </virtualizer> |
| 78 | + </if> |
83 | 79 | </div> |
84 | 80 | </section> |
85 | 81 |
|
86 | | - <!-- ── Grid ─────────────────────────────────────────────────────── --> |
87 | | - |
88 | 82 | <section> |
89 | 83 | <h2>Grid</h2> |
90 | | - <p class="note"> |
91 | | - Grid = two <code><virtualizer></code> tags composed, |
92 | | - sharing the same scroll element. |
93 | | - </p> |
94 | | - <div/$gridScroll class="scroll-container" style="height: 400px; width: 400px; overflow: auto"> |
95 | | - <virtualizer|{ virtualItems: rowItems, totalSize: rowTotal }| |
96 | | - count=1000 |
97 | | - estimateSize=() => 35 |
98 | | - getScrollElement=gridScroll |
99 | | - > |
100 | | - <virtualizer|{ virtualItems: colItems, totalSize: colTotal }| |
| 84 | + <div/gridScroll class="scroll-grid-container"> |
| 85 | + <if=mounted> |
| 86 | + <virtualizer|{ virtualItems: rowItems, totalSize: rowTotal }| |
101 | 87 | count=1000 |
102 | | - estimateSize=() => 100 |
103 | | - horizontal=true |
| 88 | + estimateSize=() => 35 |
104 | 89 | getScrollElement=gridScroll |
105 | 90 | > |
106 | | - <div style=`height: ${rowTotal}px; width: ${colTotal}px; position: relative`> |
107 | | - <for|row| of=rowItems> |
108 | | - <for|col| of=colItems> |
109 | | - <div |
110 | | - class=(col.index + row.index) % 2 === 0 ? "item item-even" : "item item-odd" |
111 | | - style=` |
112 | | - position: absolute; |
113 | | - top: 0; |
114 | | - left: 0; |
115 | | - width: ${col.size}px; |
116 | | - height: ${row.size}px; |
117 | | - transform: translateX(${col.start}px) translateY(${row.start}px); |
118 | | - display: flex; |
119 | | - align-items: center; |
120 | | - justify-content: center; |
121 | | - ` |
122 | | - > |
123 | | - ${row.index}, ${col.index} |
124 | | - </div> |
| 91 | + <virtualizer|{ virtualItems: colItems, totalSize: colTotal }| |
| 92 | + count=1000 |
| 93 | + estimateSize=() => 100 |
| 94 | + horizontal=true |
| 95 | + getScrollElement=gridScroll |
| 96 | + > |
| 97 | + <div style=`height: ${rowTotal}px; width: ${colTotal}px; position: relative`> |
| 98 | + <for|row| of=rowItems> |
| 99 | + <for|col| of=colItems> |
| 100 | + <div |
| 101 | + class=(col.index + row.index) % 2 === 0 ? "cell cell-even" : "cell cell-odd" |
| 102 | + style=`position: absolute; top: 0; left: 0; width: ${col.size}px; height: ${row.size}px; transform: translateX(${col.start}px) translateY(${row.start}px); display: flex; align-items: center; justify-content: center` |
| 103 | + > |
| 104 | + ${row.index},${col.index} |
| 105 | + </div> |
| 106 | + </for> |
125 | 107 | </for> |
126 | | - </for> |
127 | | - </div> |
| 108 | + </div> |
| 109 | + </virtualizer> |
128 | 110 | </virtualizer> |
129 | | - </virtualizer> |
| 111 | + </if> |
130 | 112 | </div> |
131 | 113 | </section> |
132 | 114 |
|
133 | | - </if> |
134 | | -</div> |
135 | | - |
136 | | -<style> |
137 | | - .page { |
138 | | - font-family: system-ui, sans-serif; |
139 | | - max-width: 600px; |
140 | | - margin: 0 auto; |
141 | | - padding: 24px; |
142 | | - } |
143 | | -
|
144 | | - h1 { font-size: 24px; margin-bottom: 8px; } |
145 | | - h2 { font-size: 18px; margin: 24px 0 12px; } |
146 | | - p { color: #555; margin-bottom: 16px; } |
147 | | -
|
148 | | - .note { font-size: 13px; } |
149 | | - code { background: #f3f4f6; padding: 1px 4px; border-radius: 3px; } |
150 | | -
|
151 | | - section { margin-bottom: 32px; } |
152 | | -
|
153 | | - .scroll-container { |
154 | | - border: 1px solid #e5e7eb; |
155 | | - border-radius: 6px; |
156 | | - position: relative; |
157 | | - } |
158 | | -
|
159 | | - .item { |
160 | | - display: flex; |
161 | | - align-items: center; |
162 | | - padding: 0 12px; |
163 | | - font-size: 13px; |
164 | | - border-bottom: 1px solid #f3f4f6; |
165 | | - box-sizing: border-box; |
166 | | - } |
167 | | -
|
168 | | - .item-even { background: #f9fafb; } |
169 | | - .item-odd { background: #ffffff; } |
170 | | -</style> |
| 115 | + </body> |
| 116 | +</html> |
0 commit comments