Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): stop keystrokes reaching the note while a dialog opens (closes #7996)

matt wilkie61d924d53ef4
diff --git a/apps/client/src/services/dialog.spec.ts b/apps/client/src/services/dialog.spec.ts
index 502edf505e..755ab0598c 100644
--- a/apps/client/src/services/dialog.spec.ts
+++ b/apps/client/src/services/dialog.spec.ts
@@ -49,6 +49,18 @@ describe("dialog service", () => {
});
describe("openDialog", () => {
+ afterEach(() => {
+ document.body.innerHTML = "";
+ });
+
+ function focusNewInput(parent: HTMLElement) {
+ const input = document.createElement("input");
+ parent.appendChild(input);
+ input.focus();
+ expect(document.activeElement).toBe(input);
+ return input;
+ }
+
it("closes the active dialog, sets the new one, saves focus, shows the modal and updates shortcuts", async () => {
const previous = makeDialog();
glob.activeDialog = previous;
@@ -116,6 +128,41 @@ describe("dialog service", () => {
expect(focusSavedElement).not.toHaveBeenCalled();
});
+ it("saves the focused element, then blurs it before the modal shows", async () => {
+ const input = focusNewInput(document.body);
+ let savedElement: Element | null = null;
+ let focusedAtShow: Element | null = null;
+ saveFocusedElement.mockImplementationOnce(() => {
+ savedElement = document.activeElement;
+ });
+ modalShow.mockImplementationOnce(() => {
+ focusedAtShow = document.activeElement;
+ });
+
+ await openDialog(makeDialog(), false);
+
+ expect(savedElement).toBe(input);
+ expect(focusedAtShow).toBe(document.body);
+ expect(document.activeElement).toBe(document.body);
+ });
+
+ it("leaves focus alone when the dialog is opened with focus: false", async () => {
+ const input = focusNewInput(document.body);
+
+ await openDialog(makeDialog(), false, { focus: false });
+
+ expect(document.activeElement).toBe(input);
+ });
+
+ it("does not blur an element inside the dialog itself", async () => {
+ const $dialog = makeDialog().appendTo(document.body);
+ const input = focusNewInput($dialog[0]);
+
+ await openDialog($dialog, false);
+
+ expect(document.activeElement).toBe(input);
+ });
+
it("closes the autocomplete dropdown on hide", async () => {
const $dialog = makeDialog();
await openDialog($dialog, false);
diff --git a/apps/client/src/services/dialog.ts b/apps/client/src/services/dialog.ts
index d9b5118e5d..6142410f1f 100644
--- a/apps/client/src/services/dialog.ts
+++ b/apps/client/src/services/dialog.ts
@@ -21,6 +21,15 @@ export async function openDialog($dialog: JQuery<HTMLElement>, closeActDialog =
saveFocusedElement();
+ // Bootstrap moves focus only when the opening transition ends, so blur now to keep keystrokes
+ // typed during the transition out of the previously focused element, e.g. the note editor.
+ if (config?.focus !== false) {
+ const activeElement = document.activeElement;
+ if (activeElement instanceof HTMLElement && !$dialog[0].contains(activeElement)) {
+ activeElement.blur();
+ }
+ }
+
// Lift this dialog above whatever is already open, if anything is (see raiseAboveWhatIsOpen).
const bumpedZIndex = raiseAboveWhatIsOpen($dialog[0], declaredZIndex);