Files
immich/web/src/lib/actions/focus-outside.ts
Ben c14e2914f8 fix(web): rating stars accessibility (#11966)
* fix(web): exif ratings accessibility

* chore: add tests

* fix: eslint errors

* fix: clean up issues from changes in use:focusOutside
2024-08-23 12:34:12 -04:00

25 lines
571 B
TypeScript

interface Options {
onFocusOut?: (event: FocusEvent) => void;
}
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);
},
};
}