@@ -100,26 +100,38 @@ export class FocusTrap {
100100
101101 /**
102102 * 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.
104106 */
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+ } ) ;
107111 }
108112
109113 /**
110114 * Waits for the zone to stabilize, then focuses
111115 * 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.
112118 */
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+ } ) ;
115123 }
116124
117125 /**
118126 * Waits for the zone to stabilize, then focuses
119127 * 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.
120130 */
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+ } ) ;
123135 }
124136
125137 /**
@@ -146,30 +158,47 @@ export class FocusTrap {
146158 markers [ markers . length - 1 ] : this . _getLastTabbableElement ( this . _element ) ;
147159 }
148160
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+
152168 if ( redirectToElement ) {
153169 redirectToElement . focus ( ) ;
154- } else {
155- this . focusFirstTabbableElement ( ) ;
170+ return true ;
156171 }
172+
173+ return this . focusFirstTabbableElement ( ) ;
157174 }
158175
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+
162183 if ( redirectToElement ) {
163184 redirectToElement . focus ( ) ;
164185 }
186+
187+ return ! ! redirectToElement ;
165188 }
166189
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+
170197 if ( redirectToElement ) {
171198 redirectToElement . focus ( ) ;
172199 }
200+
201+ return ! ! redirectToElement ;
173202 }
174203
175204 /** Get the first tabbable element from a DOM subtree (inclusive). */
0 commit comments