Search: make the advanced panel usable on a phone #98
No files matched your search
+16
-2
@@ -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; }
|
||||
|
||||
@@ -20,6 +20,7 @@ export function SearchBar() {
|
||||
const [q, setQ] = useState("");
|
||||
const [adv, setAdv] = useState(false);
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
const formRef = useRef<HTMLFormElement>(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 (
|
||||
<form className="searchbar" role="search" onSubmit={submit}>
|
||||
<form ref={formRef} className="searchbar" role="search" onSubmit={submit}>
|
||||
<div className="search-input">
|
||||
<Search size={18} className="muted" />
|
||||
<input ref={inputRef} type="search" placeholder={events ? t("Search events") : t("Search mail (from:, to:, subject:, has:attachment, is:unread, in:, before:, after:)")} value={q} onChange={(e) => setQ(e.target.value)} aria-label={events ? t("Search events") : t("Search mail")} enterKeyHint="search" />
|
||||
@@ -93,12 +122,12 @@ export function SearchBar() {
|
||||
<div className="row"><DateField aria-label={t("After")} value={advFields.after} onChange={(v) => setAdvFields({ ...advFields, after: v })} /><span className="muted">{t("to")}</span><DateField aria-label={t("Before")} value={advFields.before} onChange={(v) => setAdvFields({ ...advFields, before: v })} /></div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="row" style={{ justifyContent: "space-between", marginTop: 4 }}>
|
||||
<div className="row search-panel-foot">
|
||||
<div className="row gap-16">
|
||||
<label className="check"><input type="checkbox" checked={advFields.hasAttachment} onChange={(e) => setAdvFields({ ...advFields, hasAttachment: e.target.checked })} /> {t("Has attachment")}</label>
|
||||
<label className="check"><input type="checkbox" checked={advFields.unread} onChange={(e) => setAdvFields({ ...advFields, unread: e.target.checked })} /> {t("Unread only")}</label>
|
||||
</div>
|
||||
<div className="row">
|
||||
<div className="row search-panel-actions">
|
||||
<button type="button" className="btn btn-ghost" onClick={() => setAdv(false)}>{t("Cancel")}</button>
|
||||
<button type="button" className="btn btn-primary" onClick={applyAdvanced}>{t("Search")}</button>
|
||||
</div>
|
||||
|
||||
@@ -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(
|
||||
<Router hook={memory.hook}>
|
||||
<SearchBar />
|
||||
<p className="elsewhere">page</p>
|
||||
</Router>,
|
||||
);
|
||||
});
|
||||
await act(async () => {
|
||||
host.querySelector<HTMLButtonElement>('[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<string, unknown> = {}) => {
|
||||
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();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user