mirror of
https://github.com/immich-app/immich.git
synced 2025-12-28 17:24:56 +03:00
30 lines
701 B
TypeScript
30 lines
701 B
TypeScript
interface Options {
|
|
onFocusOut?: (event: FocusEvent) => void;
|
|
}
|
|
|
|
/**
|
|
* Calls a function when focus leaves the element.
|
|
* @param node
|
|
* @param options Object containing onFocusOut function
|
|
*/
|
|
export function focusOutside(node: HTMLElement, options: Options = {}) {
|
|
const { onFocusOut } = options;
|
|
|
|
const handleFocusOut = (event: FocusEvent) => {
|
|
if (
|
|
onFocusOut &&
|
|
(!event.relatedTarget || (event.relatedTarget instanceof Node && !node.contains(event.relatedTarget as Node)))
|
|
) {
|
|
onFocusOut(event);
|
|
}
|
|
};
|
|
|
|
node.addEventListener('focusout', handleFocusOut);
|
|
|
|
return {
|
|
destroy() {
|
|
node.removeEventListener('focusout', handleFocusOut);
|
|
},
|
|
};
|
|
}
|