Skip to content

Commit

Permalink
KDropdownMenu emits close and tab events for fine-tuned focus management
Browse files Browse the repository at this point in the history
When a user hits Tab they will leave the focus of the popover but currently the focus just returns to the root of the page rather than the next item in the dom.

Here we do two things - first we emit a "close" event so that users can react on close. This is less necessary to the stated purpose but seems a worthy addition to allow us to react to the closing of a dropdown.

The second is to emit a `tab` event which occurs when the user hits `tab`. Note that in this case, we pass the JS event so that it can be `preventDefault`-ed if needed (which was the motivation for this to begin with)
  • Loading branch information
nucleogenesis committed Nov 16, 2023
1 parent 41a45de commit 3f95cd9
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/KDropdownMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
sibling ? this.$nextTick(() => sibling.focus()) : this.$nextTick(() => this.setFocus());
// if a TAB key, not an arrow key, close the popover and advance to next item in the tab index
} else if ((event.key == 'Tab' || event.keyCode == 9) && popoverIsOpen) {
this.$emit("tab", event);
this.closePopover();
}
},
Expand All @@ -145,6 +146,7 @@
this.closePopover();
},
closePopover() {
this.$emit('close');
this.$refs.popover.close();
},
focusOnButton() {
Expand Down

0 comments on commit 3f95cd9

Please sign in to comment.