Skip to content

Commit 3071e25

Browse files
committed
optimize adding onPointerMove
1 parent 4a44a86 commit 3071e25

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

packages/@react-aria/toast/src/useToastRegion.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,6 @@ export function useToastRegion<T>(props: AriaToastRegionProps, state: ToastState
4747
let isHovered = useRef(false);
4848
let pointerPosition = useRef({x: 0, y: 0});
4949

50-
useEffect(() => {
51-
let onPointerMove = (e: PointerEvent) => {
52-
pointerPosition.current = {x: e.clientX, y: e.clientY};
53-
};
54-
if (state.visibleToasts.length > 1) {
55-
document.addEventListener('pointermove', onPointerMove);
56-
}
57-
return () => {
58-
document.removeEventListener('pointermove', onPointerMove);
59-
};
60-
}, [state.visibleToasts.length]);
61-
6250
let {hoverProps} = useHover({
6351
onHoverStart: () => {
6452
isHovered.current = true;
@@ -87,6 +75,20 @@ export function useToastRegion<T>(props: AriaToastRegionProps, state: ToastState
8775
prevToastCount.current = currentCount;
8876
}, [ref, state]);
8977

78+
useEffect(() => {
79+
let onPointerMove = (e: PointerEvent) => {
80+
pointerPosition.current = {x: e.clientX, y: e.clientY};
81+
};
82+
if (prevToastCount.current === 1 && state.visibleToasts.length === 2) {
83+
document.addEventListener('pointermove', onPointerMove);
84+
} else if (prevToastCount.current === 2 && state.visibleToasts.length === 1) {
85+
document.removeEventListener('pointermove', onPointerMove);
86+
}
87+
return () => {
88+
document.removeEventListener('pointermove', onPointerMove);
89+
};
90+
}, [state.visibleToasts]);
91+
9092
// Manage focus within the toast region.
9193
// If a focused containing toast is removed, move focus to the next toast, or the previous toast if there is no next toast.
9294
// We might be making an assumption with how this works if someone implements the priority queue differently, or

0 commit comments

Comments
 (0)