The action already checks event.defaultPrevented before dispatching clickoutside, but that branch had no test. Add the missing case and add a one-line comment explaining why capture phase is used. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
17 lines
501 B
TypeScript
17 lines
501 B
TypeScript
export function clickOutside(node: HTMLElement): { destroy: () => void } {
|
|
function handleClick(event: MouseEvent) {
|
|
if (node && !node.contains(event.target as Node) && !event.defaultPrevented) {
|
|
node.dispatchEvent(new CustomEvent('clickoutside'));
|
|
}
|
|
}
|
|
|
|
// Capture phase (true) ensures this fires before any child stopPropagation() calls.
|
|
document.addEventListener('click', handleClick, true);
|
|
|
|
return {
|
|
destroy() {
|
|
document.removeEventListener('click', handleClick, true);
|
|
}
|
|
};
|
|
}
|