Skip to content

Commit

Permalink
add focus outside composables
Browse files Browse the repository at this point in the history
  • Loading branch information
zernonia committed Jul 15, 2023
1 parent ce2022d commit 75addac
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/radix-vue/src/shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { onFocusOutside } from "./onFocusOutside";
export { useArrowNavigation } from "./useArrowNavigation";
export { useMouseInElement } from "./useMouseInElement";
export { useHoverDelay } from "./useHoverDelay";
Expand Down
27 changes: 27 additions & 0 deletions packages/radix-vue/src/shared/onFocusOutside.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { type MaybeElementRef, unrefElement } from "@vueuse/core";
import { onMounted, onUnmounted } from "vue";

export const onFocusOutside = (
element: MaybeElementRef,
handler: (event: FocusEvent) => void
) => {
const handleFocusOut = (ev: FocusEvent) => {
const el = unrefElement(element);
const isFocusInsideElement = el?.contains(ev.relatedTarget as Node);

if (!isFocusInsideElement) {
handler(ev);
}
};

onMounted(() => {
const el = unrefElement(element);
// @ts-ignore
el?.addEventListener("focusout", handleFocusOut);
});
onUnmounted(() => {
const el = unrefElement(element);
// @ts-ignore
el?.removeEventListener("focusout", handleFocusOut);
});
};

0 comments on commit 75addac

Please sign in to comment.