Skip to content

Commit 290eb14

Browse files
committed
Simplify the close watcher integration a lot
1 parent 79b1d0f commit 290eb14

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

src/a11y-dialog.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ export type A11yDialogInstance = InstanceType<typeof A11yDialog>
55

66
const SCOPE = 'data-a11y-dialog'
77

8-
let closeWatcher: CloseWatcher | null = null
9-
108
export default class A11yDialog {
119
private $el: HTMLElement
1210
private id: string
@@ -50,8 +48,7 @@ export default class A11yDialog {
5048
if (destroyEvent.defaultPrevented) return this
5149

5250
// Hide the dialog to avoid destroying an open instance
53-
if (closeWatcher) closeWatcher.requestClose()
54-
else this.hide()
51+
this.hide()
5552

5653
// Remove the click event delegates for our openers and closers
5754
document.removeEventListener('click', this.handleTriggerClicks, true)
@@ -78,13 +75,10 @@ export default class A11yDialog {
7875
if (showEvent.defaultPrevented) return this
7976

8077
// When opening the dialog, create a new `CloseWatcher` instance, and listen
81-
// for a close event to call our `.hide(..)` method and nuking the close
82-
// watcher once it’s been consumed
78+
// for a close event to call our `.hide(..)` method (mainly to support the
79+
// Android back button as well as the “Back” command for VoiceOcer)
8380
if (typeof CloseWatcher !== 'undefined') {
84-
closeWatcher = new CloseWatcher()
85-
closeWatcher.onclose = event => {
86-
this.hide(event)
87-
}
81+
new CloseWatcher().onclose = this.hide
8882
}
8983

9084
// Keep a reference to the currently focused element to be able to restore
@@ -217,10 +211,10 @@ export default class A11yDialog {
217211
// boundaries
218212
// See: https://github.com/KittyGiraudel/a11y-dialog/issues/712
219213
if (opener) this.show(event)
220-
if (explicitCloser || implicitCloser) {
221-
if (closeWatcher) closeWatcher.requestClose()
222-
else this.hide(event)
223-
}
214+
// The reason we do not replace all internal usages of `.hide(..)` (such as
215+
// this one) with a watcher interaction is because we would lose the event
216+
// details, which can be important to determine the cause of the event
217+
if (explicitCloser || implicitCloser) this.hide(event)
224218
}
225219

226220
/**
@@ -258,8 +252,7 @@ export default class A11yDialog {
258252
!hasOpenPopover
259253
) {
260254
event.preventDefault()
261-
if (closeWatcher) closeWatcher.requestClose()
262-
else this.hide(event)
255+
this.hide(event)
263256
}
264257

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

0 commit comments

Comments
 (0)