Debian Codegraph @

www/src/content/patterns/01-loading/02-infinite-scroll.md

raw symbols SCIP
1 ---
2 includeMockServer: true
3 title: "Infinite Scroll"
4 description: Load content when you scroll to bottom
5 icon: "icon-[mdi--arrow-expand-down]"
6 ---
7
8 <script>
9 server.get("/demo", () => `
10 <div class="relative">
11 <div class="max-h-[300px] overflow-y-auto mask-b-from-60%">
12 <table class="w-full border-collapse">
13 <thead><tr><th class="text-left px-3 py-2 text-neutral-450 dark:text-neutral-400 font-semibold text-xs uppercase tracking-wide sticky top-0 bg-white dark:bg-neutral-930 z-10 shadow-[inset_0_-1px_0_var(--color-neutral-100)] dark:shadow-[inset_0_-1px_0_var(--color-neutral-850)]">Name</th><th class="text-left px-3 py-2 text-neutral-450 dark:text-neutral-400 font-semibold text-xs uppercase tracking-wide sticky top-0 bg-white dark:bg-neutral-930 z-10 shadow-[inset_0_-1px_0_var(--color-neutral-850)]">Email</th></tr></thead>
14 <tbody class="[&>tr:last-child>td]:border-b-0">${rows(0)}</tbody>
15 </table>
16 </div>
17 <div class="absolute -bottom-2 inset-x-0 flex justify-center pointer-events-none">
18 <span class="text-[0.6875rem] text-neutral-600 dark:text-neutral-400 flex items-center gap-1">Scroll to load more <span class="translate-y-px">&darr;</span></span>
19 </div>
20 </div>`);
21
22 server.get(/\/contacts.*/, (req) => rows(parseInt(req.params.page)));
23
24 function rows(page) {
25 const PAGE_SIZE = 3;
26 const start = page * PAGE_SIZE;
27 let html = Array.from({ length: PAGE_SIZE }, (_, i) => {
28 const n = start + i;
29 return `<tr class="starting:opacity-0 transition-opacity duration-300 ease-out"><td class="px-3 py-2.5 border-b border-neutral-100 dark:border-neutral-850 text-sm text-neutral-600 dark:text-neutral-300">Agent Smith</td><td class="px-3 py-2.5 border-b border-neutral-100 dark:border-neutral-850 text-sm text-neutral-600 dark:text-neutral-300">agent${n}@smith.org</td></tr>`;
30 }).join("\n");
31
32 html += `
33 <tr class="starting:opacity-0 transition-opacity duration-300 ease-out" hx-get="/contacts?page=${page + 1}"
34 hx-trigger="intersect once"
35 hx-swap="outerHTML">
36 <td colspan="2" class="px-3 py-2.5 text-center text-[0.8125rem] italic text-neutral-400 dark:text-neutral-600 border-b border-neutral-100 dark:border-neutral-850">Loading more...</td>
37 </tr>`;
38 return html;
39 }
40
41 server.start("/demo");
42 </script>
43
44 <div id="demo-content" class="not-prose demo-container min-h-[380px]"></div>
45
46 ## Basic usage
47
48 On the client, put a placeholder row at the end of your table.
49
50 ```html
51 <tr hx-get="/contacts?page=2"
52 hx-swap="outerHTML"
53 hx-trigger="revealed">
54 <td>Loading more...</td>
55 </tr>
56 ```
57
58 - [`hx-get`](/reference/attributes/hx-get) requests `/contacts?page=2`.
59 - [`hx-swap`](/reference/attributes/hx-swap)=[`"outerHTML"`](/reference/attributes/hx-swap#outerhtml) sets the strategy to replace the target.
60 - [`hx-trigger`](/reference/attributes/hx-trigger)=[`"revealed"`](/reference/attributes/hx-trigger#revealed) fires when the element scrolls into view. Use [`intersect`](/reference/attributes/hx-trigger#intersect) [`once`](/reference/attributes/hx-trigger#once) instead if your container has `overflow-y: scroll`.
61
62 On the server, respond with the next rows plus a fresh placeholder pointing to `?page=3`:
63
64 ```html
65 <tr><td>Agent Smith</td><td>[email protected]</td></tr>
66 <tr><td>Agent Smith</td><td>[email protected]</td></tr>
67 <tr><td>Agent Smith</td><td>[email protected]</td></tr>
68
69 <!-- When there are no more pages, just omit this row -->
70 <tr hx-get="/contacts?page=3"
71 hx-swap="outerHTML"
72 hx-trigger="revealed">
73 <td>Loading more...</td>
74 </tr>
75 ```
76
77 Because [`outerHTML`](/reference/attributes/hx-swap#outerhtml) replaces the old placeholder, the new one takes its place, creating a self-extending chain.