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

Fix web components as openers/closers not working properly #713

Merged
merged 2 commits into from
Sep 9, 2024
Merged
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
8 changes: 4 additions & 4 deletions cypress/e2e/webComponents.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ describe('Web Components', () => {
const handlers = {
show: event => {
expect(event.detail.target.tagName).to.eq('MY-DIALOG')
expect(event.detail.composedPath()[0].tagName).to.eq('FANCY-BUTTON')
expect(event.detail.composedPath()[0].tagName).to.eq('SLOT')
},
hide: event => {
expect(event.detail.target.tagName).to.eq('MY-DIALOG')
expect(event.detail.composedPath()[0].tagName).to.eq('FANCY-BUTTON')
expect(event.detail.composedPath()[0].tagName).to.eq('SLOT')
},
}

cy.spy(handlers, 'show').as('show')
cy.spy(handlers, 'hide').as('hide')
cy.window().its('instance').invoke('on', 'show', handlers.show)
cy.window().its('instance').invoke('on', 'hide', handlers.hide)
cy.get('my-dialog').shadow().find('fancy-button').first().click()
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests were passing because .click() was clicking the exact shadow root. A .realClick() however would click the inner <slot> and cause the test to fail, as reported in #712.

cy.get('my-dialog').shadow().find('fancy-button').first().realClick()
cy.get('@show').should('have.been.called')
cy.get('my-dialog').then(shouldBeVisible)
cy.get('my-dialog').shadow().find('fancy-button').last().click()
cy.get('my-dialog').shadow().find('fancy-button').last().realClick()
cy.get('@hide').should('have.been.called')
cy.get('my-dialog').then(shouldBeHidden)
cy.window().its('instance').invoke('off', 'show', handlers.show)
Expand Down
27 changes: 13 additions & 14 deletions src/a11y-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,19 @@ export default class A11yDialog {
// target
// See: https://github.com/KittyGiraudel/a11y-dialog/issues/582
const target = event.composedPath()[0] as HTMLElement

// We use `.closest(..)` and not `.matches(..)` here so that clicking
// an element nested within a dialog opener does cause the dialog to open
if (target.closest(`[${SCOPE}-show="${this.id}"]`)) {
this.show(event)
}

if (
target.closest(`[${SCOPE}-hide="${this.id}"]`) ||
(target.closest(`[${SCOPE}-hide]`) &&
target.closest('[aria-modal="true"]') === this.$el)
) {
this.hide(event)
}
const opener = closest(`[${SCOPE}-show="${this.id}"]`, target)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code was massaged a bit to be easier to read but the fix is the use of closest(..).

const explicitCloser = closest(`[${SCOPE}-hide="${this.id}"]`, target)
const implicitCloser =
closest(`[${SCOPE}-hide]`, target) &&
closest('[aria-modal="true"]', target) === this.$el

// We use `closest(..)` (instead of `matches(..)`) so that clicking an
// element nested within a dialog opener does cause the dialog to open, and
// we use our custom `closest(..)` function so that it can cross shadow
// boundaries
// See: https://github.com/KittyGiraudel/a11y-dialog/issues/712
Comment on lines +203 to +205
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the actual fix explanation.

if (opener) this.show(event)
if (explicitCloser || implicitCloser) this.hide(event)
}

/**
Expand Down
Loading