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

Commit 7051c21

Browse files
fix(modal): Improve ARIA support by adding a live region.
chore(build): Fix package.json so grunt-cli does not need to be globally installed. chore(demo): Add command to run demo locally.
1 parent 9881a27 commit 7051c21

File tree

5 files changed

+69
-8
lines changed

5 files changed

+69
-8
lines changed

Diff for: .travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ addons:
1313
before_install:
1414
- export DISPLAY=:99.0
1515
- sh -e /etc/init.d/xvfb start
16-
- npm install --quiet -g grunt-cli karma
16+
- npm install --quiet -g karma
1717

1818
script: grunt
1919
sudo: false

Diff for: package.json

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
],
1616
"main": "index.js",
1717
"scripts": {
18+
"demo": "grunt after-test && static dist -a 0.0.0.0 -H '{\"Cache-Control\": \"no-cache, must-revalidate\"}'",
1819
"test": "grunt"
1920
},
2021
"repository": {
@@ -26,6 +27,7 @@
2627
"angular-mocks": "1.5.8",
2728
"angular-sanitize": "1.5.8",
2829
"grunt": "^0.4.5",
30+
"grunt-cli": "^1.2.0",
2931
"grunt-contrib-concat": "^1.0.0",
3032
"grunt-contrib-copy": "^1.0.0",
3133
"grunt-contrib-uglify": "^1.0.1",
@@ -44,6 +46,7 @@
4446
"load-grunt-tasks": "^3.3.0",
4547
"lodash": "^4.1.0",
4648
"marked": "^0.3.5",
49+
"node-static": "^0.7.8",
4750
"semver": "^5.0.1",
4851
"shelljs": "^0.6.0"
4952
},

Diff for: src/modal/docs/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
`$uibModal` is a service to create modal windows.
2-
Creating modals is straightforward: create a template, a controller and reference them when using `$uibModal`.
2+
Creating modals is straightforward: create a template and controller, and reference them when using `$uibModal`.
33

44
The `$uibModal` service has only one method: `open(options)`.
55

Diff for: src/modal/modal.js

+51-3
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap', 'ui.bootstrap.p
163163
// {@link Attribute#$observe} on it. For more details please see {@link TableColumnResize}.
164164
scope.$isRendered = true;
165165

