refactor(web): svelte actions (#9701)

This commit is contained in:
martin
2024-05-23 19:56:48 +02:00
committed by GitHub
parent 8bfa6769a5
commit 832d728940
32 changed files with 43 additions and 41 deletions
+21
View File
@@ -0,0 +1,21 @@
interface Options {
onFocusOut?: () => void;
}
export function focusOutside(node: HTMLElement, options: Options = {}) {
const { onFocusOut } = options;
const handleFocusOut = (event: FocusEvent) => {
if (onFocusOut && event.relatedTarget instanceof Node && !node.contains(event.relatedTarget as Node)) {
onFocusOut();
}
};
node.addEventListener('focusout', handleFocusOut);
return {
destroy() {
node.removeEventListener('focusout', handleFocusOut);
},
};
}