Skip to content

Build your personal knowledge base with Trilium Notes

feat(geomap): offer to clear the search bar

Elian Dorancb630831806d
diff --git a/apps/client/src/widgets/collections/geomap/SearchBox.css b/apps/client/src/widgets/collections/geomap/SearchBox.css
index 7cdeb74655..0d1fe46d49 100644
--- a/apps/client/src/widgets/collections/geomap/SearchBox.css
+++ b/apps/client/src/widgets/collections/geomap/SearchBox.css
@@ -50,6 +50,24 @@ body.mobile .geo-map-container .geo-search-toolbar {
opacity: 0.6;
}
+/*
+ * Sized to the field beside it rather than to a toolbar's own buttons, so the bar keeps its height.
+ *
+ * It holds its place in the bar with nothing to clear rather than leaving it, which would take the
+ * bar's width with it — hidden rather than gone, so it is out of reach of the pointer and the tab
+ * key as well as out of sight.
+ */
+.geo-map-container .geo-search-toolbar .geo-search-clear {
+ --icon-button-size: 24px;
+ width: 24px;
+ height: 24px;
+ visibility: hidden;
+}
+
+.geo-map-container .geo-search-toolbar .geo-search-clear.shown {
+ visibility: visible;
+}
+
/* The dropdown is portalled to the body, so these are not scoped under .geo-map-container. */
.form-autocomplete-dropdown .geo-search-entry {
display: flex;
diff --git a/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx b/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx
index 44d67894d5..747e91268e 100644
--- a/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx
+++ b/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx
@@ -421,6 +421,34 @@ describe("geo map SearchBox", () => {
expect(labels()).toEqual([ "Tokyo" ]);
});
+ it("offers to clear the field only once there is something in it to clear", async () => {
+ mockGeocoder([ TOKYO ]);
+ const container = renderSearchBox(fakeMap(), mapNotes());
+ const clearButton = () => container.querySelector<HTMLButtonElement>(".geo-search-clear");
+ const isShown = () => clearButton()?.classList.contains("shown");
+
+ // Standing in the bar either way, so the field beside it does not move as it comes and goes.
+ expect(clearButton()).not.toBeNull();
+ expect(isShown()).toBe(false);
+
+ await type(container, "tokyo");
+ expect(isShown()).toBe(true);
+
+ await pickNamed("geo-map.search-online(tokyo)");
+ await pickNamed("Tokyo");
+ expect(picked).toEqual([ TOKYO ]);
+
+ await act(async () => { clearButton()?.click(); });
+ await settle();
+
+ expect(field(container).value).toBe("");
+ expect(isShown()).toBe(false);
+ // Clearing the field is what takes the place off the map, panel and pin alike.
+ expect(picked).toEqual([ TOKYO, null ]);
+ // Rarely the end of searching, so the field is where the reader was left.
+ expect(document.activeElement).toBe(field(container));
+ });
+
it("reports moving on from a place once the field is emptied", async () => {
mockGeocoder([ TOKYO ]);
const container = renderSearchBox(fakeMap(), []);
diff --git a/apps/client/src/widgets/collections/geomap/SearchBox.tsx b/apps/client/src/widgets/collections/geomap/SearchBox.tsx
index 3a3bea2830..e3f1a9c6e7 100644
--- a/apps/client/src/widgets/collections/geomap/SearchBox.tsx
+++ b/apps/client/src/widgets/collections/geomap/SearchBox.tsx
@@ -1,5 +1,6 @@
import "./SearchBox.css";
+import clsx from "clsx";
import { useCallback, useContext, useRef, useState } from "preact/hooks";
import type { Map as MapLibreGLMap } from "maplibre-gl";
@@ -11,7 +12,7 @@ import { getMeasurementSystem } from "../../../utils/formatters";
import { formatDistance } from "../../../utils/units";
import FormAutocomplete from "../../react/FormAutocomplete";
import Icon from "../../react/Icon";
-import OverlayToolbar from "../../react/OverlayToolbar";
+import OverlayToolbar, { OverlayToolbarButton } from "../../react/OverlayToolbar";
import { DEFAULT_GEOCODING_PROVIDER_NAME, type GeoBounds, GEOCODING_PROVIDERS, type GeoSearchResult, SEARCH_RADIUS_M } from "./geocoding";
import { ParentMap } from "./map";
import { LOCATION_ATTRIBUTE, parseLocation } from "./Markers";
@@ -119,6 +120,7 @@ export default function SearchBox({ notes, onPickPlace }: SearchBoxProps) {
// Empties the list once a marker or place has been taken, which is what closes the dropdown under
// `keepOpenOnPick`. Typing again clears it.
const [ dismissed, setDismissed ] = useState(false);
+ const inputRef = useRef<HTMLInputElement>(null);
const entriesByKey = useRef(new Map<string, SearchEntry>());
// Discards a run superseded by a later one, since each reports through the same state.
const latestRun = useRef(0);
@@ -168,6 +170,13 @@ export default function SearchBox({ notes, onPickPlace }: SearchBoxProps) {
}
}, [ provider, map ]);
+ /** Empties the field, which is also what takes a searched place off the map (see `changeQuery`). */
+ const clearSearch = useCallback(() => {
+ changeQuery("");
+ // The field is where the reader was, and clearing it is rarely the end of searching.
+ inputRef.current?.focus();
+ }, [ changeQuery ]);
+
const pickEntry = useCallback((key: string) => {
const entry = entriesByKey.current.get(key);
if (!entry || !map) return;
@@ -229,20 +238,30 @@ export default function SearchBox({ notes, onPickPlace }: SearchBoxProps) {
return (
<OverlayToolbar className="geo-search-toolbar" titlePosition="bottom">
- <Icon icon="bx bx-search" className="geo-search-icon" />
- <FormAutocomplete
- className="geo-search-input"
- currentValue={query}
- onChange={changeQuery}
- onPick={pickEntry}
- source={source}
- renderItem={renderEntry}
- isHeading={isHeading}
- dropdownMinWidth={RESULT_LIST_WIDTH}
- autoActivate
- keepOpenOnPick
- placeholder={t("geo-map.search-placeholder")}
- aria-label={t("geo-map.search")}
+ <Icon icon="bx bx-search" className="geo-search-icon" />
+ <FormAutocomplete
+ className="geo-search-input"
+ inputRef={inputRef}
+ currentValue={query}
+ onChange={changeQuery}
+ onPick={pickEntry}
+ source={source}
+ renderItem={renderEntry}
+ isHeading={isHeading}
+ dropdownMinWidth={RESULT_LIST_WIDTH}
+ autoActivate
+ keepOpenOnPick
+ placeholder={t("geo-map.search-placeholder")}
+ aria-label={t("geo-map.search")}
+ />
+ {/* Kept in the bar whether or not there is anything to clear, and only shown when there
+ is: a button coming and going would take the bar's width with it, moving the field
+ under the reader mid-word. */}
+ <OverlayToolbarButton
+ className={clsx("geo-search-clear", query && "shown")}
+ icon="bx bx-x"
+ text={t("options.search_clear")}
+ onClick={clearSearch}
/>
</OverlayToolbar>
);
diff --git a/apps/client/src/widgets/react/OverlayToolbar.tsx b/apps/client/src/widgets/react/OverlayToolbar.tsx
index afc7894312..1c83a32065 100644
--- a/apps/client/src/widgets/react/OverlayToolbar.tsx
+++ b/apps/client/src/widgets/react/OverlayToolbar.tsx
@@ -1,5 +1,6 @@
import "./OverlayToolbar.css";
+import clsx from "clsx";
import { type ComponentChildren, createContext } from "preact";
import { useContext } from "preact/hooks";
@@ -43,12 +44,12 @@ export default function OverlayToolbar({ className, titlePosition, children }: O
* open its tooltips off the bottom edge — overridable per button where one of them is placed
* differently from its neighbours.
*/
-export function OverlayToolbarButton({ titlePosition, ...props }: ActionButtonProps) {
+export function OverlayToolbarButton({ titlePosition, className, ...props }: ActionButtonProps) {
const barDirection = useContext(TooltipDirection);
return <ActionButton
{...props}
- className="tn-tool-button"
+ className={clsx("tn-tool-button", className)}
noIconActionClass
titlePosition={titlePosition ?? barDirection}
/>;