Skip to content

Commit 9d53847

Browse files
committed
feat: fix review useOutsideClick
1 parent ec45f3f commit 9d53847

File tree

6 files changed

+47
-53
lines changed

6 files changed

+47
-53
lines changed

src/components/utils/useOnClickOutside/index.ts

-2
This file was deleted.

src/components/utils/useOnClickOutside/useOnClickOutside.tsx

-45
This file was deleted.

src/components/utils/useOnClickOutside/__tests__/Demo.tsx src/components/utils/useOutsideClick/__tests__/Demo.tsx

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable jsx-a11y/click-events-have-key-events */
22
import React from 'react';
33

4-
import {useOnClickOutside} from '../useOnClickOutside';
4+
import {useOutsideClick} from '../useOutsideClick';
55

66
export const Demo = () => {
77
const observerRef = React.useRef(null);
@@ -15,10 +15,9 @@ export const Demo = () => {
1515
setStatus(1);
1616
};
1717

18-
useOnClickOutside({
19-
observedRef: observerRef,
20-
enabled: true,
21-
handleCallback: handleOutsideClick,
18+
useOutsideClick({
19+
ref: observerRef,
20+
handler: handleOutsideClick,
2221
});
2322

2423
return (

src/components/utils/useOnClickOutside/__tests__/useOnClickOutside.test.tsx src/components/utils/useOutsideClick/__tests__/useOutsideClick.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import userEvent from '@testing-library/user-event';
55

66
import {Demo} from './Demo';
77

8-
test('Check useOnClickOutside correct work', async () => {
8+
test('Check useOutsideClick correct work', async () => {
99
render(<Demo />);
1010

1111
expect(screen.getByRole('heading')).toHaveTextContent('0');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export {useOutsideClick} from './useOutsideClick';
2+
export type {UseOutsideClickProps} from './useOutsideClick';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React from 'react';
2+
3+
export interface UseOutsideClickProps<T> {
4+
ref: React.RefObject<T>;
5+
handler?: () => void;
6+
}
7+
8+
type UseOutsideClickType = <K extends HTMLElement>(props: UseOutsideClickProps<K>) => void;
9+
10+
/**
11+
* Hook for observing clicks outside a given target
12+
*
13+
* @param ref - purpose of observation
14+
* @param handler - callback when a click is triggered outside the observation target
15+
*
16+
* @return - nothing
17+
*/
18+
export const useOutsideClick: UseOutsideClickType = ({ref, handler}) => {
19+
React.useEffect(() => {
20+
if (ref) {
21+
const callback = (e: MouseEvent | TouchEvent) => {
22+
const elem = ref?.current;
23+
24+
if (elem && !elem.contains(e.target as Node) && handler) {
25+
handler();
26+
}
27+
};
28+
29+
window.addEventListener('click', callback, {capture: true});
30+
window.addEventListener('touchstart', callback, {capture: true});
31+
32+
return () => {
33+
window.removeEventListener('click', callback, {capture: true});
34+
window.removeEventListener('touchstart', callback, {capture: true});
35+
};
36+
}
37+
38+
return undefined;
39+
}, [handler, ref]);
40+
};

0 commit comments

Comments
 (0)