This PR introduces the useClickOutside hook for the library, providing a standardized way to handle clicks occurring outside specific elements. This is essential for components like dropdowns, menus, and popovers.
- Flexible Targets: Supports both a single ref and an array of refs.
- Exclusion Logic: Allows excluding specific elements (via
excludeRefs) to prevent unwanted closures when clicking on triggers or related UI. - Event Handling: Listens for both
mousedownandtouchstartto ensure mobile compatibility. - Conditional Activation: Includes an
enabledflag to easily toggle the listener without re-rendering the hook logic manually. - Auto-Cleanup: Ensures listeners are properly removed on unmount.
- Created
src/hooks-d/use-click-outside.ts. - Created
src/hooks-d/use-click-outside.test.tswith comprehensive unit tests. - Migrated
src/components/menu.tsxto use the new hook. - Migrated
src/components/popover.tsxto use the new hook.
- ✅ Unit Tests: 7 test cases covering single/multiple refs, exclusion, enabled flag, and mobile touch events.
- ✅ Build: Successfully ran
npm run build.