Feature/use escape key to close documents modal (#2222)

* Add ability to use Esc keypress to close modal for documents

* move escape close to hook

---------

Co-authored-by: Mr Simon C <iamontheinternet@yahoo.com>
This commit is contained in:
Timothy Carambat 2024-09-04 16:42:40 -07:00 committed by GitHub
parent 7594841dac
commit aa4c9533aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -127,19 +127,32 @@ const ModalTabSwitcher = ({ selectedTab, setSelectedTab }) => {
</div> </div>
); );
}; };
export function useManageWorkspaceModal() { export function useManageWorkspaceModal() {
const { user } = useUser(); const { user } = useUser();
const [showing, setShowing] = useState(false); const [showing, setShowing] = useState(false);
const showModal = () => { function showModal() {
if (user?.role !== "default") { if (user?.role !== "default") {
setShowing(true); setShowing(true);
} }
}; }
const hideModal = () => { function hideModal() {
setShowing(false); setShowing(false);
}
useEffect(() => {
function onEscape(event) {
if (!showing || event.key !== "Escape") return;
setShowing(false);
}
document.addEventListener("keydown", onEscape);
return () => {
document.removeEventListener("keydown", onEscape);
}; };
}, [showing]);
return { showing, showModal, hideModal }; return { showing, showModal, hideModal };
} }