Skip to content

Commit

Permalink
Merge pull request #37 from Ashish-simpleCoder/feature/develop-hooks
Browse files Browse the repository at this point in the history
Fix working with ref for use-outside-click hook
  • Loading branch information
Ashish-simpleCoder authored May 7, 2024
2 parents b0d0c82 + 034f910 commit 7c4fbb7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-grapes-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'classic-react-hooks': minor
---

Fix working with ref for use-outside-click hook
16 changes: 16 additions & 0 deletions src/lib/use-outside-click/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ describe('use-outside-click', () => {
expect(removeSpy).toHaveBeenCalledTimes(1)
})

it('should fire listener when clicked outside of target element when ref is provided', () => {
const div = document.createElement('div')
const ref = { current: div }
const listener = vi.fn()

renderHook(() => {
useOutsideClick(ref, listener)
})

const event = new Event('click')
document.dispatchEvent(event)

expect(listener).toHaveBeenCalledTimes(1)
expect(listener).toHaveBeenCalledWith(event)
})

it('should fire listener when clicked outside of target element', () => {
const div = document.createElement('div')

Expand Down
4 changes: 2 additions & 2 deletions src/lib/use-outside-click/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export default function useOutsideClick(target: Target, handler: (event: Documen

if (
node &&
((node as Node).contains(event.target as Node) ||
('current' in node && (node.current as Node).contains(event.target as Node)))
(('contains' in node && (node as Node).contains(event.target as Node)) ||
('current' in node && 'contains' && (node.current as Node).contains(event.target as Node)))
) {
return
}
Expand Down

0 comments on commit 7c4fbb7

Please sign in to comment.