Skip to content

Commit

Permalink
fix(@toss/react): change container array conversion logic, change use…
Browse files Browse the repository at this point in the history
…Effect order to improve readability, add dependencies (toss#481)

Co-authored-by: 박찬혁 <[email protected]>
  • Loading branch information
Collection50 and okinawaa authored May 26, 2024
1 parent 0beaed1 commit cbd7278
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/react/react/src/hooks/useOutsideClickEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ type OneOrMore<T> = T | T[];
export function useOutsideClickEffect(container: OneOrMore<HTMLElement | null>, callback: () => void) {
const containers = useRef<HTMLElement[]>([]);

useEffect(() => {
containers.current = (Array.isArray(container) ? container : [container]).filter(isNotNil);
}, [container]);

const handleDocumentClick = useCallback(
({ target }: MouseEvent | TouchEvent) => {
if (target === null) {
Expand All @@ -30,6 +26,10 @@ export function useOutsideClickEffect(container: OneOrMore<HTMLElement | null>,
[callback]
);

useEffect(() => {
containers.current = [container].flat(1).filter(isNotNil);
}, [container]);

useEffect(() => {
document.addEventListener('click', handleDocumentClick);
document.addEventListener('touchstart', handleDocumentClick);
Expand All @@ -38,5 +38,5 @@ export function useOutsideClickEffect(container: OneOrMore<HTMLElement | null>,
document.removeEventListener('click', handleDocumentClick);
document.removeEventListener('touchstart', handleDocumentClick);
};
});
}, [handleDocumentClick]);
}

0 comments on commit cbd7278

Please sign in to comment.