Skip to content

Commit

Permalink
Add custom event in escape handler
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-watkins committed Aug 8, 2024
1 parent 148270a commit 8ca3d25
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/a11y-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,25 @@ export default class A11yDialog {
// If the dialog is shown and the ESC key is pressed, prevent any further
// effects from the ESC key and hide the dialog, unless:
// - its role is `alertdialog`, which means it should be modal
// - or it contains an open popover, in which case ESC should close it
// - it contains an open popover, in which case ESC should close it
// - or the custom event dialog:escape is cancelled
if (
event.key === 'Escape' &&
this.$el.getAttribute('role') !== 'alertdialog' &&
!hasOpenPopover
) {
event.preventDefault()
this.hide(event)
const escapeEvent = new CustomEvent('dialog:escape', {
bubbles: true,
cancelable: true,
detail: { originalEvent: event },
})

this.$el.dispatchEvent(escapeEvent)

if (!escapeEvent.defaultPrevented) {
event.preventDefault()
this.hide(event)
}
}

// If the dialog is shown and the TAB key is pressed, make sure the focus
Expand Down

0 comments on commit 8ca3d25

Please sign in to comment.