Skip to content

Commit 287aa43

Browse files
committed
fixup! change focus trap to find focusable child element if cdkFocusInitial element isn't focusable
1 parent 83acefe commit 287aa43

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/cdk/a11y/focus-trap/focus-trap.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export class FocusTrap {
130130
* Waits for the zone to stabilize, then either focuses the first element that the
131131
* user specified, or the first tabbable element.
132132
* @returns Returns a promise that resolves with a boolean, depending
133-
* on whether focus was moved successfuly.
133+
* on whether focus was moved successfully.
134134
*/
135135
focusInitialElementWhenReady(): Promise<boolean> {
136136
return new Promise<boolean>(resolve => {
@@ -142,7 +142,7 @@ export class FocusTrap {
142142
* Waits for the zone to stabilize, then focuses
143143
* the first tabbable element within the focus trap region.
144144
* @returns Returns a promise that resolves with a boolean, depending
145-
* on whether focus was moved successfuly.
145+
* on whether focus was moved successfully.
146146
*/
147147
focusFirstTabbableElementWhenReady(): Promise<boolean> {
148148
return new Promise<boolean>(resolve => {
@@ -154,7 +154,7 @@ export class FocusTrap {
154154
* Waits for the zone to stabilize, then focuses
155155
* the last tabbable element within the focus trap region.
156156
* @returns Returns a promise that resolves with a boolean, depending
157-
* on whether focus was moved successfuly.
157+
* on whether focus was moved successfully.
158158
*/
159159
focusLastTabbableElementWhenReady(): Promise<boolean> {
160160
return new Promise<boolean>(resolve => {
@@ -195,7 +195,7 @@ export class FocusTrap {
195195

196196
/**
197197
* Focuses the element that should be focused when the focus trap is initialized.
198-
* @returns Whether focus was moved successfuly.
198+
* @returns Whether focus was moved successfully.
199199
*/
200200
focusInitialElement(): boolean {
201201
// Contains the deprecated version of selector, for temporary backwards comparability.
@@ -217,6 +217,12 @@ export class FocusTrap {
217217
console.warn(`Element matching '[cdkFocusInitial]' is not focusable.`, redirectToElement);
218218
}
219219

220+
if (!this._checker.isFocusable(redirectToElement)) {
221+
const focusableChild = this._getFirstTabbableElement(redirectToElement) as HTMLElement;
222+
focusableChild?.focus();
223+
return !!focusableChild;
224+
}
225+
220226
redirectToElement.focus();
221227
return true;
222228
}
@@ -226,7 +232,7 @@ export class FocusTrap {
226232

227233
/**
228234
* Focuses the first tabbable element within the focus trap region.
229-
* @returns Whether focus was moved successfuly.
235+
* @returns Whether focus was moved successfully.
230236
*/
231237
focusFirstTabbableElement(): boolean {
232238
const redirectToElement = this._getRegionBoundary('start');
@@ -240,7 +246,7 @@ export class FocusTrap {
240246

241247
/**
242248
* Focuses the last tabbable element within the focus trap region.
243-
* @returns Whether focus was moved successfuly.
249+
* @returns Whether focus was moved successfully.
244250
*/
245251
focusLastTabbableElement(): boolean {
246252
const redirectToElement = this._getRegionBoundary('end');
@@ -253,7 +259,7 @@ export class FocusTrap {
253259
}
254260

255261
/**
256-
* Checks whether the focus trap has successfuly been attached.
262+
* Checks whether the focus trap has successfully been attached.
257263
*/
258264
hasAttached(): boolean {
259265
return this._hasAttached;
@@ -396,7 +402,7 @@ export class CdkTrapFocus implements OnDestroy, AfterContentInit, OnChanges, DoC
396402
set enabled(value: boolean) { this.focusTrap.enabled = coerceBooleanProperty(value); }
397403

398404
/**
399-
* Whether the directive should automatially move focus into the trapped region upon
405+
* Whether the directive should automatically move focus into the trapped region upon
400406
* initialization and return focus to the previous activeElement upon destruction.
401407
*/
402408
@Input('cdkTrapFocusAutoCapture')

0 commit comments

Comments
 (0)