Skip to content

Commit 073b98c

Browse files
crisbetommalerba
authored andcommitted
fix(popover-edit): prevent default escape action (#16747)
Adds a `preventDefault` to the escape key presses of the popover edit and ignores escape presses that have a modifier key. This prevents Safari from exiting fullscreen mode and avoids interfering with other OS-level actions. We already made a pass to add this logic to all other overlay components in #16202, but it looks like we missed the popover edit. (cherry picked from commit 48cc514)
1 parent 6c51995 commit 073b98c

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

src/cdk-experimental/popover-edit/lens-directives.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
Input,
1717
HostListener,
1818
} from '@angular/core';
19+
import {hasModifierKey} from '@angular/cdk/keycodes';
1920
import {EDIT_PANE_SELECTOR} from './constants';
2021
import {closest} from './polyfill';
2122
import {EditRef} from './edit-ref';
@@ -130,8 +131,9 @@ export class CdkEditControl<FormValue> implements OnDestroy, OnInit {
130131
// tslint:disable:no-host-decorator-in-concrete
131132
@HostListener('keydown', ['$event'])
132133
_handleKeydown(event: KeyboardEvent) {
133-
if (event.key === 'Escape') {
134+
if (event.key === 'Escape' && !hasModifierKey(event)) {
134135
this.close();
136+
event.preventDefault();
135137
}
136138
}
137139

src/cdk-experimental/popover-edit/popover-edit.spec.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -771,10 +771,26 @@ cdkPopoverEditTabOut`, fakeAsync(() => {
771771
it('closes the lens on escape', fakeAsync(() => {
772772
component.openLens();
773773

774-
component.getNameInput()!.dispatchEvent(
775-
new KeyboardEvent('keydown', {bubbles: true, key: 'Escape'}));
774+
const event = new KeyboardEvent('keydown', {bubbles: true, key: 'Escape'});
775+
spyOn(event, 'preventDefault').and.callThrough();
776+
component.getNameInput()!.dispatchEvent(event);
776777

777778
expect(component.lensIsOpen()).toBe(false);
779+
expect(event.preventDefault).toHaveBeenCalled();
780+
clearLeftoverTimers();
781+
}));
782+
783+
it('does not close the lens on escape with a modifier key', fakeAsync(() => {
784+
component.openLens();
785+
786+
const event = new KeyboardEvent('keydown', {bubbles: true, key: 'Escape'});
787+
Object.defineProperty(event, 'altKey', {get: () => true});
788+
789+
spyOn(event, 'preventDefault').and.callThrough();
790+
component.getNameInput()!.dispatchEvent(event);
791+
792+
expect(component.lensIsOpen()).toBe(true);
793+
expect(event.preventDefault).not.toHaveBeenCalled();
778794
clearLeftoverTimers();
779795
}));
780796

src/material-experimental/popover-edit/popover-edit.spec.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,10 +690,27 @@ matPopoverEditTabOut`, fakeAsync(() => {
690690
it('closes the lens on escape', fakeAsync(() => {
691691
component.openLens();
692692

693-
component.getInput()!.dispatchEvent(
694-
new KeyboardEvent('keydown', {bubbles: true, key: 'Escape'}));
693+
const event = new KeyboardEvent('keydown', {bubbles: true, key: 'Escape'});
694+
695+
spyOn(event, 'preventDefault').and.callThrough();
696+
component.getInput()!.dispatchEvent(event);
695697

696698
expect(component.lensIsOpen()).toBe(false);
699+
expect(event.preventDefault).toHaveBeenCalled();
700+
clearLeftoverTimers();
701+
}));
702+
703+
it('does not close the lens on escape with a modifier key', fakeAsync(() => {
704+
component.openLens();
705+
706+
const event = new KeyboardEvent('keydown', {bubbles: true, key: 'Escape'});
707+
Object.defineProperty(event, 'altKey', {get: () => true});
708+
709+
spyOn(event, 'preventDefault').and.callThrough();
710+
component.getInput()!.dispatchEvent(event);
711+
712+
expect(component.lensIsOpen()).toBe(true);
713+
expect(event.preventDefault).not.toHaveBeenCalled();
697714
clearLeftoverTimers();
698715
}));
699716

0 commit comments

Comments
 (0)