Skip to content

Add useOutsideClickRef that returns a callback ref #611

Done

Description

@hyesungoh

From #270 by @wo-o29.

useOutsideClickEffect takes the elements, so each one needs its own state and setter:

const [buttonEl, setButtonEl] = useState<HTMLButtonElement | null>(null);
const [menuEl, setMenuEl] = useState<HTMLDivElement | null>(null);
useOutsideClickEffect([buttonEl, menuEl], () => setOpen(false));

<button ref={setButtonEl} />
<div ref={setMenuEl} />

A hook that returns a callback ref keeps it in one place:

const ref = useOutsideClickRef(() => setOpen(false));

<button ref={ref} />
<div ref={ref} />

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.
  • Keep listening to click, or move to pointerdown like the sketch in useOutsideClickEffect internal implementation question #270? With click, pressing inside and releasing outside can count as an outside click.

Activity

  1. Choi-Jung-Hyeon commented on Nov 22, 2026

    @Choi-Jung-Hyeon
    Contributor

    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?

  2. Antoliny0919 commented on Nov 22, 2026

    @Antoliny0919
    Contributor

    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 👍

  3. Choi-Jung-Hyeon commented on Nov 22, 2026

    @Choi-Jung-Hyeon
    Contributor

    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.

  4. hyesungoh commented on Nov 23, 2026

    @hyesungoh
    MemberAuthor

    thanks for the thoughts both 🙏 I opened #616 for this, here's what I went with

    1. keep both hooks. useOutsideClickEffect takes the targets and the callback together, which is explicit as @Antoliny0919 said
    2. useOutsideClickRef 👍
    3. 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
    4. 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 👀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions