You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add useOutsideClickRef that returns a callback ref #611
useImpressionRef and useIntersectionObserver already return a callback ref.
Notes:
React 18 ignores the cleanup a ref callback returns and calls it with null, so with one ref on several elements it can't tell which one left. Dropping elements that are no longer isConnected when a click comes in works on both 18 and 19.
The ref has to keep its identity across renders, or React detaches and reattaches it on every render.
Calling the ref by hand (ref(element)) still covers elements the component doesn't render.
Open questions:
Add useOutsideClickRef and deprecate useOutsideClickEffect, or keep both?
useOutsideClickRef follows useImpressionRef. Other names are welcome.
I'd like to work on this once the open questions are settled. My take on them:
Add useOutsideClickRef and deprecate useOutsideClickEffect, removed in v2 like useDebounce/useThrottle (Reuse internal hooks in deprecated useDebounce and useThrottle #612). Calling the ref by hand covers elements the component doesn't render, so I don't see a case only the old hook can handle.
useOutsideClickRef sounds good to me.
I'd keep click here so both hooks behave the same, and decide pointerdown for both separately. With pointerdown, pressing a toggle button outside closes the menu and the button's click opens it again.
#615 changes the click handling in useOutsideClickEffect, so I'd move that handling into _internal and share it, after #615 is merged. Should this go to v1-base?
Thanks for proposing this and starting the discussion. I think it’s a great idea and a valuable improvement.
Let me share my thoughts ...
Add useOutsideClickRef and deprecate useOutsideClickEffect, or keep both?
If there are cases where the behavioral differences still make useOutsideClickEffect useful, I think we should keep it. Before deprecating it, we should make sure existing use cases can migrate to the new API without much difficulty.
This will probably depend on how we implement useOutsideClickRef.
One thing I like about useOutsideClickEffect is that the target elements and the callback are specified together, which makes it a little more explicit. With useOutsideClickRef, the callback would be passed to the hook, while the same ref could be attached to multiple elements throughout the JSX.
So I think useOutsideClickEffect still has some advantages. It’s a trade off 😁
That said, I’m not sure how many users would prefer this approach, or whether that alone is a strong enough reason to keep the hook.
The name useOutsideClickRef follows useImpressionRef. Other suggestions are welcome.
useOutsideClickRef sounds good to me. Even if useOutsideClickEffect is eventually deprecated and removed, I’d still prefer the Ref suffix for consistency with the other hooks.
Should we keep listening to click, or switch to pointerdown like the sketch in #270? With click, pressing inside and releasing outside can count as an outside click.
Most of the similar hooks I looked at in other libraries use mousedown and touchstart.
For outside click detection, wouldn’t it be unexpected for pressing inside and then releasing outside to trigger the callback?
I think pointerdonw would be a good choice 👍
Agreed that press-inside/release-outside shouldn't fire. My concern with pointerdown is a toggle button outside the menu: pointerdown closes the menu, then the button's click opens it again. Users would need to add the button to the targets. Happy to go with pointerdown if that's acceptable.
thanks for the thoughts both 🙏 I opened #616 for this, here's what I went with
keep both hooks. useOutsideClickEffect takes the targets and the callback together, which is explicit as @Antoliny0919 said
useOutsideClickRef 👍
options object: useOutsideClickRef({ onOutsideClick, enabled })
v1 hooks are moving to options objects (useInterval, useTimeout), so I followed that instead of the positional sketch above enabled (default true) is in from the start, since a trigger holding the ref would otherwise call back while the menu is closed
it decides on a capture click and remembers where the pointerdown started
so press inside → release outside won't fire, and starting a touch scroll won't close it either (it would with pointerdown only). same as VueUse and React Aria
the trigger still needs the same ref
the capture listener never sees the click that opened it, so fix(useOutsideClickEffect): ignore the click that mounted the hook #615's handling isn't needed here and the two hooks stay separate
@Choi-Jung-Hyeon thanks for offering 🙏 I already had a plan for this one so I went ahead. reviews are very welcome 👀
Activity
Choi-Jung-Hyeon commentedon Nov 22, 2026
I'd like to work on this once the open questions are settled. My take on them:
useOutsideClickRefand deprecateuseOutsideClickEffect, removed in v2 likeuseDebounce/useThrottle(Reuse internal hooks in deprecated useDebounce and useThrottle #612). Calling the ref by hand covers elements the component doesn't render, so I don't see a case only the old hook can handle.useOutsideClickRefsounds good to me.clickhere so both hooks behave the same, and decidepointerdownfor both separately. Withpointerdown, pressing a toggle button outside closes the menu and the button'sclickopens it again.#615 changes the click handling in
useOutsideClickEffect, so I'd move that handling into_internaland share it, after #615 is merged. Should this go tov1-base?Antoliny0919 commentedon Nov 22, 2026
Thanks for proposing this and starting the discussion. I think it’s a great idea and a valuable improvement.
Let me share my thoughts ...
If there are cases where the behavioral differences still make
useOutsideClickEffectuseful, I think we should keep it. Before deprecating it, we should make sure existing use cases can migrate to the new API without much difficulty.This will probably depend on how we implement
useOutsideClickRef.One thing I like about
useOutsideClickEffectis that the target elements and the callback are specified together, which makes it a little more explicit. WithuseOutsideClickRef, the callback would be passed to the hook, while the same ref could be attached to multiple elements throughout the JSX.So I think
useOutsideClickEffectstill has some advantages. It’s a trade off 😁That said, I’m not sure how many users would prefer this approach, or whether that alone is a strong enough reason to keep the hook.
useOutsideClickRefsounds good to me. Even ifuseOutsideClickEffectis eventually deprecated and removed, I’d still prefer the Ref suffix for consistency with the other hooks.Most of the similar hooks I looked at in other libraries use
mousedownandtouchstart.For outside click detection, wouldn’t it be unexpected for pressing inside and then releasing outside to trigger the callback?
I think
pointerdonwwould be a good choice 👍Choi-Jung-Hyeon commentedon Nov 22, 2026
Agreed that press-inside/release-outside shouldn't fire. My concern with
pointerdownis a toggle button outside the menu:pointerdowncloses the menu, then the button'sclickopens it again. Users would need to add the button to the targets. Happy to go withpointerdownif that's acceptable.hyesungoh commentedon Nov 23, 2026
thanks for the thoughts both 🙏 I opened #616 for this, here's what I went with
useOutsideClickEffecttakes the targets and the callback together, which is explicit as @Antoliny0919 saiduseOutsideClickRef👍useOutsideClickRef({ onOutsideClick, enabled })v1 hooks are moving to options objects (
useInterval,useTimeout), so I followed that instead of the positional sketch aboveenabled(defaulttrue) is in from the start, since a trigger holding the ref would otherwise call back while the menu is closedclickand remembers where thepointerdownstartedso press inside → release outside won't fire, and starting a touch scroll won't close it either (it would with
pointerdownonly). same as VueUse and React Ariathe trigger still needs the same ref
the capture listener never sees the click that opened it, so fix(useOutsideClickEffect): ignore the click that mounted the hook #615's handling isn't needed here and the two hooks stay separate
@Choi-Jung-Hyeon thanks for offering 🙏 I already had a plan for this one so I went ahead. reviews are very welcome 👀