1
1
The ` a11y ` package provides a number of tools to improve accessibility, described below.
2
2
3
- ## ListKeyManager
3
+ ### ListKeyManager
4
+
4
5
` ListKeyManager ` manages the active option in a list of items based on keyboard interaction.
5
6
Intended to be used with components that correspond to a ` role="menu" ` or ` role="listbox" ` pattern.
6
7
7
- ### Basic usage
8
+ #### Basic usage
9
+
8
10
Any component that uses a ` ListKeyManager ` will generally do three things:
9
11
* Create a ` @ViewChildren ` query for the options being managed.
10
12
* Initialize the ` ListKeyManager ` , passing in the options.
@@ -18,16 +20,19 @@ interface ListKeyManagerOption {
18
20
}
19
21
```
20
22
21
- ### Wrapping
23
+ #### Wrapping
24
+
22
25
Navigation through options can be made to wrap via the ` withWrap ` method
23
26
``` ts
24
27
this .keyManager = new FocusKeyManager (... ).withWrap ();
25
28
```
26
29
27
- ### Types of key managers
30
+ #### Types of key managers
31
+
28
32
There are two varieties of ` ListKeyManager ` , ` FocusKeyManager ` and ` ActiveDescendantKeyManager ` .
29
33
30
- #### FocusKeyManager
34
+ ##### FocusKeyManager
35
+
31
36
Used when options will directly receive browser focus. Each item managed must implement the
32
37
` FocusableOption ` interface:
33
38
``` ts
@@ -36,7 +41,8 @@ interface FocusableOption extends ListKeyManagerOption {
36
41
}
37
42
```
38
43
39
- #### ActiveDescendantKeyManager
44
+ ##### ActiveDescendantKeyManager
45
+
40
46
Used when options will be marked as active via ` aria-activedescendant ` .
41
47
Each item managed must implement the
42
48
` Highlightable ` interface:
@@ -50,15 +56,17 @@ interface Highlightable extends ListKeyManagerOption {
50
56
Each item must also have an ID bound to the listbox's or menu's ` aria-activedescendant ` .
51
57
52
58
53
- ## FocusTrap
59
+ ### FocusTrap
60
+
54
61
The ` cdkTrapFocus ` directive traps <kbd >Tab</kbd > key focus within an element. This is intended to
55
62
be used to create accessible experience for components like
56
63
[ modal dialogs] ( https://www.w3.org/TR/wai-aria-practices-1.1/#dialog_modal ) , where focus must be
57
64
constrained.
58
65
59
66
This directive is declared in ` A11yModule ` .
60
67
61
- ### Example
68
+ #### Example
69
+
62
70
``` html
63
71
<div class =" my-inner-dialog-content" cdkTrapFocus >
64
72
<!-- Tab and Shift + Tab will not leave this element. -->
@@ -68,7 +76,8 @@ This directive is declared in `A11yModule`.
68
76
This directive will not prevent focus from moving out of the trapped region due to mouse
69
77
interaction.
70
78
71
- ### Regions
79
+ #### Regions
80
+
72
81
Regions can be declared explicitly with an initial focus element by using
73
82
the ` cdkFocusRegionStart ` , ` cdkFocusRegionEnd ` and ` cdkFocusInitial ` DOM attributes.
74
83
` cdkFocusInitial ` specifies the element that will receive focus upon initialization of the region.
@@ -88,18 +97,21 @@ For example:
88
97
will happen unless you've enabled the ` cdkTrapFocusAutoCapture ` option as well. This is due to
89
98
` CdkTrapFocus ` not capturing focus on initialization by default.
90
99
91
- ## InteractivityChecker
100
+ ### InteractivityChecker
101
+
92
102
` InteractivityChecker ` is used to check the interactivity of an element, capturing disabled,
93
103
visible, tabbable, and focusable states for accessibility purposes. See the API docs for more
94
104
details.
95
105
96
106
97
- ## LiveAnnouncer
107
+ ### LiveAnnouncer
108
+
98
109
` LiveAnnouncer ` is used to announce messages for screen-reader users using an ` aria-live ` region.
99
110
See [ the W3C's WAI-ARIA] ( https://www.w3.org/WAI/PF/aria-1.1/states_and_properties#aria-live )
100
111
for more information on aria-live regions.
101
112
102
- ### Example
113
+ #### Example
114
+
103
115
``` ts
104
116
@Component ({... })
105
117
export class MyComponent {
@@ -110,10 +122,11 @@ export class MyComponent {
110
122
}
111
123
```
112
124
113
- ## FocusMonitor
125
+ ### FocusMonitor
126
+
114
127
The ` FocusMonitor ` is an injectable service that can be used to listen for changes in the focus
115
128
state of an element. It's more powerful than just listening for ` focus ` or ` blur ` events because it
116
- tells you how the element was focused (via mouse, keyboard, touch, or programmatically). It also
129
+ tells you how the element was focused (via the mouse, keyboard, touch, or programmatically). It also
117
130
allows listening for focus on descendant elements if desired.
118
131
119
132
To listen for focus changes on an element, use the ` monitor ` method which takes an element to
@@ -133,7 +146,7 @@ to the element when focused. It will add `.cdk-focused` if the element is focuse
133
146
add ` .cdk-${origin}-focused ` (with ` ${origin} ` being ` mouse ` , ` keyboard ` , ` touch ` , or ` program ` ) to
134
147
indicate how the element was focused.
135
148
136
- Note: currently the ` FocusMonitor ` emits on the observable _ outside_ of the Angular zone. Therefore
149
+ Note: currently the ` FocusMonitor ` emits on the observable _ outside_ of the Angular zone. Therefore,
137
150
if you ` markForCheck ` in the subscription you must put yourself back in the Angular zone.
138
151
139
152
``` ts
@@ -148,12 +161,13 @@ Any element that is monitored by calling `monitor` should eventually be unmonito
148
161
It is possible to falsify the ` FocusOrigin ` when setting the focus programmatically by using the
149
162
` focusVia ` method of ` FocusMonitor ` . This method accepts an element to focus and the ` FocusOrigin `
150
163
to use. If the element being focused is currently being monitored by the ` FocusMonitor ` it will
151
- report the ` FocusOrigin ` that was passed in. If the element is not currently being monitored it will
152
- just be focused like normal.
164
+ report the ` FocusOrigin ` that was passed in. If the element is not currently being monitored, it
165
+ will just be focused like normal.
153
166
154
167
<!-- example(focus-monitor-focus-via) -->
155
168
156
- ### cdkMonitorElementFocus and cdkMonitorSubtreeFocus
169
+ #### cdkMonitorElementFocus and cdkMonitorSubtreeFocus
170
+
157
171
For convenience, the CDK also provides two directives that allow for easily monitoring an element.
158
172
` cdkMonitorElementFocus ` is the equivalent of calling ` monitor ` on the host element with
159
173
` checkChildren ` set to ` false ` . ` cdkMonitorSubtreeFocus ` is the equivalent of calling ` monitor ` on
@@ -162,7 +176,8 @@ the host element with `checkChildren` set to `true`. Each of these directives ha
162
176
163
177
<!-- example(focus-monitor-directives) -->
164
178
165
- ## Styling utilities
179
+ ### Styling utilities
180
+
166
181
The CDK ` a11y ` package comes with a set of CSS styles that can be used when building accessible
167
182
components. To take advantage of them, you have to include the styles in your global stylesheet.
168
183
If you're using Material together with the CDK, these styles have been included for you already.
@@ -173,9 +188,10 @@ If you're using Material together with the CDK, these styles have been included
173
188
@include cdk-a11y ();
174
189
```
175
190
176
- ### Hiding elements, while keeping them available for screen readers
191
+ #### Hiding elements in an accessible way
192
+
177
193
By default, screen readers and other assistive technology will skip elements that have
178
- ` display: none ` , ` visibility: hidden ` etc. In some cases you may need to visually hide an element,
194
+ ` display: none ` , ` visibility: hidden ` , etc. In some cases you may need to visually hide an element,
179
195
while keeping it available for assistive technology. You can do so using the ` cdk-visually-hidden `
180
196
class:
181
197
@@ -185,7 +201,8 @@ class:
185
201
</div >
186
202
```
187
203
188
- ### Targeting high contrast users
204
+ #### Targeting high contrast users
205
+
189
206
The ` a11y ` package offers a mixin that allows you to target users that have the Windows high
190
207
contrast mode turned on. To target high contrast users, you can wrap your styles with the
191
208
` cdk-high-contrast ` mixin. The mixin works by targeting a CSS class which is added to the ` body `
0 commit comments