Skip to content

Commit 35421c8

Browse files
feat(cdk/focus-trap) Backwards compatible factory create method (#18336)
Optionally accept a boolean instead of a ConfigurableFocusTrapConfig object in the ConfigurableFocusTrapFactory create method, so that it is backwards compatible with FocusTrapFactory create. This will enable apps to turn on the new functionality with {provide: FocusTrapFactory, useClass: ConfigurableFocusTrapFactory}.
1 parent 74b3441 commit 35421c8

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
export class ConfigurableFocusTrapConfig<D = any> {
9+
/**
10+
* Configuration for creating a ConfigurableFocusTrap.
11+
*/
12+
export class ConfigurableFocusTrapConfig {
13+
/**
14+
* Whether to defer the creation of FocusTrap elements to be
15+
* done manually by the user. Default is to create them
16+
* automatically.
17+
*/
1018
defer: boolean = false;
11-
}
19+
}

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,28 @@ export class ConfigurableFocusTrapFactory {
4141
/**
4242
* Creates a focus-trapped region around the given element.
4343
* @param element The element around which focus will be trapped.
44-
* @param deferCaptureElements Defers the creation of focus-capturing elements to be done
45-
* manually by the user.
44+
* @param config The focus trap configuration.
4645
* @returns The created focus trap instance.
4746
*/
48-
create(element: HTMLElement, config: ConfigurableFocusTrapConfig =
47+
create(element: HTMLElement, config?: ConfigurableFocusTrapConfig): ConfigurableFocusTrap;
48+
49+
/**
50+
* @deprecated Pass a config object instead of the `deferCaptureElements` flag.
51+
* @breaking-change 11.0.0
52+
*/
53+
create(element: HTMLElement, deferCaptureElements: boolean): ConfigurableFocusTrap;
54+
55+
create(element: HTMLElement, config: ConfigurableFocusTrapConfig | boolean =
4956
new ConfigurableFocusTrapConfig()): ConfigurableFocusTrap {
57+
let configObject: ConfigurableFocusTrapConfig;
58+
if (typeof config === 'boolean') {
59+
configObject = new ConfigurableFocusTrapConfig();
60+
configObject.defer = config;
61+
} else {
62+
configObject = config;
63+
}
5064
return new ConfigurableFocusTrap(
5165
element, this._checker, this._ngZone, this._document, this._focusTrapManager,
52-
this._inertStrategy, config);
66+
this._inertStrategy, configObject);
5367
}
5468
}

tools/public_api_guard/cdk/a11y.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export declare class ConfigurableFocusTrap extends FocusTrap implements ManagedF
6969
export declare class ConfigurableFocusTrapFactory {
7070
constructor(_checker: InteractivityChecker, _ngZone: NgZone, _focusTrapManager: FocusTrapManager, _document: any, _inertStrategy?: FocusTrapInertStrategy);
7171
create(element: HTMLElement, config?: ConfigurableFocusTrapConfig): ConfigurableFocusTrap;
72+
create(element: HTMLElement, deferCaptureElements: boolean): ConfigurableFocusTrap;
7273
static ɵfac: i0.ɵɵFactoryDef<ConfigurableFocusTrapFactory>;
7374
static ɵprov: i0.ɵɵInjectableDef<ConfigurableFocusTrapFactory>;
7475
}

0 commit comments

Comments
 (0)