Skip to content

fix(popover-edit): focus trap not being destroyed and potential leaks with takeUntil #16091

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 1 commit into from
Jul 24, 2019
Merged
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
16 changes: 10 additions & 6 deletions src/cdk-experimental/popover-edit/table-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,24 @@ export class CdkEditable implements AfterViewInit, OnDestroy {
}

private _listenForTableEvents(): void {
const element = this.elementRef.nativeElement!;

const element = this.elementRef.nativeElement;
const toClosest = (selector: string) =>
map((event: UIEvent) => closest(event.target, selector));

this.ngZone.runOutsideAngular(() => {
// Track mouse movement over the table to hide/show hover content.
fromEvent<MouseEvent>(element, 'mouseover').pipe(
takeUntil(this.destroyed),
toClosest(ROW_SELECTOR),
takeUntil(this.destroyed),
).subscribe(this.editEventDispatcher.hovering);
fromEvent<MouseEvent>(element, 'mouseleave').pipe(
takeUntil(this.destroyed),
mapTo(null),
takeUntil(this.destroyed),
).subscribe(this.editEventDispatcher.hovering);
fromEvent<MouseEvent>(element, 'mousemove').pipe(
takeUntil(this.destroyed),
throttleTime(MOUSE_MOVE_THROTTLE_TIME_MS),
toClosest(ROW_SELECTOR),
takeUntil(this.destroyed),
).subscribe(this.editEventDispatcher.mouseMove);

// Track focus within the table to hide/show/make focusable hover content.
Expand Down Expand Up @@ -135,9 +134,9 @@ export class CdkEditable implements AfterViewInit, OnDestroy {
).subscribe(this.editEventDispatcher.allRows);

fromEvent<KeyboardEvent>(element, 'keyup').pipe(
takeUntil(this.destroyed),
filter(event => event.key === 'Enter'),
toClosest(CELL_SELECTOR),
takeUntil(this.destroyed),
).subscribe(this.editEventDispatcher.editing);

// Keydown must be used here or else key autorepeat does not work properly on some platforms.
Expand Down Expand Up @@ -217,6 +216,11 @@ export class CdkPopoverEdit<C> implements AfterViewInit, OnDestroy {
this.destroyed.next();
this.destroyed.complete();

if (this.focusTrap) {
this.focusTrap.destroy();
this.focusTrap = undefined;
}

if (this.overlayRef) {
this.overlayRef.dispose();
}
Expand Down