Skip to content

Commit fa3e607

Browse files
author
dasathyakuma
committed
Fix more issues
1 parent 34d5fb0 commit fa3e607

File tree

2 files changed

+33
-32
lines changed

2 files changed

+33
-32
lines changed

examples/marko/dynamic/src/routes/+page.marko

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@
3636
>
3737
<div style=`height: ${totalSize}px; width: 100%; position: relative`>
3838
<for|item| of=virtualItems>
39-
<div/$el
39+
<div/itemEl
40+
data-index=item.index
4041
class=item.index % 2 === 0 ? "item item-even" : "item item-odd"
4142
style=`position: absolute; top: 0; left: 0; width: 100%; transform: translateY(${item.start}px)`
4243
>
4344
<script() {
44-
const node = el()
45+
const node = itemEl()
4546
if (node && measureElement) measureElement(node)
4647
}/>
4748
<strong>${items[item.index]!.id}.</strong> ${items[item.index]!.text}

examples/marko/infinite-scroll/src/routes/+page.marko

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,56 +13,56 @@
1313
.item { display: flex; align-items: center; padding: 0 16px; font-size: 14px; border-bottom: 1px solid #f3f4f6; box-sizing: border-box; }
1414
.item-even { background: #f9fafb; }
1515
.item-odd { background: #ffffff; }
16-
.status { color: #9ca3af; font-style: italic; font-size: 13px; }
16+
.placeholder { color: #9ca3af; font-style: italic; font-size: 13px; }
1717
.footer { font-size: 12px; color: #9ca3af; margin-top: 8px; text-align: right; }
1818
</style>
1919
</head>
2020
<body>
2121
<h1>Infinite Scroll Virtualisation</h1>
22-
<p>Rows are fetched in pages as you scroll toward the end of the list.</p>
22+
<p>500 rows fetched in pages of 20 as you scroll. The virtualizer reserves all 500 slots upfront — unloaded rows show a placeholder.</p>
2323

24-
<const/TOTAL = 500/>
24+
<const/TOTAL = 500/>
2525
<const/PAGE_SIZE = 20/>
2626

27-
<let/rows = ([] as string[])/>
28-
<let/hasMore = true/>
27+
<let/rows = ([] as string[])/>
2928
<let/loading = false/>
30-
<let/nextOffset = 0/>
29+
<let/hasMore = true/>
30+
<let/offset = 0/>
3131
<let/mounted = false/>
3232

33-
<const/loadPage = async () => {
34-
if (loading || !hasMore) return
35-
loading = true
36-
await new Promise<void>(r => setTimeout(r, 500))
37-
const offset = nextOffset
38-
const count = Math.min(PAGE_SIZE, TOTAL - offset)
39-
const newRows = Array.from({ length: count }, (_, k) =>
40-
"Row #" + (offset + k + 1) + " — loaded at offset " + offset
41-
)
42-
rows = [...rows, ...newRows]
43-
nextOffset = offset + count
44-
hasMore = nextOffset < TOTAL
45-
loading = false
46-
}/>
47-
4833
<script() {
4934
mounted = true
50-
loadPage()
35+
loading = true
36+
new Promise<void>(r => setTimeout(r, 400)).then(() => {
37+
const n = Math.min(PAGE_SIZE, TOTAL)
38+
rows = Array.from({ length: n }, (_, k) => "Row #" + (k + 1))
39+
offset = n
40+
hasMore = n < TOTAL
41+
loading = false
42+
})
5143
}/>
5244

53-
<const/count = hasMore ? rows.length + 1 : rows.length/>
54-
5545
<div/scrollEl class="scroll-container">
5646
<if=mounted>
5747
<virtualizer|{ virtualItems, totalSize }|
58-
count=count
48+
count=TOTAL
5949
estimateSize=() => 52
6050
getScrollElement=scrollEl
6151
overscan=5
6252
>
6353
<script() {
6454
const last = virtualItems[virtualItems.length - 1]
65-
if (last && last.index >= rows.length - 1) loadPage()
55+
if (last && last.index >= rows.length - 1 && !loading && hasMore) {
56+
const off = offset
57+
loading = true
58+
new Promise<void>(r => setTimeout(r, 400)).then(() => {
59+
const n = Math.min(PAGE_SIZE, TOTAL - off)
60+
rows = [...rows, ...Array.from({ length: n }, (_, k) => "Row #" + (off + k + 1))]
61+
offset = off + n
62+
hasMore = offset < TOTAL
63+
loading = false
64+
})
65+
}
6666
}/>
6767

6868
<div style=`height: ${totalSize}px; width: 100%; position: relative`>
@@ -75,7 +75,9 @@
7575
${rows[item.index]}
7676
</if>
7777
<else>
78-
<span class="status">${loading ? "Loading more…" : hasMore ? "Scroll to load more" : "All rows loaded"}</span>
78+
<span class="placeholder">
79+
${loading && item.index === rows.length ? "Loading…" : ""}
80+
</span>
7981
</else>
8082
</div>
8183
</for>
@@ -84,8 +86,6 @@
8486
</if>
8587
</div>
8688

87-
<p class="footer">
88-
${rows.length} of ${TOTAL} rows loaded
89-
</p>
89+
<p class="footer">${rows.length} of ${TOTAL} rows loaded</p>
9090
</body>
9191
</html>

0 commit comments

Comments
 (0)