@@ -88,8 +88,13 @@ export class FocusTrap {
8888 }
8989
9090 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+ } ) ;
9398
9499 if ( this . _element . parentNode ) {
95100 this . _element . parentNode . insertBefore ( this . _startAnchor ! , this . _element ) ;
@@ -100,26 +105,38 @@ export class FocusTrap {
100105
101106 /**
102107 * 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.
104111 */
105- focusInitialElementWhenReady ( ) {
106- this . _executeOnStable ( ( ) => this . focusInitialElement ( ) ) ;
112+ focusInitialElementWhenReady ( ) : Promise < boolean > {
113+ return new Promise < boolean > ( resolve => {
114+ this . _executeOnStable ( ( ) => resolve ( this . focusInitialElement ( ) ) ) ;
115+ } ) ;
107116 }
108117
109118 /**
110119 * Waits for the zone to stabilize, then focuses
111120 * 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.
112123 */
113- focusFirstTabbableElementWhenReady ( ) {
114- this . _executeOnStable ( ( ) => this . focusFirstTabbableElement ( ) ) ;
124+ focusFirstTabbableElementWhenReady ( ) : Promise < boolean > {
125+ return new Promise < boolean > ( resolve => {
126+ this . _executeOnStable ( ( ) => resolve ( this . focusFirstTabbableElement ( ) ) ) ;
127+ } ) ;
115128 }
116129
117130 /**
118131 * Waits for the zone to stabilize, then focuses
119132 * 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.
120135 */
121- focusLastTabbableElementWhenReady ( ) {
122- this . _executeOnStable ( ( ) => this . focusLastTabbableElement ( ) ) ;
136+ focusLastTabbableElementWhenReady ( ) : Promise < boolean > {
137+ return new Promise < boolean > ( resolve => {
138+ this . _executeOnStable ( ( ) => resolve ( this . focusLastTabbableElement ( ) ) ) ;
139+ } ) ;
123140 }
124141
125142 /**
@@ -146,30 +163,47 @@ export class FocusTrap {
146163 markers [ markers . length - 1 ] : this . _getLastTabbableElement ( this . _element ) ;
147164 }
148165
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+
152173 if ( redirectToElement ) {
153174 redirectToElement . focus ( ) ;
154- } else {
155- this . focusFirstTabbableElement ( ) ;
175+ return true ;
156176 }
177+
178+ return this . focusFirstTabbableElement ( ) ;
157179 }
158180
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+
162188 if ( redirectToElement ) {
163189 redirectToElement . focus ( ) ;
164190 }
191+
192+ return ! ! redirectToElement ;
165193 }
166194
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+
170202 if ( redirectToElement ) {
171203 redirectToElement . focus ( ) ;
172204 }
205+
206+ return ! ! redirectToElement ;
173207 }
174208
175209 /** Get the first tabbable element from a DOM subtree (inclusive). */
0 commit comments