Skip to content

Commit

Permalink
Try/catch a new DOM query which could fail in unsupported browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
KittyGiraudel committed Oct 3, 2023
1 parent 36dccb3 commit a446b5c
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/a11y-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,27 @@ export default class A11yDialog {
return
}

let hasOpenPopover = false
try {
hasOpenPopover = !!this.$el.querySelector(
'[popover]:not([popover="manual"]):popover-open'
)
} catch {
// Run that DOM query in a try/catch because not all browsers support the
// `:popover-open` selector, which would cause the whole expression to
// fail
// See: https://caniuse.com/mdn-css_selectors_popover-open
// See: https://github.com/KittyGiraudel/a11y-dialog/pull/578#discussion_r1343215149
}

// 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
if (
event.key === 'Escape' &&
this.$el.getAttribute('role') !== 'alertdialog' &&
!this.$el.querySelector('[popover]:not([popover="manual"]):popover-open')
!hasOpenPopover
) {
event.preventDefault()
this.hide(event)
Expand Down

0 comments on commit a446b5c

Please sign in to comment.