move escape close logic to document listener; #1177

pull/1186/head
Cory LaViska 2023-02-06 11:32:06 -05:00
rodzic b8695b70a9
commit 12f62075ad
1 zmienionych plików z 8 dodań i 8 usunięć

Wyświetl plik

@ -155,6 +155,14 @@ export default class SlDropdown extends ShoelaceElement {
}
handleDocumentKeyDown(event: KeyboardEvent) {
// Close when escape or tab is pressed
if (event.key === 'Escape' && this.open) {
event.stopPropagation();
this.focusOnTrigger();
this.hide();
return;
}
// Handle tabbing
if (event.key === 'Tab') {
// Tabbing within an open menu should close the dropdown and refocus the trigger
@ -217,14 +225,6 @@ export default class SlDropdown extends ShoelaceElement {
}
handleTriggerKeyDown(event: KeyboardEvent) {
// Close when escape or tab is pressed
if (event.key === 'Escape' && this.open) {
event.stopPropagation();
this.focusOnTrigger();
this.hide();
return;
}
// When spacebar/enter is pressed, show the panel but don't focus on the menu. This let's the user press the same
// key again to hide the menu in case they don't want to make a selection.
if ([' ', 'Enter'].includes(event.key)) {