Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove autofocusing in the modal #106

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 7 additions & 27 deletions src/components/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,40 +41,20 @@ module.exports = React.createClass({
};
},
componentWillReceiveProps: function(nextProps) {
if (nextProps.isOpen) {
setTimeout(() => this.handleAccessibility());
document.body.style.overflow = 'hidden';
} else {
setTimeout(() => this.removeAccessibilityHandlers());
if (nextProps.isOpen !== this.props.isOpen) {
if (nextProps.isOpen) {
setTimeout(() => this.handleAccessibility());
document.body.style.overflow = 'hidden';
} else {
setTimeout(() => this.removeAccessibilityHandlers());
}
}
},
handleAccessibility () {
// Remember the element that was focused before we opened the modal
// so we can return focus to it once we close the modal.
this.focusedElementBeforeModalOpened = document.activeElement;

// We're using a transition to reveal the modal,
// so wait until the element is visible, before
// finding the first keyboard focusable element
// and passing focus to it, otherwise the browser
// might scroll the document to reveal the element
// receiving focus
ally.when.visibleArea({
context: this.modalElement,
callback: function(context) {
// the modal is visible on screen, so find the first
// keyboard focusable element (giving any element with
// autofocus attribute precendence). If the modal does
// not contain any keyboard focusabe elements, focus will
// be given to the modal itself.
var element = ally.query.firstTabbable({
context: context,
defaultToContext: true,
});
element.focus();
},
});

// Make sure that no element outside of the modal
// can be interacted with while the modal is visible.
this.disabledHandle = ally.maintain.disabled({
Expand Down