mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-09 23:15:36 +00:00
feat: add dropdown component
This commit is contained in:
22
inertia/hooks/use_click_outside.tsx
Normal file
22
inertia/hooks/use_click_outside.tsx
Normal file
@@ -0,0 +1,22 @@
|
||||
import { RefObject, useEffect } from 'react';
|
||||
|
||||
// Source : https://stackoverflow.com/a/63359693
|
||||
|
||||
/**
|
||||
* This Hook can be used for detecting clicks outside the Opened Menu
|
||||
*/
|
||||
export default function useClickOutside(
|
||||
ref: RefObject<HTMLElement>,
|
||||
onClickOutside: () => void
|
||||
) {
|
||||
useEffect(() => {
|
||||
function handleClickOutside(event: MouseEvent) {
|
||||
if (ref?.current && !ref.current?.contains(event.target as any)) {
|
||||
onClickOutside();
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('mousedown', handleClickOutside);
|
||||
return () => document.removeEventListener('mousedown', handleClickOutside);
|
||||
}, [ref, onClickOutside]);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useState } from 'react';
|
||||
|
||||
const useModal = (defaultValue: boolean = false) => {
|
||||
const useToggle = (defaultValue: boolean = false) => {
|
||||
const [isShowing, setIsShowing] = useState<boolean>(defaultValue);
|
||||
|
||||
const toggle = () => setIsShowing((value) => !value);
|
||||
@@ -15,4 +15,4 @@ const useModal = (defaultValue: boolean = false) => {
|
||||
};
|
||||
};
|
||||
|
||||
export default useModal;
|
||||
export default useToggle;
|
||||
|
||||
Reference in New Issue
Block a user