Skip to content

Build your personal knowledge base with Trilium Notes

client/ui/note autocomplete: add a flag to make the dropdown width to match the field's width

Adorian Doran3b4f2e2ba160
diff --git a/apps/client/src/widgets/dialogs/edit_link.tsx b/apps/client/src/widgets/dialogs/edit_link.tsx
index a667d5310e..4b469d4c38 100644
--- a/apps/client/src/widgets/dialogs/edit_link.tsx
+++ b/apps/client/src/widgets/dialogs/edit_link.tsx
@@ -598,6 +598,7 @@ function useEditLinkForm(opts: EditLinkOpts | undefined) {
<NoteAutocomplete
inputRef={autocompleteRef}
handleRef={handleRef}
+ isListAsWideAsField
// A URL picked from the search belongs on the web link side.
onChange={(picked) => {
if (picked?.externalLink) {
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
index 84bf9d917d..60f956a058 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
@@ -1074,6 +1074,18 @@ describe("NoteAutocomplete's suggestion list", () => {
expect(document.querySelector<HTMLElement>(".note-autocomplete-menu")?.style.width).toBe("640px");
});
+ it("keeps the list as wide as a narrow field when asked to", async () => {
+ const input = await mount({ isListAsWideAsField: true });
+ const group = input.closest(".input-group");
+ if (!group) throw new Error("no input group rendered");
+ group.getBoundingClientRect = () => DOMRect.fromRect({ width: 320, height: 30 });
+ await type(input, "a");
+
+ const menu = document.querySelector<HTMLElement>(".note-autocomplete-menu");
+ expect(menu).not.toBeNull();
+ expect(menu?.style.width).toBe("320px");
+ });
+
it("picks a clicked note", async () => {
const noteIdChanged = vi.fn();
const input = await mount({ noteIdChanged });
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.tsx b/apps/client/src/widgets/react/NoteAutocomplete.tsx
index b803dfa151..9737391749 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx
@@ -72,6 +72,8 @@ export interface NoteAutocompleteProps {
* Note does.
*/
searchFooter?: boolean;
+ /** Keeps the list as wide as the field, rather than widening it past a narrow one. */
+ isListAsWideAsField?: boolean;
/** Called with the suggestion the list highlights, or `undefined` while it highlights none. */
onHighlight?: (suggestion: Suggestion | undefined) => void;
}
@@ -91,7 +93,11 @@ export interface NoteAutocompleteHandle {
clear(): void;
}
-export default function NoteAutocomplete({ id, inputRef: externalInputRef, text, placeholder, container, containerStyle, opts, onChange, onTextChange, onKeyDown, onBlur, noteIdChanged, noteId, filter, readOnly, required, tabIndex, handleRef, anchorRef, searchFooter, onHighlight }: NoteAutocompleteProps) {
+export default function NoteAutocomplete({
+ id, inputRef: externalInputRef, text, placeholder, container, containerStyle, opts, onChange,
+ onTextChange, onKeyDown, onBlur, noteIdChanged, noteId, filter, readOnly, required, tabIndex,
+ handleRef, anchorRef, searchFooter, isListAsWideAsField, onHighlight
+}: NoteAutocompleteProps) {
const inputRef = useSyncedRef<HTMLInputElement>(externalInputRef);
const groupRef = useRef<HTMLDivElement>(null);
const [ value, setValue ] = useState("");
@@ -429,8 +435,7 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,
// Rendered in the field's modal so the list stacks above it: the note picker
// raises its modal (2000) over `.tn-popup` in the body (1200).
container={anchor.closest<HTMLElement>(".modal") ?? undefined}
- // The list spans the whole field, buttons included, and widens past a narrow one.
- style={{ width: `${Math.max(anchor.getBoundingClientRect().width, DROPDOWN_MIN_WIDTH)}px` }}
+ style={{ width: `${getListWidth(anchor, !!isListAsWideAsField)}px` }}
escapeDismisses={false}
onDismiss={autocomplete.close}
>
@@ -698,6 +703,15 @@ function NoteSuggestionPopup({ className, ...props }: PopupProps) {
);
}
+/**
+ * The width of the list that hangs from `anchor`, the whole field with its buttons: the field's
+ * width, widened to `DROPDOWN_MIN_WIDTH` for a narrow field unless `isAsWideAsField`.
+ */
+function getListWidth(anchor: Element, isAsWideAsField: boolean) {
+ const fieldWidth = anchor.getBoundingClientRect().width;
+ return isAsWideAsField ? fieldWidth : Math.max(fieldWidth, DROPDOWN_MIN_WIDTH);
+}
+
/**
* Lists the keys of a list with a search footer in the shortcut hints pane, which the footer's button
* opens. Only such a list adds hints, as they replace whatever the host registered.