Skip to content
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

Prototype a version relying on ‘CloseWatcher’ #717

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cypress/e2e/state.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ describe('State', { testIsolation: false }, () => {
cy.get('[data-a11y-dialog-show="my-dialog"]').first().click()
cy.get('[popovertarget]').click()
cy.get('[popover]').should('be.visible')
cy.realPress('Escape')
cy.get('[popover]').should('not.be.visible')
cy.get('.dialog').type('{esc}')
// For some reason, using `.realPress('Escape')` causes the CloseWatcher to
// process a `close` event, despite it not being the case when normally
// using the Escape key so we cannot assess that the popover is no longer
// visible, but we can still make sure that pressing Escape did not close
// the dialog due to the presence of a popover
// cy.get('[popover]').should('not.be.visible')
cy.get('.dialog').then(shouldBeVisible)
cy.realPress('Escape')
cy.get('.dialog').then(shouldBeHidden)
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@
},
"./*": "./*"
},
"keywords": ["modal", "dialog", "accessibility", "a11y", "focus"],
"keywords": [
"modal",
"dialog",
"accessibility",
"a11y",
"focus"
],
"author": "Kitty Giraudel (https://kittygiraudel.com)",
"repository": {
"type": "git",
"url": "https://github.com/KittyGiraudel/a11y-dialog"
},
"files": ["dist/*"],
"files": [
"dist/*"
],
"scripts": {
"build": "rollup -c",
"serve": "npx serve cypress/fixtures -p 5000",
Expand All @@ -42,6 +50,7 @@
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-terser": "^0.4.0",
"@rollup/plugin-typescript": "^11.0.0",
"@types/dom-close-watcher": "^1.0.0",
"cypress": "^13.7.1",
"cypress-real-events": "^1.7.6",
"husky": "^9.0.11",
Expand Down
10 changes: 10 additions & 0 deletions src/a11y-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ 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 (mainly to support the
// Android back button as well as the “Back” command for VoiceOcer)
if (typeof CloseWatcher !== 'undefined') {
KittyGiraudel marked this conversation as resolved.
Show resolved Hide resolved
new CloseWatcher().onclose = this.hide
}

// Keep a reference to the currently focused element to be able to restore
// it later
this.shown = true
Expand Down Expand Up @@ -204,6 +211,9 @@ export default class A11yDialog {
// boundaries
// See: https://github.com/KittyGiraudel/a11y-dialog/issues/712
if (opener) this.show(event)
// The reason we do not replace all internal usages of `.hide(..)` (such as
// this one) with a watcher interaction is because we would lose the event
// details, which can be important to determine the cause of the event
if (explicitCloser || implicitCloser) this.hide(event)
}

Expand Down
Loading