Skip to content

Commit a84d737

Browse files
committed
fix: improve hover interaction timeout handling in RenderNode and useHoverInteractions for render perfomance
1 parent 13432b6 commit a84d737

2 files changed

Lines changed: 36 additions & 24 deletions

File tree

src/react-obj-view/components/RenderNode.tsx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,33 @@ export const RenderNode: React.FC<ObjectViewRenderRowProps> = (props) => {
5050
[_options, isSearchMatch]
5151
)
5252

53-
const actionActiveTimeoutRef = useRef<any>(undefined)
5453
const [actionActive, setActionActive] = useState(false)
55-
54+
const actionActiveRef = useRef({ timeout: undefined as any, active: actionActive })
55+
actionActiveRef.current.active = actionActive;
56+
5657
const onMouseEnter = useCallback(
5758
() => {
5859
options.onMouseEnter(renderIndex)
59-
clearTimeout(actionActiveTimeoutRef.current);
60-
actionActiveTimeoutRef.current = setTimeout(() => setActionActive(true), 40)
61-
// console.log("Enter", renderIndex)
60+
clearTimeout(actionActiveRef.current.timeout);
61+
actionActiveRef.current.timeout = setTimeout(
62+
() => {
63+
setActionActive(true)
64+
65+
},
66+
50
67+
)
6268
},
6369
[options.onMouseEnter, renderIndex]
6470
)
6571

6672
const onMouseLeave = useCallback(
6773
() => {
6874
options.onMouseLeave(renderIndex)
69-
clearTimeout(actionActiveTimeoutRef.current);
70-
setActionActive(false);
71-
// console.log("Leave", renderIndex)
72-
75+
clearTimeout(actionActiveRef.current.timeout);
76+
actionActiveRef.current.timeout = setTimeout(
77+
() => actionActiveRef.current.active && setActionActive(false),
78+
50
79+
)
7380
},
7481
[options.onMouseLeave, renderIndex]
7582
)

src/react-obj-view/hooks/useHoverInteractions.tsx

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,31 @@ export function useHoverInteractions(
1616

1717
const onMouseEnter = useCallback(
1818
(index: number) => {
19-
const { getNodeByIndex, childCount } = ref.current
20-
let containerStyles = containerRef?.current?.style;
21-
if (index < childCount && containerStyles) {
22-
let node = getNodeByIndex(index)
23-
if (!node) return;
24-
let parentIndex = node.childCount > 1
25-
? node.parentIndex.at(-1)
26-
: node.parentIndex.at(-2)
27-
?? 0
28-
// console.log({ parentIndex })
29-
containerStyles.setProperty('--active-index', String(index));
30-
containerStyles.setProperty('--active-parent', String(parentIndex));
31-
}
19+
20+
clearTimeout(ref.current.timeout);
21+
22+
ref.current.timeout = setTimeout(() => {
23+
24+
const { getNodeByIndex, childCount } = ref.current
25+
let containerStyles = containerRef?.current?.style;
26+
if (index < childCount && containerStyles) {
27+
let node = getNodeByIndex(index)
28+
if (!node) return;
29+
let parentIndex = node.childCount > 1
30+
? node.parentIndex.at(-1)
31+
: node.parentIndex.at(-2)
32+
?? 0
33+
containerStyles.setProperty('--active-index', String(index));
34+
containerStyles.setProperty('--active-parent', String(parentIndex));
35+
}
36+
}, 50);
37+
3238
},
3339
[containerRef, ref]
3440
);
3541

3642
const onMouseLeave = useCallback(
3743
(index: number) => {
38-
3944
clearTimeout(ref.current.timeout);
4045

4146
ref.current.timeout = setTimeout(() => {
@@ -51,7 +56,7 @@ export function useHoverInteractions(
5156
}
5257

5358
}
54-
}, 25);
59+
}, 100);
5560
},
5661
[containerRef, ref]
5762
);

0 commit comments

Comments
 (0)