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

x-collapse not working properly when used with click.away #2201

Merged
merged 3 commits into from Oct 11, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/alpinejs/src/utils/on.js
Expand Up @@ -30,6 +30,10 @@ export default function on (el, event, modifiers, callback) {

if (el.offsetWidth < 1 && el.offsetHeight < 1) return

// Additional check for special implementations like x-collapse
// where the element doesn't have display: none
if (el._x_isShown === false) return

next(e)
})
}
Expand Down
31 changes: 31 additions & 0 deletions tests/cypress/integration/plugins/collapse.spec.js
Expand Up @@ -15,3 +15,34 @@ test('can collapse and expand element',
get('h1').should(haveComputedStyle('height', '0px'))
},
)

test('@click.away with x-collapse (prevent race condition)',
html`
<div x-data="{ show: false }">
<button @click="show = true">Show</button>

<h1 x-show="show" @click.away="show = false" x-collapse>h1</h1>
</div>
`,
({ get }) => {
get('h1').should(haveComputedStyle('height', '0px'))
get('button').click()
get('h1').should(haveAttribute('style', 'overflow: hidden; height: auto;'))
}
)


test('@click.away with x-collapse and borders (prevent race condition)',
html`
<div x-data="{ show: false }">
<button @click="show = true">Show</button>

<h1 style="border: 1x solid" x-show="show" @click.away="show = false" x-collapse>h1</h1>
</div>
`,
({ get }) => {
get('h1').should(haveComputedStyle('height', '0px'))
get('button').click()
get('h1').should(haveAttribute('style', 'overflow: hidden; height: auto;'))
}
)