Files
familienarchiv/frontend/src/lib/actions/clickOutside.ts
Marcel 4446e80875 test(actions): add defaultPrevented coverage for clickOutside (#195)
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>
2026-04-15 12:46:04 +02:00

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);
}
};
}