Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

fix(modal): keyboard=false should not stop trapping tab focus. #5005

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ template/**/*.js
.git
docs
misc
src
template
.editorconfig
.gitattributes
.gitignore
Expand All @@ -37,3 +35,6 @@ karma.conf.js
ROADMAP.md

dist/assets
dist/index.html
dist/versions-mapping.json
dist/*-SNAPSHOT*
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
"version": "1.0.0-SNAPSHOT",
"homepage": "http://angular-ui.github.io/bootstrap/",
"dependencies": {},
"directories": {
"lib": "src/"
},
"files": [
"dist/", "src/", "template/"
],
"scripts":{
"test": "grunt"
},
Expand Down
31 changes: 15 additions & 16 deletions src/modal/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap'])
};

function linkFn(scope, element, attrs) {
// Temporary fix for prefixing
element.addClass('modal-backdrop');

if (attrs.modalInClass) {
if ($animateCss) {
$animateCss(element, {
Expand Down Expand Up @@ -216,10 +213,10 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap'])

.directive('uibModalTransclude', function() {
return {
link: function($scope, $element, $attrs, controller, $transclude) {
$transclude($scope.$parent, function(clone) {
$element.empty();
$element.append(clone);
link: function(scope, element, attrs, controller, transclude) {
transclude(scope.$parent, function(clone) {
element.empty();
element.append(clone);
});
}
};
Expand Down Expand Up @@ -354,13 +351,15 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap'])
}

var modal = openedWindows.top();
if (modal && modal.value.keyboard) {
if (modal) {
switch (evt.which) {
case 27: {
evt.preventDefault();
$rootScope.$apply(function() {
$modalStack.dismiss(modal.key, 'escape key press');
});
if (modal.value.keyboard) {
evt.preventDefault();
$rootScope.$apply(function() {
$modalStack.dismiss(modal.key, 'escape key press');
});
}
break;
}
case 9: {
Expand Down Expand Up @@ -418,12 +417,12 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap'])
backdropScope = $rootScope.$new(true);
backdropScope.modalOptions = modal;
backdropScope.index = currBackdropIndex;
var angularBackgroundDomEl = angular.element('<div uib-modal-backdrop="modal-backdrop"></div>');
angularBackgroundDomEl.attr('backdrop-class', modal.backdropClass);
backdropDomEl = angular.element('<div uib-modal-backdrop="modal-backdrop"></div>');
backdropDomEl.attr('backdrop-class', modal.backdropClass);
if (modal.animation) {
angularBackgroundDomEl.attr('modal-animation', 'true');
backdropDomEl.attr('modal-animation', 'true');
}
backdropDomEl = $compile(angularBackgroundDomEl)(backdropScope);
$compile(backdropDomEl)(backdropScope);
$animate.enter(backdropDomEl, appendToElement);
}

Expand Down
40 changes: 40 additions & 0 deletions src/modal/test/modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,46 @@ describe('$uibModal', function () {

initialPage.remove();
});

it('should change focus to first element when tab key was pressed even if keyboard=false options is provided', function() {
var initialPage = angular.element('<a href="#" id="cannot-get-focus-from-modal">Outland link</a>');
angular.element(document.body).append(initialPage);
initialPage.focus();

open({
template:'<a href="#" id="tab-focus-link"><input type="text" id="tab-focus-input1"/><input type="text" id="tab-focus-input2"/>' +
'<button id="tab-focus-button">Open me!</button>',
keyboard: false
});
expect($document).toHaveModalsOpen(1);

var lastElement = angular.element(document.getElementById('tab-focus-button'));
lastElement.focus();
triggerKeyDown(lastElement, 9);
expect(document.activeElement.getAttribute('id')).toBe('tab-focus-link');

initialPage.remove();
});

it('should change focus to last element when shift+tab key is pressed even if keyboard=false options is provided', function() {
var initialPage = angular.element('<a href="#" id="cannot-get-focus-from-modal">Outland link</a>');
angular.element(document.body).append(initialPage);
initialPage.focus();

open({
template:'<a href="#" id="tab-focus-link"><input type="text" id="tab-focus-input1"/><input type="text" id="tab-focus-input2"/>' +
'<button id="tab-focus-button">Open me!</button>',
keyboard: false
});
expect($document).toHaveModalsOpen(1);

var lastElement = angular.element(document.getElementById('tab-focus-link'));
lastElement.focus();
triggerKeyDown(lastElement, 9, true);
expect(document.activeElement.getAttribute('id')).toBe('tab-focus-button');

initialPage.remove();
});
});

describe('default options can be changed in a provider', function() {
Expand Down
3 changes: 2 additions & 1 deletion template/modal/backdrop.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<div uib-modal-animation-class="fade"
<div class="modal-backdrop"
uib-modal-animation-class="fade"
modal-in-class="in"
ng-style="{'z-index': 1040 + (index && 1 || 0) + index*10}"
></div>