diff --git a/apps/readest-app/src/components/Dropdown.tsx b/apps/readest-app/src/components/Dropdown.tsx index 020426eb..e375e786 100644 --- a/apps/readest-app/src/components/Dropdown.tsx +++ b/apps/readest-app/src/components/Dropdown.tsx @@ -1,6 +1,5 @@ import clsx from 'clsx'; import React, { useState, isValidElement, ReactElement } from 'react'; -import useOutsideClick from '@/hooks/useOutsideClick'; interface DropdownProps { className?: string; @@ -30,33 +29,30 @@ const Dropdown: React.FC = ({ onToggle?.(isOpen); }; - const handleBlur = (event: React.FocusEvent) => { - if (!dropdownRef.current?.contains(event.relatedTarget as Node)) { - setIsDropdownOpen(false); - } - }; - - const dropdownRef = useOutsideClick(() => setIsDropdownOpen(false)); const childrenWithToggle = isValidElement(children) ? React.cloneElement(children, { setIsDropdownOpen }) : children; return ( -
-
- {toggleButton} + <> + {isOpen && ( +
setIsDropdownOpen(false)} /> + )} +
+
+ {toggleButton} +
+ {isOpen && childrenWithToggle}
- {isOpen && childrenWithToggle} -
+ ); }; diff --git a/apps/readest-app/src/hooks/useOutsideClick.ts b/apps/readest-app/src/hooks/useOutsideClick.ts deleted file mode 100644 index df7a39e9..00000000 --- a/apps/readest-app/src/hooks/useOutsideClick.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { useEffect, useRef } from 'react'; - -function useOutsideClick(callback: () => void) { - const ref = useRef(null); - - useEffect(() => { - const handleClickOutside = (event: MouseEvent | Event) => { - if (event instanceof MouseEvent) { - if (ref.current && !ref.current.contains(event.target as Node)) { - callback(); - } - } else if (event instanceof MessageEvent) { - if (event.data && event.data.type === 'iframe-mousedown') { - callback(); - } - } - }; - - window.addEventListener('mousedown', handleClickOutside); - window.addEventListener('message', handleClickOutside); - - return () => { - window.removeEventListener('mousedown', handleClickOutside); - window.removeEventListener('message', handleClickOutside); - }; - }, []); - - return ref; -} - -export default useOutsideClick;