166-
// Deferred object that will be resolved when this modal is render.
166+
// Deferred object that will be resolved when this modal is rendered.
167167
var modalRenderDeferObj = $q.defer();
168168
// Resolve render promise post-digest
169169
scope.$$postDigest(function() {
@@ -196,7 +196,7 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap', 'ui.bootstrap.p
196196

197197
/**
198198
* If something within the freshly-opened modal already has focus (perhaps via a
199-
* directive that causes focus). then no need to try and focus anything.
199+
* directive that causes focus) then there's no need to try to focus anything.
200200
*/
201201
if (!($document[0].activeElement && element[0].contains($document[0].activeElement))) {
202202
var inputWithAutofocus = element[0].querySelector('[autofocus]');
@@ -254,6 +254,7 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap', 'ui.bootstrap.p
254254
};
255255
var topModalIndex = 0;
256256
var previousTopOpenedModal = null;
257+
var ARIA_HIDDEN_ATTRIBUTE_NAME = 'data-bootstrap-modal-aria-hidden-count';
257258

258259
//Modal focus behavior
259260
var tabbableSelector = 'a[href], area[href], input:not([disabled]):not([tabindex=\'-1\']), ' +
@@ -529,6 +530,7 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap', 'ui.bootstrap.p
529530
'role': 'dialog',
530531
'aria-labelledby': modal.ariaLabelledBy,
531532
'aria-describedby': modal.ariaDescribedBy,
533+
'aria-live': 'polite',
532534
'size': modal.size,
533535
'index': topModalIndex,
534536
'animate': 'animate',
@@ -555,20 +557,65 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap', 'ui.bootstrap.p
555557

556558
openedWindows.top().value.modalDomEl = angularDomEl;
557559
openedWindows.top().value.modalOpener = modalOpener;
560+
561+
applyAriaHidden(angularDomEl[0]);
562+
563+
function applyAriaHidden(el) {
564+
if (!el || el.tagName === 'BODY') {
565+
return;
566+
}
567+
568+
getSiblings(el).forEach(function(sibling) {
569+
var elemIsAlreadyHidden = sibling.getAttribute('aria-hidden') === 'true',
570+
ariaHiddenCount =
571+
parseInt(sibling.getAttribute(ARIA_HIDDEN_ATTRIBUTE_NAME), 10) ||
572+
elemIsAlreadyHidden ? 1 : 0;
573+
574+
sibling.setAttribute(ARIA_HIDDEN_ATTRIBUTE_NAME, ariaHiddenCount + 1);
575+
sibling.setAttribute('aria-hidden', 'true');
576+
});
577+
578+
return applyAriaHidden(el.parentElement);
579+
580+
function getSiblings(el) {
581+
var children = el.parentElement ? el.parentElement.children : [];
582+
583+
return Array.prototype.filter.call(children, function(child) {
584+
return child !== el;
585+
});
586+
}
587+
}
558588
};
559589

560590
function broadcastClosing(modalWindow, resultOrReason, closing) {
561591
return !modalWindow.value.modalScope.$broadcast('modal.closing', resultOrReason, closing).defaultPrevented;
562592
}
563593

564594
$modalStack.close = function(modalInstance, result) {
565-
var modalWindow = openedWindows.get(modalInstance);
595+
var modalWindow;
596+
597+
Array.prototype.forEach.call(
598+
document.querySelectorAll('[' + ARIA_HIDDEN_ATTRIBUTE_NAME + ']'),
599+
function(hiddenEl) {
600+
var ariaHiddenCount = parseInt(hiddenEl.getAttribute(ARIA_HIDDEN_ATTRIBUTE_NAME), 10),
601+
newHiddenCount = ariaHiddenCount - 1;
602+
hiddenEl.setAttribute(ARIA_HIDDEN_ATTRIBUTE_NAME, newHiddenCount);
603+
604+
if (!newHiddenCount) {
605+
hiddenEl.removeAttribute(ARIA_HIDDEN_ATTRIBUTE_NAME);
606+
hiddenEl.removeAttribute('aria-hidden');
607+
}
608+
}
609+
);
610+
611+
modalWindow = openedWindows.get(modalInstance);
566612
if (modalWindow && broadcastClosing(modalWindow, result, true)) {
567613
modalWindow.value.modalScope.$$uibDestructionScheduled = true;
568614
modalWindow.value.deferred.resolve(result);
569615
removeModalWindow(modalInstance, modalWindow.value.modalOpener);
570616
return true;
571617
}
618+
572619
return !modalWindow;
573620
};
574621

@@ -596,6 +643,7 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap', 'ui.bootstrap.p
596643

597644
$modalStack.modalRendered = function(modalInstance) {
598645
var modalWindow = openedWindows.get(modalInstance);
646+
$modalStack.focusFirstFocusableElement($modalStack.loadFocusElementList(modalWindow));
599647
if (modalWindow) {
600648
modalWindow.value.renderDeferred.resolve();
601649
}

Diff for: src/modal/test/modal.spec.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ describe('$uibModal', function() {
526526

527527
var modal = open({template: '<div>Content<button>inside modal</button></div>'});
528528
$rootScope.$digest();
529-
expect(document.activeElement.tagName).toBe('DIV');
529+
expect(document.activeElement.tagName).toBe('BUTTON');
530530
expect($document).toHaveModalsOpen(1);
531531

532532
triggerKeyDown($document, 27);
@@ -656,7 +656,7 @@ describe('$uibModal', function() {
656656
it('should not focus on the element that has autofocus attribute when the modal is opened and something in the modal already has focus and the animations have finished', function() {
657657
function openAndCloseModalWithAutofocusElement() {
658658

659-
var modal = open({template: '<div><input type="text" id="auto-focus-element" autofocus><input type="text" id="pre-focus-element" focus-me></div>'});
659+
var modal = open({template: '<div><input type="text" id="pre-focus-element" focus-me><input type="text" id="auto-focus-element" autofocus></div>'});
660660
$rootScope.$digest();
661661
expect(angular.element('#auto-focus-element')).not.toHaveFocus();
662662
expect(angular.element('#pre-focus-element')).toHaveFocus();
@@ -698,7 +698,7 @@ describe('$uibModal', function() {
698698
$rootScope.$digest();
699699
$animate.flush();
700700

701-
expect(document.activeElement.tagName).toBe('DIV');
701+
expect(document.activeElement.tagName).toBe('INPUT');
702702

703703
close(modal, 'closed ok');
704704

@@ -1586,6 +1586,16 @@ describe('$uibModal', function() {
15861586
expect($document.find('.modal').attr('aria-describedby')).toEqual('modal-description');
15871587
});
15881588
});
1589+
1590+
describe('ariaLive', function() {
1591+
it('should add the aria-live property to the modal', function() {
1592+
open({
1593+
template: '<p>Modal content</p>'
1594+
});
1595+
1596+
expect($document.find('.modal').attr('aria-live')).toEqual('polite');
1597+
});
1598+
});
15891599
});
15901600

15911601
describe('modal window', function() {

0 commit comments

Comments
 (0)