Skip to content

chore(a11y): Allow FocusTrap to be extended #15770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/cdk/a11y/focus-trap/focus-trap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export class FocusTrap {
private _hasAttached = false;

// Event listeners for the anchors. Need to be regular functions so that we can unbind them later.
private _startAnchorListener = () => this.focusLastTabbableElement();
private _endAnchorListener = () => this.focusFirstTabbableElement();
protected startAnchorListener = () => this.focusLastTabbableElement();
protected endAnchorListener = () => this.focusFirstTabbableElement();

/** Whether the focus trap is active. */
get enabled(): boolean { return this._enabled; }
Expand Down Expand Up @@ -70,15 +70,15 @@ export class FocusTrap {
const endAnchor = this._endAnchor;

if (startAnchor) {
startAnchor.removeEventListener('focus', this._startAnchorListener);
startAnchor.removeEventListener('focus', this.startAnchorListener);

if (startAnchor.parentNode) {
startAnchor.parentNode.removeChild(startAnchor);
}
}

if (endAnchor) {
endAnchor.removeEventListener('focus', this._endAnchorListener);
endAnchor.removeEventListener('focus', this.endAnchorListener);

if (endAnchor.parentNode) {
endAnchor.parentNode.removeChild(endAnchor);
Expand All @@ -103,12 +103,12 @@ export class FocusTrap {
this._ngZone.runOutsideAngular(() => {
if (!this._startAnchor) {
this._startAnchor = this._createAnchor();
this._startAnchor!.addEventListener('focus', this._startAnchorListener);
this._startAnchor!.addEventListener('focus', this.startAnchorListener);
}

if (!this._endAnchor) {
this._endAnchor = this._createAnchor();
this._endAnchor!.addEventListener('focus', this._endAnchorListener);
this._endAnchor!.addEventListener('focus', this.endAnchorListener);
}
});

Expand Down
2 changes: 2 additions & 0 deletions tools/public_api_guard/cdk/a11y.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export declare type FocusOrigin = 'touch' | 'mouse' | 'keyboard' | 'program' | n

export declare class FocusTrap {
enabled: boolean;
protected endAnchorListener: () => boolean;
protected startAnchorListener: () => boolean;
constructor(_element: HTMLElement, _checker: InteractivityChecker, _ngZone: NgZone, _document: Document, deferAnchors?: boolean);
attachAnchors(): boolean;
destroy(): void;
Expand Down