Skip to content

Commit 6b4d12c

Browse files
committed
main 🧊 rework click outside
1 parent 2136b29 commit 6b4d12c

File tree

3 files changed

+39
-56
lines changed

3 files changed

+39
-56
lines changed

‎src/hooks/useClickOutside/useClickOutside.demo.tsx

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,30 @@ const Demo = () => {
1010
});
1111

1212
return (
13-
<div
14-
ref={clickOutsideRef}
15-
style={{
16-
width: 200,
17-
height: 200,
18-
display: 'flex',
19-
userSelect: 'none',
20-
flexDirection: 'column',
21-
justifyContent: 'center',
22-
alignItems: 'center',
23-
border: `1px solid ${counter.value < 5 ? 'red' : 'green'}`
24-
}}
25-
>
26-
<p>Click more than 5 times:</p>
27-
<p>
28-
<code>{counter.value}</code>
29-
</p>
13+
<div>
14+
<p>Click more than five times: <code>{counter.value}</code></p>
15+
16+
17+
<div
18+
ref={clickOutsideRef}
19+
style={{
20+
padding: '50px 25px',
21+
position: 'relative',
22+
display: 'flex',
23+
userSelect: 'none',
24+
flexDirection: 'column',
25+
justifyContent: 'center',
26+
borderRadius: '10px',
27+
alignItems: 'center',
28+
border: `1.5px solid ${counter.value < 5 ? 'red' : 'green'}`
29+
}}
30+
>
31+
{counter.value <= 5 && 'Click outside'}
32+
{counter.value > 5 && counter.value <= 25 && 'Nice work'}
33+
{counter.value > 25 && 'That are a lot of clicks'}
34+
</div>
3035
</div>
36+
3137
);
3238
};
3339

‎src/hooks/useClickOutside/useClickOutside.test.ts

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@ it('Should call callback when ref connected to the document', () => {
1717

1818
expect(callback).not.toBeCalled();
1919

20-
act(() => {
21-
document.dispatchEvent(new Event('mousedown'));
22-
document.dispatchEvent(new Event('touchstart'));
23-
});
20+
act(() => document.dispatchEvent(new Event('click')));
2421

25-
expect(callback).toBeCalledTimes(2);
22+
expect(callback).toBeCalledTimes(1);
2623
});
2724

2825
it('Should call callback when clicked outside the element', () => {
@@ -33,12 +30,9 @@ it('Should call callback when clicked outside the element', () => {
3330

3431
expect(callback).not.toBeCalled();
3532

36-
act(() => {
37-
document.dispatchEvent(new Event('mousedown'));
38-
document.dispatchEvent(new Event('touchstart'));
39-
});
33+
act(() => document.dispatchEvent(new Event('click')));
4034

41-
expect(callback).toBeCalledTimes(2);
35+
expect(callback).toBeCalledTimes(1);
4236
});
4337

4438
it('Should call callback when clicked outside the ref', () => {
@@ -49,12 +43,9 @@ it('Should call callback when clicked outside the ref', () => {
4943

5044
expect(callback).not.toBeCalled();
5145

52-
act(() => {
53-
document.dispatchEvent(new Event('mousedown'));
54-
document.dispatchEvent(new Event('touchstart'));
55-
});
46+
act(() => document.dispatchEvent(new Event('click')));
5647

57-
expect(callback).toBeCalledTimes(2);
48+
expect(callback).toBeCalledTimes(1);
5849
});
5950

6051
it('Should call callback when clicked outside the function that returns an element', () => {
@@ -65,12 +56,9 @@ it('Should call callback when clicked outside the function that returns an eleme
6556

6657
expect(callback).not.toBeCalled();
6758

68-
act(() => {
69-
document.dispatchEvent(new Event('mousedown'));
70-
document.dispatchEvent(new Event('touchstart'));
71-
});
59+
act(() => document.dispatchEvent(new Event('click')));
7260

73-
expect(callback).toBeCalledTimes(2);
61+
expect(callback).toBeCalledTimes(1);
7462
});
7563

7664
it('Should not call callback when clicked inside the ref', () => {
@@ -80,10 +68,7 @@ it('Should not call callback when clicked inside the ref', () => {
8068

8169
renderHook(() => useClickOutside(ref, callback));
8270

83-
act(() => {
84-
ref.current.dispatchEvent(new Event('mousedown'));
85-
ref.current.dispatchEvent(new Event('touchstart'));
86-
});
71+
act(() => ref.current.dispatchEvent(new Event('click')));
8772

8873
expect(callback).not.toBeCalled();
8974
});
@@ -96,10 +81,7 @@ it('Should not call callback when clicked inside the element', () => {
9681

9782
renderHook(() => useClickOutside(element, callback));
9883

99-
act(() => {
100-
element.dispatchEvent(new Event('mousedown'));
101-
element.dispatchEvent(new Event('touchstart'));
102-
});
84+
act(() => element.dispatchEvent(new Event('click')));
10385

10486
expect(callback).not.toBeCalled();
10587
});
@@ -113,10 +95,7 @@ it('Should not call callback when clicked inside the function that returns an el
11395

11496
renderHook(() => useClickOutside(getElement, callback));
11597

116-
act(() => {
117-
element.dispatchEvent(new Event('mousedown'));
118-
element.dispatchEvent(new Event('touchstart'));
119-
});
98+
act(() => element.dispatchEvent(new Event('click')));
12099

121100
expect(callback).not.toBeCalled();
122101
});
@@ -136,7 +115,7 @@ it('Should call callback when clicked outside the element (multiple targets)', (
136115

137116
renderHook(() => useClickOutside([element, ref, getElement], callback));
138117

139-
act(() => document.dispatchEvent(new Event('mousedown')));
118+
act(() => document.dispatchEvent(new Event('click')));
140119

141120
expect(callback).toBeCalledTimes(1);
142121
});

‎src/hooks/useClickOutside/useClickOutside.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface UseClickOutside {
1717
callback: (event: Event) => void
1818
): void;
1919

20-
<Target extends UseClickOutsideTarget | UseClickOutsideTarget[]>(
20+
<Target extends UseClickOutsideTarget>(
2121
callback: (event: Event) => void,
2222
target?: never
2323
): (node: Target) => void;
@@ -43,7 +43,7 @@ export interface UseClickOutside {
4343
* @returns {(node: Target) => void} A React ref to attach to the target element
4444
*
4545
* @example
46-
* const ref = useClickOutside<HMLDiTvElement>(() => console.log('click outside'));
46+
* const ref = useClickOutside<HTMLDivElement>(() => console.log('click outside'));
4747
*/
4848
export const useClickOutside = ((...params: any[]) => {
4949
const target = (typeof params[1] === 'undefined' ? undefined : params[0]) as
@@ -79,12 +79,10 @@ export const useClickOutside = ((...params: any[]) => {
7979
}
8080
};
8181

82-
document.addEventListener('mousedown', handler);
83-
document.addEventListener('touchstart', handler);
82+
document.addEventListener('click', handler);
8483

8584
return () => {
86-
document.removeEventListener('mousedown', handler);
87-
document.removeEventListener('touchstart', handler);
85+
document.removeEventListener('click', handler);
8886
};
8987
}, [internalRef, target]);
9088

0 commit comments

Comments
 (0)