Skip to content

Commit c61d6a4

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

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {PortalModule, CdkPortalOutlet, TemplatePortal} from '@angular/cdk/portal
55
import {A11yModule, FocusTrap, CdkTrapFocus} from '../index';
66

77

8-
describe('FocusTrap', () => {
8+
fdescribe('FocusTrap', () => {
99

1010
beforeEach(waitForAsync(() => {
1111
TestBed.configureTestingModule({

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

Lines changed: 19 additions & 9 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,11 +195,11 @@ 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.
202-
const redirectToElement = this._element.querySelector(`[cdk-focus-initial], ` +
202+
let redirectToElement = this._element.querySelector(`[cdk-focus-initial], ` +
203203
`[cdkFocusInitial]`) as HTMLElement;
204204

205205
if (redirectToElement) {
@@ -217,6 +217,16 @@ export class FocusTrap {
217217
console.warn(`Element matching '[cdkFocusInitial]' is not focusable.`, redirectToElement);
218218
}
219219

220+
if (!this._checker.isFocusable(redirectToElement)) {
221+
redirectToElement = this._getFirstTabbableElement(redirectToElement) as HTMLElement;
222+
if (redirectToElement) {
223+
redirectToElement.focus();
224+
return true;
225+
} else {
226+
return false;
227+
}
228+
}
229+
220230
redirectToElement.focus();
221231
return true;
222232
}
@@ -226,7 +236,7 @@ export class FocusTrap {
226236

227237
/**
228238
* Focuses the first tabbable element within the focus trap region.
229-
* @returns Whether focus was moved successfuly.
239+
* @returns Whether focus was moved successfully.
230240
*/
231241
focusFirstTabbableElement(): boolean {
232242
const redirectToElement = this._getRegionBoundary('start');
@@ -240,7 +250,7 @@ export class FocusTrap {
240250

241251
/**
242252
* Focuses the last tabbable element within the focus trap region.
243-
* @returns Whether focus was moved successfuly.
253+
* @returns Whether focus was moved successfully.
244254
*/
245255
focusLastTabbableElement(): boolean {
246256
const redirectToElement = this._getRegionBoundary('end');
@@ -253,7 +263,7 @@ export class FocusTrap {
253263
}
254264

255265
/**
256-
* Checks whether the focus trap has successfuly been attached.
266+
* Checks whether the focus trap has successfully been attached.
257267
*/
258268
hasAttached(): boolean {
259269
return this._hasAttached;
@@ -396,7 +406,7 @@ export class CdkTrapFocus implements OnDestroy, AfterContentInit, OnChanges, DoC
396406
set enabled(value: boolean) { this.focusTrap.enabled = coerceBooleanProperty(value); }
397407

398408
/**
399-
* Whether the directive should automatially move focus into the trapped region upon
409+
* Whether the directive should automatically move focus into the trapped region upon
400410
* initialization and return focus to the previous activeElement upon destruction.
401411
*/
402412
@Input('cdkTrapFocusAutoCapture')

0 commit comments

Comments
 (0)