Skip to content

Commit

Permalink
Revert stickIfOpen logic for now
Browse files Browse the repository at this point in the history
  • Loading branch information
atomiks committed Dec 26, 2024
1 parent 8f8cb9f commit d48852e
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions packages/react/src/popover/root/usePopoverRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ export function usePopoverRoot(params: usePopoverRoot.Parameters): usePopoverRoo
const [triggerElement, setTriggerElement] = React.useState<Element | null>(null);
const [positionerElement, setPositionerElement] = React.useState<HTMLElement | null>(null);
const [openReason, setOpenReason] = React.useState<OpenChangeReason | null>(null);
const [stickIfOpen, setStickIfOpen] = React.useState(true);
const [clickEnabled, setClickEnabled] = React.useState(true);

const popupRef = React.useRef<HTMLElement>(null);
const stickIfOpenTimeoutRef = React.useRef(-1);
const clickEnabledTimeoutRef = React.useRef(-1);

const [open, setOpenUnwrapped] = useControlled({
controlled: externalOpen,
Expand All @@ -57,8 +57,8 @@ export function usePopoverRoot(params: usePopoverRoot.Parameters): usePopoverRoo
state: 'open',
});

if (!open && !stickIfOpen) {
setStickIfOpen(true);
if (!open && !clickEnabled) {
setClickEnabled(true);
}

const onOpenChange = useEventCallback(onOpenChangeProp);
Expand Down Expand Up @@ -87,7 +87,7 @@ export function usePopoverRoot(params: usePopoverRoot.Parameters): usePopoverRoo

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

Expand All @@ -104,11 +104,11 @@ export function usePopoverRoot(params: usePopoverRoot.Parameters): usePopoverRoo
}

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);
stickIfOpenTimeoutRef.current = window.setTimeout(() => {
setStickIfOpen(false);
// Prevent impatient clicks from unexpectedly closing the popover.
setClickEnabled(false);
clearTimeout(clickEnabledTimeoutRef.current);
clickEnabledTimeoutRef.current = window.setTimeout(() => {
setClickEnabled(true);
}, PATIENT_CLICK_THRESHOLD);

ReactDOM.flushSync(changeState);
Expand Down Expand Up @@ -138,7 +138,8 @@ export function usePopoverRoot(params: usePopoverRoot.Parameters): usePopoverRoo
});

const click = useClick(context, {
stickIfOpen,
enabled: clickEnabled,
stickIfOpen: false,
});

const dismiss = useDismiss(context);
Expand Down

0 comments on commit d48852e

Please sign in to comment.