Skip to content

Commit

Permalink
feat(utils): use-outside-click hook
Browse files Browse the repository at this point in the history
  • Loading branch information
oxiqod committed Sep 19, 2024
1 parent 85488de commit 33fdbed
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions utils/use-outside-click/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@atls-utils/use-outside-click",
"version": "1.0.0",
"license": "BSD-3-Clause",
"type": "module",
"main": "./src/index.ts",
"files": [
"dist"
],
"scripts": {
"build": "yarn library build",
"prepack": "yarn run build",
"postpack": "rm -rf dist"
},
"devDependencies": {
"@types/react": "18.2.48"
},
"peerDependencies": {
"react": "*",
"react-dom": "*"
},
"publishConfig": {
"access": "public",
"exports": {
"./package.json": "./package.json",
".": {
"import": "./dist/index.js",
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"main": "dist/index.js",
"types": "dist/index.d.ts"
}
}
1 change: 1 addition & 0 deletions utils/use-outside-click/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './use-outside-click.hook.js'
24 changes: 24 additions & 0 deletions utils/use-outside-click/src/use-outside-click.hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { UseOutsideClickType } from './use-outside-click.interfaces.js'

import { useEffect } from 'react'
import { useRef } from 'react'

export const useOutsideClick: UseOutsideClickType = (action) => {
const ref = useRef<HTMLDivElement>(null)

useEffect(() => {
const handleClickOutside = (event: MouseEvent): void => {
if (ref.current && !ref.current.contains(event.target as Node)) {
action()
}
}

document.addEventListener('mousedown', handleClickOutside)

return () => {
document.removeEventListener('mousedown', handleClickOutside)
}
}, [action])

return { ref }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type UseOutsideClickType = (action: VoidFunction) => { ref: React.RefObject<HTMLDivElement> }
11 changes: 11 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,17 @@ __metadata:
languageName: unknown
linkType: soft

"@atls-utils/use-outside-click@workspace:utils/use-outside-click":
version: 0.0.0-use.local
resolution: "@atls-utils/use-outside-click@workspace:utils/use-outside-click"
dependencies:
"@types/react": "npm:18.2.48"
peerDependencies:
react: "*"
react-dom: "*"
languageName: unknown
linkType: soft

"@atls-utils/use-popover@workspace:utils/use-popover":
version: 0.0.0-use.local
resolution: "@atls-utils/use-popover@workspace:utils/use-popover"
Expand Down

0 comments on commit 33fdbed

Please sign in to comment.