UNPKG

@mirawision/reactive-hooks

Version:

A comprehensive collection of 50+ React hooks for state management, UI interactions, device APIs, async operations, drag & drop, audio/speech, and more. Full TypeScript support with SSR safety.

27 lines (26 loc) 1.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useEventOnToggle = useEventOnToggle; const react_1 = require("react"); /** * Hook that conditionally adds/removes event listeners based on a dependency * * @param element - The element to attach the event listener to * @param dependency - Boolean that determines if the listener should be active * @param type - Event type to listen for * @param listener - Event listener function * @param options - Optional event listener options */ function useEventOnToggle(element, dependency, type, listener, options) { (0, react_1.useEffect)(() => { if (!element || !listener) return; if (dependency) { element.addEventListener(type, listener, options); } else { element.removeEventListener(type, listener, options); } return () => element.removeEventListener(type, listener, options); }, [dependency, element, type, listener, options]); }