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

@kaviresh01/use-clickaway

v1.0.0

Published

A zero-dependency React hook that detects clicks outside elements. Handles portals, Shadow DOM, and SSR.

Downloads

20

Readme

use-clickaway

A tiny, zero-dependency React hook that fires a callback when a click or touch occurs outside a given element. Handles React Portals, Shadow DOM, and SSR correctly.

npm version bundle size license


Why another outside-click hook?

Most existing packages:

  • ❌ Break with React Portals (modals, dropdowns rendered via createPortal)
  • ❌ Don't handle Shadow DOM (web components, browser extensions)
  • ❌ Re-attach event listeners on every render
  • ❌ Ignore touch events for mobile
  • ❌ Have unnecessary dependencies

@kaviresh01/use-clickaway fixes all of this.


Install

npm install @kaviresh01/use-clickaway
# or
yarn add @kaviresh01/use-clickaway
# or
pnpm add @kaviresh01/use-clickaway

Basic Usage

import { useRef, useState } from "react";
import { useClickaway } from "@kaviresh01/use-clickaway";

function Dropdown() {
  const [open, setOpen] = useState(false);
  const ref = useRef<HTMLDivElement>(null);

  useClickaway(ref, () => setOpen(false));

  return (
    <div ref={ref}>
      <button onClick={() => setOpen(true)}>Open</button>
      {open && <ul><li>Item 1</li><li>Item 2</li></ul>}
    </div>
  );
}

API

useClickaway(ref, handler, options?)

Parameters

| Parameter | Type | Required | Description | |-----------|------|----------|-------------| | ref | RefObject<Element> | RefObject<Element>[] | ✅ | One or more refs to watch | | handler | (event: MouseEvent \| TouchEvent) => void | ✅ | Called on outside click/touch | | options | Options | — | See below |

Options

| Option | Type | Default | Description | |--------|------|---------|-------------| | enabled | boolean | true | Toggle the listener on/off without unmounting | | detectTouch | boolean | true | Also listen for touchstart (mobile) | | ignoreRefs | RefObject<Element>[] | [] | Extra refs that should NOT trigger the handler |


Examples

Multiple refs (e.g. trigger button + dropdown panel)

const buttonRef = useRef<HTMLButtonElement>(null);
const menuRef = useRef<HTMLUListElement>(null);

useClickaway([buttonRef, menuRef], () => setOpen(false));

Conditional / toggled

useClickaway(ref, () => setOpen(false), { enabled: open });

Ignore a specific element

const tooltipRef = useRef<HTMLDivElement>(null);
const triggerRef = useRef<HTMLButtonElement>(null);

// Clicking the trigger won't close the tooltip
useClickaway(tooltipRef, () => setVisible(false), {
  ignoreRefs: [triggerRef],
});

Works with React Portals

// Even if your modal is rendered via createPortal, this still works.
const modalRef = useRef<HTMLDivElement>(null);
useClickaway(modalRef, () => setModalOpen(false));

How it works

Instead of checking event.target, this hook uses event.composedPath()[0] — the deepest element in the event's composed path. This correctly resolves clicks that originate inside:

  • React Portals — rendered outside the DOM tree of the ref
  • Shadow DOM — used by web components and browser extensions

The handler is stored in a stable ref, so the event listener is never re-attached on re-renders — only when enabled or detectTouch changes.


License

MIT