Problem/Motivation
Drupal.Ajax.success
attempts to set focus to the element that triggered the ajax request.
You can stop by adding the attribute data-disable-refocus
to the element if another InvokeCommand is returned that sets focus.
This functionality is not actually tested anywhere and it does not work if the element is inside a dialog.
In #2995689: Allow reordering blocks without a pointer device it would be great if this worked because we are using an ajax element in dialog.
Proposed resolution
In the AJAX success handler, after the $(target).trigger('focus');
check to see if target
is in a dialog.
If it is in a dialog AND that dialog does not have a dedicated focus listener (we don't want to override a deliberately added focus listener), add a temporary focus listener to the dialog for when it receives focus due to autofocus. The focus listener callback will reroute focus back to target
where it was intended to go.