xonecas.io
8356
Wx

www-xonecas-io / commit 6912ed9

header search suggests projects and topics, first one selected (1037)

Co-Authored-By: Claude Opus 5.5

Sean Caetano Martin2026-11-08 6912ed9752464123ca2933ccb7bfba52a50f866d parent e65d87b

8 files changed

internal/site/assets/site.css+8−0
@@ -133,3 +133,11 @@
133133 .pool > a:active { transform: none; box-shadow: none; }
134134 @keyframes pop { from { opacity: 0; transform: translateY(4px) scale(.85); } }
135135 @media (prefers-reduced-motion: reduce) { .pool > a { animation: none; transition: none; } .pool > a:hover, .pool > a:focus-visible { transform: none; } }
136+
137+/* Search suggestions (search.js): a card under the header search, the selected row in accent. */
138+#suggest { position: absolute; z-index: 20; top: calc(100% + 4px); right: 0; width: 100%; min-width: min(24rem, calc(100vw - 2rem)); background: var(--color-card); border: 1.5px solid var(--color-ink); border-radius: 2px; box-shadow: 3px 3px 0 var(--color-ink); font-size: .875rem; }
139+#suggest a { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: .75rem; align-items: baseline; padding: .375rem .75rem; color: inherit; text-decoration: none; }
140+#suggest a + a { border-top: 1px solid var(--color-line); }
141+#suggest span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--color-soft); font-size: .8rem; }
142+#suggest a[aria-selected=true] { background: var(--color-accent); color: var(--color-paper); }
143+#suggest a[aria-selected=true] span { color: inherit; opacity: .85; }
internal/site/build.go+3−0
@@ -83,6 +83,9 @@ func Build(projects []project.Project, out, tailwind string) error {
8383 if err := writeActivity(out, shown, feed); err != nil {
8484 return err
8585 }
86+ if err := writeSearch(out, shown); err != nil {
87+ return err
88+ }
8689 if err := os.WriteFile(filepath.Join(out, ".stamp"), []byte(stamp), 0o644); err != nil {
8790 return err
8891 }
internal/site/search.go+47−0
@@ -0,0 +1,47 @@
1+package site
2+
3+import (
4+ "cmp"
5+ "encoding/json"
6+ "os"
7+ "path/filepath"
8+ "slices"
9+
10+ "xonecas.io/site/internal/project"
11+)
12+
13+// SearchIndex is /search.json, what the header search suggests: projects by last commit, and topics, commonest first.
14+type SearchIndex struct {
15+ Projects []SearchProject `json:"projects"`
16+ Topics []string `json:"topics"`
17+}
18+
19+// SearchProject is one project the header search can suggest.
20+type SearchProject struct {
21+ Name string `json:"n"`
22+ Desc string `json:"d"`
23+ Topics []string `json:"t"`
24+}
25+
26+// writeSearch writes /search.json for the shown projects.
27+func writeSearch(out string, shown []project.Project) error {
28+ var idx SearchIndex
29+ counts := map[string]int{}
30+ shown = slices.Clone(shown)
31+ slices.SortStableFunc(shown, func(a, b project.Project) int { return cmp.Compare(b.Last, a.Last) })
32+ for _, p := range shown {
33+ idx.Projects = append(idx.Projects, SearchProject{p.Name, p.Description, p.Topics})
34+ for _, t := range p.Topics {
35+ counts[t]++
36+ }
37+ }
38+ for t := range counts {
39+ idx.Topics = append(idx.Topics, t)
40+ }
41+ slices.SortFunc(idx.Topics, func(a, b string) int { return cmp.Or(counts[b]-counts[a], cmp.Compare(a, b)) })
42+ b, err := json.Marshal(idx)
43+ if err != nil {
44+ return err
45+ }
46+ return os.WriteFile(filepath.Join(out, "search.json"), b, 0o644)
47+}
internal/site/static/search.js+49−0
@@ -0,0 +1,49 @@
1+// The header search suggests projects and topics as you type, from /search.json, with the first one selected:
2+// Enter opens the selected one, the arrows move, Escape closes. Not on /projects, where the list filters as you type.
3+const input = document.querySelector("header input[name=q]"), box = document.getElementById("suggest");
4+let index, items = [], at = 0;
5+const load = () => index ??= fetch("/search.json", { cache: "no-cache" }).then(r => r.json());
6+
7+function draw() {
8+ box.replaceChildren(...items.map((it, i) => {
9+ const a = Object.assign(document.createElement("a"), { href: it.href, id: "suggest-" + i, role: "option" });
10+ a.ariaSelected = i === at;
11+ a.append(Object.assign(document.createElement("b"), { textContent: it.label }), Object.assign(document.createElement("span"), { textContent: it.note }));
12+ return a;
13+ }));
14+ box.hidden = !items.length;
15+ input.ariaExpanded = !box.hidden;
16+ input.setAttribute("aria-activedescendant", items.length ? "suggest-" + at : "");
17+}
18+
19+async function suggest() {
20+ const q = input.value.trim().toLowerCase(), { projects, topics } = await load();
21+ const starts = p => !p.n.toLowerCase().startsWith(q);
22+ items = !q ? [] : [
23+ ...projects.filter(p => p.n.toLowerCase().includes(q)).sort((a, b) => starts(a) - starts(b)).slice(0, 6)
24+ .map(p => ({ label: p.n, note: p.d, href: `/projects/${p.n}/` })),
25+ ...topics.filter(t => t.includes(q)).slice(0, 3)
26+ .map(t => ({ label: t, note: `topic · ${projects.filter(p => p.t?.includes(t)).length} projects`, href: `/projects/?q=${encodeURIComponent(t)}` })),
27+ ];
28+ at = 0;
29+ draw();
30+}
31+
32+if (input && box && location.pathname !== "/projects/") {
33+ input.setAttribute("role", "combobox");
34+ input.setAttribute("aria-controls", "suggest");
35+ input.setAttribute("aria-autocomplete", "list");
36+ input.autocomplete = "off";
37+ input.addEventListener("focus", load, { once: true });
38+ input.addEventListener("input", suggest);
39+ input.addEventListener("keydown", e => {
40+ if (box.hidden) return;
41+ if (e.key === "ArrowDown" || e.key === "ArrowUp") at = (at + (e.key === "ArrowDown" ? 1 : items.length - 1)) % items.length, draw();
42+ else if (e.key === "Enter") location.href = items[at].href;
43+ else if (e.key === "Escape") items = [], draw();
44+ else return;
45+ e.preventDefault();
46+ });
47+ input.addEventListener("blur", () => { items = [], draw(); });
48+ box.addEventListener("mousedown", e => e.preventDefault()); // keep focus, so the click lands on the link
49+}
internal/site/templates/components.html+2−1
@@ -8,7 +8,8 @@
88 {{range $l := list "Overview" "Projects" "Activity"}}<a href="{{if eq $l "Overview"}}/{{else}}/{{lower $l}}/{{end}}" class="{{if eq $l $}}underline underline-offset-[6px] decoration-[3px]{{else}}no-underline{{end}}">{{$l}}</a>{{end}}
99 </nav>
1010 <button id="theme" type="button" title="Light or dark" aria-label="Switch between light and dark" class="ml-auto p-1 cursor-pointer hover:text-accent"><svg viewBox="0 0 16 16" width="20" height="20" aria-hidden="true"><circle cx="8" cy="8" r="6.5" fill="none" stroke="currentColor" stroke-width="1.5"/><path d="M8 1.5a6.5 6.5 0 0 1 0 13z" fill="currentColor"/></svg></button>
11- <form action="/projects/" class="w-full sm:w-auto"><input name="q" placeholder="Search projects" class="w-full sm:w-80 px-3 py-1.5 border-[1.5px] border-ink rounded-[2px] bg-card text-sm placeholder:text-soft"></form>
11+ <form action="/projects/" class="relative w-full sm:w-auto"><input name="q" placeholder="Search projects" class="w-full sm:w-80 px-3 py-1.5 border-[1.5px] border-ink rounded-[2px] bg-card text-sm placeholder:text-soft">
12+ <div id="suggest" role="listbox" aria-label="Suggestions" hidden></div></form>
1213 </div></header>
1314 {{end}}
1415
internal/site/templates/layout.html+1−0
@@ -17,6 +17,7 @@ addEventListener("click", e => {
1717 });
1818 </script>
1919 <script src="{{asset "/egg.js"}}" defer></script>
20+<script src="{{asset "/search.js"}}" defer></script>
2021 <body class="bg-paper text-ink font-sans text-[15.5px] leading-snug">
2122 {{end}}
2223 {{define "foot"}}</body>
tickets/1037-needs-a-autoselect-dropdown.md+38−0
@@ -0,0 +1,38 @@
1+---
2+id: 1037
3+title: needs a autoselect dropdown
4+type: feature
5+status: review
6+priority: p2
7+created: 2026-11-08T05:20:43Z
8+updated: 2026-11-08T05:31:10Z
9+source: devnote
10+moves: 2026-11-08T05:21:37Z inbox doing drag; 2026-11-08T05:31:10Z doing review agent
11+area: site
12+---
13+
14+## Context
15+
16+Filed from `http://192.168.122.10:8080/`, on `input.w-full.sm:w-80.px-3`.
17+
18+## Notes
19+
20+- 2026-11-08 05:31Z (agent): Done: the header search shows a dropdown of suggestions as you type, with the first one already selected (the autoselect).
21+
22+ - Enter opens the selected suggestion, the up and down arrows move the selection (wrapping around), and Escape or leaving the field closes it. With nothing suggested, Enter submits to /projects/?q= as before.
23+ - Suggestions: up to 6 projects whose name contains the text, names that start with it first, each with its description. Then up to 3 topics that contain it, shown as 'topic · N projects' and linking to /projects/?q=<topic>.
24+ - The dropdown is a card under the input with the same ink border and hard shadow as the rest of the site, and the selected row in accent.
25+ - It's off on /projects, where typing already filters the list below.
26+
27+ How: the build writes /search.json (82 projects with name, description and topics, plus the topics most common first). search.js fetches it the first time the field gets focus, so pages carry no extra data. The input gets combobox, aria-controls, aria-expanded and aria-activedescendant.
28+
29+ Files: internal/site/search.go (new), static/search.js (new), build.go, components.html, layout.html, assets/site.css (#suggest).
30+
31+ Checked in agent-browser:
32+ - On / typing 'mini' gives mini-coding-agent (selected) then mini-coder. ArrowDown selects mini-coder, and Enter opens /projects/mini-coder/.
33+ - 'sql' gives 'sqlite topic · 7 projects'. Escape hides the dropdown.
34+ - At 390px on /activity/ the dropdown for 'e' is 9 rows at x 16–374 with no horizontal scroll.
35+ - On /projects/ it stays hidden while the list filters to 2.
36+ - go vet and go test pass.
37+
38+ Not checked: a real mouse click on a suggestion, only keyboard. The mousedown handler keeps focus so the click reaches the link.
tickets/attachments/1037/typing mini on the front page.png+0−0
Binary file, not shown.