@@ -12,14 +12,23 @@ assistive technologies used by persons with disabilities.
12
12
13
13
##Including ngAria
14
14
15
- Using ngAria is as simple as requiring the ngAria module in your application. ngAria hooks into
15
+ Using {@link ngAria ngAria} is as simple as requiring the ngAria module in your application. ngAria hooks into
16
16
standard AngularJS directives and quietly injects accessibility support into your application
17
17
at runtime.
18
18
19
19
```js
20
20
angular.module('myApp', ['ngAria'])...
21
21
```
22
22
23
+ ###Using ngAria
24
+ Most of what ngAria does is only visible "under the hood". To see the module in action, once you've
25
+ added it as a dependency, you can test a few things:
26
+ * Using your favorite element inspector, look for ngAria attributes in your own code.
27
+ * Test using your keyboard to ensure `tabindex` is used correctly.
28
+ * Fire up a screen reader such as VoiceOver to listen for ARIA support.
29
+ [Helpful screen reader tips.](http://webaim.org/articles/screenreader_testing/)
30
+
31
+ ##Supported directives
23
32
Currently, ngAria interfaces with the following directives:
24
33
25
34
* <a href="#ngmodel">ngModel</a>
@@ -31,7 +40,7 @@ Currently, ngAria interfaces with the following directives:
31
40
32
41
<h2 id="ngmodel">ngModel</h2>
33
42
34
- Most of ngAria's heavy lifting happens in the [ ngModel](https://docs.angularjs.org/api/ng/directive/ ngModel)
43
+ Most of ngAria's heavy lifting happens in the {@link ngModel ngModel}
35
44
directive. For elements using ngModel, special attention is paid by ngAria if that element also
36
45
has a a role or type of `checkbox`, `radio`, `range` or `textbox`.
37
46
@@ -47,15 +56,76 @@ attributes (if they have not been explicitly specified by the developer):
47
56
48
57
###Example
49
58
50
- ```html
51
- <md-checkbox ng-model="val" required>
52
- ```
53
-
54
- Becomes:
55
-
56
- ```html
57
- <md-checkbox ng-model="val" required aria-required="true" tabIndex="0">
58
- ```
59
+ <example module="ngAria_ngModelExample" deps="angular-aria.js">
60
+ <file name="index.html">
61
+ <style>
62
+ [role=checkbox] {
63
+ cursor: pointer;
64
+ display: block;
65
+ }
66
+ [role=checkbox] .icon:before {
67
+ content: '\2610';
68
+ display: inline-block;
69
+ font-size: 2em;
70
+ line-height: 1;
71
+ vertical-align: middle;
72
+ speak: none;
73
+ }
74
+ [role=checkbox].active .icon:before {
75
+ content: '\2611';
76
+ }
77
+ pre {
78
+ white-space: pre-wrap;
79
+ }
80
+ </style>
81
+ <div>
82
+ <form ng-controller="formsController">
83
+ <some-checkbox role="checkbox" ng-model="checked" ng-class="{active: checked}"
84
+ ng-disabled="isDisabled" ng-click="toggleCheckbox()"
85
+ required aria-label="Custom Checkbox" show-attrs>
86
+ <span class="icon" aria-hidden="true"></span>
87
+ Custom Checkbox
88
+ </some-checkbox>
89
+ </form>
90
+ </div>
91
+ <script>
92
+ var app = angular.module('ngAria_ngModelExample', ['ngAria'])
93
+ .controller('formsController', function($scope){
94
+ $scope.checked = false;
95
+ $scope.toggleCheckbox = function(){
96
+ $scope.checked = !$scope.checked;
97
+ }
98
+ })
99
+ .directive('someCheckbox', function(){
100
+ return {
101
+ restrict: 'E',
102
+ link: function($scope, $el, $attrs) {
103
+ $scope.$on('keypress', function(event){
104
+ event.preventDefault();
105
+ });
106
+ }
107
+ }
108
+ })
109
+ .directive('showAttrs', function() {
110
+ return function($scope, $el, $attrs) {
111
+ var pre = document.createElement('pre');
112
+ $el.after(pre);
113
+ $scope.$watch(function() {
114
+ var $attrs = {};
115
+ Array.prototype.slice.call($el[0].attributes, 0).forEach(function(item) {
116
+ if (item.name !== 'show-$attrs') {
117
+ $attrs[item.name] = item.value;
118
+ }
119
+ });
120
+ return $attrs;
121
+ }, function(newAttrs, oldAttrs) {
122
+ pre.innerText = JSON.stringify(newAttrs, null, 2);
123
+ }, true);
124
+ }
125
+ });
126
+ </script>
127
+ </file>
128
+ </example>
59
129
60
130
ngAria will also add `tabIndex`, ensuring custom elements with these roles will be reachable from
61
131
the keyboard. It is still up to **you** as a developer to **ensure custom controls will be
@@ -106,6 +176,24 @@ screen reader users won't accidentally focus on "mystery elements". Managing tab
106
176
child control can be complex and affect performance, so it’s best to just stick with the default
107
177
`display: none` CSS. See the [fourth rule of ARIA use](http://www.w3.org/TR/aria-in-html/#fourth-rule-of-aria-use).
108
178
179
+ ###Example
180
+ ```css
181
+ .ng-hide {
182
+ display: block;
183
+ opacity: 0;
184
+ }
185
+ ```
186
+ ```html
187
+ <div ng-show="false" class="ng-hide" aria-hidden="true"></div>
188
+ ```
189
+
190
+ Becomes:
191
+
192
+ ```html
193
+ <div ng-show="true" aria-hidden="false"></div>
194
+ ```
195
+ *Note: Child links, buttons or other interactive controls must also be removed from the tab order.*
196
+
109
197
<h2 id="nghide">ngHide</h2>
110
198
111
199
>The [ngHide](https://docs.angularjs.org/api/ng/directive/ngHide) directive shows or hides the
@@ -116,33 +204,125 @@ The default CSS for `ngHide`, the inverse method to `ngShow`, makes ngAria redun
116
204
`aria-hidden` on the directive when it is hidden or shown, but the content is already hidden with
117
205
`display: none`. See explanation for <a href="#ngshow">ngShow</a> when overriding the default CSS.
118
206
119
- <h2 id="ngclick">ngClick and ngDblClick</h2>
120
- If `ngClick` or `ngDblClick` is encountered, ngAria will add `tabIndex` if it isn't there already.
121
- Even with this, you must currently still add `ng-keypress` to non-interactive elements such as
122
- `<div>` or `<taco-button>` to enable keyboard access. Conversation is
123
- [currently ongoing](https://github.com/angular/angular.js/issues/9254) about whether ngAria
124
- should also bind `ng-keypress` to be more useful.
207
+ <h2 id="ngclick-and-ngdblclick">ngClick and ngDblclick</h2>
208
+ If `ng-click` or `ng-dblclick` is encountered, ngAria will add `tabindex` if it isn't there already.
209
+ Even with this, you must currently still add `ng-keypress` to non-interactive elements such as `div`
210
+ or `taco-button` to enable keyboard access. Conversation is currently ongoing about whether ngAria
211
+ should also bind `ng-keypress`.
212
+
213
+ <h3>Example</h3>
214
+ ```html
215
+ <div ng-click="toggleMenu()"></div>
216
+ ```
217
+
218
+ Becomes:
219
+ ```html
220
+ <div ng-click="toggleMenu()" tabindex="0"></div>
221
+ ```
222
+ *Note: ngAria still requires `ng-keypress` to be added manually to non-native controls like divs.*
125
223
126
224
<h2 id="ngmessages">ngMessages</h2>
127
225
128
226
The new ngMessages module makes it easy to display form validation or other messages with priority
129
227
sequencing and animation. To expose these visual messages to screen readers,
130
228
ngAria injects `aria-live="polite"`, causing them to be read aloud any time a message is shown,
131
229
regardless of the user's focus location.
230
+ ###Example
231
+
232
+ ```html
233
+ <div ng-messages="myForm.myName.$error">
234
+ <div ng-message="required">You did not enter a field</div>
235
+ <div ng-message="maxlength">Your field is too long</div>
236
+ </div>
237
+ ```
132
238
133
- * * *
239
+ Becomes:
240
+
241
+ ```html
242
+ <div ng-messages="myForm.myName.$error" aria-live="polite">
243
+ <div ng-message="required">You did not enter a field</div>
244
+ <div ng-message="maxlength">Your field is too long</div>
245
+ </div>
246
+ ```
134
247
135
248
##Disabling attributes
136
249
The attribute magic of ngAria may not work for every scenario. To disable individual attributes,
137
- you can use the {@link ngAria.$ariaProvider#config config} method:
138
-
139
- ```
140
- angular.module('myApp', ['ngAria'], function config($ariaProvider) {
141
- $ariaProvider.config({
142
- tabindex: false
250
+ you can use the {@link ngAria.$ariaProvider#config config} method. Just keep in mind this will
251
+ tell ngAria to ignore the attribute globally.
252
+
253
+ <example module="ngAria_ngDisabledExample" deps="angular-aria.js">
254
+ <file name="index.html">
255
+ <style>
256
+ [role=checkbox] {
257
+ cursor: pointer;
258
+ display: block;
259
+ }
260
+ [role=checkbox] .icon:before {
261
+ content: '\2610';
262
+ display: inline-block;
263
+ font-size: 2em;
264
+ line-height: 1;
265
+ vertical-align: middle;
266
+ speak: none;
267
+ }
268
+ [role=checkbox].active .icon:before {
269
+ content: '\2611';
270
+ }
271
+ </style>
272
+ <form ng-controller="formsController">
273
+ <div ng-model="someModel" show-attrs>
274
+ Div with ngModel and aria-invalid disabled
275
+ </div>
276
+ <div role="checkbox" ng-model="checked" ng-class="{active: checked}"
277
+ aria-label="Custom Checkbox" ng-click="toggleCheckbox()"
278
+ ng-keypress="toggleCheckbox()" some-checkbox show-attrs>
279
+ <span class="icon" aria-hidden="true"></span>
280
+ Custom Checkbox for comparison
281
+ </div>
282
+ </form>
283
+ <script>
284
+ angular.module('ngAria_ngDisabledExample', ['ngAria'], function config($ariaProvider) {
285
+ $ariaProvider.config({
286
+ ariaInvalid: false,
287
+ tabindex: true
288
+ });
289
+ })
290
+ .controller('formsController', function($scope){
291
+ $scope.checked = false;
292
+ $scope.toggleCheckbox = function(){
293
+ $scope.checked = !$scope.checked;
294
+ }
295
+ })
296
+ .directive('someCheckbox', function(){
297
+ return {
298
+ restrict: 'A',
299
+ link: function($scope, $el, $attrs) {
300
+ $scope.$on('keypress', function(event){
301
+ event.preventDefault();
302
+ });
303
+ }
304
+ }
305
+ })
306
+ .directive('showAttrs', function() {
307
+ return function(scope, el, attrs) {
308
+ var pre = document.createElement('pre');
309
+ el.after(pre);
310
+ scope.$watch(function() {
311
+ var attrs = {};
312
+ Array.prototype.slice.call(el[0].attributes, 0).forEach(function(item) {
313
+ if (item.name !== 'show-attrs') {
314
+ attrs[item.name] = item.value;
315
+ }
316
+ });
317
+ return attrs;
318
+ }, function(newAttrs, oldAttrs) {
319
+ pre.innerText = JSON.stringify(newAttrs, null, 2);
320
+ }, true);
321
+ }
143
322
});
144
- });
145
- ```
323
+ </script>
324
+ </file>
325
+ </example>
146
326
147
327
##Common Accessibility Patterns
148
328
@@ -171,6 +351,7 @@ Accessibility best practices that apply to web apps in general also apply to Ang
171
351
172
352
* [Using ARIA in HTML](http://www.w3.org/TR/aria-in-html/)
173
353
* [AngularJS Accessibility at ngEurope](https://www.youtube.com/watch?v=dmYDggEgU-s&list=UUEGUP3TJJfMsEM_1y8iviSQ)
354
+ * [Testing with Screen Readers](http://webaim.org/articles/screenreader_testing/)
174
355
* [Chrome Accessibility Developer Tools](https://chrome.google.com/webstore/detail/accessibility-developer-t/fpkknkljclfencbdbgkenhalefipecmb?hl=en)
175
356
* [W3C Accessibility Testing](http://www.w3.org/wiki/Accessibility_testing)
176
357
* [WebAIM](http://webaim.org)
0 commit comments