Skip to content

Commit

Permalink
feat: Ignore classes in useClickOutside hook (#2135)
Browse files Browse the repository at this point in the history
  • Loading branch information
omernave authored May 28, 2024
1 parent 229cb72 commit ea42d2b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ This hook is used when you want to capture click events outside your component.
description="Callback function to execute on outside clicks."
required
/>
<FunctionArgument
name="ignoreClasses"
type="string[]"
description="A list of classes to ignore when checking if the click is outside the element."
/>
<FunctionArgument
name="eventName"
type="keyof HTMLElementEventMap | string"
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/hooks/useClickOutside/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { GenericEventCallback } from "../../types/events";
export default function useClickOutside({
ref,
callback,
ignoreClasses,
eventName = "click"
}: {
ref: RefObject<HTMLElement>;
callback: GenericEventCallback;
ignoreClasses?: string[];
eventName?: keyof HTMLElementEventMap | string;
}) {
const onClickOutsideListener = useCallback(
Expand All @@ -17,6 +19,11 @@ export default function useClickOutside({
return;
}

const shouldIgnoreClasses = ignoreClasses && event.target instanceof HTMLElement;
if (shouldIgnoreClasses && event.target.closest(ignoreClasses.join(","))) {
return;
}

callback(event);
},

Expand Down

0 comments on commit ea42d2b

Please sign in to comment.