Skip to content

Commit

Permalink
optimize adding onPointerMove
Browse files Browse the repository at this point in the history
  • Loading branch information
reidbarber committed Jan 31, 2025
1 parent 4a44a86 commit 3071e25
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions packages/@react-aria/toast/src/useToastRegion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,6 @@ export function useToastRegion<T>(props: AriaToastRegionProps, state: ToastState
let isHovered = useRef(false);
let pointerPosition = useRef({x: 0, y: 0});

useEffect(() => {
let onPointerMove = (e: PointerEvent) => {
pointerPosition.current = {x: e.clientX, y: e.clientY};
};
if (state.visibleToasts.length > 1) {
document.addEventListener('pointermove', onPointerMove);
}
return () => {
document.removeEventListener('pointermove', onPointerMove);
};
}, [state.visibleToasts.length]);

let {hoverProps} = useHover({
onHoverStart: () => {
isHovered.current = true;
Expand Down Expand Up @@ -87,6 +75,20 @@ export function useToastRegion<T>(props: AriaToastRegionProps, state: ToastState
prevToastCount.current = currentCount;
}, [ref, state]);

useEffect(() => {
let onPointerMove = (e: PointerEvent) => {
pointerPosition.current = {x: e.clientX, y: e.clientY};
};
if (prevToastCount.current === 1 && state.visibleToasts.length === 2) {
document.addEventListener('pointermove', onPointerMove);
} else if (prevToastCount.current === 2 && state.visibleToasts.length === 1) {
document.removeEventListener('pointermove', onPointerMove);
}
return () => {
document.removeEventListener('pointermove', onPointerMove);
};
}, [state.visibleToasts]);

// Manage focus within the toast region.
// If a focused containing toast is removed, move focus to the next toast, or the previous toast if there is no next toast.
// We might be making an assumption with how this works if someone implements the priority queue differently, or
Expand Down

0 comments on commit 3071e25

Please sign in to comment.