useOnClickOutside

Custom hook that handles clicks outside a specified element.

Custom hook that handles clicks outside a specified element.

Usage

import { useOnClickOutside } from "@tilt-legal/cubitt-components/utilities/hooks";

const containerRef = useRef(null);
useOnClickOutside([containerRef], () => {
  // Handle clicks outside the container.
});

API

Signature

declare function useOnClickOutside<T extends HTMLElement = HTMLElement>(ref: RefObject<T> | RefObject<T>[], handler: (event: MouseEvent | TouchEvent | FocusEvent) => void, eventType?: EventType, eventListenerOptions?: AddEventListenerOptions): void;

Parameters

ParameterTypeDescriptionDefault
ref`RefObject<T>RefObject<T>[]`The React ref object(s) representing the element(s) to watch for outside clicks.
handler`(event: MouseEventTouchEventFocusEvent) => void`
[eventType]EventTypeThe mouse event type to listen for (optional, default is 'mousedown').-
[eventListenerOptions]?AddEventListenerOptionsThe options object to be passed to the addEventListener method (optional).-

Return Value

ValueTypeDescription
returnvoidValue returned by the hook.

On this page