Skip to content

Build your personal knowledge base with Trilium Notes

style(autocomplete): draw the note list as the app's dropdown menus

Elian Doran74496121d353
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.css b/apps/client/src/widgets/react/NoteAutocomplete.css
index 548b7da904..60d95f88dd 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.css
+++ b/apps/client/src/widgets/react/NoteAutocomplete.css
@@ -36,22 +36,21 @@
}
}
-.algolia-autocomplete {
- z-index: 2000 !important;
-}
+.note-autocomplete-menu .dropdown-item {
+ /* Outranks Bootstrap's `nowrap`, so a long note path wraps within the field's width. */
+ white-space: normal;
-.algolia-autocomplete .aa-dropdown-menu {
- width: 100%;
- background-color: var(--main-background-color);
- border: 1px solid var(--main-border-color);
- border-top: none;
- max-height: 500px;
- overflow: auto;
- padding: 0;
- margin: 0;
+ .search-result-attributes {
+ display: block;
+ font-size: small;
+ color: var(--muted-text-color);
+ }
}
-/* In a popup and in a host's container alike, as `autocomplete.js` set them inline. */
+/*
+ * The lists in a host's container, such as the empty tab's and Jump to Note's, in the markup of
+ * `autocomplete.js`. It set the `display` inline.
+ */
span.aa-dropdown-menu,
span.aa-dropdown-menu .aa-suggestions {
display: block;
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
index 7bc24bebba..7057728190 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
@@ -173,21 +173,22 @@ describe("NoteAutocomplete's suggestion list", () => {
}
function rows() {
- return [ ...document.querySelectorAll<HTMLElement>(".algolia-autocomplete .aa-dropdown-menu .aa-suggestion") ];
+ return [ ...document.querySelectorAll<HTMLElement>(".note-autocomplete-menu > .tn-menu-scroll > .dropdown-item") ];
}
- it("lists the notes in the plugin's markup, the first one highlighted", async () => {
+ it("lists the notes as the rows of a menu, the first one highlighted", async () => {
const input = await mount();
await type(input, "al");
expect(getNoteSuggestions).toHaveBeenLastCalledWith("al");
const [ alpha, beta ] = rows();
expect(rows()).toHaveLength(2);
- expect(alpha.closest(".aa-suggestions")?.parentElement?.className).toBe("aa-dataset-0");
- expect(alpha.className).toBe("aa-suggestion aa-cursor");
- expect(alpha.querySelector(".note-suggestion .icon")?.className).toBe("icon bx bx-file");
+ expect(document.querySelector(".note-autocomplete-menu")?.className)
+ .toContain("dropdown-menu show tn-dropdown-menu tn-menu-keyboard");
+ expect(alpha.className).toBe("dropdown-item tn-menu-active");
+ expect(alpha.querySelector(".tn-icon")?.className).toBe("bx bx-file tn-icon");
expect(alpha.querySelector(".search-result-title")?.innerHTML).toBe("<b>Al</b>pha");
- expect(beta.querySelector(".icon")?.className).toBe("icon bx bx-note");
+ expect(beta.querySelector(".tn-icon")?.className).toBe("bx bx-note tn-icon");
expect(beta.querySelector(".search-result-attributes")?.textContent).toBe("#tag");
expect(input.getAttribute("aria-expanded")).toBe("true");
expect(input.getAttribute("aria-activedescendant")).toBe(alpha.id);
@@ -206,10 +207,9 @@ describe("NoteAutocomplete's suggestion list", () => {
const input = await mount();
await type(input, "x");
- expect(rows().map((row) => row.querySelector(".icon")?.className)).toEqual([
- "icon bx bx-search", "icon bx bx-plus", "icon bx bx-subdirectory-right", "icon bx bx-link-external", "icon bx bx-note"
+ expect(rows().map((row) => row.querySelector(".tn-icon")?.className)).toEqual([
+ "bx bx-search tn-icon", "bx bx-plus tn-icon", "bx bx-subdirectory-right tn-icon", "bx bx-link-external tn-icon", "bx bx-note tn-icon"
]);
- expect(rows()[0].querySelector(".note-suggestion")?.classList.contains("search-notes-action")).toBe(true);
expect(rows()[4].textContent).toBe("T");
});
@@ -219,7 +219,7 @@ describe("NoteAutocomplete's suggestion list", () => {
const input = await mount({ onChange, noteIdChanged });
await type(input, "b");
await press(input, "ArrowDown");
- expect(rows()[1].classList.contains("aa-cursor")).toBe(true);
+ expect(rows()[1].classList.contains("tn-menu-active")).toBe(true);
await press(input, "Enter");
expect(onChange).toHaveBeenCalledWith(notes[1]);
@@ -310,7 +310,7 @@ describe("NoteAutocomplete's suggestion list", () => {
expect(menu?.querySelectorAll(".aa-suggestion")).toHaveLength(2);
// In the host's flow, as `autocomplete.js` left it: its contained menu had only `display: block`.
expect(menu?.hasAttribute("style")).toBe(false);
- expect(document.querySelector(".algolia-autocomplete")).toBeNull();
+ expect(document.querySelector(".note-autocomplete-menu")).toBeNull();
await act(async () => {
input.focus();
@@ -334,18 +334,22 @@ describe("NoteAutocomplete's suggestion list", () => {
});
it("lists the commands for a `>` in a command palette, in the plugin's markup", async () => {
- const input = await mount({ opts: { isCommandPalette: true } });
+ // In a host's container, as Jump to Note lists them.
+ const host = document.createElement("div");
+ document.body.append(host);
+ const input = await mount({ opts: { isCommandPalette: true }, container: { current: host } });
await type(input, "> cmd");
expect(getCommandSuggestions).toHaveBeenLastCalledWith("> cmd");
expect(getNoteSuggestions).not.toHaveBeenCalled();
- const [ described, bare ] = rows();
+ const [ described, bare ] = host.querySelectorAll<HTMLElement>(".aa-suggestion");
expect(described.querySelector(".command-suggestion > .command-icon")?.className).toBe("command-icon bx bx-cog");
expect(described.querySelector(".command-content > .command-name")?.textContent).toBe("Cmd One");
expect(described.querySelector(".command-content > .command-description")?.textContent).toBe("Does a thing");
expect(described.querySelector(".command-suggestion > kbd.command-shortcut")?.textContent).toBe("Ctrl+1");
expect(bare.querySelector(".command-icon")?.className).toBe("command-icon bx bx-terminal");
expect(bare.querySelector(".command-description, .command-shortcut")).toBeNull();
+ host.remove();
// Elsewhere a `>` is only text to search for.
const plain = await mount();
@@ -496,7 +500,7 @@ describe("NoteAutocomplete's suggestion list", () => {
input.getBoundingClientRect = () => DOMRect.fromRect({ width: 200, height: 30 });
await type(input, "a");
- expect(document.querySelector<HTMLElement>(".algolia-autocomplete")?.style.width).toBe("320px");
+ expect(document.querySelector<HTMLElement>(".note-autocomplete-menu")?.style.width).toBe("320px");
});
it("picks a clicked note", async () => {
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.tsx b/apps/client/src/widgets/react/NoteAutocomplete.tsx
index 0ac8d028a0..dfc30120cd 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx
@@ -1,3 +1,4 @@
+import "./Menu.css";
import "./NoteAutocomplete.css";
import clsx from "clsx";
@@ -10,6 +11,7 @@ import { t } from "../../services/i18n";
import { createSearchScheduler, getCommandSuggestions, getNoteSuggestions, type Options, type Suggestion } from "../../services/note_autocomplete";
import { useAutocomplete } from "./FormAutocomplete";
import { useSyncedRef } from "./hooks";
+import Icon from "./Icon";
import Popup from "./Popup";
import RawHtml from "./RawHtml";
@@ -262,14 +264,14 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,
<Popup
anchor={groupRef.current}
placement="bottom-start"
- capHeight={false}
- className="algolia-autocomplete"
+ // The pointer moves the highlighted row, so `:hover` marks no second one.
+ className="dropdown-menu show tn-dropdown-menu tn-menu-keyboard note-autocomplete-menu"
// The list spans the whole field, buttons included.
style={{ width: `${groupRef.current.getBoundingClientRect().width}px` }}
escapeDismisses={false}
onDismiss={autocomplete.close}
>
- <NoteSuggestionList autocomplete={autocomplete} searchingFor={isSearchingFullText ? value : undefined} />
+ <NoteSuggestionMenu autocomplete={autocomplete} searchingFor={isSearchingFullText ? value : undefined} />
</Popup>
))}
</div>
@@ -318,6 +320,63 @@ function NoteSuggestionList({ autocomplete, searchingFor }: {
);
}
+/**
+ * The list of suggestions as the rows of a menu, for the popup, so it looks like the app's other
+ * dropdowns. With `searchingFor`, it shows a row saying a search is in progress instead.
+ */
+function NoteSuggestionMenu({ autocomplete, searchingFor }: {
+ autocomplete: ReturnType<typeof useAutocomplete<Suggestion>>;
+ searchingFor?: string;
+}) {
+ return (
+ <menu
+ className="tn-menu-scroll"
+ role="listbox"
+ // Keeps the input focused, so its blur does not close the list before the click lands
+ // on a suggestion.
+ onMouseDown={(e) => e.preventDefault()}
+ >
+ {searchingFor !== undefined && (
+ <li className="dropdown-item disabled">
+ <NoteSuggestionMenuItem suggestion={{ noteTitle: searchingFor, highlightedNotePathTitle: t("quick-search.searching") }} />
+ </li>
+ )}
+ {searchingFor === undefined && autocomplete.items.map((suggestion, index) => (
+ <li
+ key={`${suggestion.action ?? ""}:${suggestion.notePath ?? suggestion.commandId ?? index}`}
+ id={autocomplete.itemId(index)}
+ className={clsx("dropdown-item", index === autocomplete.activeIndex && "tn-menu-active")}
+ role="option"
+ aria-selected={index === autocomplete.activeIndex}
+ onMouseEnter={() => autocomplete.setActiveIndex(index)}
+ onClick={() => autocomplete.pick(suggestion)}
+ >
+ <NoteSuggestionMenuItem suggestion={suggestion} />
+ </li>
+ ))}
+ </menu>
+ );
+}
+
+/** One row of the menu, laid out as the rows of `FormListItem` are. */
+function NoteSuggestionMenuItem({ suggestion }: { suggestion: Suggestion }) {
+ const isCommand = suggestion.action === "command";
+ const icon = isCommand ? (suggestion.icon || "bx bx-terminal") : suggestionIcon(suggestion);
+ const description = isCommand ? suggestion.commandDescription : suggestion.highlightedAttributeSnippet;
+
+ return (
+ <span>
+ <Icon icon={icon} />
+ <span className="tn-menu-gap" />
+ <div className="note-suggestion-text">
+ <RawHtml className="search-result-title" html={suggestion.highlightedNotePathTitle ?? ""} />
+ {description && <RawHtml className="search-result-attributes" html={description} />}
+ </div>
+ {isCommand && suggestion.commandShortcut && <kbd>{suggestion.commandShortcut}</kbd>}
+ </span>
+ );
+}
+
/** One row of the list, in the markup of the jQuery plugin's suggestion template. */
function NoteSuggestion({ suggestion }: { suggestion: Suggestion }) {
if (suggestion.action === "command") {