Debian Codegraph @

www/src/content/patterns/01-loading/01-click-to-load.md

raw symbols SCIP
1 ---
2 includeMockServer: true
3 title: "Click to Load"
4 description: Load more items when you click a button
5 icon: "icon-[mdi--cursor-pointer]"
6 ---
7
8 <script>
9
10 server.get("/demo", () => `
11 <div class="overflow-y-auto">
12 <div class="flex flex-col gap-4" id="comments">
13 ${items(0)}
14 </div>
15 </div>`);
16
17 server.get(/\/comments.*/, (req) => items(parseInt(req.params.page)));
18
19 function items(page) {
20 const comments = [
21 { author: "1cg", time: "2h ago", text: "daily reminder that the browser is the framework" },
22 { author: "S4RF", time: "3h ago", text: "In 1997 I would have shipped this with a Perl script and a cronjob" },
23 { author: "uncle k2", time: "5h ago", text: "I am begging a front end dev to look at an element in the browser DOM inspector one (1) single time." },
24 { author: "gnut", time: "8h ago", text: "oh heyyy!! pretty cool experiment. A lot of IDE's hate it though, thinks they are invalid CSS properties." },
25 { author: "wyrmisis", time: "12h ago", text: "Sir, another React Dev Tool has hit the browser." },
26 { author: "M379KL", time: "1d ago", text: "Build step for CSS was nuts to first realize they were doing. JavaScript required for CSS also nuts." },
27 { author: "fizzy", time: "1d ago", text: "I made this meme for a different conversation but in case the need ever arises for an anti-joke, here ya go" },
28 { author: "Reiss", time: "2d ago", text: "this was everybody in europe 23 years ago" },
29 { author: "pavlos", time: "2d ago", text: "Real" },
30 ];
31 const PAGE_SIZE = 3;
32 const start = page * PAGE_SIZE;
33 const slice = comments.slice(start, start + PAGE_SIZE);
34 let html = slice.map((c, i) => `
35 <div class="starting:opacity-0 starting:translate-y-1 transition duration-300 ease-out flex gap-3" style="transition-delay:${i * 80}ms">
36 <div class="size-8 rounded-full bg-neutral-100 dark:bg-neutral-800 flex items-center justify-center text-xs font-semibold text-neutral-500 dark:text-neutral-400 shrink-0">
37 ${c.author.split(' ').map(w => w[0]).join('')}
38 </div>
39 <div class="flex-1 min-w-0">
40 <div class="flex items-baseline gap-2 mb-0.5">
41 <span class="text-sm font-semibold text-neutral-800 dark:text-neutral-200">${c.author}</span>
42 <span class="text-xs text-neutral-600 dark:text-neutral-400">${c.time}</span>
43 </div>
44 <p class="text-sm text-neutral-600 dark:text-neutral-400 leading-relaxed">${c.text}</p>
45 </div>
46 </div>`).join("\n");
47
48 if (start + PAGE_SIZE < comments.length) {
49 html += `
50 <button class="w-full flex items-center justify-center gap-2.5 py-2 text-[0.8125rem] font-medium bg-transparent rounded-md cursor-pointer transition hover:bg-neutral-75 dark:hover:bg-neutral-850 hover:text-neutral-700 dark:hover:text-neutral-200 active:scale-[0.98] starting:opacity-0 starting:translate-y-1 transition duration-300 ease-out"
51 hx-get="/comments/?page=${page + 1}"
52 hx-swap="outerHTML"
53 hx-target="this">
54 Show more comments <span class="translate-y-px">&#8595;</span>
55 </button>`;
56 }
57 return html;
58 }
59
60 server.start("/demo");
61 </script>
62
63 <div id="demo-content" class="not-prose demo-container min-h-[328px]"></div>
64
65 ## Basic usage
66
67 On the client, put a button at the end of your list.
68
69 ```html
70 <button hx-get="/comments?page=2"
71 hx-swap="outerHTML"
72 hx-target="this">
73 Show more comments
74 </button>
75 ```
76
77 When clicked, it extends the list (without any client-side state).
78
79 - [`hx-get`](/reference/attributes/hx-get) requests `/comments?page=2`.
80 - [`hx-swap`](/reference/attributes/hx-swap)=[`"outerHTML"`](/reference/attributes/hx-swap#outerhtml) sets the strategy to replace the target.
81 - [`hx-target`](/reference/attributes/hx-target)=[`"this"`](/reference/attributes/hx-target#this) sets the target to itself.
82
83 On the server, respond with the next items plus a button pointing to `?page=3`:
84
85 ```html
86 <div class="comment">...</div>
87 <div class="comment">...</div>
88 <div class="comment">...</div>
89
90 <!-- When there are no more pages, just omit this button -->
91 <button hx-get="/comments/?page=3"
92 hx-swap="outerHTML"
93 hx-target="this">
94 Show more comments
95 </button>
96 ```
97
98 Because [`outerHTML`](/reference/attributes/hx-swap#outerhtml) replaces the old button, the new one takes its place.