Skip to content

Commit 127a303

Browse files
committed
fix: update hooks when users changes callbacks, fixes #17
1 parent 0f62e2a commit 127a303

File tree

1 file changed

+39
-21
lines changed

1 file changed

+39
-21
lines changed

src/Effect.tsx

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {hideOthers} from 'aria-hidden';
55
import {InteractivityDisabler} from "./InteractivityDisabler";
66
import {EffectProps} from "./types";
77
import {focusHiddenMarker} from "./medium";
8+
import {useEffect, useRef, useState} from "react";
89

910
const extractRef = (ref: React.RefObject<any> | HTMLElement): HTMLElement => (
1011
('current' in ref) ? ref.current : ref
@@ -22,12 +23,11 @@ export function Effect(
2223
onDeactivation,
2324
noIsolation
2425
}: EffectProps) {
25-
React.useEffect(() => {
26-
let _undo = () => {
27-
return
28-
};
29-
let lastEventTarget: EventTarget;
26+
const [activeNode, setActive] = useState<HTMLElement | null>(null);
3027

28+
const lastEventTarget = useRef<EventTarget>(null);
29+
30+
React.useEffect(() => {
3131
const onKeyDown = (event: KeyboardEvent) => {
3232
if (event.defaultPrevented) {
3333
return;
@@ -38,7 +38,7 @@ export function Effect(
3838
};
3939

4040
const onClick = (event: MouseEvent | TouchEvent) => {
41-
if (event.defaultPrevented || event.target === lastEventTarget) {
41+
if (event.defaultPrevented || event.target === lastEventTarget.current) {
4242
return;
4343
}
4444
if (
@@ -54,41 +54,59 @@ export function Effect(
5454
}
5555
};
5656

57+
if (activeNode) {
58+
document.addEventListener('keydown', onKeyDown);
59+
document.addEventListener('click', onClick);
60+
document.addEventListener('touchend', onClick);
61+
62+
return () => {
63+
document.removeEventListener('keydown', onKeyDown);
64+
document.removeEventListener('click', onClick);
65+
document.removeEventListener('touchend', onClick);
66+
}
67+
}
68+
}, [activeNode, onClickOutside, onEscapeKey]);
69+
70+
useEffect(() => {
71+
if (activeNode) {
72+
if (onActivation) {
73+
onActivation(activeNode);
74+
}
75+
} else {
76+
if (onDeactivation) {
77+
onDeactivation();
78+
}
79+
}
80+
}, [activeNode]);
81+
82+
useEffect(() => {
83+
let _undo = () => null;
84+
5785
const onNodeActivation = (node: HTMLElement) => {
5886
_undo = hideOthers(
5987
[node, ...(shards || []).map(extractRef)],
6088
document.body,
6189
noIsolation ? undefined : focusHiddenMarker
6290
);
63-
if (onActivation) {
64-
onActivation(node);
65-
}
66-
document.addEventListener('keydown', onKeyDown);
67-
document.addEventListener('click', onClick);
68-
document.addEventListener('touchend', onClick);
91+
92+
setActive(node);
6993
};
7094

7195
const onNodeDeactivation = () => {
7296
_undo();
73-
if (onDeactivation) {
74-
onDeactivation();
75-
}
76-
document.removeEventListener('keydown', onKeyDown);
77-
document.removeEventListener('click', onClick);
78-
document.removeEventListener('touchend', onClick);
97+
setActive(null);
7998
};
8099

81100
setLockProps({
82101
onClick: (e: React.MouseEvent) => {
83-
lastEventTarget = e.target
102+
lastEventTarget.current = e.target
84103
},
85104
onTouchEnd: (e: React.TouchEvent) => {
86-
lastEventTarget = e.target
105+
lastEventTarget.current = e.target
87106
},
88107
onActivation: onNodeActivation,
89108
onDeactivation: onNodeDeactivation,
90109
});
91-
92110
}, []);
93111

94112
return (

0 commit comments

Comments
 (0)