Clicking a control to open a panel, and then clicking anywhere outside that panel to dismiss it, is a conventional interaction — and the underlying mechanism is simple. Once the panel is visible, attach a listener to window for further events. When an event arrives, test whether it originated inside the panel with thing.contains(event.target); if it did not, close the panel.

Watching the propagation path

Kitty Giraudel documented the pattern, and flagged one requirement that quickly breaks naive implementations. The click on the toggle must not propagate upward:

we have to stop the propagationg of the click event on the toggle itself. Otherwise, it goes up to the window click listener, and since the toggle is not contained within the menu, it would close the latter as soon as we try to open it.

Without that guard, the opening click reaches the window listener, misses the containment check because the toggle sits outside the menu, and the menu closes immediately after it opens.

A component wrapper rather than a hook

The same pattern appears in many places on CodePen's codebase, and like Giraudel's work it is implemented in React. The CodePen version is a component wrapper rather than a function or hook, used generically around any content that should respond to an outside click:

<ClickOutsideDetector 
  listen 
  onClickOutside={() => { closeTheThing(); }}
>
  A Menu or Modal or something.
</ClickOutsideDetector>

Several additions sit on top of the basic mechanism:

  • A component prop lets the wrapper render as the semantically appropriate element instead of always being a <div>.
  • The listen prop toggles whether the wrapper is currently listening for events, providing a quick way to short-circuit it.
  • Pressing ESC is treated as equivalent to an outside click.
  • Touch events are handled alongside clicks.
  • A click landing in an <iframe> produces a blur on window rather than a click, and that case is handled.
  • Elements can be passed in as exceptions, so specific targets that should not count as an outside click are declared explicitly instead of relying on the stopPropagation trick.

Edge cases that keep the component moving

The work is a good illustration of real-world development: a single small behavior, but with a long list of considerations and edge cases, and it is never truly finished. The component was touched again only a few months ago, because a third-party tool changed how it operates in a way that affected iframes on the page. The fix was to watch for a blur event and then inspect the classList of document.activeElement to determine whether that element was the one consuming the outside click.

A slightly simplified version of the CodePen component is available as a demo.

One behavior still missing

Giraudel's post opens with a requirement the CodePen implementation does not yet cover:

we needed a way to close the menu when clicking outside of it or tabbing out of it.

Keyboard focus leaving the menu is not handled, and it has been noted as a TODO in the code.