Skip to content

Commit

Permalink
Clear on open change
Browse files Browse the repository at this point in the history
  • Loading branch information
atomiks committed Jan 8, 2025
1 parent fb043b6 commit ae4e611
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/react/src/menu/root/useMenuRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,21 @@ export function useMenuRoot(parameters: useMenuRoot.Parameters): useMenuRoot.Ret
},
});

const clearStickIfOpenTimeout = useEventCallback(() => {
clearTimeout(stickIfOpenTimeoutRef.current);
});

React.useEffect(() => {
if (!open) {
clearStickIfOpenTimeout();
}
}, [clearStickIfOpenTimeout, open]);

React.useEffect(() => {
return () => {
clearTimeout(stickIfOpenTimeoutRef.current);
clearStickIfOpenTimeout();
};
}, []);
}, [clearStickIfOpenTimeout]);

const floatingRootContext = useFloatingRootContext({
elements: {
Expand All @@ -126,7 +136,7 @@ export function useMenuRoot(parameters: useMenuRoot.Parameters): useMenuRoot.Ret
if (isHover) {
// Only allow "patient" clicks to close the popover if it's open.
// If they clicked within 500ms of the popover opening, keep it open.
clearTimeout(stickIfOpenTimeoutRef.current);
clearStickIfOpenTimeout();
stickIfOpenTimeoutRef.current = window.setTimeout(() => {
setStickIfOpen(false);
}, PATIENT_CLICK_THRESHOLD);
Expand Down

0 comments on commit ae4e611

Please sign in to comment.