From cc8374e6abfc325eda08a9fc61221717a9c48175 Mon Sep 17 00:00:00 2001 From: zasbu <136494048+zasbu@users.noreply.github.com> Date: Sat, 7 Nov 2026 14:43:57 +1100 Subject: [PATCH 1/5] Keep menus closable on phones without anchor positioning Browsers without CSS anchor positioning (Firefox, Safari before 26) ignore a menu's placement and pin it to the window's top-left corner, where on a phone it covers the menu button: a second tap then lands on the menu itself and it can't be closed. There the menu is now placed under its trigger by hand as it opens. iOS Safari also doesn't count a tap on something that isn't clickable as a press outside, so any press outside an open menu and its trigger now closes it too. --- web/src/ui/Menu.tsx | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/web/src/ui/Menu.tsx b/web/src/ui/Menu.tsx index 2af025d..726a658 100644 --- a/web/src/ui/Menu.tsx +++ b/web/src/ui/Menu.tsx @@ -5,6 +5,7 @@ import { type KeyboardEvent, type ReactNode, type ToggleEvent, + useEffect, useId, useRef, useState, @@ -36,6 +37,7 @@ export function Menu({ }) { const id = useId(); const menu = useRef(null); + const button = useRef(null); // Mirrored onto the trigger, so its open look can follow the popover. const [open, setOpen] = useState(false); const items = () => @@ -47,9 +49,48 @@ export function Menu({ function onToggle(event: ToggleEvent) { setOpen(event.newState === "open"); - if (event.newState === "open") items()[0]?.focus(); + if (event.newState === "open") { + placeWithoutAnchors(); + items()[0]?.focus(); + } } + /** + * Browsers without CSS anchor positioning (Firefox, Safari before 26) ignore the placement styles and pin the + * menu to the window's top-left corner, where on a phone it covers the menu button: a second tap then lands + * on the menu and it can't be closed. There, it's placed under its trigger by hand as it opens. + */ + function placeWithoutAnchors() { + const element = menu.current; + const trigger = button.current; + if (!element || !trigger || CSS.supports("position-area: bottom")) return; + const at = trigger.getBoundingClientRect(); + Object.assign(element.style, { + position: "fixed", + inset: "auto", + top: `${at.bottom}px`, + ...(align === "end" + ? { right: `${document.documentElement.clientWidth - at.right}px` } + : { left: `${at.left}px` }), + ...(matchTriggerWidth && { minWidth: `${at.width}px` }), + }); + } + + // The browser closes the menu on a press outside it, but not on every phone: iOS Safari doesn't count a tap + // on something that isn't clickable. Any press outside the menu and its trigger closes it here too. + useEffect(() => { + if (!open) return; + function onPointerDown(event: PointerEvent) { + const target = event.target as Node; + if (menu.current?.contains(target) || button.current?.contains(target)) + return; + if (menu.current?.matches(":popover-open")) menu.current.hidePopover(); + } + document.addEventListener("pointerdown", onPointerDown, true); + return () => + document.removeEventListener("pointerdown", onPointerDown, true); + }, [open]); + function onKeyDown(event: KeyboardEvent) { const all = items(); const index = all.indexOf(document.activeElement as HTMLElement); @@ -68,6 +109,7 @@ export function Menu({ return ( <> diff --git a/web/src/features/site/HeaderSearch.tsx b/web/src/features/site/HeaderSearch.tsx new file mode 100644 index 0000000..2ad2e3f --- /dev/null +++ b/web/src/features/site/HeaderSearch.tsx @@ -0,0 +1,485 @@ +"use client"; +// The header's search: players and maps as you type, in a panel under the field laid out as ok-website's is, +// each result a picture, its kind, its name and a figure. Arrow keys move through the results and Enter opens +// one; `/` anywhere jumps to the field. On narrower screens it's a button that opens the field across the header. +// +// Fast without flicker, as ok-website's: a short pause after typing before asking, each keystroke cancels the +// request still out for the old text, a sequence number drops any late reply, the results shown stay up until +// the next ones arrive, and answers are kept, so text already searched (backspacing, retyping) shows at once. +import { Search, X } from "lucide-react"; +import { usePathname, useRouter } from "next/navigation"; +import { + type KeyboardEvent, + type ReactNode, + useEffect, + useId, + useRef, + useState, +} from "react"; +import { api, type Schemas } from "@/api/client"; +import { formatSince } from "@/features/runs/time"; +import { Avatar } from "@/ui/Avatar"; +import { classNames } from "@/ui/classNames"; +import { Flag } from "@/ui/Flag"; +import { Link } from "@/ui/Link"; + +const PAUSE_MS = 110; +const SHOWN = 5; +/** Answers kept for text already searched; the oldest go first. */ +const KEPT = 40; + +interface Found { + query: string; + /** When the answer arrived, for the rows' "ago": time is read where data arrives, never while rendering. */ + at: Date; + players: Schemas["PlayerSummary"][]; + maps: Schemas["MapSummary"][]; +} + +type State = + | { status: "idle" } + | { status: "searching" } + | { status: "found"; found: Found } + | { status: "failed" }; + +export function HeaderSearch() { + const router = useRouter(); + const pathname = usePathname(); + const listId = useId(); + const input = useRef(null); + const [text, setText] = useState(""); + const [state, setState] = useState({ status: "idle" }); + const [open, setOpen] = useState(false); + /** On narrow screens the field is hidden behind a button until opened. */ + const [expanded, setExpanded] = useState(false); + const [active, setActive] = useState(-1); + const timer = useRef(undefined); + const controller = useRef(null); + const sequence = useRef(0); + const kept = useRef(new Map()); + + // `/` focuses the search from anywhere that isn't already a text field. + useEffect(() => { + function onKey(event: globalThis.KeyboardEvent) { + const target = event.target as HTMLElement; + if ( + event.key !== "/" || + event.metaKey || + event.ctrlKey || + event.altKey || + /^(INPUT|TEXTAREA|SELECT)$/.test(target.tagName) || + target.isContentEditable + ) + return; + event.preventDefault(); + setExpanded(true); + requestAnimationFrame(() => input.current?.focus()); + } + document.addEventListener("keydown", onKey); + return () => document.removeEventListener("keydown", onKey); + }, []); + + // A page change closes the list; what was typed stays, so going back to it is one key away. Compared during + // render, React's way to reset state when a value changes, so the old list never paints on the new page. + const [shownOn, setShownOn] = useState(pathname); + if (shownOn !== pathname) { + setShownOn(pathname); + setOpen(false); + setExpanded(false); + } + + useEffect( + () => () => { + window.clearTimeout(timer.current); + controller.current?.abort(); + }, + [], + ); + + function show(found: Found) { + setState({ status: "found", found }); + setActive(-1); + setOpen(true); + } + + function search(value: string) { + const query = value.trim(); + window.clearTimeout(timer.current); + // Abort on the keystroke, not when the pause ends: a request in flight answers text that's gone. + controller.current?.abort(); + const seq = ++sequence.current; + if (!query) { + setState({ status: "idle" }); + setOpen(false); + return; + } + const known = kept.current.get(query.toLowerCase()); + if (known) { + show(known); + return; + } + timer.current = window.setTimeout(async () => { + const abort = new AbortController(); + controller.current = abort; + // Results already up stay until the new ones land; only a first search shows "Searching". + setState((current) => + current.status === "found" ? current : { status: "searching" }, + ); + setOpen(true); + try { + const [players, maps] = await Promise.all([ + api.GET("/v1/players", { + params: { query: { q: query, limit: SHOWN } }, + signal: abort.signal, + }), + api.GET("/v1/maps", { + params: { query: { q: query, sort: "popular", limit: SHOWN } }, + signal: abort.signal, + }), + ]); + if (seq !== sequence.current) return; + if (!players.data && !maps.data) throw new Error("search failed"); + const found = { + query, + at: new Date(), + players: players.data?.players ?? [], + maps: maps.data?.maps ?? [], + }; + kept.current.set(query.toLowerCase(), found); + if (kept.current.size > KEPT) { + kept.current.delete(kept.current.keys().next().value!); + } + show(found); + } catch (error) { + if ((error as Error).name === "AbortError") return; + if (seq === sequence.current) setState({ status: "failed" }); + } + }, PAUSE_MS); + } + + const found = state.status === "found" ? state.found : null; + /** Every result in the order the arrow keys walk them: players, then maps. */ + const targets = found + ? [ + ...found.players.map((p) => `/players/${p.steamid64}`), + ...found.maps.map((m) => `/maps/${encodeURIComponent(m.name)}`), + ] + : []; + + function close() { + setOpen(false); + setActive(-1); + } + + function go(href: string) { + close(); + input.current?.blur(); + router.push(href); + } + + function onKeyDown(event: KeyboardEvent) { + if (event.key === "Escape") { + if (open) close(); + else { + input.current?.blur(); + setExpanded(false); + } + return; + } + if (event.key === "ArrowDown" || event.key === "ArrowUp") { + if (targets.length === 0) return; + event.preventDefault(); + setOpen(true); + const step = event.key === "ArrowDown" ? 1 : -1; + // -1 is the field itself, so the keys walk field, results, field. + const slots = targets.length + 1; + setActive((at) => ((at + 1 + step + slots) % slots) - 1); + return; + } + if (event.key === "Enter") { + event.preventDefault(); + const query = text.trim(); + if (active >= 0 && targets[active]) go(targets[active]); + // No result picked: the player search page holds every match, and finds a SteamID too. + else if (query) go(`/players?q=${encodeURIComponent(query)}`); + } + } + + const optionId = (index: number) => `${listId}-${index}`; + const showList = open && state.status !== "idle"; + + return ( +
+ {/* The button that opens the field where the header has no room for it. */} + +
+
+ + = 0 ? optionId(active) : undefined} + autoComplete="off" + spellCheck={false} + onChange={(event) => { + setText(event.target.value); + search(event.target.value); + }} + onFocus={() => { + if (text.trim()) setOpen(true); + }} + onBlur={() => { + // Late enough that a click on a result lands first. + window.setTimeout(() => { + close(); + if (!text.trim()) setExpanded(false); + }, 150); + }} + onKeyDown={onKeyDown} + className="border-border bg-surface text-text placeholder:text-text-subtle hover:border-border-strong focus:border-accent focus:bg-bg h-9 w-full rounded-lg border pr-14 pl-9 text-sm transition-colors duration-(--duration-fast) outline-none" + /> + {text ? ( + + ) : ( + + / + + )} +
+ + + {showList && ( +
event.preventDefault()} + className="border-border-strong bg-surface-raised shadow-popover absolute top-full right-0 mt-2 max-h-120 overflow-y-auto overscroll-contain rounded-lg border p-1.5 text-sm max-xl:inset-x-4 xl:w-96" + > + {state.status === "searching" && ( +

+ Searching… +

+ )} + {state.status === "failed" && ( +

+ Search isn't available right now. Try again in a moment. +

+ )} + {found && + (found.players.length === 0 && found.maps.length === 0 ? ( +

+ No players or maps match “{found.query}”. +

+ ) : ( + <> + {found.players.length > 0 && ( + + {found.players.map((player, index) => ( + + )} + {found.maps.length > 0 && ( + + {found.maps.map((map, i) => { + const index = found.players.length + i; + return ( + + )} +
+ + All players matching “{found.query}” + + + All maps + +
+ + ))} +
+ )} +
+
+ ); +} + +function Group({ label, children }: { label: string; children: ReactNode }) { + return ( +
+
+ {label} +
+ {children} +
+ ); +} + +/** One result: its picture, its kind above its name, and a figure at the end, as ok-website lays them out. */ +function Option({ + id, + href, + active, + onPick, + picture, + kind, + name, + figure, +}: { + id: string; + href: string; + active: boolean; + onPick: () => void; + picture: ReactNode; + kind: string; + name: ReactNode; + figure: string; +}) { + return ( + + {picture} + + {kind} + + {name} + + + + {figure} + + + ); +} diff --git a/web/src/features/site/SignInButton.tsx b/web/src/features/site/SignInButton.tsx index 9b66024..908bef3 100644 --- a/web/src/features/site/SignInButton.tsx +++ b/web/src/features/site/SignInButton.tsx @@ -1,17 +1,15 @@ "use client"; // Sends the player to Steam to sign in, and back to this page afterwards. -import { ButtonAnchor } from "@/ui/Button"; +import { SteamButton } from "@/ui/SteamButton"; import { useReturnTo } from "./useReturnTo"; export function SignInButton() { const returnTo = useReturnTo(); return ( - // A full page load, not a client navigation: the API redirects to Steam. - - Sign in with Steam - + Sign in with Steam + ); } diff --git a/web/src/features/site/SiteFooter.tsx b/web/src/features/site/SiteFooter.tsx index 88694a5..4662dd8 100644 --- a/web/src/features/site/SiteFooter.tsx +++ b/web/src/features/site/SiteFooter.tsx @@ -1,38 +1,37 @@ -// The footer on every page: the ruleset, and SourceJump's community elsewhere. +// The footer on every page, one quiet line: the ruleset, what the site is, and SourceJump's community by their +// logos in their own colors. +import { ScrollText } from "lucide-react"; +import { BrandIcon } from "@/ui/BrandIcon"; import { Link } from "@/ui/Link"; - -/** SourceJump's own pages on other sites. The Discord invite is the one the plugin spec links to. */ -const communityLinks = [ - { label: "Discord", href: "https://discord.gg/RYP5G4tuwX" }, - { label: "YouTube", href: "https://www.youtube.com/user/SourceJump" }, - { - label: "Steam group", - href: "https://steamcommunity.com/groups/SourceJump", - }, - { label: "TikTok", href: "https://www.tiktok.com/@sourcejump.net" }, -]; +import { communityLinks } from "./navigation"; export function SiteFooter() { return ( -