npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

outside-click-hook

v0.1.3

Published

A tiny, zero-dependency React hook for detecting clicks outside one or more elements.

Readme

outside-click-hook

npm version CI Bundle size License: MIT

A tiny, zero-dependency, TypeScript-first React hook for detecting interactions outside one or more elements.

Installation

npm install outside-click-hook
pnpm add outside-click-hook
yarn add outside-click-hook

Quick Start

import { useClickOutside } from "outside-click-hook";

export function Menu({ onClose }: { onClose: () => void }) {
  const ref = useClickOutside<HTMLDivElement>(() => {
    onClose();
  });

  return <div ref={ref}>Menu content</div>;
}

API

Create and return one ref

const ref = useClickOutside(() => {
  console.log("Outside click");
});

Configure behavior

const ref = useClickOutside(onClose, {
  enabled: true,
  event: "pointerdown",
});

Use existing refs

const modalRef = useRef<HTMLDivElement>(null);
const dropdownRef = useRef<HTMLDivElement>(null);

useClickOutside([modalRef, dropdownRef], onClose);

Options

| Option | Type | Default | Description | | --- | --- | --- | --- | | enabled | boolean | true | Enables or disables all listeners. | | event | "click" \| "mousedown" \| "mouseup" \| "pointerdown" | "pointerdown" | Document event used for outside interactions. | | escapeKey | boolean | true | Calls the handler when Escape is pressed. | | ignoreScrollbar | boolean | true | Ignores clicks on the browser scrollbar. | | ignore | Array<React.RefObject<HTMLElement \| null>> | [] | Refs that should not trigger the outside handler. |

Examples

Modal

import { useClickOutside } from "outside-click-hook";

function Modal({ onClose }: { onClose: () => void }) {
  const ref = useClickOutside<HTMLDivElement>(onClose, { escapeKey: true });

  return (
    <div role="dialog" aria-modal="true" ref={ref}>
      Modal content
    </div>
  );
}

Dropdown with ignored trigger

const buttonRef = useRef<HTMLButtonElement>(null);
const menuRef = useClickOutside<HTMLDivElement>(onClose, {
  ignore: [buttonRef],
});

Popover with multiple refs

useClickOutside([triggerRef, popoverRef], onClose, {
  enabled: open,
  event: "pointerdown",
});

More complete examples live in examples.

TypeScript

The package is written in TypeScript and ships generated declaration files. Pass the element type as a generic when using the returned ref:

const ref = useClickOutside<HTMLButtonElement>(onOutside);

The handler receives the native event that triggered it:

const ref = useClickOutside<HTMLDivElement>((event) => {
  event.type;
});

Browser Compatibility

pointerdown is the default because it works well across mouse, pen, and touch input in modern browsers. You can switch to click, mousedown, or mouseup when a specific interaction model needs it.

The hook is SSR safe: it does not access window or document during render.

FAQ

Does this package support React 19?

Yes. React is a peer dependency and the supported range supports React 18 and newer.

Does the hook work with portals?

Yes. The listener runs on document in the capture phase and uses composedPath() when available, which helps with portals and shadow DOM boundaries.

Does it have runtime dependencies?

No. The package has zero runtime dependencies.

Why is Escape enabled by default?

Escape is a common close gesture for modals, popovers, context menus, and dropdowns. Disable it with escapeKey: false.

Contributing

  1. Install dependencies with npm install.
  2. Run npm run lint.
  3. Run npm run typecheck.
  4. Run npm test.
  5. Run npm run build.

Please add or update tests for behavior changes.

License

MIT (c) outside-click-hook contributors