-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1162 from NSUWAL123/fix-project-details-ui-additions
Fix project details UI additions
- Loading branch information
Showing
4 changed files
with
206 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useState, useEffect, useCallback, useRef } from 'react'; | ||
|
||
/** | ||
* A React custom hook that handles click events outside a specific DOM element. | ||
* | ||
* Returns a ref, toggle state and a function to handle toggle state. | ||
* | ||
* @usage | ||
* Put the ref on the element. | ||
* Toggle state to change the display from none to block or vice-versa. | ||
* Toggle handler on the element to trigger the toggle. | ||
* | ||
*/ | ||
const useOutsideClick = (type = 'single') => { | ||
const ref: any = useRef(); | ||
const [toggle, setToggle] = useState<boolean>(false); | ||
const onOutsideClick = useCallback((e) => { | ||
if (!ref.current.contains(e.target)) { | ||
setToggle(false); | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (toggle) { | ||
window.addEventListener('click', onOutsideClick); | ||
} else { | ||
window.removeEventListener('click', onOutsideClick); | ||
} | ||
|
||
return () => { | ||
window.removeEventListener('click', onOutsideClick); | ||
}; | ||
}, [toggle, onOutsideClick]); | ||
|
||
const handleToggle = (): void => { | ||
if (type === 'multiple') { | ||
setToggle(true); | ||
} else { | ||
setToggle((prev) => !prev); | ||
} | ||
}; | ||
|
||
return [ref, toggle, handleToggle]; | ||
}; | ||
|
||
export default useOutsideClick; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters