Skip to content

Commit

Permalink
Rely on the ‘fire’ method for the ‘escape’ event
Browse files Browse the repository at this point in the history
  • Loading branch information
KittyGiraudel committed Aug 9, 2024
1 parent 8ca3d25 commit fed40ec
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions src/a11y-dialog.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getActiveElement, moveFocusToDialog, trapTabKey } from './dom-utils'

export type A11yDialogEvent = 'show' | 'hide' | 'destroy'
export type A11yDialogEvent = 'show' | 'hide' | 'destroy' | 'escape'
export type A11yDialogInstance = InstanceType<typeof A11yDialog>

export default class A11yDialog {
Expand Down Expand Up @@ -156,12 +156,15 @@ export default class A11yDialog {
* code
*/
private fire(type: A11yDialogEvent, event?: Event) {
this.$el.dispatchEvent(
new CustomEvent(type, {
detail: event,
cancelable: true,
})
)
const customEvent = new CustomEvent(type, {
detail: event,
bubbles: true,
cancelable: true,
})

this.$el.dispatchEvent(customEvent)

return customEvent
}

/**
Expand Down Expand Up @@ -220,13 +223,7 @@ export default class A11yDialog {
this.$el.getAttribute('role') !== 'alertdialog' &&
!hasOpenPopover
) {
const escapeEvent = new CustomEvent('dialog:escape', {
bubbles: true,
cancelable: true,
detail: { originalEvent: event },
})

this.$el.dispatchEvent(escapeEvent)
const escapeEvent = this.fire('escape', event)

if (!escapeEvent.defaultPrevented) {
event.preventDefault()
Expand Down

0 comments on commit fed40ec

Please sign in to comment.