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:
parent
dcca9eb6e5
commit
23a844dd12
1 changed files with 12 additions and 5 deletions
|
|
@ -68,6 +68,8 @@ export function clickOutside(
|
||||||
|
|
||||||
return {
|
return {
|
||||||
update(newOptions: ClickOutsideOptions | (() => void)) {
|
update(newOptions: ClickOutsideOptions | (() => void)) {
|
||||||
|
const wasEnabled = enabled
|
||||||
|
|
||||||
// Remove old listener
|
// Remove old listener
|
||||||
document.removeEventListener('click', handleClick, true)
|
document.removeEventListener('click', handleClick, true)
|
||||||
|
|
||||||
|
|
@ -80,11 +82,16 @@ export function clickOutside(
|
||||||
callback = newOptions.callback
|
callback = newOptions.callback
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add new listener if enabled
|
// Only modify listener if enabled state actually changed
|
||||||
if (enabled) {
|
if (wasEnabled !== enabled) {
|
||||||
setTimeout(() => {
|
if (enabled) {
|
||||||
document.addEventListener('click', handleClick, true)
|
setTimeout(() => {
|
||||||
}, 0)
|
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() {
|
destroy() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue