Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board/search & filtering: tweak the search field

Adorian Doranaf2b591c088f
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index b56b5d6bd0..4dbe5929ae 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -3493,7 +3493,8 @@
"move_column": "Move the column left / right",
"move_column_to_edge": "Move the column to the far left / right"
},
- "status-alias": "Status"
+ "status-alias": "Status",
+ "filter-placeholder": "Filter board..."
},
"dashboard_view": {
"refresh-widget": "Refresh",
@@ -4195,8 +4196,9 @@
},
"collection_filter": {
"placeholder": "Filter...",
- "tooltip": "Narrows the collection to the notes matching a search query. Press Enter to apply; clear the box to show everything again.",
+ "tooltip": "Narrows the collection to the notes matching a search query. Press Enter or the search button to apply; clear the box to show everything again.",
"clear": "Clear the filter",
- "fetch-error": "The filter could not be run."
+ "fetch-error": "The filter could not be run.",
+ "submit": "Apply the filter"
}
}
diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsx
index 2b89faec92..4da5c08ac1 100644
--- a/apps/client/src/widgets/collections/board/index.tsx
+++ b/apps/client/src/widgets/collections/board/index.tsx
@@ -749,7 +749,10 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
<div className="board-view">
<CollectionProperties
note={parentNote}
- centerChildren={<CollectionFilterInput filter={filter} />}
+ rightChildren={<CollectionFilterInput
+ filter={filter}
+ placeholder={t("board_view.filter-placeholder")}
+ />}
/>
<BoardActionsContext.Provider value={boardActions}>
<BoardPromotedAttributesContext.Provider value={shownAttributes}>
diff --git a/apps/client/src/widgets/collections/collection_filter.css b/apps/client/src/widgets/collections/collection_filter.css
index ecc4153910..872a30bb99 100644
--- a/apps/client/src/widgets/collections/collection_filter.css
+++ b/apps/client/src/widgets/collections/collection_filter.css
@@ -1,4 +1,25 @@
+/* Painted from the quick search box's own variables, so the two boxes stay in step under a theme
+ that styles it; the fallbacks cover the themes that define none of them. */
:where(:root) {
+ --collection-filter-color:
+ var(--quick-search-color, var(--muted-text-color));
+ --collection-filter-background:
+ var(--quick-search-background, var(--accented-background-color));
+ --collection-filter-hover-background:
+ var(--quick-search-hover-background, var(--accented-background-color));
+ --collection-filter-focus-color:
+ var(--quick-search-focus-color, var(--main-text-color));
+ --collection-filter-focus-background:
+ var(--quick-search-focus-background, var(--main-background-color));
+ --collection-filter-focus-border-color:
+ var(--quick-search-focus-border, var(--main-border-color));
+ --collection-filter-button-hover-background:
+ var(--left-pane-item-action-button-hover-background, var(--main-border-color));
+ --collection-filter-clear-background:
+ var(--dropdown-item-icon-destructive-color, #de4027);
+ --collection-filter-clear-hover-background: color-mix(
+ in srgb, var(--collection-filter-clear-background) 85%, black);
+ --collection-filter-clear-color: white;
--collection-filter-active-border-color: var(--main-text-color);
--collection-filter-error-color: #c73a35;
--collection-filter-error-background: var(--main-background-color);
@@ -6,26 +27,111 @@
}
.collection-filter {
- display: inline-flex;
+ /* The buttons sit over the trailing end of the box, so the input reserves room for them in its
+ own padding rather than standing beside them. */
+ --collection-filter-button-size: 22px;
+ --collection-filter-button-inset: 4px;
+ --collection-filter-button-gap: 2px;
+
+ display: inline-block;
position: relative;
- align-items: center;
- gap: 0.25em;
+ width: clamp(110px, 16vw, 200px);
+ border: 1px solid transparent;
+ border-radius: 6px;
+ background: var(--collection-filter-background);
+ color: var(--collection-filter-color);
+ transition: background-color 200ms ease-in;
+
+ &:hover {
+ background: var(--collection-filter-hover-background);
+ transition: background-color 75ms ease-out;
+ }
+
+ &:focus-within {
+ border-color: var(--collection-filter-focus-border-color);
+ background: var(--collection-filter-focus-background);
+ color: var(--collection-filter-focus-color);
+ transition: background-color 100ms ease-out;
+ }
+
+ &.active {
+ border-color: var(--collection-filter-active-border-color);
+ }
> input.form-control {
- width: clamp(120px, 30vw, 260px);
- padding: 2px 8px;
+ width: 100%;
+ box-shadow: none;
+ border: 0;
+ padding: 6px 10px;
+ padding-inline-end: calc(
+ var(--collection-filter-button-size) + 2 * var(--collection-filter-button-inset));
+ background: transparent;
+ color: inherit;
font-size: inherit;
+
+ &::placeholder {
+ color: var(--collection-filter-color);
+ }
}
- &.active > input.form-control {
- border-color: var(--collection-filter-active-border-color);
+ &:has(.collection-filter-clear) > input.form-control {
+ padding-inline-end: calc(
+ 2 * var(--collection-filter-button-size) + 2 * var(--collection-filter-button-inset)
+ + var(--collection-filter-button-gap));
+ }
+
+ .collection-filter-buttons {
+ display: flex;
+ position: absolute;
+ top: 0;
+ bottom: 0;
+ inset-inline-end: var(--collection-filter-button-inset);
+ gap: var(--collection-filter-button-gap);
+ align-items: center;
+ }
+
+ button {
+ display: flex;
+ flex: none;
+ align-items: center;
+ justify-content: center;
+ width: var(--collection-filter-button-size);
+ height: var(--collection-filter-button-size);
+ border: 0;
+ border-radius: 50%;
+ padding: 0;
+ background: transparent;
+ color: inherit;
+ font-size: 1.1em;
+ line-height: 1;
+ transition: background-color 100ms ease-out;
+
+ /* The colour is restated because Bootstrap's own `.btn:hover` would otherwise take it. */
+ &:hover {
+ background: var(--collection-filter-button-hover-background);
+ color: inherit;
+ }
+
+ &:active {
+ transform: scale(0.85);
+ }
+ }
+
+ .collection-filter-clear {
+ background: var(--collection-filter-clear-background);
+ color: var(--collection-filter-clear-color);
+
+ &:hover {
+ background: var(--collection-filter-clear-hover-background);
+ color: var(--collection-filter-clear-color);
+ }
}
.collection-filter-error {
display: block;
position: absolute;
top: calc(100% + 4px);
- left: 0;
+ inset-inline-end: 0;
z-index: 100;
max-width: 60vw;
border: 1px solid var(--collection-filter-error-border-color);
diff --git a/apps/client/src/widgets/collections/collection_filter.spec.tsx b/apps/client/src/widgets/collections/collection_filter.spec.tsx
index 7dbf1e0004..85e8723d42 100644
--- a/apps/client/src/widgets/collections/collection_filter.spec.tsx
+++ b/apps/client/src/widgets/collections/collection_filter.spec.tsx
@@ -309,6 +309,29 @@ describe("CollectionFilterInput", () => {
expect(filter.setQuery).toHaveBeenCalledWith("#done");
});
+ it("submits what is typed through the search button", async () => {
+ const { filter, input } = mountInput();
+
+ await act(async () => type(input, "#done"));
+ const submit = container.querySelector<HTMLElement>(".collection-filter-submit");
+ expect(submit).not.toBeNull();
+ await act(async () => submit?.click());
+
+ expect(filter.setQuery).toHaveBeenCalledWith("#done");
+ });
+
+ it("puts the clear button before the search button", async () => {
+ const { input } = mountInput();
+
+ await act(async () => type(input, "#done"));
+ const buttons = [ ...container.querySelectorAll("button") ].map(button => button.className);
+ const clear = buttons.findIndex(name => name.includes("collection-filter-clear"));
+ const submit = buttons.findIndex(name => name.includes("collection-filter-submit"));
+
+ expect(clear).toBeGreaterThanOrEqual(0);
+ expect(submit).toBe(clear + 1);
+ });
+
it("clears the filter through the button and refocuses the box", async () => {
const { filter } = mountInput({ query: "#done" });
diff --git a/apps/client/src/widgets/collections/collection_filter.tsx b/apps/client/src/widgets/collections/collection_filter.tsx
index 79e68706c5..be6eb2e04d 100644
--- a/apps/client/src/widgets/collections/collection_filter.tsx
+++ b/apps/client/src/widgets/collections/collection_filter.tsx
@@ -1,6 +1,7 @@
import "./collection_filter.css";
import type { HighlightedTokenInfo } from "@triliumnext/commons";
+import type { Tooltip } from "bootstrap";
import clsx from "clsx";
import { useEffect, useMemo, useRef, useState } from "preact/hooks";
@@ -10,11 +11,20 @@ import type LoadResults from "../../services/load_results";
import searchService from "../../services/search";
import ActionButton from "../react/ActionButton";
import FormTextBox from "../react/FormTextBox";
-import { useTriliumEvent } from "../react/hooks";
+import { useStaticTooltip, useTriliumEvent } from "../react/hooks";
/** How long to wait after a change before re-running the active filter. */
const RERUN_DEBOUNCE_MS = 300;
+// Hover only: the box is typed into, and a tooltip opened by the focus would sit over the header
+// for as long as the query is being written.
+const TOOLTIP_CONFIG: Partial<Tooltip.Options> = {
+ title: t("collection_filter.tooltip"),
+ placement: "bottom",
+ trigger: "hover",
+ animation: false
+};
+
export interface CollectionFilter {
/** The submitted query, empty while no filter is active. */
query: string;
@@ -133,14 +143,20 @@ export function useCollectionFilter(note: FNote, {
}
/**
- * The filter box for a collection header. Enter submits what is typed and the button clears it.
+ * The filter box for a collection header, styled after the quick search box. Enter or the search
+ * button submits what is typed, and the clear button beside it drops the filter.
*
* What is typed is held here until it is submitted, so an unrelated redraw cannot apply a
* half-typed query.
*/
-export function CollectionFilterInput({ filter }: { filter: CollectionFilter }) {
+export function CollectionFilterInput({ filter, placeholder }: {
+ filter: CollectionFilter;
+ /** Names the collection being filtered, for a view that has a word for what it holds. */
+ placeholder?: string;
+}) {
const [ typed, setTyped ] = useState(filter.query);
const inputRef = useRef<HTMLInputElement>(null);
+ useStaticTooltip(inputRef, TOOLTIP_CONFIG);
// Adopt a query submitted elsewhere, or the stored one arriving on mount.
useEffect(() => setTyped(filter.query), [ filter.query ]);
@@ -152,8 +168,7 @@ export function CollectionFilterInput({ filter }: { filter: CollectionFilter })
<FormTextBox
inputRef={inputRef}
currentValue={typed}
- placeholder={t("collection_filter.placeholder")}
- title={t("collection_filter.tooltip")}
+ placeholder={placeholder ?? t("collection_filter.placeholder")}
onChange={setTyped}
onKeyDown={(e: KeyboardEvent) => {
if (e.key === "Enter") {
@@ -167,18 +182,28 @@ export function CollectionFilterInput({ filter }: { filter: CollectionFilter })
}
}}
/>
- {(!!typed || isActive) && (
+ <div className="collection-filter-buttons">
+ {(!!typed || isActive) && (
+ <ActionButton
+ className="collection-filter-clear"
+ noIconActionClass
+ icon="bx bx-x"
+ text={t("collection_filter.clear")}
+ onClick={() => {
+ setTyped("");
+ filter.setQuery("");
+ inputRef.current?.focus();
+ }}
+ />
+ )}
<ActionButton
- className="collection-filter-clear"
- icon="bx bx-x"
- text={t("collection_filter.clear")}
- onClick={() => {
- setTyped("");
- filter.setQuery("");
- inputRef.current?.focus();
- }}
+ className="collection-filter-submit"
+ noIconActionClass
+ icon="bx bx-search"
+ text={t("collection_filter.submit")}
+ onClick={() => filter.setQuery(typed)}
/>
- )}
+ </div>
{filter.error && <span className="collection-filter-error">{filter.error}</span>}
</div>
);