-
Notifications
You must be signed in to change notification settings - Fork 131
Prototype a version relying on ‘CloseWatcher’ #717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,6 +5,8 @@ export type A11yDialogInstance = InstanceType<typeof A11yDialog> | |||||||||||||||
|
|
||||||||||||||||
| const SCOPE = 'data-a11y-dialog' | ||||||||||||||||
|
|
||||||||||||||||
| let closeWatcher: CloseWatcher | null = null | ||||||||||||||||
|
|
||||||||||||||||
| export default class A11yDialog { | ||||||||||||||||
| private $el: HTMLElement | ||||||||||||||||
| private id: string | ||||||||||||||||
|
|
@@ -48,7 +50,8 @@ export default class A11yDialog { | |||||||||||||||
| if (destroyEvent.defaultPrevented) return this | ||||||||||||||||
|
|
||||||||||||||||
| // Hide the dialog to avoid destroying an open instance | ||||||||||||||||
| this.hide() | ||||||||||||||||
| if (closeWatcher) closeWatcher.requestClose() | ||||||||||||||||
|
||||||||||||||||
| else this.hide() | ||||||||||||||||
|
|
||||||||||||||||
| // Remove the click event delegates for our openers and closers | ||||||||||||||||
| document.removeEventListener('click', this.handleTriggerClicks, true) | ||||||||||||||||
|
|
@@ -74,6 +77,16 @@ export default class A11yDialog { | |||||||||||||||
| // If the event was prevented, do not continue with the normal behavior | ||||||||||||||||
| if (showEvent.defaultPrevented) return this | ||||||||||||||||
|
|
||||||||||||||||
| // When opening the dialog, create a new `CloseWatcher` instance, and listen | ||||||||||||||||
| // for a close event to call our `.hide(..)` method and nuking the close | ||||||||||||||||
| // watcher once it’s been consumed | ||||||||||||||||
| if (typeof CloseWatcher !== 'undefined') { | ||||||||||||||||
KittyGiraudel marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
| closeWatcher = new CloseWatcher() | ||||||||||||||||
| closeWatcher.onclose = event => { | ||||||||||||||||
| this.hide(event) | ||||||||||||||||
| } | ||||||||||||||||
|
||||||||||||||||
| closeWatcher.onclose = event => { | |
| this.hide(event) | |
| } | |
| closeWatcher.onclose = event => { | |
| this.hide(event) | |
| closeWatcher = null | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it should be defined within local scope to ensure that it automatically gets garbage collected?
Edit: It looks like 290eb14 changes the approach to use a locally scoped CloseWatcher 👍
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a breaking change, because we no longer can provide access to the “closer” (as in the element that was interacted with to close the dialog) via the close watcher. That’s because we can’t pass an event to the .close(..) or .requestClose(..) method which would be forwarded to the onclose callback. So this is a bit shitty and may be a deal breaker for this integration.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First thing I don’t love about the
CloseWatcherAPI is that it’s single-use.Once the watcher received a close event, it no longer processes anything else. That means we need to instantiate a new watcher every time we open the dialog.
There is also no way to know from an existing instance whether or not it’s been consumed or if it can process a close event. That’s not exposed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I agree the API shape is a bit weird. I guess it makes sense that you don't want to constantly keep listening for a close request.
Does this cause an actual problem?