Skip to content
usefy

useClickAnyWhere

Events & gestures

A lightweight React hook for detecting document-wide click events

Install

$ npm install @usefy/use-click-any-where

Quick start

useClickAnyWhere.tsx
import { useClickAnyWhere } from "@usefy/use-click-any-where";

function ClickTracker() {
  const [lastClick, setLastClick] = useState({ x: 0, y: 0 });

  useClickAnyWhere((event) => {
    setLastClick({ x: event.clientX, y: event.clientY });
  });

  return (
    <div>
      Last click: ({lastClick.x}, {lastClick.y})
    </div>
  );
}

API reference

useClickAnyWhere(handler, options?)

A hook that listens for document-wide click events.

Parameters

ParameterTypeDescription
handler(event: MouseEvent) => voidCallback function called on every document click
optionsUseClickAnyWhereOptionsConfiguration options

Options

OptionTypeDefaultDescription
enabledbooleantrueWhether the event listener is active
capturebooleanfalseUse event capture phase instead of bubble
passivebooleanundefined (browser default)Mark the listener as passive. A passive listener cannot call event.preventDefault(). Left unset by default (non-passive) — there is no performance benefit for click, so only set it if you specifically want passive semantics.

Returns

void


Go deeper

This page is the quick reference. For every example, prop, and edge case, read the full README — or open Storybook to change props live.

More in events & gestures