@@ -88,8 +88,13 @@ export class FocusTrap {
88
88
}
89
89
90
90
this . _ngZone . runOutsideAngular ( ( ) => {
91
- this . _startAnchor ! . addEventListener ( 'focus' , ( ) => this . focusLastTabbableElement ( ) ) ;
92
- this . _endAnchor ! . addEventListener ( 'focus' , ( ) => this . focusFirstTabbableElement ( ) ) ;
91
+ this . _startAnchor ! . addEventListener ( 'focus' , ( ) => {
92
+ this . focusLastTabbableElement ( ) ;
93
+ } ) ;
94
+
95
+ this . _endAnchor ! . addEventListener ( 'focus' , ( ) => {
96
+ this . focusFirstTabbableElement ( ) ;
97
+ } ) ;
93
98
94
99
if ( this . _element . parentNode ) {
95
100
this . _element . parentNode . insertBefore ( this . _startAnchor ! , this . _element ) ;
@@ -100,26 +105,38 @@ export class FocusTrap {
100
105
101
106
/**
102
107
* Waits for the zone to stabilize, then either focuses the first element that the
103
- * user specified, or the first tabbable element..
108
+ * user specified, or the first tabbable element.
109
+ * @returns Returns a promise that resolves with a boolean, depending
110
+ * on whether focus was moved successfuly.
104
111
*/
105
- focusInitialElementWhenReady ( ) {
106
- this . _executeOnStable ( ( ) => this . focusInitialElement ( ) ) ;
112
+ focusInitialElementWhenReady ( ) : Promise < boolean > {
113
+ return new Promise < boolean > ( resolve => {
114
+ this . _executeOnStable ( ( ) => resolve ( this . focusInitialElement ( ) ) ) ;
115
+ } ) ;
107
116
}
108
117
109
118
/**
110
119
* Waits for the zone to stabilize, then focuses
111
120
* the first tabbable element within the focus trap region.
121
+ * @returns Returns a promise that resolves with a boolean, depending
122
+ * on whether focus was moved successfuly.
112
123
*/
113
- focusFirstTabbableElementWhenReady ( ) {
114
- this . _executeOnStable ( ( ) => this . focusFirstTabbableElement ( ) ) ;
124
+ focusFirstTabbableElementWhenReady ( ) : Promise < boolean > {
125
+ return new Promise < boolean > ( resolve => {
126
+ this . _executeOnStable ( ( ) => resolve ( this . focusFirstTabbableElement ( ) ) ) ;
127
+ } ) ;
115
128
}
116
129
117
130
/**
118
131
* Waits for the zone to stabilize, then focuses
119
132
* the last tabbable element within the focus trap region.
133
+ * @returns Returns a promise that resolves with a boolean, depending
134
+ * on whether focus was moved successfuly.
120
135
*/
121
- focusLastTabbableElementWhenReady ( ) {
122
- this . _executeOnStable ( ( ) => this . focusLastTabbableElement ( ) ) ;
136
+ focusLastTabbableElementWhenReady ( ) : Promise < boolean > {
137
+ return new Promise < boolean > ( resolve => {
138
+ this . _executeOnStable ( ( ) => resolve ( this . focusLastTabbableElement ( ) ) ) ;
139
+ } ) ;
123
140
}
124
141
125
142
/**
@@ -146,30 +163,47 @@ export class FocusTrap {
146
163
markers [ markers . length - 1 ] : this . _getLastTabbableElement ( this . _element ) ;
147
164
}
148
165
149
- /** Focuses the element that should be focused when the focus trap is initialized. */
150
- focusInitialElement ( ) {
151
- let redirectToElement = this . _element . querySelector ( '[cdk-focus-initial]' ) as HTMLElement ;
166
+ /**
167
+ * Focuses the element that should be focused when the focus trap is initialized.
168
+ * @returns Returns whether focus was moved successfuly.
169
+ */
170
+ focusInitialElement ( ) : boolean {
171
+ const redirectToElement = this . _element . querySelector ( '[cdk-focus-initial]' ) as HTMLElement ;
172
+
152
173
if ( redirectToElement ) {
153
174
redirectToElement . focus ( ) ;
154
- } else {
155
- this . focusFirstTabbableElement ( ) ;
175
+ return true ;
156
176
}
177
+
178
+ return this . focusFirstTabbableElement ( ) ;
157
179
}
158
180
159
- /** Focuses the first tabbable element within the focus trap region. */
160
- focusFirstTabbableElement ( ) {
161
- let redirectToElement = this . _getRegionBoundary ( 'start' ) ;
181
+ /**
182
+ * Focuses the first tabbable element within the focus trap region.
183
+ * @returns Returns whether focus was moved successfuly.
184
+ */
185
+ focusFirstTabbableElement ( ) : boolean {
186
+ const redirectToElement = this . _getRegionBoundary ( 'start' ) ;
187
+
162
188
if ( redirectToElement ) {
163
189
redirectToElement . focus ( ) ;
164
190
}
191
+
192
+ return ! ! redirectToElement ;
165
193
}
166
194
167
- /** Focuses the last tabbable element within the focus trap region. */
168
- focusLastTabbableElement ( ) {
169
- let redirectToElement = this . _getRegionBoundary ( 'end' ) ;
195
+ /**
196
+ * Focuses the last tabbable element within the focus trap region.
197
+ * @returns Returns whether focus was moved successfuly.
198
+ */
199
+ focusLastTabbableElement ( ) : boolean {
200
+ const redirectToElement = this . _getRegionBoundary ( 'end' ) ;
201
+
170
202
if ( redirectToElement ) {
171
203
redirectToElement . focus ( ) ;
172
204
}
205
+
206
+ return ! ! redirectToElement ;
173
207
}
174
208
175
209
/** Get the first tabbable element from a DOM subtree (inclusive). */
0 commit comments