Debian Codegraph @

www/src/content/patterns/02-forms/01-active-search.md

raw symbols SCIP
1 ---
2 includeMockServer: true
3 title: Active Search
4 description: Filter search results as you type
5 icon: "icon-[mdi--magnify]"
6 ---
7
8 <script>
9 server.get("/demo", () => searchUI());
10
11 server.post(/\/search.*/, (req) => {
12 const search = req.params["search"] || "";
13 return resultsUI(findContacts(search));
14 });
15
16 function searchUI() {
17 return `
18 <div>
19 <span class="text-sm font-semibold text-neutral-700 dark:text-neutral-200 mb-3 block">Search Contacts</span>
20 <div class="relative">
21 <input type="search"
22 name="search" placeholder="Begin typing to search..."
23 class="w-full px-3 py-2 [&::-webkit-search-cancel-button]:hidden border border-neutral-200 dark:border-neutral-700 rounded-md text-sm bg-white dark:bg-neutral-900 text-neutral-800 dark:text-neutral-200 outline-none transition-colors focus:border-neutral-400 dark:focus:border-neutral-500 focus:ring-2 focus:ring-neutral-400/15 dark:focus:ring-neutral-500/20 placeholder:text-neutral-400 dark:placeholder:text-neutral-500"
24 hx-post="/search"
25 hx-trigger="input changed delay:200ms, keyup[key=='Enter'], load"
26 hx-target="#search-results"
27 hx-swap="innerHTML"
28 hx-indicator="#search-spinner">
29 <span id="search-spinner" class="absolute right-3.5 top-1/2 -translate-y-1/2 size-3.5 border-2 border-neutral-300 dark:border-neutral-600 border-t-neutral-700 dark:border-t-neutral-300 rounded-full animate-spin [animation-duration:0.5s] opacity-0 [&.htmx-request]:opacity-100 text-neutral-700 dark:text-neutral-300 transition-opacity duration-200"></span>
30 </div>
31 <table class="w-full border-collapse mt-3">
32 <thead>
33 <tr>
34 <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)]">First Name</th>
35 <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)]">Last Name</th>
36 <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)]">Email</th>
37 </tr>
38 </thead>
39 <tbody id="search-results" class="[&>tr:last-child>td]:border-b-0">
40 </tbody>
41 </table>
42 </div>`;
43 }
44
45 function resultsUI(contacts) {
46 return contacts.map((c) =>
47 `<tr class="starting:opacity-0 transition-opacity duration-150 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">${c.first}</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">${c.last}</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">${c.email}</td></tr>`
48 ).join("\n");
49 }
50
51 function findContacts(str) {
52 const contacts = [
53 { first: "Venus", last: "Grimes", email: "[email protected]" },
54 { first: "Fletcher", last: "Owen", email: "[email protected]" },
55 { first: "William", last: "Hale", email: "[email protected]" },
56 { first: "TaShya", last: "Cash", email: "[email protected]" },
57 { first: "Jakeem", last: "Walker", email: "[email protected]" },
58 { first: "Malcolm", last: "Trujillo", email: "[email protected]" },
59 { first: "Wynne", last: "Rice", email: "[email protected]" },
60 ];
61 const s = str.toLowerCase();
62 return contacts.filter((c) =>
63 c.first.toLowerCase().includes(s) ||
64 c.last.toLowerCase().includes(s) ||
65 c.email.toLowerCase().includes(s)
66 );
67 }
68
69 server.start("/demo");
70 </script>
71
72 <div id="demo-content" class="not-prose demo-container min-h-[482px]"></div>
73
74 ## Basic usage
75
76 On the client, create a search input that targets a results container.
77
78 ```html
79 <input type="search" name="search"
80 hx-post="/search"
81 hx-trigger="input changed delay:200ms, keyup[key=='Enter'], load"
82 hx-target="#results"
83 hx-indicator=".htmx-indicator">
84
85 <tbody id="results"></tbody>
86 ```
87
88 - [`hx-post`](/reference/attributes/hx-post) sends the input value to `/search`.
89 - [`hx-trigger`](/reference/attributes/hx-trigger) combines three triggers:
90 - [`input`](https://developer.mozilla.org/en-US/docs/Web/API/Element/input_event) [`changed`](/reference/attributes/hx-trigger#changed) [`delay:200ms`](/reference/attributes/hx-trigger#delay) debounces keystrokes and ignores keys that don't change the value (e.g. arrows).
91 - `keyup[key=='Enter']` sends immediately on Enter, using an [event filter](/reference/attributes/hx-trigger#event-filters).
92 - [`load`](/reference/attributes/hx-trigger#load) populates the table on page load.
93 - [`hx-target`](/reference/attributes/hx-target) puts the response into the `#results` tbody.
94 - [`hx-indicator`](/reference/attributes/hx-indicator) shows a loading indicator while the request is in flight.
95
96 On the server, respond with matching table rows:
97
98 ```html
99 <tr>
100 <td>Venus</td>
101 <td>Grimes</td>
102 <td>[email protected]</td>
103 </tr>
104 <tr>
105 <td>Fletcher</td>
106 <td>Owen</td>
107 <td>[email protected]</td>
108 </tr>
109 ```
110
111 ## Notes
112
113 ### Indicators
114
115 The [`htmx-indicator`](/reference/attributes/hx-indicator) class hides an element by default and shows it while a request is in flight:
116
117 ```html
118 <span class="htmx-indicator">Searching...</span>
119 ```