mirror of
https://github.com/immich-app/immich.git
synced 2025-12-26 01:11:47 +03:00
22 lines
503 B
TypeScript
22 lines
503 B
TypeScript
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);
|
|
},
|
|
};
|
|
}
|