fix(actions): prevent clickOutside race condition in update method

Fix a race condition where the clickOutside action's update() method
would remove and re-add the event listener every time it was called,
even when the enabled state hadn't changed. This caused clicks to be
missed during the setTimeout delay.

Changes:
- Track previous enabled state before updating
- Only use setTimeout when transitioning from disabled to enabled
- Immediately re-add listener when enabled state stays true
- No listener changes when enabled state stays false

This ensures click-outside events are consistently detected without
gaps in event listener registration.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Justin Edmund 2025-10-07 22:13:30 -07:00
parent dcca9eb6e5
commit 23a844dd12

View file

@ -68,6 +68,8 @@ export function clickOutside(
return {
update(newOptions: ClickOutsideOptions | (() => void)) {
const wasEnabled = enabled
// Remove old listener
document.removeEventListener('click', handleClick, true)
@ -80,12 +82,17 @@ export function clickOutside(
callback = newOptions.callback
}
// Add new listener if enabled
// Only modify listener if enabled state actually changed
if (wasEnabled !== enabled) {
if (enabled) {
setTimeout(() => {
document.addEventListener('click', handleClick, true)
}, 0)
}
} else if (enabled) {
// State didn't change but we're still enabled - re-add immediately
document.addEventListener('click', handleClick, true)
}
},
destroy() {
document.removeEventListener('click', handleClick, true)