Skip to content

Focus the primary button in confirmation dialogs so Enter confirms - #5692

Merged
freescout-help-desk merged 1 commit into
freescout-help-desk:masterfrom
JayOfTheKeyboard:fix/confirm-dialog-enter
Oct 2, 2026
Merged

freescout-help-desk merged 1 commit into
freescout-help-desk:masterfrom
JayOfTheKeyboard:fix/confirm-dialog-enter

Conversation

@JayOfTheKeyboard

Copy link
Copy Markdown
Contributor

Follow-up to #5683.

Problem

When a confirmation dialog opens (for example after pressing Delete on a conversation), focus stays on the dialog container. Enter does nothing, and Tab starts from the dialog's first focusable element. With #5683 the toolbar can be used from the keyboard, but every delete still needs the mouse, or several Tabs, to confirm.

To reproduce: open a conversation, focus the Delete icon and press Enter. The dialog opens, and pressing Enter again does nothing (document.activeElement is div.modal).

Fix

showModalDialog() now focuses the dialog's primary button once the body is in place, after any on_show callback has run:

var on_show = options.on_show;
options.on_show = function(modal, a) {
    if (typeof(on_show) == "function") {
        on_show(modal, a);
    }
    var modal_body = modal.children().find('.modal-body:first');
    if (!modal_body.find('input:visible,textarea:visible,select:visible').length) {
        modal_body.find('.btn-primary:enabled:visible:first').focus();
    }
};

This covers every dialog opened through showModalDialog() or showModalConfirm(): deleting a conversation, bulk delete, discarding a draft, deleting a photo, resetting a password, and similar.

Dialogs with a field to fill in are left as they are, so a dialog like Delete User, which asks you to type DELETE, can't be confirmed with a stray Enter.

Key Before After
Enter, right after the dialog opens nothing confirms
Tab first focusable element in the dialog moves from the primary button to Cancel
Escape closes closes (unchanged)

Not changed

  • The primary button gets focus, not Cancel. For a delete that means Enter, Enter deletes. If you'd rather focus Cancel for destructive dialogs, it's a one-line change.
  • Dialogs whose body is loaded by ajax (remote) are not affected.

Tested

On master at 2f1739e, Chrome with real key events (CDP Input.dispatchKeyEvent):

  • Before: after the Delete dialog opens, focus is div.modal and Enter does nothing.
  • After: the Delete button has focus, Tab moves to Cancel, Enter on Cancel closes the dialog without deleting, and Enter on Delete deletes.

The skip for dialogs with fields comes from the input:visible,... check above. I read it against the Delete User dialog but did not drive that dialog in the test.

Files

  • public/js/main.js: showModalDialog()

Confirmation dialogs opened with showModalDialog() leave focus on the modal container, so Enter does nothing and Tab starts from the close button. Focus the primary button after the dialog body is inserted. Dialogs with a field to fill in (such as Delete User, which asks to type DELETE) are left as they are.
@freescout-help-desk
freescout-help-desk merged commit 2eb7624 into freescout-help-desk:master Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants