javascript - Preserve focus on closing a modal or flyout - Stack Overflow

I have a question on accessibility.There are several layersmodals or flyout windows which are opened

I have a question on accessibility. There are several layers/modals or flyout windows which are opened on click of a button or link.

I will need to preserve the focus on the original element which was clicked to open a modal or popup or flyout once they are closed the focus should be back to the clicked element.

Currently when I tab on the page after closing the modal windows or flyouts, focus starts from begining

i am using angular bootstrap modal and custom flyout windows opened via angular state provider configuration.

I have a question on accessibility. There are several layers/modals or flyout windows which are opened on click of a button or link.

I will need to preserve the focus on the original element which was clicked to open a modal or popup or flyout once they are closed the focus should be back to the clicked element.

Currently when I tab on the page after closing the modal windows or flyouts, focus starts from begining

i am using angular bootstrap modal and custom flyout windows opened via angular state provider configuration.

Share Improve this question asked Apr 25, 2017 at 17:50 ShashiShashi 1,1722 gold badges18 silver badges32 bronze badges 1
  • 2 Save the event target from the event that opened the modal/flyout and set focus on that target when the modal/flyout closes. See MDN HTMLelement Reference - focus method. – georgeawg Commented Apr 25, 2017 at 18:40
Add a ment  | 

3 Answers 3

Reset to default 5

The official remendation from WAI-ARIA Authoring Practices - Modal Dialog states:

When a dialog closes, focus returns to the element that invoked the dialog unless either:

  • The invoking element no longer exists. Then, focus is set on another element that provides logical work flow.
  • The work flow design includes the following conditions that can occasionally make focusing a different element a more logical choice:
    1. It is very unlikely users need to immediately re-invoke the dialog.
    2. The task pleted in the dialog is directly related to a subsequent step in the work flow.

To return focus to the element that was focused before your modal opened:

  1. Before opening the modal, save a reference to document.activeElement.
  2. After closing the modal, focus the reference to the previous activeElement.

Example:

let previousActiveElement = document.activeElement;
// Open and close the modal
if (document.body.contains(previousActiveElement)) {
    previousActiveElement.focus();
}

Solved the above issue by preserving the current focus before opening the modal and restore the focus back when modal is closed

To return focus to the element that was focused before your modal opened:

Before opening the modal, save a reference to document.activeElement. After closing the modal, focus the reference to the previous activeElement.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744833634a4596131.html

相关推荐

  • javascript - Preserve focus on closing a modal or flyout - Stack Overflow

    I have a question on accessibility.There are several layersmodals or flyout windows which are opened

    2天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信