Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): move focus to the backdrop while a dialog opens

matt wilkie8a6c7f4637f2
diff --git a/apps/client/src/services/dialog.spec.ts b/apps/client/src/services/dialog.spec.ts
index 755ab0598c..47a34201a1 100644
--- a/apps/client/src/services/dialog.spec.ts
+++ b/apps/client/src/services/dialog.spec.ts
@@ -128,38 +128,53 @@ describe("dialog service", () => {
expect(focusSavedElement).not.toHaveBeenCalled();
});
- it("saves the focused element, then blurs it before the modal shows", async () => {
+ it("saves the focused element, then moves focus to the backdrop", async () => {
const input = focusNewInput(document.body);
+ const backdrop = $("<div class='modal-backdrop'></div>").appendTo(document.body)[0];
let savedElement: Element | null = null;
- let focusedAtShow: Element | null = null;
+ let focusTakenBy: EventTarget | null = null;
saveFocusedElement.mockImplementationOnce(() => {
savedElement = document.activeElement;
});
- modalShow.mockImplementationOnce(() => {
- focusedAtShow = document.activeElement;
+ input.addEventListener("focusout", (e) => {
+ focusTakenBy = e.relatedTarget;
});
await openDialog(makeDialog(), false);
expect(savedElement).toBe(input);
- expect(focusedAtShow).toBe(document.body);
- expect(document.activeElement).toBe(document.body);
+ expect(document.activeElement).toBe(backdrop);
+ // Editors that save on focusout (AttributeEditorOverlay) read this to stay open.
+ expect(focusTakenBy).toBe(backdrop);
});
- it("leaves focus alone when the dialog is opened with focus: false", async () => {
- const input = focusNewInput(document.body);
+ it("blurs the focused element when the dialog has no backdrop", async () => {
+ focusNewInput(document.body);
+ $("<div class='modal-backdrop'></div>").appendTo(document.body);
- await openDialog(makeDialog(), false, { focus: false });
+ await openDialog(makeDialog(), false, { backdrop: false });
- expect(document.activeElement).toBe(input);
+ expect(document.activeElement).toBe(document.body);
});
- it("does not blur an element inside the dialog itself", async () => {
+ it("does not take focus back from a dialog that is shown at once", async () => {
+ focusNewInput(document.body);
+ $("<div class='modal-backdrop'></div>").appendTo(document.body);
const $dialog = makeDialog().appendTo(document.body);
- const input = focusNewInput($dialog[0]);
+ const dialogInput = document.createElement("input");
+ $dialog[0].appendChild(dialogInput);
+ modalShow.mockImplementationOnce(() => dialogInput.focus());
await openDialog($dialog, false);
+ expect(document.activeElement).toBe(dialogInput);
+ });
+
+ 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);
});
diff --git a/apps/client/src/services/dialog.ts b/apps/client/src/services/dialog.ts
index 6142410f1f..7f0c5900b2 100644
--- a/apps/client/src/services/dialog.ts
+++ b/apps/client/src/services/dialog.ts
@@ -21,15 +21,6 @@ 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);
@@ -53,6 +44,11 @@ export async function openDialog($dialog: JQuery<HTMLElement>, closeActDialog =
// would undo the layer this gives it there.
showAboveWhateverHasTheScreen($dialog[0]);
+ // After the backdrop has its final place in the page, because moving an element drops its focus.
+ if (config?.focus !== false) {
+ takeFocusUntilShown($dialog[0], config?.backdrop !== false);
+ }
+
$dialog.on("hidden.bs.modal", () => {
sendDialogHome($dialog[0]);
@@ -71,6 +67,29 @@ export async function openDialog($dialog: JQuery<HTMLElement>, closeActDialog =
return $dialog;
}
+/**
+ * Bootstrap moves focus into the dialog only when the opening transition ends. Until then, keeps
+ * keystrokes out of the element that had focus, e.g. the note editor. Focus goes to the backdrop
+ * where there is one, so a `focusout` handler can tell from `relatedTarget` that a dialog took it.
+ */
+function takeFocusUntilShown(dialogEl: HTMLElement, hasBackdrop: boolean) {
+ const activeElement = document.activeElement;
+ if (!(activeElement instanceof HTMLElement) || dialogEl.contains(activeElement)) {
+ return;
+ }
+
+ const backdrops = document.querySelectorAll<HTMLElement>(".modal-backdrop");
+ const ownBackdrop = hasBackdrop ? backdrops[backdrops.length - 1] : undefined;
+ if (!ownBackdrop) {
+ activeElement.blur();
+ return;
+ }
+
+ ownBackdrop.tabIndex = -1;
+ ownBackdrop.style.outline = "none";
+ ownBackdrop.focus({ preventScroll: true });
+}
+
/** Where a dialog stands while nothing has the screen, what came with it, and how to stop watching. */
const dialogHomes = new WeakMap<HTMLElement, {
parent: Node;