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