1
1
The ` a11y ` package provides a number of tools to improve accessibility, described below.
2
2
3
- ### ListKeyManager
3
+ ## ListKeyManager
4
4
` ListKeyManager ` manages the active option in a list of items based on keyboard interaction.
5
5
Intended to be used with components that correspond to a ` role="menu" ` or ` role="listbox" ` pattern.
6
6
7
- #### Basic usage
7
+ ### Basic usage
8
8
Any component that uses a ` ListKeyManager ` will generally do three things:
9
9
* Create a ` @ViewChildren ` query for the options being managed.
10
10
* Initialize the ` ListKeyManager ` , passing in the options.
@@ -18,16 +18,16 @@ interface ListKeyManagerOption {
18
18
}
19
19
```
20
20
21
- #### Wrapping
21
+ ### Wrapping
22
22
Navigation through options can be made to wrap via the ` withWrap ` method
23
23
``` ts
24
24
this .keyManager = new FocusKeyManager (... ).withWrap ();
25
25
```
26
26
27
- #### Types of key managers
27
+ ### Types of key managers
28
28
There are two varieties of ` ListKeyManager ` , ` FocusKeyManager ` and ` ActiveDescendantKeyManager ` .
29
29
30
- ##### FocusKeyManager
30
+ #### FocusKeyManager
31
31
Used when options will directly receive browser focus. Each item managed must implement the
32
32
` FocusableOption ` interface:
33
33
``` ts
@@ -36,7 +36,7 @@ interface FocusableOption extends ListKeyManagerOption {
36
36
}
37
37
```
38
38
39
- ##### ActiveDescendantKeyManager
39
+ #### ActiveDescendantKeyManager
40
40
Used when options will be marked as active via ` aria-activedescendant ` .
41
41
Each item managed must implement the
42
42
` Highlightable ` interface:
@@ -50,15 +50,15 @@ interface Highlightable extends ListKeyManagerOption {
50
50
Each item must also have an ID bound to the listbox's or menu's ` aria-activedescendant ` .
51
51
52
52
53
- ### FocusTrap
53
+ ## FocusTrap
54
54
The ` cdkTrapFocus ` directive traps <kbd >Tab</kbd > key focus within an element. This is intended to
55
55
be used to create accessible experience for components like
56
56
[ modal dialogs] ( https://www.w3.org/TR/wai-aria-practices-1.1/#dialog_modal ) , where focus must be
57
57
constrained.
58
58
59
59
This directive is declared in ` A11yModule ` .
60
60
61
- #### Example
61
+ ### Example
62
62
``` html
63
63
<div class =" my-inner-dialog-content" cdkTrapFocus >
64
64
<!-- Tab and Shift + Tab will not leave this element. -->
@@ -68,7 +68,7 @@ This directive is declared in `A11yModule`.
68
68
This directive will not prevent focus from moving out of the trapped region due to mouse
69
69
interaction.
70
70
71
- #### Regions
71
+ ### Regions
72
72
Regions can be declared explicitly with an initial focus element by using
73
73
the ` cdkFocusRegionStart ` , ` cdkFocusRegionEnd ` and ` cdkFocusInitial ` DOM attributes.
74
74
` cdkFocusInitial ` specifies the element that will receive focus upon initialization of the region.
@@ -85,18 +85,18 @@ For example:
85
85
```
86
86
87
87
88
- ### InteractivityChecker
88
+ ## InteractivityChecker
89
89
` InteractivityChecker ` is used to check the interactivity of an element, capturing disabled,
90
90
visible, tabbable, and focusable states for accessibility purposes. See the API docs for more
91
91
details.
92
92
93
93
94
- ### LiveAnnouncer
94
+ ## LiveAnnouncer
95
95
` LiveAnnouncer ` is used to announce messages for screen-reader users using an ` aria-live ` region.
96
96
See [ the W3C's WAI-ARIA] ( https://www.w3.org/TR/wai-aria/states_and_properties#aria-live )
97
97
for more information on aria-live regions.
98
98
99
- #### Example
99
+ ### Example
100
100
``` ts
101
101
@Component ({... })
102
102
export class MyComponent {
@@ -106,3 +106,55 @@ export class MyComponent {
106
106
}
107
107
}
108
108
```
109
+
110
+ ## FocusMonitor
111
+ The ` FocusMonitor ` is an injectable service that can be used to listen for changes in the focus
112
+ state of an element. It's more powerful than just listening for ` focus ` or ` blur ` events because it
113
+ tells you how the element was focused (via mouse, keyboard, touch, or programmatically). It also
114
+ allows listening for focus on descendant elements if desired.
115
+
116
+ To listen for focus changes on an element, use the ` monitor ` method which takes an element to
117
+ monitor and an optional boolean flag ` checkChildren ` . Passing true for ` checkChildren ` will tell the
118
+ ` FocusMonitor ` to consider the element focused if any of its descendants are focused. This option
119
+ defaults to ` false ` if not specified. The ` monitor ` method will return an Observable that emits the
120
+ ` FocusOrigin ` whenever the focus state changes. The ` FocusOrigin ` will be one of the following:
121
+
122
+ * ` 'mouse' ` indicates the element was focused with the mouse
123
+ * ` 'keyboard' ` indicates the element was focused with the keyboard
124
+ * ` 'touch' ` indicates the element was focused by touching on a touchscreen
125
+ * ` 'program' ` indicates the element was focused programmatically
126
+ * ` null ` indicates the element was blurred
127
+
128
+ In addition to emitting on the observable, the ` FocusMonitor ` will automatically apply CSS classes
129
+ to the element when focused. It will add ` .cdk-focused ` if the element is focused and will further
130
+ add ` .cdk-${origin}-focused ` (with ` ${origin} ` being ` mouse ` , ` keyboard ` , ` touch ` , or ` program ` ) to
131
+ indicate how the element was focused.
132
+
133
+ Note: currently the ` FocusMonitor ` emits on the observable _ outside_ of the Angular zone. Therefore
134
+ if you ` markForCheck ` in the subscription you must put yourself back in the Angular zone.
135
+
136
+ ``` ts
137
+ focusMonitor .monitor (el ).subscribe (origin => this .ngZone .run (() => /* ... */ ));
138
+ ```
139
+
140
+ Any element that is monitored by calling ` monitor ` should eventually be unmonitored by calling
141
+ ` stopMonitoring ` with the same element.
142
+
143
+ <!-- example(focus-monitor-overview) -->
144
+
145
+ It is possible to falsify the ` FocusOrigin ` when setting the focus programmatically by using the
146
+ ` focusVia ` method of ` FocusMonitor ` . This method accepts an element to focus and the ` FocusOrigin `
147
+ to use. If the element being focused is currently being monitored by the ` FocusMonitor ` it will
148
+ report the ` FocusOrigin ` that was passed in. If the element is not currently being monitored it will
149
+ just be focused like normal.
150
+
151
+ <!-- example(focus-monitor-focus-via) -->
152
+
153
+ ### cdkMonitorElementFocus and cdkMonitorSubtreeFocus
154
+ For convenience, the CDK also provides two directives that allow for easily monitoring an element.
155
+ ` cdkMonitorElementFocus ` is the equivalent of calling ` monitor ` on the host element with
156
+ ` checkChildren ` set to ` false ` . ` cdkMonitorSubtreeFocus ` is the equivalent of calling ` monitor ` on
157
+ the host element with ` checkChildren ` set to ` true ` . Each of these directives has an ` @Output() `
158
+ ` cdkFocusChange ` that will emit the new ` FocusOrigin ` whenever it changes.
159
+
160
+ <!-- example(focus-monitor-directives) -->
0 commit comments