refactor: Enhance FileMenu component with click handling and outside click detection
This commit is contained in:
parent
0b0e1e3d8b
commit
949dbbca8d
|
@ -1,4 +1,4 @@
|
||||||
import React, { useState } from "react";
|
import React, { useState, useEffect, useRef } from "react";
|
||||||
import RenameInput from "./inputs/RenameInput";
|
import RenameInput from "./inputs/RenameInput";
|
||||||
import { ArrowIcon } from "../icons/ExportCommonIcons";
|
import { ArrowIcon } from "../icons/ExportCommonIcons";
|
||||||
import MenuBar from "./menu/menu";
|
import MenuBar from "./menu/menu";
|
||||||
|
@ -6,24 +6,48 @@ import { ProjectIcon } from "../icons/HeaderIcons";
|
||||||
|
|
||||||
const FileMenu: React.FC = () => {
|
const FileMenu: React.FC = () => {
|
||||||
const [openMenu, setOpenMenu] = useState(false);
|
const [openMenu, setOpenMenu] = useState(false);
|
||||||
|
const containerRef = useRef<HTMLButtonElement>(null);
|
||||||
|
let clickTimeout: NodeJS.Timeout | null = null;
|
||||||
|
|
||||||
|
const handleClick = () => {
|
||||||
|
if (clickTimeout) return;
|
||||||
|
setOpenMenu((prev) => !prev);
|
||||||
|
clickTimeout = setTimeout(() => {
|
||||||
|
clickTimeout = null;
|
||||||
|
}, 800);
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleClickOutside = (event: MouseEvent) => {
|
||||||
|
if (
|
||||||
|
containerRef.current &&
|
||||||
|
!containerRef.current.contains(event.target as Node)
|
||||||
|
) {
|
||||||
|
setOpenMenu(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
document.addEventListener("mousedown", handleClickOutside);
|
||||||
|
return () => document.removeEventListener("mousedown", handleClickOutside);
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="project-dropdowm-container">
|
<button
|
||||||
|
className="project-dropdowm-container"
|
||||||
|
ref={containerRef}
|
||||||
|
onClick={handleClick}
|
||||||
|
>
|
||||||
<div className="project-name">
|
<div className="project-name">
|
||||||
<div className="icon">
|
<div className="icon">
|
||||||
<ProjectIcon />
|
<ProjectIcon />
|
||||||
</div>
|
</div>
|
||||||
<RenameInput value="untitled" />
|
<RenameInput value="untitled" />
|
||||||
</div>
|
</div>
|
||||||
<div
|
<div className="more-options-button">
|
||||||
className="more-options-button"
|
|
||||||
onClick={() => {
|
|
||||||
setOpenMenu(!openMenu);
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<ArrowIcon />
|
<ArrowIcon />
|
||||||
{openMenu && <MenuBar setOpenMenu={setOpenMenu} />}
|
{openMenu && <MenuBar setOpenMenu={setOpenMenu} />}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</button>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue