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
3 Answers
Reset to default 5The 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:
- It is very unlikely users need to immediately re-invoke the dialog.
- 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:
- Before opening the modal, save a reference to
document.activeElement
. - After closing the modal,
focus
the reference to the previousactiveElement
.
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
评论列表(0条)