Skip to content

Commit

Permalink
Merge pull request #141 from Selody-project/140-bug-dropdown-close
Browse files Browse the repository at this point in the history
issue #140 fix: Fix dropdown close error
  • Loading branch information
sikkzz authored Nov 25, 2023
2 parents c535af8 + 48871d8 commit d534872
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/components/Header/Header/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,19 @@ const Header = () => {

const [isDropdownOpen, setIsDropdownOpen] = useState(false);

const closeDropdown = (e) => {
if (
!profileRef.current.contains(e.target) &&
!dropdownRef.current.contains(e.target)
) {
const handleDropdown = (e) => {
if (isDropdownOpen && !dropdownRef.current.contains(e.target)) {
setIsDropdownOpen(false);
} else if (profileRef.current.contains(e.target)) {
setIsDropdownOpen(true);
}
};

useEffect(() => {
if (isDropdownOpen) {
window.addEventListener("click", closeDropdown);
}
window.addEventListener("click", handleDropdown);

return () => {
window.removeEventListener("click", closeDropdown);
window.removeEventListener("click", handleDropdown);
};
});

Expand Down Expand Up @@ -92,7 +89,6 @@ const Header = () => {
<ProfileDiv>
<ProfileImg
ref={profileRef}
onClick={() => setIsDropdownOpen(true)}
src="https://yt3.ggpht.com/ytc/AOPolaSlb8-cH_rN_lZDD1phXr7aHFpoOqMVoepaGuTm=s48-c-k-c0x00ffffff-no-rj"
alt="user-profile"
/>
Expand Down

0 comments on commit d534872

Please sign in to comment.