diff --git a/web/src/styles/app.css b/web/src/styles/app.css index efc098c..9017d4f 100644 --- a/web/src/styles/app.css +++ b/web/src/styles/app.css @@ -1271,10 +1271,15 @@ a.menu-item:hover { color: var(--fg); } .searchbar { flex: 1 1 auto; min-width: 0; max-width: 720px; margin: 0 auto; position: relative; } .searchbar .search-input { display: flex; align-items: center; gap: 8px; height: 44px; padding: 0 8px 0 14px; border-radius: 999px; background: var(--bg-sunken); border: 1px solid transparent; transition: background .12s, box-shadow .12s, border-color .12s; } .searchbar .search-input:focus-within { background: var(--bg-elev); box-shadow: var(--shadow-1); border-color: var(--border); } -.searchbar input { flex: 1; border: 0; background: transparent; outline: none; min-width: 0; height: 100%; } -.searchbar input::placeholder { color: var(--fg-faint); } +/* Only the bar's own field: the advanced panel inside the same form has + ordinary inputs and checkboxes, which this used to strip of their borders + and squeeze to nothing (#94). */ +.searchbar .search-input input { flex: 1; border: 0; background: transparent; outline: none; min-width: 0; height: 100%; } +.searchbar .search-input input::placeholder { color: var(--fg-faint); } .search-panel { position: absolute; top: calc(100% + 6px); left: 0; right: 0; background: var(--bg-elev); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-2); padding: 16px; z-index: 60; animation: pop .12s var(--ease); } .search-panel .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 14px; } +.search-panel-foot { justify-content: space-between; flex-wrap: wrap; row-gap: 4px; margin-top: 4px; } +.search-panel-actions { margin-left: auto; } .search-suggest { position: absolute; top: calc(100% + 6px); left: 0; right: 0; background: var(--bg-elev); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-2); padding: 6px; z-index: 60; } .topbar-actions { display: flex; align-items: center; gap: 4px; } /* Live-updates indicator. Three states, and a raised bead rather than a flat @@ -2247,6 +2252,15 @@ select optgroup { background-color: var(--bg-elev); color: var(--fg); } .topbar { padding: 0 max(6px, env(safe-area-inset-right)) 0 max(6px, env(safe-area-inset-left)); gap: 4px; } .topbar .brand .brand-name { display: none; } .searchbar .search-input { height: 40px; } + /* + On a phone the search field is only what's left between the menu, logo + and account buttons, too narrow to hang the advanced panel from. The panel + takes the width of the top bar instead (the nearest positioned box once + the bar isn't), in one column, and scrolls if the keyboard leaves it short. + */ + .searchbar { position: static; } + .search-panel { left: 8px; right: 8px; max-height: calc(100dvh - var(--topbar-h) - 16px); overflow-y: auto; } + .search-panel .grid { grid-template-columns: minmax(0, 1fr); } .topbar-actions .hide-mobile { display: none; } .msg-row { padding-right: 8px; } .mail-list { padding-bottom: 72px; } diff --git a/web/src/views/SearchBar.tsx b/web/src/views/SearchBar.tsx index 93adeb7..59c1350 100644 --- a/web/src/views/SearchBar.tsx +++ b/web/src/views/SearchBar.tsx @@ -20,6 +20,7 @@ export function SearchBar() { const [q, setQ] = useState(""); const [adv, setAdv] = useState(false); const inputRef = useRef(null); + const formRef = useRef(null); const mailboxes = useMail((s) => s.mailboxes); const [advFields, setAdvFields] = useState({ from: "", to: "", subject: "", words: "", hasAttachment: false, unread: false, folder: "", after: "", before: "" }); @@ -31,6 +32,34 @@ export function SearchBar() { } else setQ(""); }, [location, search, resultsPath]); + // The bar stays on screen from page to page, so the panel would too. + useEffect(() => setAdv(false), [location]); + + /* + * The panel closes on a tap or click outside it and on Escape, as a menu + * does. A date picker opened from it lives in a portal elsewhere in the + * page, so a tap there counts as inside; it handles its own Escape first. + */ + useEffect(() => { + if (!adv) return; + const onDown = (e: MouseEvent | TouchEvent) => { + const target = e.target as Element | null; + if (formRef.current?.contains(target) || target?.closest?.(".popover")) return; + setAdv(false); + }; + const onKey = (e: KeyboardEvent) => { + if (e.key === "Escape") setAdv(false); + }; + document.addEventListener("mousedown", onDown); + document.addEventListener("touchstart", onDown); + document.addEventListener("keydown", onKey); + return () => { + document.removeEventListener("mousedown", onDown); + document.removeEventListener("touchstart", onDown); + document.removeEventListener("keydown", onKey); + }; + }, [adv]); + useEffect(() => keyboard.pushScope("search", [{ keys: "/", description: "Search mail", group: "Navigation", handler: () => inputRef.current?.focus() }]), []); const submit = (e?: FormEvent) => { @@ -61,7 +90,7 @@ export function SearchBar() { }; return ( -
+
setQ(e.target.value)} aria-label={events ? t("Search events") : t("Search mail")} enterKeyHint="search" /> @@ -93,12 +122,12 @@ export function SearchBar() {
setAdvFields({ ...advFields, after: v })} />{t("to")} setAdvFields({ ...advFields, before: v })} />
-
+
-
+
diff --git a/web/src/views/__tests__/search-panel.test.tsx b/web/src/views/__tests__/search-panel.test.tsx new file mode 100644 index 0000000..464e875 --- /dev/null +++ b/web/src/views/__tests__/search-panel.test.tsx @@ -0,0 +1,70 @@ +import { act } from "react"; +import { createRoot, type Root } from "react-dom/client"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { Router } from "wouter"; +import { memoryLocation } from "wouter/memory-location"; +import { SearchBar } from "../SearchBar"; + +(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; + +/* + * The search bar stays on screen from page to page, so its advanced panel + * has to close by itself: on a tap outside it, on Escape, and when the page + * changes. On a phone its Cancel button could end up off screen (#94). + */ +describe("the advanced search panel", () => { + let host: HTMLDivElement; + let root: Root; + let navigate: (to: string) => void; + + beforeEach(async () => { + host = document.createElement("div"); + document.body.appendChild(host); + root = createRoot(host); + const memory = memoryLocation({ path: "/mail" }); + navigate = memory.navigate; + await act(async () => { + root.render( + + +

page

+
, + ); + }); + await act(async () => { + host.querySelector('[aria-label="Advanced search"]')!.click(); + }); + }); + + afterEach(async () => { + await act(async () => root.unmount()); + host.remove(); + }); + + const panel = () => host.querySelector(".search-panel"); + const press = async (target: EventTarget, type: string, init: Record = {}) => { + const ev = type === "keydown" ? new KeyboardEvent(type, { bubbles: true, ...init }) : new MouseEvent(type, { bubbles: true }); + await act(async () => target.dispatchEvent(ev)); + }; + + it("stays open while it is being filled in", async () => { + expect(panel()).not.toBeNull(); + await press(panel()!.querySelector("input")!, "mousedown"); + expect(panel()).not.toBeNull(); + }); + + it("closes on a click outside it", async () => { + await press(host.querySelector(".elsewhere")!, "mousedown"); + expect(panel()).toBeNull(); + }); + + it("closes on Escape", async () => { + await press(document, "keydown", { key: "Escape" }); + expect(panel()).toBeNull(); + }); + + it("closes when the page changes", async () => { + await act(async () => navigate("/settings")); + expect(panel()).toBeNull(); + }); +});